Pārlūkot izejas kodu

implement influx and mqtt

Thomas Buck 2 gadus atpakaļ
vecāks
revīzija
f5681f98fe
6 mainītis faili ar 80 papildinājumiem un 106 dzēšanām
  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 Parādīt failu

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

+ 7
- 4
include/config.h Parādīt failu

1
 /*
1
 /*
2
  * config.h
2
  * config.h
3
  *
3
  *
4
- * ESP8266 / ESP32 Environmental Sensor
4
+ * ESP8266 / ESP32 Relais Actor
5
  *
5
  *
6
  * ----------------------------------------------------------------------------
6
  * ----------------------------------------------------------------------------
7
  * "THE BEER-WARE LICENSE" (Revision 42):
7
  * "THE BEER-WARE LICENSE" (Revision 42):
37
 // MQTT settings
37
 // MQTT settings
38
 #define MQTT_HOST "10.23.42.14"
38
 #define MQTT_HOST "10.23.42.14"
39
 #define MQTT_PORT 1883
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
 // InfluxDB settings
43
 // InfluxDB settings
42
 #define INFLUXDB_HOST "10.23.42.14"
44
 #define INFLUXDB_HOST "10.23.42.14"
44
 #define INFLUXDB_DATABASE "roomsensorsdiy"
46
 #define INFLUXDB_DATABASE "roomsensorsdiy"
45
 
47
 
46
 // feature selection
48
 // feature selection
47
-//#define ENABLE_MQTT
49
+#define ENABLE_MQTT
48
 //#define ENABLE_INFLUXDB_LOGGING
50
 //#define ENABLE_INFLUXDB_LOGGING
49
-#define ENABLE_NTP
51
+//#define ENABLE_NTP
50
 
52
 
51
 // all given in milliseconds
53
 // all given in milliseconds
52
 #define SERVER_HANDLE_INTERVAL 10
54
 #define SERVER_HANDLE_INTERVAL 10
55
 #define LED_INIT_BLINK_INTERVAL 500
57
 #define LED_INIT_BLINK_INTERVAL 500
56
 #define LED_CONNECT_BLINK_INTERVAL 250
58
 #define LED_CONNECT_BLINK_INTERVAL 250
57
 #define LED_ERROR_BLINK_INTERVAL 100
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
 #if defined(ARDUINO_ARCH_ESP8266)
63
 #if defined(ARDUINO_ARCH_ESP8266)
60
 
64
 
69
 #endif
73
 #endif
70
 
74
 
71
 #endif // __ESP_ENV_CONFIG__
75
 #endif // __ESP_ENV_CONFIG__
72
-

+ 1
- 1
include/relais.h Parādīt failu

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

+ 1
- 1
src/SimpleUpdater.cpp Parādīt failu

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

+ 69
- 98
src/main.cpp Parādīt failu

1
 /*
1
 /*
2
  * main.cpp
2
  * main.cpp
3
  *
3
  *
4
- * ESP8266 / ESP32 Environmental Sensor
4
+ * ESP8266 / ESP32 Relais Actor
5
  *
5
  *
6
  * ----------------------------------------------------------------------------
6
  * ----------------------------------------------------------------------------
7
  * "THE BEER-WARE LICENSE" (Revision 42):
7
  * "THE BEER-WARE LICENSE" (Revision 42):
50
 
50
 
51
 #define INFLUX_MAX_ERRORS_RESET 10
51
 #define INFLUX_MAX_ERRORS_RESET 10
52
 int error_count = 0;
52
 int error_count = 0;
53
+
54
+void writeDatabase();
53
 #endif // ENABLE_INFLUXDB_LOGGING
55
 #endif // ENABLE_INFLUXDB_LOGGING
54
 
56
 
55
 #ifdef ENABLE_NTP
57
 #ifdef ENABLE_NTP
64
 unsigned long last_db_write_time = 0;
66
 unsigned long last_db_write_time = 0;
65
 unsigned long last_led_blink_time = 0;
67
 unsigned long last_led_blink_time = 0;
66
 unsigned long last_timing_check_time = 0;
68
 unsigned long last_timing_check_time = 0;
69
+unsigned long last_mqtt_reconnect_time = 0;
67
 
70
 
68
 String relais_name[RELAIS_COUNT] = {
71
 String relais_name[RELAIS_COUNT] = {
69
     String(" (Small Light)"),
72
     String(" (Small Light)"),
122
 
125
 
123
     message += F("\n<p>\n");
126
     message += F("\n<p>\n");
124
     for (int i = 0; i < RELAIS_COUNT; i++) {
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
     message += F("</p>\n");
130
     message += F("</p>\n");
128
 
131
 
235
     // only reset to default after 15min
238
     // only reset to default after 15min
236
     last_timing_check_time = millis();
239
     last_timing_check_time = millis();
237
 
240
 
241
+#ifdef ENABLE_INFLUXDB_LOGGING
242
+    writeDatabase();
243
+#endif // ENABLE_INFLUXDB_LOGGING
244
+
238
     handlePage(1, id);
245
     handlePage(1, id);
239
 }
246
 }
240
 
247
 
255
     // only reset to default after 15min
262
     // only reset to default after 15min
256
     last_timing_check_time = millis();
263
     last_timing_check_time = millis();
257
 
264
 
265
+#ifdef ENABLE_INFLUXDB_LOGGING
266
+    writeDatabase();
267
+#endif // ENABLE_INFLUXDB_LOGGING
268
+
258
     handlePage(0, id);
269
     handlePage(0, id);
259
 }
270
 }
260
 
271
 
267
     int state = 0;
278
     int state = 0;
268
     int id = 0;
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
     } else {
300
     } else {
293
         return;
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
 void mqttReconnect() {
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
 #endif // ENABLE_MQTT
337
 #endif // ENABLE_MQTT
320
 
338
 
448
 }
466
 }
449
 
467
 
450
 void writeDatabase() {
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
         measurement.addTag("location", SENSOR_LOCATION);
471
         measurement.addTag("location", SENSOR_LOCATION);
481
         measurement.addTag("device", WiFi.macAddress());
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
         writeMeasurement(measurement);
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
 #endif // ENABLE_INFLUXDB_LOGGING
478
 #endif // ENABLE_INFLUXDB_LOGGING
506
 
479
 
522
 #endif // ENABLE_NTP
495
 #endif // ENABLE_NTP
523
 
496
 
524
 #ifdef ENABLE_MQTT
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
         mqttReconnect();
500
         mqttReconnect();
527
     }
501
     }
502
+
528
     mqtt.loop();
503
     mqtt.loop();
529
 #endif // ENABLE_MQTT
504
 #endif // ENABLE_MQTT
530
 
505
 
531
 #ifdef ENABLE_INFLUXDB_LOGGING
506
 #ifdef ENABLE_INFLUXDB_LOGGING
507
+#if DB_WRITE_INTERVAL > 0
532
     if ((millis() - last_db_write_time) >= DB_WRITE_INTERVAL) {
508
     if ((millis() - last_db_write_time) >= DB_WRITE_INTERVAL) {
533
         last_db_write_time = millis();
509
         last_db_write_time = millis();
534
         writeDatabase();
510
         writeDatabase();
535
     }
511
     }
512
+#endif // DB_WRITE_INTERVAL > 0
536
     
513
     
537
 #ifdef INFLUX_MAX_ERRORS_RESET
514
 #ifdef INFLUX_MAX_ERRORS_RESET
538
     if (error_count >= INFLUX_MAX_ERRORS_RESET) {
515
     if (error_count >= INFLUX_MAX_ERRORS_RESET) {
546
         last_led_blink_time = millis();
523
         last_led_blink_time = millis();
547
         digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
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 Parādīt failu

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

Notiek ielāde…
Atcelt
Saglabāt