| ... | ... |
@@ -1,17 +1,20 @@ |
| 1 |
-#include <FS.h> //this needs to be first, e folosit de salvarea credentialelor pe FS |
|
| 1 |
+#include <FS.h> // librarie folosita de salvarea credentialelor pe FS |
|
| 2 | 2 |
#include <ESP8266WiFi.h> |
| 3 | 3 |
#include <PubSubClient.h> |
| 4 |
-#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager |
|
| 5 |
-#include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson pentru salvarea credentialelor in eprom |
|
| 4 |
+#include <WiFiManager.h> // librarie folosita pentru salvarea credentialelor WIFI in eprom |
|
| 5 |
+#include <ArduinoJson.h> // librarie folosita pentru salvarea credentialelor MQTT in eprom |
|
| 6 | 6 |
#include <WiFiClient.h> |
| 7 | 7 |
#include <OneWire.h> |
| 8 | 8 |
#include <DallasTemperature.h> |
| 9 | 9 |
#include <ESP8266HTTPClient.h> |
| 10 | 10 |
#include <ESP8266httpUpdate.h> |
| 11 | 11 |
|
| 12 |
-char mqtt_user[9]; //le va lua din FS |
|
| 13 |
-char mqtt_devid[5]; //le va lua din FS |
|
| 14 |
-char mqttPassword[15]; //urmatoarele le va genera cu functia gen |
|
| 12 |
+// variabilele user si devid sunt incarcate din eprom folosind FS |
|
| 13 |
+char mqtt_user[9]; |
|
| 14 |
+char mqtt_devid[5]; |
|
| 15 |
+ |
|
| 16 |
+// variabile generate cu functia generate_vars, folosind user si devid |
|
| 17 |
+char mqttPassword[15]; |
|
| 15 | 18 |
char mqttSUB[22]; |
| 16 | 19 |
char espName[13]; |
| 17 | 20 |
char mqttESP[22]; |
| ... | ... |
@@ -19,26 +22,22 @@ char mqttTEMP[23]; |
| 19 | 19 |
char mqttLWT[22]; |
| 20 | 20 |
|
| 21 | 21 |
const String model = "NodeMCU Dallas"; |
| 22 |
-const String ver = "v2.0.1"; |
|
| 22 |
+const String ver = "v2.0.2"; |
|
| 23 | 23 |
const char* mqttServer = "mqtt.clickhome.ro"; |
| 24 | 24 |
const int mqttPort = 1883; |
| 25 |
-long loopTimer = 900000; // miliseconds - by default trimite la 15 minute |
|
| 25 |
+long loopTimer = 900000; // by default trimite temperatura la 15 minute |
|
| 26 | 26 |
long lastMsg = 0; |
| 27 | 27 |
float loopTemp = 0; |
| 28 | 28 |
int inPin = 5; |
| 29 | 29 |
String mqttMessage; |
| 30 | 30 |
|
| 31 |
-// senzor de temperatura DALLAS |
|
| 32 |
-#define ONE_WIRE_BUS D4 // pinul de date (la mine am pus senzor pe D4) |
|
| 31 |
+#define ONE_WIRE_BUS D1 // pinul de date folosit la nodeMCU este D1 |
|
| 33 | 32 |
OneWire oneWire(ONE_WIRE_BUS); |
| 34 | 33 |
DallasTemperature sensors(&oneWire); |
| 35 |
- |
|
| 36 |
-//********************************************************************* |
|
| 37 |
- |
|
| 38 | 34 |
WiFiClient espClient; |
| 39 | 35 |
PubSubClient client(espClient); |
| 40 | 36 |
|
| 41 |
-void generate_vars(){
|
|
| 37 |
+void generate_vars() {
|
|
| 42 | 38 |
strcpy (mqttPassword, "8219CH"); |
| 43 | 39 |
strcat (mqttPassword, mqtt_user); |
| 44 | 40 |
|
| ... | ... |
@@ -63,7 +62,7 @@ void generate_vars(){
|
| 63 | 63 |
strcat (mqttLWT, "/LWT"); |
| 64 | 64 |
} |
| 65 | 65 |
|
| 66 |
-String ipToString(IPAddress ip){
|
|
| 66 |
+String ipToString(IPAddress ip) {
|
|
| 67 | 67 |
String s=""; |
| 68 | 68 |
for (int i=0; i<4; i++) |
| 69 | 69 |
s += i ? "." + String(ip[i]) : String(ip[i]); |
| ... | ... |
@@ -84,11 +83,11 @@ String getMacAddress() {
|
| 84 | 84 |
} |
| 85 | 85 |
|
| 86 | 86 |
void reconectez() {
|
| 87 |
- while (String(mqtt_user)==""){
|
|
| 88 |
- Serial.println("Invalid user!");
|
|
| 89 |
- delay(99999999); |
|
| 87 |
+ // daca nu am credentialele MQTT, nu fac nimic, delay 1000 de ore |
|
| 88 |
+ while (String(mqtt_user)=="") {
|
|
| 89 |
+ Serial.println("User MQTT invalid!");
|
|
| 90 |
+ delay(3600000000); |
|
| 90 | 91 |
} |
| 91 |
- |
|
| 92 | 92 |
// ma conectez la mqtt server |
| 93 | 93 |
while (!client.connected()) {
|
| 94 | 94 |
client.setServer(mqttServer, mqttPort); |
| ... | ... |
@@ -98,60 +97,57 @@ void reconectez() {
|
| 98 | 98 |
Serial.println("connected");
|
| 99 | 99 |
client.publish(mqttLWT,"Online",TRUE); |
| 100 | 100 |
// trimit informatii utile cand ma conectez |
| 101 |
- String esp_info = "{\"ESPMac\":\"";
|
|
| 102 |
- esp_info += getMacAddress(); |
|
| 103 |
- esp_info += "\", \"IPAddress\":\""; |
|
| 104 |
- esp_info += ipToString(WiFi.localIP()); |
|
| 105 |
- esp_info += "\"}"; |
|
| 106 |
- String netinfo = "{\"Module\":\"";
|
|
| 107 |
- netinfo += String (model); |
|
| 108 |
- netinfo += "\", \"Version\":\""; |
|
| 109 |
- netinfo += String (ver); |
|
| 110 |
- netinfo += "\"}"; |
|
| 101 |
+ String esp_info = " {\"ESPMac\":\"";
|
|
| 102 |
+ esp_info += getMacAddress(); |
|
| 103 |
+ esp_info += "\", \"IPAddress\":\""; |
|
| 104 |
+ esp_info += ipToString(WiFi.localIP()); |
|
| 105 |
+ esp_info += "\"}"; |
|
| 106 |
+ String netinfo = " {\"Module\":\"";
|
|
| 107 |
+ netinfo += String (model); |
|
| 108 |
+ netinfo += "\", \"Version\":\""; |
|
| 109 |
+ netinfo += String (ver); |
|
| 110 |
+ netinfo += "\"}"; |
|
| 111 | 111 |
|
| 112 |
- client.publish(mqttESP, netinfo.c_str(),TRUE); |
|
| 113 |
- Serial.println(netinfo); |
|
| 114 |
- client.publish(mqttESP, esp_info.c_str(),TRUE); |
|
| 115 |
- Serial.println(esp_info); |
|
| 116 |
- client.publish(mqttESP, "15 minute",TRUE); // trimit intervalul default |
|
| 117 |
- client.subscribe(mqttSUB); |
|
| 118 |
- } else {
|
|
| 112 |
+ client.publish(mqttESP, netinfo.c_str(),TRUE); |
|
| 113 |
+ Serial.println(netinfo); |
|
| 114 |
+ client.publish(mqttESP, esp_info.c_str(),TRUE); |
|
| 115 |
+ Serial.println(esp_info); |
|
| 116 |
+ client.publish(mqttESP, "15 minute",TRUE); // trimit intervalul default |
|
| 117 |
+ client.subscribe(mqttSUB); |
|
| 118 |
+ } |
|
| 119 |
+ else {
|
|
| 119 | 120 |
Serial.print("failed, rc=");
|
| 120 | 121 |
Serial.print(client.state()); |
| 121 |
- Serial.println(" try again in 60 seconds");
|
|
| 122 |
- // Wait 60 seconds before retrying |
|
| 122 |
+ Serial.println("Incerc din nou in 60 de secunde");
|
|
| 123 | 123 |
delay(60000); |
| 124 | 124 |
} |
| 125 | 125 |
} |
| 126 | 126 |
} |
| 127 | 127 |
|
| 128 |
-void setup() |
|
| 129 |
-{
|
|
| 128 |
+void setup() {
|
|
| 130 | 129 |
Serial.begin(115200); |
| 131 | 130 |
//generez variabilele |
| 132 | 131 |
readFS(); |
| 133 |
- // The extra parameters to be configured (can be either global or just in the setup) |
|
| 134 |
- // After connecting, parameter.getValue() will get you the configured value |
|
| 135 |
- // id/name placeholder/prompt default length |
|
| 136 |
- |
|
| 137 | 132 |
WiFiManagerParameter custom_mqtt_user("user", "mqtt user", mqtt_user, 9);
|
| 138 | 133 |
WiFiManagerParameter custom_mqtt_devid("devid", "mqtt devid", mqtt_devid, 5);
|
| 139 |
- |
|
| 140 | 134 |
// ma conectez la AP via wifi |
| 141 |
- WiFiManager wifi; |
|
| 142 |
- wifi.setConfigPortalTimeout(120); // a timeout so the ESP doesn't hang waiting to be configured, for instance after a power failure |
|
| 143 |
- wifi.setTimeout(180); // sta AP 3 minute apoi se reseteaza din nou |
|
| 144 |
- if (!wifi.autoConnect("ClickHome")) {
|
|
| 145 |
- Serial.println("timeout - going to sleep");
|
|
| 135 |
+ WiFiManager wifi; |
|
| 136 |
+ // ESP-ul asteapta ca sa fie configurat 2 minute |
|
| 137 |
+ wifi.setConfigPortalTimeout(120); |
|
| 138 |
+ // sta AP 3 minute apoi se reseteaza din nou |
|
| 139 |
+ wifi.setTimeout(180); |
|
| 140 |
+ if (!wifi.autoConnect("ClickHome")) {
|
|
| 141 |
+ Serial.println("timeout - going to sleep"); // EU aici nu am inteles ce se intampla ... poate ii punem un delay!!
|
|
| 146 | 142 |
} |
| 147 | 143 |
delay(200); |
| 148 |
- |
|
| 149 |
- strcpy(mqtt_user, custom_mqtt_user.getValue()); //citesc datele salvate anterior pe FS |
|
| 144 |
+ //citesc datele salvate anterior pe FS |
|
| 145 |
+ strcpy(mqtt_user, custom_mqtt_user.getValue()); |
|
| 150 | 146 |
strcpy(mqtt_devid, custom_mqtt_devid.getValue()); |
| 151 |
- generate_vars(); //genereaza topicurile de mqtt in baza mqtt_user si mqtt_devid |
|
| 147 |
+ //genereaza topicurile de mqtt in baza mqtt_user si mqtt_devid |
|
| 148 |
+ generate_vars(); |
|
| 152 | 149 |
reconectez(); |
| 153 |
- client.setCallback(getMessage); //setez functia care parseaza mesajele venite prin mqtt |
|
| 154 |
- |
|
| 150 |
+ //setez functia care parseaza mesajele venite prin mqtt |
|
| 151 |
+ client.setCallback(getMessage); |
|
| 155 | 152 |
pinMode(inPin, INPUT); |
| 156 | 153 |
sensors.begin(); |
| 157 | 154 |
} |
| ... | ... |
@@ -159,99 +155,88 @@ void setup() |
| 159 | 159 |
void getMessage(char* topic, byte* payload, unsigned int length) {
|
| 160 | 160 |
float t = 0; |
| 161 | 161 |
mqttMessage=""; |
| 162 |
- Serial.print("Message arrived in topic: ");
|
|
| 162 |
+ Serial.print("Mesaj primit pe topicul: ");
|
|
| 163 | 163 |
Serial.println(topic); |
| 164 |
- Serial.print("Message:");
|
|
| 164 |
+ Serial.print("Mesaj:");
|
|
| 165 | 165 |
for (int i = 0; i < length; i++) {
|
| 166 | 166 |
mqttMessage += (char)payload[i]; |
| 167 | 167 |
} |
| 168 | 168 |
Serial.println(mqttMessage); |
| 169 | 169 |
|
| 170 |
- // daca a primit sendTemp trimite temperatura |
|
| 171 |
- if (mqttMessage == "temp") |
|
| 172 |
- {
|
|
| 170 |
+ // daca primeste 'temp' pe mqtt, trimite temperatura |
|
| 171 |
+ if (mqttMessage == "temp") {
|
|
| 173 | 172 |
Serial.println("Trimit temperatura");
|
| 174 | 173 |
sensors.setResolution(12); |
| 175 | 174 |
sensors.requestTemperatures(); // Send the command to get Temperatures |
| 176 | 175 |
t = sensors.getTempCByIndex(0); |
| 177 |
- String json = "{\"Temp\":";
|
|
| 176 |
+ String json = " {\"Temp\":";
|
|
| 178 | 177 |
json += String (t); |
| 179 | 178 |
json += "}"; |
| 180 | 179 |
client.publish(mqttTEMP, json.c_str(),TRUE); |
| 181 | 180 |
Serial.println(json); |
| 182 | 181 |
} |
| 183 |
-// reset la ESP |
|
| 184 |
- if (mqttMessage == "reset") |
|
| 185 |
- {
|
|
| 186 |
- String lastwords="Am fost resetat ..."; |
|
| 187 |
- client.publish(mqttESP, lastwords.c_str(), TRUE); |
|
| 188 |
- delay (3000); |
|
| 189 |
- ESP.reset(); |
|
| 190 |
- delay (5000); |
|
| 182 |
+ |
|
| 183 |
+ // daca primeste 'reset' pe mqtt, isi da reset |
|
| 184 |
+ if (mqttMessage == "reset") {
|
|
| 185 |
+ String lastwords="Am fost resetat ..."; |
|
| 186 |
+ client.publish(mqttESP, lastwords.c_str(), TRUE); |
|
| 187 |
+ delay (3000); |
|
| 188 |
+ ESP.reset(); |
|
| 189 |
+ delay (5000); |
|
| 191 | 190 |
} |
| 192 |
-// procedura de software update via WEB |
|
| 193 |
- if (mqttMessage == "update") |
|
| 194 |
- {
|
|
| 195 |
- String msg="Software update: "; |
|
| 196 |
- t_httpUpdate_return ret; |
|
| 197 |
- //ESPhttpUpdate.rebootOnUpdate(false); |
|
| 198 |
- ret = ESPhttpUpdate.update("http://update.clickhome.ro/senzor/dallas/arduino.bin");
|
|
| 199 |
- //ret = ESPhttpUpdate.update("update.clickhome.ro", 80, "/senzor/dallas/arduino.bin");
|
|
| 200 |
- switch(ret) {
|
|
| 201 |
- case HTTP_UPDATE_FAILED: |
|
| 202 |
- msg.concat(" eroare:");
|
|
| 203 |
- msg.concat(ESPhttpUpdate.getLastError()); |
|
| 204 |
- msg.concat(" motiv:");
|
|
| 205 |
- msg.concat(ESPhttpUpdate.getLastErrorString().c_str()); |
|
| 206 |
- break; |
|
| 207 |
- case HTTP_UPDATE_NO_UPDATES: |
|
| 208 |
- msg.concat(" no update.");
|
|
| 209 |
- break; |
|
| 210 |
- case HTTP_UPDATE_OK: |
|
| 211 |
- msg.concat(" success.");
|
|
| 212 |
- break; |
|
| 213 |
- } |
|
| 214 |
- Serial.println(msg); |
|
| 215 |
- } |
|
| 216 |
- |
|
| 217 |
- // daca a primit valoarea in minute seteaza loopTimer si-l trimite in DB |
|
| 218 | 191 |
|
| 219 |
- if (mqttMessage == "debug") |
|
| 220 |
- {
|
|
| 221 |
- loopTimer=30000; // setez sa trimita valori la 30 de secunde ca sa fac debug |
|
| 222 |
- client.publish(mqttESP, "{\"LoopInterval\":\"30 secunde\"}",TRUE);
|
|
| 192 |
+ // daca primeste 'update' isi face software update via WEB |
|
| 193 |
+ if (mqttMessage == "update") {
|
|
| 194 |
+ String msg="Software update: "; |
|
| 195 |
+ t_httpUpdate_return ret; |
|
| 196 |
+ //ESPhttpUpdate.rebootOnUpdate(false); |
|
| 197 |
+ ret = ESPhttpUpdate.update("http://update.clickhome.ro/senzor/dallas/arduino.bin");
|
|
| 198 |
+ //ret = ESPhttpUpdate.update("update.clickhome.ro", 80, "/senzor/dallas/arduino.bin");
|
|
| 199 |
+ switch(ret) {
|
|
| 200 |
+ case HTTP_UPDATE_FAILED: |
|
| 201 |
+ msg.concat(" eroare:");
|
|
| 202 |
+ msg.concat(ESPhttpUpdate.getLastError()); |
|
| 203 |
+ msg.concat(" motiv:");
|
|
| 204 |
+ msg.concat(ESPhttpUpdate.getLastErrorString().c_str()); |
|
| 205 |
+ break; |
|
| 206 |
+ case HTTP_UPDATE_NO_UPDATES: |
|
| 207 |
+ msg.concat(" no update.");
|
|
| 208 |
+ break; |
|
| 209 |
+ case HTTP_UPDATE_OK: |
|
| 210 |
+ msg.concat(" success.");
|
|
| 211 |
+ break; |
|
| 212 |
+ } |
|
| 213 |
+ Serial.println(msg); |
|
| 214 |
+ } |
|
| 215 |
+ |
|
| 216 |
+ // daca primeste valoarea in minute seteaza loopTimer si-l trimite in DB |
|
| 217 |
+ if (mqttMessage == "debug") {
|
|
| 218 |
+ loopTimer=30000; |
|
| 219 |
+ client.publish(mqttESP, " {\"LoopInterval\":\"30 secunde\"}",TRUE);
|
|
| 223 | 220 |
} |
| 224 |
- if (mqttMessage == "10") |
|
| 225 |
- {
|
|
| 226 |
- loopTimer=600000; // setez sa trimita valori la 10 minute |
|
| 227 |
- client.publish(mqttESP, "{\"LoopInterval\":\"10 minute\"}",TRUE);
|
|
| 221 |
+ if (mqttMessage == "10") {
|
|
| 222 |
+ loopTimer=600000; |
|
| 223 |
+ client.publish(mqttESP, " {\"LoopInterval\":\"10 minute\"}",TRUE);
|
|
| 228 | 224 |
} |
| 229 |
- if (mqttMessage == "15") |
|
| 230 |
- {
|
|
| 231 |
- loopTimer=900000; // setez sa trimita valori la 15 minute |
|
| 232 |
- client.publish(mqttESP, "{\"LoopInterval\":\"15 minute\"}",TRUE);
|
|
| 225 |
+ if (mqttMessage == "15") {
|
|
| 226 |
+ loopTimer=900000; |
|
| 227 |
+ client.publish(mqttESP, " {\"LoopInterval\":\"15 minute\"}",TRUE);
|
|
| 233 | 228 |
} |
| 234 |
- if (mqttMessage == "30") |
|
| 235 |
- {
|
|
| 236 |
- loopTimer=1800000; // setez sa trimita valori la 30 de minute |
|
| 237 |
- client.publish(mqttESP, "{\"LoopInterval\":\"30 de minute\"}",TRUE);
|
|
| 229 |
+ if (mqttMessage == "30") {
|
|
| 230 |
+ loopTimer=1800000; |
|
| 231 |
+ client.publish(mqttESP, " {\"LoopInterval\":\"30 de minute\"}",TRUE);
|
|
| 238 | 232 |
} |
| 239 |
- if (mqttMessage == "60") |
|
| 240 |
- {
|
|
| 241 |
- loopTimer=3600000; // setez sa trimita valori la o ora |
|
| 242 |
- client.publish(mqttESP, "{\"LoopInterval\":\"o ora\"}",TRUE);
|
|
| 233 |
+ if (mqttMessage == "60") {
|
|
| 234 |
+ loopTimer=3600000; |
|
| 235 |
+ client.publish(mqttESP, " {\"LoopInterval\":\"o ora\"}",TRUE);
|
|
| 243 | 236 |
} |
| 244 |
- if (mqttMessage == "stop") |
|
| 245 |
- {
|
|
| 246 |
- loopTimer=3600000000; // setez sa nu trimita valori |
|
| 247 |
- client.publish(mqttESP, "{\"LoopInterval\":\"oprit\"}",TRUE);
|
|
| 237 |
+ if (mqttMessage == "stop") {
|
|
| 238 |
+ loopTimer=3600000000; // setez sa nu trimita valori, 1000 de ore |
|
| 239 |
+ client.publish(mqttESP, " {\"LoopInterval\":\"oprit\"}",TRUE);
|
|
| 248 | 240 |
} |
| 249 | 241 |
} |
| 250 | 242 |
|
| 251 |
-void readFS(){
|
|
| 252 |
- //read configuration from FS json |
|
| 253 |
- //Serial.println("mounting FS...");
|
|
| 254 |
- |
|
| 243 |
+void readFS() {
|
|
| 255 | 244 |
if (SPIFFS.begin()) {
|
| 256 | 245 |
//Serial.println("mounted file system");
|
| 257 | 246 |
if (SPIFFS.exists("/config.json")) {
|
| ... | ... |
@@ -260,11 +245,9 @@ void readFS(){
|
| 260 | 260 |
File configFile = SPIFFS.open("/config.json", "r");
|
| 261 | 261 |
if (configFile) {
|
| 262 | 262 |
//Serial.println("opened config file");
|
| 263 |
- |
|
| 264 | 263 |
size_t size = configFile.size(); |
| 265 | 264 |
// Allocate a buffer to store contents of the file. |
| 266 | 265 |
std::unique_ptr<char[]> buf(new char[size]); |
| 267 |
- |
|
| 268 | 266 |
configFile.readBytes(buf.get(), size); |
| 269 | 267 |
DynamicJsonBuffer jsonBuffer; |
| 270 | 268 |
JsonObject& json = jsonBuffer.parseObject(buf.get()); |
| ... | ... |
@@ -273,19 +256,19 @@ void readFS(){
|
| 273 | 273 |
//Serial.println("\nparsed json");
|
| 274 | 274 |
strcpy(mqtt_user, json["mqtt_user"]); |
| 275 | 275 |
strcpy(mqtt_devid, json["mqtt_devid"]); |
| 276 |
- } else {
|
|
| 276 |
+ } |
|
| 277 |
+ else {
|
|
| 277 | 278 |
//Serial.println("failed to load json config");
|
| 278 | 279 |
} |
| 279 | 280 |
} |
| 280 | 281 |
} |
| 281 |
- } else {
|
|
| 282 |
+ } |
|
| 283 |
+ else {
|
|
| 282 | 284 |
//Serial.println("failed to mount FS");
|
| 283 | 285 |
} |
| 284 |
- //end read |
|
| 285 | 286 |
} |
| 286 | 287 |
|
| 287 |
-void loop() |
|
| 288 |
-{
|
|
| 288 |
+void loop() {
|
|
| 289 | 289 |
if (!client.connected()) {
|
| 290 | 290 |
reconectez(); |
| 291 | 291 |
} |
| ... | ... |
@@ -294,15 +277,15 @@ void loop() |
| 294 | 294 |
if (now - lastMsg > loopTimer) {
|
| 295 | 295 |
lastMsg = now; |
| 296 | 296 |
sensors.setResolution(12); |
| 297 |
- sensors.requestTemperatures(); // Send the command to get loopTemperatures |
|
| 297 |
+ sensors.requestTemperatures(); |
|
| 298 | 298 |
loopTemp = sensors.getTempCByIndex(0); |
| 299 | 299 |
Serial.println(loopTemp); |
| 300 |
- if((loopTemp > -20) && (loopTemp <60)) |
|
| 301 |
- {
|
|
| 302 |
- String val = "{\"Temp\":";
|
|
| 300 |
+ if((loopTemp > -20) && (loopTemp <60)) {
|
|
| 301 |
+ String val = " {\"Temp\":";
|
|
| 303 | 302 |
val += String (loopTemp); |
| 304 | 303 |
val += "}"; |
| 305 | 304 |
client.publish(mqttESP, val.c_str(),TRUE); |
| 306 |
- } |
|
| 305 |
+ } |
|
| 307 | 306 |
} |
| 308 | 307 |
} |
| 308 |
+ |