| 1 | 1 |
old mode 100644 |
| 2 | 2 |
new mode 100755 |
| ... | ... |
@@ -1,26 +1,36 @@ |
| 1 |
+#include <FS.h> //this needs to be first, e folosit de salvarea credentialelor pe FS |
|
| 1 | 2 |
#include <ESP8266WiFi.h> |
| 2 | 3 |
#include <PubSubClient.h> |
| 3 |
-#include <WiFiManager.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 | 6 |
#include <WiFiClient.h> |
| 5 | 7 |
#include <OneWire.h> |
| 6 | 8 |
#include <PietteTech_DHT.h> |
| 7 | 9 |
#include <ESP8266HTTPClient.h> |
| 8 | 10 |
#include <ESP8266httpUpdate.h> |
| 9 | 11 |
|
| 10 |
-// Modifica cu datele tale |
|
| 11 |
-const char* mqttUser = "qbzesh"; |
|
| 12 |
-const char* mqttPassword = "8219CHqbzesh"; |
|
| 13 |
-const char* mqttSUB = "cmnd/qbzesh-1250/ESP"; |
|
| 14 |
-const char* mqttESP = "stat/qbzesh-1250/ESP"; |
|
| 15 |
-const char* mqttTEMP = "stat/qbzesh-1250/TEMP"; |
|
| 16 |
-const char* mqttLWT = "tele/qbzesh-1250/LWT"; |
|
| 17 |
-const char* espName ="qbzesh-1250"; |
|
| 18 | 12 |
|
| 19 |
-//####################################################################### |
|
| 20 |
-// NU EDITA MAI JOS |
|
| 21 |
-//####################################################################### |
|
| 22 |
-const String model = "NodeMCU DHT22"; |
|
| 23 |
-const String ver = "v1.0.6"; |
|
| 13 |
+// senzor de temperatura DHT |
|
| 14 |
+#define DHTPIN D1 // pinul de date |
|
| 15 |
+#define DHTTYPE DHT22 // DHT11 (albastru) DHT22 (alb) |
|
| 16 |
+#define DEBUG 0 //0=off 1=on |
|
| 17 |
+ |
|
| 18 |
+/********** NU edita mai jos ******************/ |
|
| 19 |
+ |
|
| 20 |
+ |
|
| 21 |
+ |
|
| 22 |
+ |
|
| 23 |
+char mqtt_user[9]; //le va lua din FS |
|
| 24 |
+char mqtt_devid[5]; //le va lua din FS |
|
| 25 |
+char mqttPassword[15]; //urmatoarele le va genera cu functia gen |
|
| 26 |
+char mqttSUB[22]; |
|
| 27 |
+char espName[13]; |
|
| 28 |
+char mqttESP[22]; |
|
| 29 |
+char mqttTEMP[23]; |
|
| 30 |
+char mqttLWT[22]; |
|
| 31 |
+ |
|
| 32 |
+const String model = "NodeMCU "; |
|
| 33 |
+const String ver = "v2.0.0"; |
|
| 24 | 34 |
const char* mqttServer = "mqtt.clickhome.ro"; |
| 25 | 35 |
const int mqttPort = 1883; |
| 26 | 36 |
long loopTimer = 900000; // miliseconds - by default trimite la 15 minute |
| ... | ... |
@@ -33,9 +43,6 @@ String mqttMessage; |
| 33 | 33 |
bool bDHTstarted; |
| 34 | 34 |
int acquireresult; |
| 35 | 35 |
|
| 36 |
-// senzor de temperatura DHT |
|
| 37 |
-#define DHTPIN D4 // pinul de date (la mine am pus senzor pe D4) |
|
| 38 |
-#define DHTTYPE DHT22 // DHT 11 |
|
| 39 | 36 |
void dht_wrapper(); |
| 40 | 37 |
PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper); |
| 41 | 38 |
void dht_wrapper() |
| ... | ... |
@@ -46,7 +53,33 @@ void dht_wrapper() |
| 46 | 46 |
|
| 47 | 47 |
WiFiClient espClient; |
| 48 | 48 |
PubSubClient client(espClient); |
| 49 |
+bool readConfigFile(); |
|
| 50 |
+ |
|
| 51 |
+ |
|
| 52 |
+void generate_vars(){
|
|
| 53 |
+ strcpy (mqttPassword, "8219CH"); |
|
| 54 |
+ strcat (mqttPassword, mqtt_user); |
|
| 55 |
+ |
|
| 56 |
+ strcpy (espName, mqtt_user); |
|
| 57 |
+ strcat (espName, "-"); |
|
| 58 |
+ strcat (espName, mqtt_devid); |
|
| 59 |
+ |
|
| 60 |
+ strcpy (mqttSUB, "cmnd/"); |
|
| 61 |
+ strcat (mqttSUB, espName); |
|
| 62 |
+ strcat (mqttSUB, "/ESP"); |
|
| 63 |
+ |
|
| 64 |
+ strcpy (mqttESP, "stat/"); |
|
| 65 |
+ strcat (mqttESP, espName); |
|
| 66 |
+ strcat (mqttESP, "/ESP"); |
|
| 67 |
+ |
|
| 68 |
+ strcpy (mqttTEMP, "stat/"); |
|
| 69 |
+ strcat (mqttTEMP, espName); |
|
| 70 |
+ strcat (mqttTEMP, "/TEMP"); |
|
| 49 | 71 |
|
| 72 |
+ strcpy (mqttLWT, "tele/"); |
|
| 73 |
+ strcat (mqttLWT, espName); |
|
| 74 |
+ strcat (mqttLWT, "/LWT"); |
|
| 75 |
+} |
|
| 50 | 76 |
|
| 51 | 77 |
String ipToString(IPAddress ip){
|
| 52 | 78 |
String s=""; |
| ... | ... |
@@ -68,15 +101,25 @@ String getMacAddress() {
|
| 68 | 68 |
return cMac; |
| 69 | 69 |
} |
| 70 | 70 |
|
| 71 |
-void reconnect() |
|
| 72 |
-{
|
|
| 73 |
- while (!client.connected()) |
|
| 74 |
- {
|
|
| 75 |
- //Serial.println("Reconectare la MQTT ...");
|
|
| 76 |
- // Incerc sa ma reconectez cu LWT din 60 in 60 secunde |
|
| 77 |
- if (client.connect(espName, mqttUser, mqttPassword, mqttLWT, 1, 1, "Offline")) |
|
| 78 |
- {
|
|
| 79 |
- Serial.println("Senzor reconectat la MQTT.");
|
|
| 71 |
+void reconectez() {
|
|
| 72 |
+ while (String(mqtt_user)==""){
|
|
| 73 |
+ #if DEBUG |
|
| 74 |
+ Serial.println("Invalid user!");
|
|
| 75 |
+ #endif |
|
| 76 |
+ delay(99999999); |
|
| 77 |
+ } |
|
| 78 |
+ |
|
| 79 |
+ // ma conectez la mqtt server |
|
| 80 |
+ while (!client.connected()) {
|
|
| 81 |
+ client.setServer(mqttServer, mqttPort); |
|
| 82 |
+ #if DEBUG |
|
| 83 |
+ Serial.print("AtloopTempting MQTT connection...");
|
|
| 84 |
+ #endif |
|
| 85 |
+ // Incerc sa ma reconectez cu LWT din 5 in 5 secunde |
|
| 86 |
+ if (client.connect(espName, mqtt_user, mqttPassword, mqttLWT, 1, 1, "Offline")) {
|
|
| 87 |
+ #if DEBUG |
|
| 88 |
+ Serial.println("connected");
|
|
| 89 |
+ #endif |
|
| 80 | 90 |
client.publish(mqttLWT,"Online",TRUE); |
| 81 | 91 |
// trimit informatii utile cand ma conectez |
| 82 | 92 |
String esp_info = "{\"ESPMac\":\"";
|
| ... | ... |
@@ -86,89 +129,85 @@ void reconnect() |
| 86 | 86 |
esp_info += "\"}"; |
| 87 | 87 |
String netinfo = "{\"Module\":\"";
|
| 88 | 88 |
netinfo += String (model); |
| 89 |
+ netinfo += "DHT"; |
|
| 90 |
+ netinfo += String (DHTTYPE); |
|
| 89 | 91 |
netinfo += "\", \"Version\":\""; |
| 90 | 92 |
netinfo += String (ver); |
| 91 | 93 |
netinfo += "\"}"; |
| 92 | 94 |
|
| 93 | 95 |
client.publish(mqttESP, netinfo.c_str(),TRUE); |
| 96 |
+ #if DEBUG |
|
| 94 | 97 |
Serial.println(netinfo); |
| 98 |
+ #endif |
|
| 95 | 99 |
client.publish(mqttESP, esp_info.c_str(),TRUE); |
| 100 |
+ #if DEBUG |
|
| 96 | 101 |
Serial.println(esp_info); |
| 102 |
+ #endif |
|
| 97 | 103 |
client.publish(mqttESP, "15 minute",TRUE); // trimit intervalul default |
| 98 | 104 |
client.subscribe(mqttSUB); |
| 99 |
- } |
|
| 100 |
- else |
|
| 101 |
- {
|
|
| 102 |
- Serial.println("Reconectare MQTT esuata: ");
|
|
| 103 |
- Serial.println(client.state()); |
|
| 104 |
- Serial.println("fac o alta incercare in 60 de secunde");
|
|
| 105 |
- delay(60000); |
|
| 105 |
+ } else {
|
|
| 106 |
+ #if DEBUG |
|
| 107 |
+ Serial.print("failed, rc=");
|
|
| 108 |
+ Serial.print(client.state()); |
|
| 109 |
+ Serial.println(" try again in 60 seconds");
|
|
| 110 |
+ #endif |
|
| 111 |
+ // Wait 60 seconds before retrying |
|
| 112 |
+ delay(60000); |
|
| 106 | 113 |
} |
| 107 | 114 |
} |
| 108 | 115 |
} |
| 109 |
- |
|
| 116 |
+ |
|
| 110 | 117 |
void setup() |
| 111 | 118 |
{
|
| 119 |
+ #if DEBUG |
|
| 112 | 120 |
Serial.begin(115200); |
| 121 |
+ #endif |
|
| 113 | 122 |
|
| 123 |
+ // Mount the filesystem |
|
| 124 |
+ bool result = SPIFFS.begin(); |
|
| 125 |
+ #if DEBUG |
|
| 126 |
+ //Serial.println("SPIFFS opened: " + result);
|
|
| 127 |
+ #endif |
|
| 128 |
+ |
|
| 129 |
+ readConfigFile(); //citesc user si devid din memorie |
|
| 130 |
+ generate_vars(); //genereaza topicurile de mqtt in baza mqtt_user si mqtt_devid |
|
| 131 |
+ |
|
| 114 | 132 |
// ma conectez la AP via wifi |
| 115 | 133 |
WiFiManager wifi; |
| 134 |
+ wifi.setConfigPortalTimeout(120); // a timeout so the ESP doesn't hang waiting to be configured, for instance after a power failure |
|
| 116 | 135 |
wifi.setTimeout(180); // sta AP 3 minute apoi se reseteaza din nou |
| 117 | 136 |
if (!wifi.autoConnect("ClickHome")) {
|
| 137 |
+ #if DEBUG |
|
| 118 | 138 |
Serial.println("timeout - going to sleep");
|
| 139 |
+ #endif |
|
| 119 | 140 |
} |
| 120 | 141 |
delay(200); |
| 121 |
- |
|
| 122 |
- // ma conectez la mqtt server |
|
| 123 |
- client.setServer(mqttServer, mqttPort); |
|
| 124 |
- while (!client.connected()) {
|
|
| 125 |
- Serial.println("Conectare la MQTT ...");
|
|
| 126 |
- // fac connect cu LWT |
|
| 127 |
- if (client.connect(espName, mqttUser, mqttPassword, mqttLWT, 1, 1, "Offline")) {
|
|
| 128 |
- Serial.println("Senzor conectat la MQTT.");
|
|
| 129 |
- // trimit starea Online pe topic LWT cand ma conectez |
|
| 130 |
- client.publish(mqttLWT,"Online",TRUE); |
|
| 131 |
- // trimit informatii utile cand ma conectez |
|
| 132 |
- String esp_info = "{\"ESPMac\":\"";
|
|
| 133 |
- esp_info += getMacAddress(); |
|
| 134 |
- esp_info += "\", \"IPAddress\":\""; |
|
| 135 |
- esp_info += ipToString(WiFi.localIP()); |
|
| 136 |
- esp_info += "\"}"; |
|
| 137 |
- String netinfo = "{\"Module\":\"";
|
|
| 138 |
- netinfo += String (model); |
|
| 139 |
- netinfo += "\", \"Version\":\""; |
|
| 140 |
- netinfo += String (ver); |
|
| 141 |
- netinfo += "\"}"; |
|
| 142 |
- client.publish(mqttESP, netinfo.c_str(),TRUE); |
|
| 143 |
- Serial.println(netinfo); |
|
| 144 |
- client.publish(mqttESP, esp_info.c_str(),TRUE); |
|
| 145 |
- Serial.println(esp_info); |
|
| 146 |
- client.publish(mqttESP, "{\"LoopInterval\":\"15 minute\"}",TRUE);
|
|
| 147 |
- client.subscribe(mqttSUB); |
|
| 148 |
- } |
|
| 149 |
- else {
|
|
| 150 |
- Serial.print("Conectare MQTT esuata: ");
|
|
| 151 |
- Serial.print(client.state()); |
|
| 152 |
- delay(2000); |
|
| 153 |
- } |
|
| 154 |
- } |
|
| 155 |
- client.setCallback(getMessage); |
|
| 142 |
+ |
|
| 143 |
+ reconectez(); |
|
| 144 |
+ client.setCallback(getMessage); //setez functia care parseaza mesajele venite prin mqtt |
|
| 145 |
+ |
|
| 156 | 146 |
} |
| 157 | 147 |
|
| 158 | 148 |
void getMessage(char* topic, byte* payload, unsigned int length) {
|
| 159 | 149 |
mqttMessage=""; |
| 150 |
+ #if DEBUG |
|
| 160 | 151 |
Serial.print("Message arrived in topic: ");
|
| 161 | 152 |
Serial.println(topic); |
| 162 | 153 |
Serial.print("Message:");
|
| 154 |
+ #endif |
|
| 163 | 155 |
for (int i = 0; i < length; i++) {
|
| 164 | 156 |
mqttMessage += (char)payload[i]; |
| 165 | 157 |
} |
| 158 |
+ #if DEBUG |
|
| 166 | 159 |
Serial.println(mqttMessage); |
| 167 |
- |
|
| 160 |
+ #endif |
|
| 161 |
+ |
|
| 168 | 162 |
// daca a primit temp va trimite temperatura |
| 169 | 163 |
if (mqttMessage == "temp") |
| 170 | 164 |
{
|
| 165 |
+ #if DEBUG |
|
| 171 | 166 |
Serial.println("Trimit temperatura si umiditatea");
|
| 167 |
+ #endif |
|
| 172 | 168 |
acquireresult = DHT.acquireAndWait(2000); |
| 173 | 169 |
if ( acquireresult == 0 ) |
| 174 | 170 |
{
|
| ... | ... |
@@ -181,23 +220,36 @@ void getMessage(char* topic, byte* payload, unsigned int length) {
|
| 181 | 181 |
json += String (h); |
| 182 | 182 |
json += "}"; |
| 183 | 183 |
client.publish(mqttTEMP, json.c_str(),TRUE); |
| 184 |
+ #if DEBUG |
|
| 184 | 185 |
Serial.println(json); |
| 186 |
+ #endif |
|
| 185 | 187 |
} |
| 186 | 188 |
else |
| 187 | 189 |
{
|
| 188 | 190 |
t = h = 0; |
| 191 |
+ #if DEBUG |
|
| 189 | 192 |
Serial.println("Nu am putut citi temperatura si umiditatea");
|
| 193 |
+ #endif |
|
| 190 | 194 |
} |
| 191 | 195 |
} |
| 192 | 196 |
|
| 193 |
- // procedura de software update via WEB |
|
| 197 |
+// reset la ESP |
|
| 198 |
+ if (mqttMessage == "reset") |
|
| 199 |
+ {
|
|
| 200 |
+ String lastwords="Am fost resetat ..."; |
|
| 201 |
+ client.publish(mqttESP, lastwords.c_str(), TRUE); |
|
| 202 |
+ delay (3000); |
|
| 203 |
+ ESP.reset(); |
|
| 204 |
+ delay (5000); |
|
| 205 |
+ } |
|
| 206 |
+// procedura de software update via WEB |
|
| 194 | 207 |
if (mqttMessage == "update") |
| 195 | 208 |
{
|
| 196 | 209 |
String msg="Software update: "; |
| 197 | 210 |
t_httpUpdate_return ret; |
| 198 | 211 |
//ESPhttpUpdate.rebootOnUpdate(false); |
| 199 |
- ret = ESPhttpUpdate.update("http://update.clickhome.ro/senzor/dht/arduino.bin");
|
|
| 200 |
- //ret = ESPhttpUpdate.update("update.clickhome.ro", 80, "/senzor/dht/arduino.bin");
|
|
| 212 |
+ ret = ESPhttpUpdate.update("http://update.clickhome.ro/senzor/dallas/arduino.bin");
|
|
| 213 |
+ //ret = ESPhttpUpdate.update("update.clickhome.ro", 80, "/senzor/dallas/arduino.bin");
|
|
| 201 | 214 |
switch(ret) {
|
| 202 | 215 |
case HTTP_UPDATE_FAILED: |
| 203 | 216 |
msg.concat(" eroare:");
|
| ... | ... |
@@ -212,10 +264,13 @@ void getMessage(char* topic, byte* payload, unsigned int length) {
|
| 212 | 212 |
msg.concat(" success.");
|
| 213 | 213 |
break; |
| 214 | 214 |
} |
| 215 |
+ #if DEBUG |
|
| 215 | 216 |
Serial.println(msg); |
| 217 |
+ #endif |
|
| 216 | 218 |
} |
| 217 | 219 |
|
| 218 | 220 |
// daca a primit valoarea in minute seteaza loopTimer si-l trimite in DB |
| 221 |
+ |
|
| 219 | 222 |
if (mqttMessage == "debug") |
| 220 | 223 |
{
|
| 221 | 224 |
loopTimer=30000; // setez sa trimita valori la 30 de secunde ca sa fac debug |
| ... | ... |
@@ -248,18 +303,58 @@ void getMessage(char* topic, byte* payload, unsigned int length) {
|
| 248 | 248 |
} |
| 249 | 249 |
} |
| 250 | 250 |
|
| 251 |
+bool readConfigFile() {
|
|
| 252 |
+ // this opens the config file in read-mode |
|
| 253 |
+ File f = SPIFFS.open("/config.json", "r");
|
|
| 254 |
+ |
|
| 255 |
+ if (!f) {
|
|
| 256 |
+ #if DEBUG |
|
| 257 |
+ Serial.println("Configuration file not found");
|
|
| 258 |
+ #endif |
|
| 259 |
+ return false; |
|
| 260 |
+ } else {
|
|
| 261 |
+ // we could open the file |
|
| 262 |
+ size_t size = f.size(); |
|
| 263 |
+ // Allocate a buffer to store contents of the file. |
|
| 264 |
+ std::unique_ptr<char[]> buf(new char[size]); |
|
| 265 |
+ |
|
| 266 |
+ // Read and store file contents in buf |
|
| 267 |
+ f.readBytes(buf.get(), size); |
|
| 268 |
+ // Closing file |
|
| 269 |
+ f.close(); |
|
| 270 |
+ // Using dynamic JSON buffer which is not the recommended memory model, but anyway |
|
| 271 |
+ // See https://github.com/bblanchon/ArduinoJson/wiki/Memory%20model |
|
| 272 |
+ DynamicJsonBuffer jsonBuffer; |
|
| 273 |
+ // Parse JSON string |
|
| 274 |
+ JsonObject& json = jsonBuffer.parseObject(buf.get()); |
|
| 275 |
+ // Test if parsing succeeds. |
|
| 276 |
+ if (!json.success()) {
|
|
| 277 |
+ #if DEBUG |
|
| 278 |
+ Serial.println("JSON parseObject() failed");
|
|
| 279 |
+ #endif |
|
| 280 |
+ return false; |
|
| 281 |
+ } |
|
| 282 |
+ #if DEBUG |
|
| 283 |
+ json.printTo(Serial); |
|
| 284 |
+ #endif |
|
| 285 |
+ strcpy(mqtt_user, json["mqtt_user"]); |
|
| 286 |
+ strcpy(mqtt_devid, json["mqtt_devid"]); |
|
| 287 |
+ } |
|
| 288 |
+ return true; |
|
| 289 |
+} |
|
| 290 |
+ |
|
| 291 |
+ |
|
| 251 | 292 |
void loop() |
| 252 | 293 |
{
|
| 253 | 294 |
if (!client.connected()) {
|
| 254 |
- reconnect(); |
|
| 295 |
+ reconectez(); |
|
| 255 | 296 |
} |
| 256 | 297 |
client.loop(); |
| 257 | 298 |
long now = millis(); |
| 258 | 299 |
if (now - lastMsg > loopTimer) {
|
| 259 | 300 |
lastMsg = now; |
| 260 | 301 |
acquireresult = DHT.acquireAndWait(2000); |
| 261 |
- if ( acquireresult == 0 ) |
|
| 262 |
- {
|
|
| 302 |
+ if ( acquireresult == 0 ) {
|
|
| 263 | 303 |
loopTemp = DHT.getCelsius(); |
| 264 | 304 |
loopHumi = DHT.getHumidity(); |
| 265 | 305 |
delay(10); |
| ... | ... |
@@ -269,10 +364,9 @@ void loop() |
| 269 | 269 |
val += String (loopHumi); |
| 270 | 270 |
val += "}"; |
| 271 | 271 |
client.publish(mqttESP, val.c_str(),TRUE); |
| 272 |
- } |
|
| 273 |
- else |
|
| 274 |
- {
|
|
| 272 |
+ } else {
|
|
| 275 | 273 |
loopTemp = loopHumi = 0; |
| 276 | 274 |
} |
| 277 | 275 |
} |
| 278 |
-} |
|
| 276 |
+} |
|
| 277 |
+ |