Browse code

integrat mqtt stare/activare/dezactivare

Andrei Bucur authored on 07/03/2018 11:21:54
Showing 1 changed files
... ...
@@ -8,11 +8,12 @@
8 8
 #include <ESP8266HTTPClient.h>
9 9
 #include <ESP8266httpUpdate.h>
10 10
 
11
-#define DEBUG 1 //0=off 1=on
11
+#define DEBUG 0 //0=off 1=on
12 12
 #define PIR D1 //  pinul de date D1 la care se conecteaza PIRul
13 13
 
14
-bool pir1,lastVAL;
15
-float t_pir1;
14
+bool ARM,lastPIR;
15
+float interval_alarma = 20000; // la ce interval sa trimita notificarile 20 sec = 20000
16
+float t_alarma=0;
16 17
 
17 18
 char mqtt_user[9]; //le va lua din FS
18 19
 char mqtt_devid[5]; //le va lua din FS
... ...
@@ -20,15 +21,14 @@ char mqttPassword[15]; //urmatoarele le va genera cu functia gen
20 20
 char mqttSUB[22];
21 21
 char espName[13];
22 22
 char mqttESP[22];
23
+char mqttALM[24];
24
+char mqttPIR[22];
23 25
 char mqttLWT[22];
24 26
 
25 27
 const String model = "NodeMCU PIR";
26 28
 const String ver = "v2.0.0";
27 29
 const char* mqttServer = "mqtt.clickhome.ro";
28 30
 const int mqttPort = 1883;
29
-long loopTimer = 1000; // miliseconds - debug
30
-long lastMsg = 0;
31
-float loopTemp = 0;
32 31
 String mqttMessage;
33 32
 
34 33
 WiFiClient espClient;
... ...
@@ -51,6 +51,14 @@ void generate_vars(){
51 51
   strcat (mqttESP, espName);
52 52
   strcat (mqttESP, "/ESP");
53 53
 
54
+  strcpy (mqttPIR, "stat/");
55
+  strcat (mqttPIR, espName);
56
+  strcat (mqttPIR, "/PIR");
57
+
58
+  strcpy (mqttALM, "stat/");
59
+  strcat (mqttALM, espName);
60
+  strcat (mqttALM, "/ALARM");
61
+
54 62
   strcpy (mqttLWT, "tele/");
55 63
   strcat (mqttLWT, espName);
56 64
   strcat (mqttLWT, "/LWT");
... ...
@@ -203,16 +211,35 @@ void getMessage(char* topic, byte* payload, unsigned int length) {
203 203
         Serial.println(msg);
204 204
         #endif
205 205
    }
206
-
207
-  // reset la ESP
208
-  if (mqttMessage == "reset")
209
-  {
210
-      String lastwords="Am fost resetat ...";
211
-      client.publish(mqttESP, lastwords.c_str(), TRUE);
212
-      delay (3000);
213
-      ESP.reset();
214
-      delay (5000);
215
-  }
206
+  
207
+    if (mqttMessage == "stare")
208
+    {
209
+      // citesc starea si o trimit
210
+      String json = "{\"ARM\":\"";
211
+      json += String (ARM);
212
+      json += "\"}";
213
+      Serial.println(json);
214
+      client.publish(mqttPIR, json.c_str(),TRUE);
215
+    }
216
+  
217
+    if (mqttMessage == "ARM" || mqttMessage == "DISARM")
218
+    {
219
+      ARM = (mqttMessage=="ARM") ? true : false;
220
+      String json = "{\"ARM\":\"";
221
+      json += String (ARM);
222
+      json += "\"}";
223
+      client.publish(mqttPIR, json.c_str(),TRUE);
224
+    }
225
+  
226
+    // reset la ESP
227
+    if (mqttMessage == "reset")
228
+    {
229
+        String lastwords="Am fost resetat ...";
230
+        client.publish(mqttPIR, lastwords.c_str(), TRUE);
231
+        delay (3000);
232
+        ESP.reset();
233
+        delay (5000);
234
+    }
216 235
 
217 236
 }
218 237
 
... ...
@@ -264,21 +291,24 @@ void loop()
264 264
   }
265 265
   client.loop();
266 266
 
267
-  if (digitalRead(PIR)==1){ //verific daca nu e falsa alarma
268
-      if (t_pir1==0)
269
-        t_pir1=millis();
270
-      if (millis() - t_pir1 > 1500)
271
-        pir1=1; //este intradevar miscare
272
-  }else{
273
-      if (t_pir1>0)  t_pir1=0;
274
-      pir1=0;
275
-  }
276
-
277
-  if (pir1!=lastVAL){
278
-    lastVAL=pir1;
279
-    #if DEBUG
280
-    Serial.println(pir1);
281
-    #endif
267
+  if (ARM==1){ //daca e armat
268
+    if (digitalRead(PIR)!=lastPIR){ //daca s-a schimbat valoarea 
269
+      if (digitalRead(PIR)==1){ //daca am alarma
270
+        if (millis() - t_alarma > interval_alarma){ //daca a trecut timpul de la alarma anterioara
271
+          String json = "{\"PIR\":1}";
272
+          client.publish(mqttALM, json.c_str(),TRUE);
273
+          t_alarma=millis();
274
+          #if DEBUG
275
+          Serial.print("Trimis mqtt alarm:");
276
+          #endif
277
+        }
278
+      }
279
+      #if DEBUG
280
+      Serial.print("Direct PIN:");
281
+      Serial.println (digitalRead(PIR));
282
+      #endif
283
+      lastPIR=digitalRead(PIR);
284
+    }
282 285
   }
283 286
 }
284 287