Bläddra i källkod

implement influx and mqtt

Thomas Buck 2 år sedan
förälder
incheckning
f5681f98fe
6 ändrade filer med 80 tillägg och 106 borttagningar
  1. 1
    1
      include/SimpleUpdater.h
  2. 7
    4
      include/config.h
  3. 1
    1
      include/relais.h
  4. 1
    1
      src/SimpleUpdater.cpp
  5. 69
    98
      src/main.cpp
  6. 1
    1
      src/relais.cpp

+ 1
- 1
include/SimpleUpdater.h Visa fil

@@ -1,7 +1,7 @@
1 1
 /*
2 2
  * SimpleUpdater.h
3 3
  *
4
- * ESP8266 / ESP32 Environmental Sensor
4
+ * ESP8266 / ESP32 Relais Actor
5 5
  *
6 6
  * ----------------------------------------------------------------------------
7 7
  * "THE BEER-WARE LICENSE" (Revision 42):

+ 7
- 4
include/config.h Visa fil

@@ -1,7 +1,7 @@
1 1
 /*
2 2
  * config.h
3 3
  *
4
- * ESP8266 / ESP32 Environmental Sensor
4
+ * ESP8266 / ESP32 Relais Actor
5 5
  *
6 6
  * ----------------------------------------------------------------------------
7 7
  * "THE BEER-WARE LICENSE" (Revision 42):
@@ -37,6 +37,8 @@
37 37
 // MQTT settings
38 38
 #define MQTT_HOST "10.23.42.14"
39 39
 #define MQTT_PORT 1883
40
+#define MQTT_USER "USERNAME" // undef to disable auth
41
+#define MQTT_PASS "PASSWORD" // undef to disable auth
40 42
 
41 43
 // InfluxDB settings
42 44
 #define INFLUXDB_HOST "10.23.42.14"
@@ -44,9 +46,9 @@
44 46
 #define INFLUXDB_DATABASE "roomsensorsdiy"
45 47
 
46 48
 // feature selection
47
-//#define ENABLE_MQTT
49
+#define ENABLE_MQTT
48 50
 //#define ENABLE_INFLUXDB_LOGGING
49
-#define ENABLE_NTP
51
+//#define ENABLE_NTP
50 52
 
51 53
 // all given in milliseconds
52 54
 #define SERVER_HANDLE_INTERVAL 10
@@ -55,6 +57,8 @@
55 57
 #define LED_INIT_BLINK_INTERVAL 500
56 58
 #define LED_CONNECT_BLINK_INTERVAL 250
57 59
 #define LED_ERROR_BLINK_INTERVAL 100
60
+#define DB_WRITE_INTERVAL 0 // 0 to disable
61
+#define MQTT_RECONNECT_INTERVAL 5000
58 62
 
59 63
 #if defined(ARDUINO_ARCH_ESP8266)
60 64
 
@@ -69,4 +73,3 @@
69 73
 #endif
70 74
 
71 75
 #endif // __ESP_ENV_CONFIG__
72
-

+ 1
- 1
include/relais.h Visa fil

@@ -1,7 +1,7 @@
1 1
 /*
2 2
  * relais.h
3 3
  *
4
- * ESP8266 / ESP32 Environmental Sensor
4
+ * ESP8266 / ESP32 Relais Actor
5 5
  *
6 6
  * ----------------------------------------------------------------------------
7 7
  * "THE BEER-WARE LICENSE" (Revision 42):

+ 1
- 1
src/SimpleUpdater.cpp Visa fil

@@ -1,7 +1,7 @@
1 1
 /*
2 2
  * SimpleUpdater.cpp
3 3
  *
4
- * ESP8266 / ESP32 Environmental Sensor
4
+ * ESP8266 / ESP32 Relais Actor
5 5
  *
6 6
  * ----------------------------------------------------------------------------
7 7
  * "THE BEER-WARE LICENSE" (Revision 42):

+ 69
- 98
src/main.cpp Visa fil

@@ -1,7 +1,7 @@
1 1
 /*
2 2
  * main.cpp
3 3
  *
4
- * ESP8266 / ESP32 Environmental Sensor
4
+ * ESP8266 / ESP32 Relais Actor
5 5
  *
6 6
  * ----------------------------------------------------------------------------
7 7
  * "THE BEER-WARE LICENSE" (Revision 42):
@@ -50,6 +50,8 @@ Influxdb influx(INFLUXDB_HOST, INFLUXDB_PORT);
50 50
 
51 51
 #define INFLUX_MAX_ERRORS_RESET 10
52 52
 int error_count = 0;
53
+
54
+void writeDatabase();
53 55
 #endif // ENABLE_INFLUXDB_LOGGING
54 56
 
55 57
 #ifdef ENABLE_NTP
@@ -64,6 +66,7 @@ unsigned long last_server_handle_time = 0;
64 66
 unsigned long last_db_write_time = 0;
65 67
 unsigned long last_led_blink_time = 0;
66 68
 unsigned long last_timing_check_time = 0;
69
+unsigned long last_mqtt_reconnect_time = 0;
67 70
 
68 71
 String relais_name[RELAIS_COUNT] = {
69 72
     String(" (Small Light)"),
@@ -122,7 +125,7 @@ void handlePage(int mode = -1, int id = 0) {
122 125
 
123 126
     message += F("\n<p>\n");
124 127
     for (int i = 0; i < RELAIS_COUNT; i++) {
125
-        message += String(F("Relais ")) + String(i) + String(F(" = ")) + (relais_state[i] ? String(F("On")) : String(F("Off"))) + String(F("<br>\n"));
128
+        message += String(F("Relais ")) + String(i) + relais_name[i] + String(F(" = ")) + (relais_state[i] ? String(F("On")) : String(F("Off"))) + String(F("<br>\n"));
126 129
     }
127 130
     message += F("</p>\n");
128 131
 
@@ -235,6 +238,10 @@ void handleOn() {
235 238
     // only reset to default after 15min
236 239
     last_timing_check_time = millis();
237 240
 
241
+#ifdef ENABLE_INFLUXDB_LOGGING
242
+    writeDatabase();
243
+#endif // ENABLE_INFLUXDB_LOGGING
244
+
238 245
     handlePage(1, id);
239 246
 }
240 247
 
@@ -255,6 +262,10 @@ void handleOff() {
255 262
     // only reset to default after 15min
256 263
     last_timing_check_time = millis();
257 264
 
265
+#ifdef ENABLE_INFLUXDB_LOGGING
266
+    writeDatabase();
267
+#endif // ENABLE_INFLUXDB_LOGGING
268
+
258 269
     handlePage(0, id);
259 270
 }
260 271
 
@@ -267,54 +278,61 @@ void mqttCallback(char* topic, byte* payload, unsigned int length) {
267 278
     int state = 0;
268 279
     int id = 0;
269 280
 
270
-    // TODO check topic matches
271
-
272
-    if (((char)payload[0] == 't')
273
-            && ((char)payload[1] == 'u')
274
-            && ((char)payload[2] == 'r')
275
-            && ((char)payload[3] == 'n')
276
-            && ((char)payload[4] == ' ')
277
-            && ((char)payload[5] == 'o')) {
278
-        if (((char)payload[6] == 'n')
279
-            && ((char)payload[7] == ' ')) {
280
-            // turn on
281
-            state = 1;
282
-            id = payload[8];
283
-        } else if (((char)payload[6] == 'f')
284
-                && ((char)payload[7] == 'f')
285
-                && ((char)payload[8] == ' ')) {
286
-            // turn off
287
-            state = 0;
288
-            id = payload[9];
289
-        } else {
290
-            return;
291
-        }
281
+    String ts(topic), ps((char *)payload);
282
+
283
+    String our_topic(SENSOR_LOCATION);
284
+    our_topic += "/";
285
+
286
+    if (!ts.startsWith(our_topic)) {
287
+        return;
288
+    }
289
+
290
+    String ids = ts.substring(our_topic.length());
291
+    id = ids.toInt();
292
+    if ((id < 0) || (id >= RELAIS_COUNT)) {
293
+        return;
294
+    }
295
+
296
+    if (ps.indexOf("on") != -1) {
297
+        state = 1;
298
+    } else if (ps.indexOf("off") != -1) {
299
+        state = 0;
292 300
     } else {
293 301
         return;
294 302
     }
295 303
 
296
-    relais_set(id - 1, state);
297
-    relais_state[id - 1] = state ? true : false;
304
+    if ((id >= 0) && (id < RELAIS_COUNT)) {
305
+        relais_set(id, state);
306
+        relais_state[id] = state ? true : false;
307
+
308
+#ifdef ENABLE_INFLUXDB_LOGGING
309
+        writeDatabase();
310
+#endif // ENABLE_INFLUXDB_LOGGING
311
+    }
298 312
 }
299 313
 
300 314
 void mqttReconnect() {
301
-    // Loop until we're reconnected
302
-    //while (!mqtt.connected()) {
303
-        // Create a random client ID
304
-        String clientId = "ESP8266Client-";
305
-        clientId += String(random(0xffff), HEX);
306
-
307
-        // Attempt to connect
308
-        if (mqtt.connect(clientId.c_str())) {
309
-            // Once connected, publish an announcement...
310
-            //mqtt.publish("outTopic", "hello world");
311
-            // ... and resubscribe
312
-            mqtt.subscribe(SENSOR_LOCATION);
313
-        } else {
314
-            // Wait 5 seconds before retrying
315
-            delay(5000);
315
+    // Create a random client ID
316
+    String clientId = "ESP8266-Relais-";
317
+    clientId += String(random(0xffff), HEX);
318
+
319
+    // Attempt to connect
320
+#if defined(MQTT_USER) && defined(MQTT_PASS)
321
+    if (mqtt.connect(clientId.c_str(), MQTT_USER, MQTT_PASS)) {
322
+#else
323
+    if (mqtt.connect(clientId.c_str())) {
324
+#endif
325
+        // Once connected, publish an announcement...
326
+        mqtt.publish(SENSOR_LOCATION, "hello world");
327
+
328
+        // ... and resubscribe
329
+        mqtt.subscribe(SENSOR_LOCATION);
330
+        for (int i = 0; i < RELAIS_COUNT; i++) {
331
+            String topic(SENSOR_LOCATION);
332
+            topic += String("/") + String(i);
333
+            mqtt.subscribe(topic.c_str());
316 334
         }
317
-    //}
335
+    }
318 336
 }
319 337
 #endif // ENABLE_MQTT
320 338
 
@@ -448,59 +466,14 @@ static boolean writeMeasurement(InfluxData &measurement) {
448 466
 }
449 467
 
450 468
 void writeDatabase() {
451
-    if (found_bme1) {
452
-        InfluxData measurement("environment");
453
-        measurement.addTag("location", SENSOR_LOCATION);
454
-        measurement.addTag("placement", "1");
455
-        measurement.addTag("device", WiFi.macAddress());
456
-        measurement.addTag("sensor", "bme280");
457
-
458
-        measurement.addValue("temperature", bme1_temp());
459
-        measurement.addValue("pressure", bme1_pressure());
460
-        measurement.addValue("humidity", bme1_humid());
461
-
462
-        writeMeasurement(measurement);
463
-    }
464
-    if (found_bme2) {
465
-        InfluxData measurement("environment");
466
-        measurement.addTag("location", SENSOR_LOCATION);
467
-        measurement.addTag("placement", "2");
468
-        measurement.addTag("device", WiFi.macAddress());
469
-        measurement.addTag("sensor", "bme280");
470
-
471
-        measurement.addValue("temperature", bme2_temp());
472
-        measurement.addValue("pressure", bme2_pressure());
473
-        measurement.addValue("humidity", bme2_humid());
474
-
475
-        writeMeasurement(measurement);
476
-    }
477
-
478
-    if (found_sht) {
479
-        InfluxData measurement("environment");
469
+    for (int i = 0; i < RELAIS_COUNT; i++) {
470
+        InfluxData measurement("relais");
480 471
         measurement.addTag("location", SENSOR_LOCATION);
481 472
         measurement.addTag("device", WiFi.macAddress());
482
-        measurement.addTag("sensor", "sht21");
483
-
484
-        measurement.addValue("temperature", sht_temp());
485
-        measurement.addValue("humidity", sht_humid());
486
-
473
+        measurement.addTag("id", String(i));
474
+        measurement.addValue("state", relais_state[i] ? 1 : 0);
487 475
         writeMeasurement(measurement);
488 476
     }
489
-    
490
-    for (int i = 0; i < moisture_count(); i++) {
491
-        int moisture = moisture_read(i);
492
-        if (moisture < moisture_max()) {
493
-            InfluxData measurement("moisture");
494
-            measurement.addTag("location", SENSOR_LOCATION);
495
-            measurement.addTag("device", WiFi.macAddress());
496
-            measurement.addTag("sensor", String(i + 1));
497
-
498
-            measurement.addValue("value", moisture);
499
-            measurement.addValue("maximum", moisture_max());
500
-
501
-            writeMeasurement(measurement);
502
-        }
503
-    }
504 477
 }
505 478
 #endif // ENABLE_INFLUXDB_LOGGING
506 479
 
@@ -522,17 +495,21 @@ void loop() {
522 495
 #endif // ENABLE_NTP
523 496
 
524 497
 #ifdef ENABLE_MQTT
525
-    if (!mqtt.connected()) {
498
+    if (!mqtt.connected() && ((millis() - last_mqtt_reconnect_time) >= MQTT_RECONNECT_INTERVAL)) {
499
+        last_mqtt_reconnect_time = millis();
526 500
         mqttReconnect();
527 501
     }
502
+
528 503
     mqtt.loop();
529 504
 #endif // ENABLE_MQTT
530 505
 
531 506
 #ifdef ENABLE_INFLUXDB_LOGGING
507
+#if DB_WRITE_INTERVAL > 0
532 508
     if ((millis() - last_db_write_time) >= DB_WRITE_INTERVAL) {
533 509
         last_db_write_time = millis();
534 510
         writeDatabase();
535 511
     }
512
+#endif // DB_WRITE_INTERVAL > 0
536 513
     
537 514
 #ifdef INFLUX_MAX_ERRORS_RESET
538 515
     if (error_count >= INFLUX_MAX_ERRORS_RESET) {
@@ -546,10 +523,4 @@ void loop() {
546 523
         last_led_blink_time = millis();
547 524
         digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
548 525
     }
549
-    
550
-    // reset ESP every 48h to be safe
551
-    if (millis() >= (48 * 60 * 60 * 1000)) {
552
-        ESP.restart();
553
-    }
554 526
 }
555
-

+ 1
- 1
src/relais.cpp Visa fil

@@ -1,7 +1,7 @@
1 1
 /*
2 2
  * relais.cpp
3 3
  *
4
- * ESP8266 / ESP32 Environmental Sensor
4
+ * ESP8266 / ESP32 Relais Actor
5 5
  *
6 6
  * ----------------------------------------------------------------------------
7 7
  * "THE BEER-WARE LICENSE" (Revision 42):

Laddar…
Avbryt
Spara