| ... | ... |
@@ -9,6 +9,8 @@ |
| 9 | 9 |
#include <ESP8266HTTPClient.h> |
| 10 | 10 |
#include <ESP8266httpUpdate.h> |
| 11 | 11 |
|
| 12 |
+#define DEBUG 0 //0=off 1=on |
|
| 13 |
+ |
|
| 12 | 14 |
char mqtt_user[9]; //le va lua din FS |
| 13 | 15 |
char mqtt_devid[5]; //le va lua din FS |
| 14 | 16 |
char mqttPassword[15]; //urmatoarele le va genera cu functia gen |
| ... | ... |
@@ -87,17 +89,23 @@ String getMacAddress() {
|
| 87 | 87 |
|
| 88 | 88 |
void reconectez() {
|
| 89 | 89 |
while (String(mqtt_user)==""){
|
| 90 |
+ #if DEBUG |
|
| 90 | 91 |
Serial.println("Invalid user!");
|
| 92 |
+ #endif |
|
| 91 | 93 |
delay(99999999); |
| 92 | 94 |
} |
| 93 | 95 |
|
| 94 | 96 |
// ma conectez la mqtt server |
| 95 | 97 |
while (!client.connected()) {
|
| 96 | 98 |
client.setServer(mqttServer, mqttPort); |
| 99 |
+ #if DEBUG |
|
| 97 | 100 |
Serial.print("AtloopTempting MQTT connection...");
|
| 101 |
+ #endif |
|
| 98 | 102 |
// Incerc sa ma reconectez cu LWT din 5 in 5 secunde |
| 99 | 103 |
if (client.connect(espName, mqtt_user, mqttPassword, mqttLWT, 1, 1, "Offline")) {
|
| 104 |
+ #if DEBUG |
|
| 100 | 105 |
Serial.println("connected");
|
| 106 |
+ #endif |
|
| 101 | 107 |
client.publish(mqttLWT,"Online",TRUE); |
| 102 | 108 |
// trimit informatii utile cand ma conectez |
| 103 | 109 |
String esp_info = "{\"ESPMac\":\"";
|
| ... | ... |
@@ -112,15 +120,21 @@ void reconectez() {
|
| 112 | 112 |
netinfo += "\"}"; |
| 113 | 113 |
|
| 114 | 114 |
client.publish(mqttESP, netinfo.c_str(),TRUE); |
| 115 |
+ #if DEBUG |
|
| 115 | 116 |
Serial.println(netinfo); |
| 117 |
+ #endif |
|
| 116 | 118 |
client.publish(mqttESP, esp_info.c_str(),TRUE); |
| 119 |
+ #if DEBUG |
|
| 117 | 120 |
Serial.println(esp_info); |
| 121 |
+ #endif |
|
| 118 | 122 |
client.publish(mqttESP, "15 minute",TRUE); // trimit intervalul default |
| 119 | 123 |
client.subscribe(mqttSUB); |
| 120 | 124 |
} else {
|
| 125 |
+ #if DEBUG |
|
| 121 | 126 |
Serial.print("failed, rc=");
|
| 122 | 127 |
Serial.print(client.state()); |
| 123 | 128 |
Serial.println(" try again in 60 seconds");
|
| 129 |
+ #endif |
|
| 124 | 130 |
// Wait 60 seconds before retrying |
| 125 | 131 |
delay(60000); |
| 126 | 132 |
} |
| ... | ... |
@@ -129,11 +143,15 @@ void reconectez() {
|
| 129 | 129 |
|
| 130 | 130 |
void setup() |
| 131 | 131 |
{
|
| 132 |
+ #if DEBUG |
|
| 132 | 133 |
Serial.begin(115200); |
| 134 |
+ #endif |
|
| 133 | 135 |
|
| 134 | 136 |
// Mount the filesystem |
| 135 | 137 |
bool result = SPIFFS.begin(); |
| 138 |
+ #if DEBUG |
|
| 136 | 139 |
//Serial.println("SPIFFS opened: " + result);
|
| 140 |
+ #endif |
|
| 137 | 141 |
|
| 138 | 142 |
readConfigFile(); //citesc user si devid din memorie |
| 139 | 143 |
generate_vars(); //genereaza topicurile de mqtt in baza mqtt_user si mqtt_devid |
| ... | ... |
@@ -143,7 +161,9 @@ void setup() |
| 143 | 143 |
wifi.setConfigPortalTimeout(120); // a timeout so the ESP doesn't hang waiting to be configured, for instance after a power failure |
| 144 | 144 |
wifi.setTimeout(180); // sta AP 3 minute apoi se reseteaza din nou |
| 145 | 145 |
if (!wifi.autoConnect("ClickHome")) {
|
| 146 |
+ #if DEBUG |
|
| 146 | 147 |
Serial.println("timeout - going to sleep");
|
| 148 |
+ #endif |
|
| 147 | 149 |
} |
| 148 | 150 |
delay(200); |
| 149 | 151 |
|
| ... | ... |
@@ -157,18 +177,24 @@ void setup() |
| 157 | 157 |
void getMessage(char* topic, byte* payload, unsigned int length) {
|
| 158 | 158 |
float t = 0; |
| 159 | 159 |
mqttMessage=""; |
| 160 |
+ #if DEBUG |
|
| 160 | 161 |
Serial.print("Message arrived in topic: ");
|
| 161 | 162 |
Serial.println(topic); |
| 162 | 163 |
Serial.print("Message:");
|
| 164 |
+ #endif |
|
| 163 | 165 |
for (int i = 0; i < length; i++) {
|
| 164 | 166 |
mqttMessage += (char)payload[i]; |
| 165 | 167 |
} |
| 168 |
+ #if DEBUG |
|
| 166 | 169 |
Serial.println(mqttMessage); |
| 170 |
+ #endif |
|
| 167 | 171 |
|
| 168 | 172 |
// daca a primit sendTemp trimite temperatura |
| 169 | 173 |
if (mqttMessage == "temp") |
| 170 | 174 |
{
|
| 175 |
+ #if DEBUG |
|
| 171 | 176 |
Serial.println("Trimit temperatura");
|
| 177 |
+ #endif |
|
| 172 | 178 |
sensors.setResolution(12); |
| 173 | 179 |
sensors.requestTemperatures(); // Send the command to get Temperatures |
| 174 | 180 |
t = sensors.getTempCByIndex(0); |
| ... | ... |
@@ -176,7 +202,9 @@ void getMessage(char* topic, byte* payload, unsigned int length) {
|
| 176 | 176 |
json += String (t); |
| 177 | 177 |
json += "}"; |
| 178 | 178 |
client.publish(mqttTEMP, json.c_str(),TRUE); |
| 179 |
+ #if DEBUG |
|
| 179 | 180 |
Serial.println(json); |
| 181 |
+ #endif |
|
| 180 | 182 |
} |
| 181 | 183 |
// reset la ESP |
| 182 | 184 |
if (mqttMessage == "reset") |
| ... | ... |
@@ -209,7 +237,9 @@ void getMessage(char* topic, byte* payload, unsigned int length) {
|
| 209 | 209 |
msg.concat(" success.");
|
| 210 | 210 |
break; |
| 211 | 211 |
} |
| 212 |
+ #if DEBUG |
|
| 212 | 213 |
Serial.println(msg); |
| 214 |
+ #endif |
|
| 213 | 215 |
} |
| 214 | 216 |
|
| 215 | 217 |
// daca a primit valoarea in minute seteaza loopTimer si-l trimite in DB |
| ... | ... |
@@ -251,7 +281,9 @@ bool readConfigFile() {
|
| 251 | 251 |
File f = SPIFFS.open("/config.json", "r");
|
| 252 | 252 |
|
| 253 | 253 |
if (!f) {
|
| 254 |
+ #if DEBUG |
|
| 254 | 255 |
Serial.println("Configuration file not found");
|
| 256 |
+ #endif |
|
| 255 | 257 |
return false; |
| 256 | 258 |
} else {
|
| 257 | 259 |
// we could open the file |
| ... | ... |
@@ -270,10 +302,14 @@ bool readConfigFile() {
|
| 270 | 270 |
JsonObject& json = jsonBuffer.parseObject(buf.get()); |
| 271 | 271 |
// Test if parsing succeeds. |
| 272 | 272 |
if (!json.success()) {
|
| 273 |
+ #if DEBUG |
|
| 273 | 274 |
Serial.println("JSON parseObject() failed");
|
| 275 |
+ #endif |
|
| 274 | 276 |
return false; |
| 275 | 277 |
} |
| 278 |
+ #if DEBUG |
|
| 276 | 279 |
json.printTo(Serial); |
| 280 |
+ #endif |
|
| 277 | 281 |
strcpy(mqtt_user, json["mqtt_user"]); |
| 278 | 282 |
strcpy(mqtt_devid, json["mqtt_devid"]); |
| 279 | 283 |
} |
| ... | ... |
@@ -293,7 +329,9 @@ void loop() |
| 293 | 293 |
sensors.setResolution(12); |
| 294 | 294 |
sensors.requestTemperatures(); // Send the command to get loopTemperatures |
| 295 | 295 |
loopTemp = sensors.getTempCByIndex(0); |
| 296 |
+ #if DEBUG |
|
| 296 | 297 |
Serial.println(loopTemp); |
| 298 |
+ #endif |
|
| 297 | 299 |
if((loopTemp > -20) && (loopTemp <60)) |
| 298 | 300 |
{
|
| 299 | 301 |
String val = "{\"Temp\":";
|