|
@@ -0,0 +1,545 @@
|
|
1
|
+/*
|
|
2
|
+ * main.cpp
|
|
3
|
+ *
|
|
4
|
+ * ESP8266 / ESP32 Environmental Sensor
|
|
5
|
+ *
|
|
6
|
+ * ----------------------------------------------------------------------------
|
|
7
|
+ * "THE BEER-WARE LICENSE" (Revision 42):
|
|
8
|
+ * <xythobuz@xythobuz.de> wrote this file. As long as you retain this notice
|
|
9
|
+ * you can do whatever you want with this stuff. If we meet some day, and you
|
|
10
|
+ * think this stuff is worth it, you can buy me a beer in return. Thomas Buck
|
|
11
|
+ * ----------------------------------------------------------------------------
|
|
12
|
+ */
|
|
13
|
+
|
|
14
|
+#include <Arduino.h>
|
|
15
|
+#include <Wire.h>
|
|
16
|
+#include <PubSubClient.h>
|
|
17
|
+
|
|
18
|
+#if defined(ARDUINO_ARCH_ESP8266)
|
|
19
|
+
|
|
20
|
+#include <ESP8266WiFi.h>
|
|
21
|
+#include <ESP8266WebServer.h>
|
|
22
|
+#include <ESP8266mDNS.h>
|
|
23
|
+
|
|
24
|
+#elif defined(ARDUINO_ARCH_ESP32)
|
|
25
|
+
|
|
26
|
+#include <WiFi.h>
|
|
27
|
+#include <WebServer.h>
|
|
28
|
+#include <ESPmDNS.h>
|
|
29
|
+
|
|
30
|
+#endif
|
|
31
|
+
|
|
32
|
+#include "config.h"
|
|
33
|
+#include "relais.h"
|
|
34
|
+#include "SimpleUpdater.h"
|
|
35
|
+
|
|
36
|
+#define BUILTIN_LED_PIN 1
|
|
37
|
+
|
|
38
|
+UPDATE_WEB_SERVER server(80);
|
|
39
|
+SimpleUpdater updater;
|
|
40
|
+
|
|
41
|
+#ifdef ENABLE_MQTT
|
|
42
|
+WiFiClient mqttClient;
|
|
43
|
+PubSubClient mqtt(mqttClient);
|
|
44
|
+#endif // ENABLE_MQTT
|
|
45
|
+
|
|
46
|
+#ifdef ENABLE_INFLUXDB_LOGGING
|
|
47
|
+#include <InfluxDb.h>
|
|
48
|
+
|
|
49
|
+Influxdb influx(INFLUXDB_HOST, INFLUXDB_PORT);
|
|
50
|
+
|
|
51
|
+#define INFLUX_MAX_ERRORS_RESET 10
|
|
52
|
+int error_count = 0;
|
|
53
|
+#endif // ENABLE_INFLUXDB_LOGGING
|
|
54
|
+
|
|
55
|
+#ifdef ENABLE_NTP
|
|
56
|
+#include <WiFiUdp.h>
|
|
57
|
+#include <NTPClient.h>
|
|
58
|
+
|
|
59
|
+WiFiUDP ntpUDP;
|
|
60
|
+NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", 2 * 60 * 60, 5 * 60 * 1000);
|
|
61
|
+#endif // ENABLE_NTP
|
|
62
|
+
|
|
63
|
+unsigned long last_server_handle_time = 0;
|
|
64
|
+unsigned long last_db_write_time = 0;
|
|
65
|
+unsigned long last_led_blink_time = 0;
|
|
66
|
+unsigned long last_timing_check_time = 0;
|
|
67
|
+
|
|
68
|
+String relais_name[RELAIS_COUNT] = {
|
|
69
|
+ String(" (Small Light)"),
|
|
70
|
+ String(" (Big Light)"),
|
|
71
|
+ String(""),
|
|
72
|
+ String(" (Fan)"),
|
|
73
|
+};
|
|
74
|
+
|
|
75
|
+bool relais_state[RELAIS_COUNT] = { false, false, false, false };
|
|
76
|
+String auto_set_state = String("");
|
|
77
|
+
|
|
78
|
+#ifdef ENABLE_NTP
|
|
79
|
+void handleAutoTimingCheck(void) {
|
|
80
|
+ relais_state[3] = true; // always keep fan on
|
|
81
|
+
|
|
82
|
+ if ((timeClient.getHours() >= 20) || (timeClient.getHours() < 10)) {
|
|
83
|
+ // small light from 20:00 to 10:00
|
|
84
|
+ relais_state[0] = true;
|
|
85
|
+ relais_state[1] = false;
|
|
86
|
+ auto_set_state = F("Small Light at Night (") + timeClient.getFormattedTime() + F(")");
|
|
87
|
+ } else {
|
|
88
|
+ // big light from 10:00 to 20:00
|
|
89
|
+ relais_state[0] = false;
|
|
90
|
+ relais_state[1] = true;
|
|
91
|
+ auto_set_state = F("Big Light while Daylight (") + timeClient.getFormattedTime() + F(")");
|
|
92
|
+ }
|
|
93
|
+
|
|
94
|
+ for (int i = 0; i < RELAIS_COUNT; i++) {
|
|
95
|
+ relais_set(i, relais_state[i] ? 1 : 0);
|
|
96
|
+ }
|
|
97
|
+}
|
|
98
|
+#endif // ENABLE_NTP
|
|
99
|
+
|
|
100
|
+void handlePage(int mode = -1, int id = 0) {
|
|
101
|
+ String message = F("<html><head>\n");
|
|
102
|
+ message += F("<title>" ESP_PLATFORM_NAME " MQTT Relais</title>\n");
|
|
103
|
+ message += F("</head><body>\n");
|
|
104
|
+ message += F("<h1>" ESP_PLATFORM_NAME " MQTT Relais</h1>\n");
|
|
105
|
+
|
|
106
|
+ message += F("\n<p>\n");
|
|
107
|
+ for (int i = 0; i < RELAIS_COUNT; i++) {
|
|
108
|
+ message += String(F("<a href=\"/on?id=")) + String(i) + String(F("\">Relais ")) + String(i) + String(F(" On")) + relais_name[i] + String(F("</a><br>\n"));
|
|
109
|
+ message += String(F("<a href=\"/off?id=")) + String(i) + String(F("\">Relais ")) + String(i) + String(F(" Off")) + relais_name[i] + String(F("</a><br>\n"));
|
|
110
|
+ }
|
|
111
|
+ message += String(F("<a href=\"/on?id=")) + String(RELAIS_COUNT) + String(F("\">All Relais On</a><br>\n"));
|
|
112
|
+ message += String(F("<a href=\"/off?id=")) + String(RELAIS_COUNT) + String(F("\">All Relais Off</a><br>\n"));
|
|
113
|
+ message += F("</p>\n");
|
|
114
|
+
|
|
115
|
+ if (mode >= 0) {
|
|
116
|
+ message += F("<p>");
|
|
117
|
+ message += F("Turned Relais ");
|
|
118
|
+ message += (id < RELAIS_COUNT) ? String(id) : String(F("1-4"));
|
|
119
|
+ message += (mode ? String(F(" On")) : String(F(" Off")));
|
|
120
|
+ message += F("</p>\n");
|
|
121
|
+ }
|
|
122
|
+
|
|
123
|
+ message += F("\n<p>\n");
|
|
124
|
+ 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"));
|
|
126
|
+ }
|
|
127
|
+ message += F("</p>\n");
|
|
128
|
+
|
|
129
|
+#ifdef ENABLE_NTP
|
|
130
|
+ message += F("<p>Time from NTP: ");
|
|
131
|
+ message += timeClient.getFormattedTime();
|
|
132
|
+ message += F("</p>\n");
|
|
133
|
+
|
|
134
|
+ message += F("<p>Auto-State: ");
|
|
135
|
+ message += auto_set_state;
|
|
136
|
+ message += F("</p>\n");
|
|
137
|
+
|
|
138
|
+ unsigned long next_min = (AUTO_TIMING_INTERVAL - (millis() - last_timing_check_time)) / (1000 * 60);
|
|
139
|
+ message += F("<p>Next Auto-State in ");
|
|
140
|
+ message += String(next_min);
|
|
141
|
+ message += F("min</p>\n");
|
|
142
|
+#endif // ENABLE_NTP
|
|
143
|
+
|
|
144
|
+ message += F("\n<p>\n");
|
|
145
|
+ message += F("Version: ");
|
|
146
|
+ message += esp_relais_version;
|
|
147
|
+ message += F("\n<br>\n");
|
|
148
|
+ message += F("Location: ");
|
|
149
|
+ message += sensor_location;
|
|
150
|
+ message += F("\n<br>\n");
|
|
151
|
+ message += F("MAC: ");
|
|
152
|
+ message += WiFi.macAddress();
|
|
153
|
+ message += F("\n</p>\n");
|
|
154
|
+
|
|
155
|
+#if defined(ARDUINO_ARCH_ESP8266)
|
|
156
|
+
|
|
157
|
+ message += F("\n<p>\n");
|
|
158
|
+ message += F("Reset reason: ");
|
|
159
|
+ message += ESP.getResetReason();
|
|
160
|
+ message += F("\n<br>\n");
|
|
161
|
+ message += F("Free heap: ");
|
|
162
|
+ message += String(ESP.getFreeHeap());
|
|
163
|
+ message += F(" (");
|
|
164
|
+ message += String(ESP.getHeapFragmentation());
|
|
165
|
+ message += F("% fragmentation)");
|
|
166
|
+ message += F("\n<br>\n");
|
|
167
|
+ message += F("Free sketch space: ");
|
|
168
|
+ message += String(ESP.getFreeSketchSpace());
|
|
169
|
+ message += F("\n<br>\n");
|
|
170
|
+ message += F("Flash chip real size: ");
|
|
171
|
+ message += String(ESP.getFlashChipRealSize());
|
|
172
|
+
|
|
173
|
+ if (ESP.getFlashChipSize() != ESP.getFlashChipRealSize()) {
|
|
174
|
+ message += F("\n<br>\n");
|
|
175
|
+ message += F("WARNING: sdk chip size (");
|
|
176
|
+ message += (ESP.getFlashChipSize());
|
|
177
|
+ message += F(") does not match!");
|
|
178
|
+ }
|
|
179
|
+
|
|
180
|
+ message += F("\n</p>\n");
|
|
181
|
+
|
|
182
|
+#elif defined(ARDUINO_ARCH_ESP32)
|
|
183
|
+
|
|
184
|
+ message += F("\n<p>\n");
|
|
185
|
+ message += F("Free heap: ");
|
|
186
|
+ message += String(ESP.getFreeHeap() / 1024.0);
|
|
187
|
+ message += F("k\n<br>\n");
|
|
188
|
+ message += F("Free sketch space: ");
|
|
189
|
+ message += String(ESP.getFreeSketchSpace() / 1024.0);
|
|
190
|
+ message += F("k\n<br>\n");
|
|
191
|
+ message += F("Flash chip size: ");
|
|
192
|
+ message += String(ESP.getFlashChipSize() / 1024.0);
|
|
193
|
+ message += F("k\n</p>\n");
|
|
194
|
+
|
|
195
|
+#endif
|
|
196
|
+
|
|
197
|
+ message += F("<p>\n");
|
|
198
|
+ message += F("Try <a href=\"/update\">/update</a> for OTA firmware updates!\n");
|
|
199
|
+ message += F("</p>\n");
|
|
200
|
+
|
|
201
|
+ message += F("<p>\n");
|
|
202
|
+#ifdef ENABLE_INFLUXDB_LOGGING
|
|
203
|
+ message += F("InfluxDB: ");
|
|
204
|
+ message += INFLUXDB_DATABASE;
|
|
205
|
+ message += F(" @ ");
|
|
206
|
+ message += INFLUXDB_HOST;
|
|
207
|
+ message += F(":");
|
|
208
|
+ message += String(INFLUXDB_PORT);
|
|
209
|
+ message += F("\n");
|
|
210
|
+#else
|
|
211
|
+ message += F("InfluxDB logging not enabled!\n");
|
|
212
|
+#endif
|
|
213
|
+ message += F("</p>\n");
|
|
214
|
+
|
|
215
|
+ message += F("</body></html>\n");
|
|
216
|
+ server.send(200, "text/html", message);
|
|
217
|
+}
|
|
218
|
+
|
|
219
|
+void handleOn() {
|
|
220
|
+ String id_string = server.arg("id");
|
|
221
|
+ int id = id_string.toInt();
|
|
222
|
+
|
|
223
|
+ if ((id >= 0) && (id < RELAIS_COUNT)) {
|
|
224
|
+ relais_set(id, 1);
|
|
225
|
+ relais_state[id] = true;
|
|
226
|
+ } else {
|
|
227
|
+ for (int i = 0; i < RELAIS_COUNT; i++) {
|
|
228
|
+ relais_set(i, 1);
|
|
229
|
+ relais_state[i] = true;
|
|
230
|
+ }
|
|
231
|
+ }
|
|
232
|
+
|
|
233
|
+ // only reset to default after 15min
|
|
234
|
+ last_timing_check_time = millis();
|
|
235
|
+
|
|
236
|
+ handlePage(1, id);
|
|
237
|
+}
|
|
238
|
+
|
|
239
|
+void handleOff() {
|
|
240
|
+ String id_string = server.arg("id");
|
|
241
|
+ int id = id_string.toInt();
|
|
242
|
+
|
|
243
|
+ if ((id >= 0) && (id < RELAIS_COUNT)) {
|
|
244
|
+ relais_set(id, 0);
|
|
245
|
+ relais_state[id] = false;
|
|
246
|
+ } else {
|
|
247
|
+ for (int i = 0; i < RELAIS_COUNT; i++) {
|
|
248
|
+ relais_set(i, 0);
|
|
249
|
+ relais_state[i] = false;
|
|
250
|
+ }
|
|
251
|
+ }
|
|
252
|
+
|
|
253
|
+ // only reset to default after 15min
|
|
254
|
+ last_timing_check_time = millis();
|
|
255
|
+
|
|
256
|
+ handlePage(0, id);
|
|
257
|
+}
|
|
258
|
+
|
|
259
|
+void handleRoot() {
|
|
260
|
+ handlePage();
|
|
261
|
+}
|
|
262
|
+
|
|
263
|
+#ifdef ENABLE_MQTT
|
|
264
|
+void mqttCallback(char* topic, byte* payload, unsigned int length) {
|
|
265
|
+ int state = 0;
|
|
266
|
+ int id = 0;
|
|
267
|
+
|
|
268
|
+ // TODO check topic matches
|
|
269
|
+
|
|
270
|
+ if (((char)payload[0] == 't')
|
|
271
|
+ && ((char)payload[1] == 'u')
|
|
272
|
+ && ((char)payload[2] == 'r')
|
|
273
|
+ && ((char)payload[3] == 'n')
|
|
274
|
+ && ((char)payload[4] == ' ')
|
|
275
|
+ && ((char)payload[5] == 'o')) {
|
|
276
|
+ if (((char)payload[6] == 'n')
|
|
277
|
+ && ((char)payload[7] == ' ')) {
|
|
278
|
+ // turn on
|
|
279
|
+ state = 1;
|
|
280
|
+ id = payload[8];
|
|
281
|
+ } else if (((char)payload[6] == 'f')
|
|
282
|
+ && ((char)payload[7] == 'f')
|
|
283
|
+ && ((char)payload[8] == ' ')) {
|
|
284
|
+ // turn off
|
|
285
|
+ state = 0;
|
|
286
|
+ id = payload[9];
|
|
287
|
+ } else {
|
|
288
|
+ return;
|
|
289
|
+ }
|
|
290
|
+ } else {
|
|
291
|
+ return;
|
|
292
|
+ }
|
|
293
|
+
|
|
294
|
+ relais_set(id - 1, state);
|
|
295
|
+ relais_state[id - 1] = state ? true : false;
|
|
296
|
+}
|
|
297
|
+
|
|
298
|
+void mqttReconnect() {
|
|
299
|
+ // Loop until we're reconnected
|
|
300
|
+ //while (!mqtt.connected()) {
|
|
301
|
+ // Create a random client ID
|
|
302
|
+ String clientId = "ESP8266Client-";
|
|
303
|
+ clientId += String(random(0xffff), HEX);
|
|
304
|
+
|
|
305
|
+ // Attempt to connect
|
|
306
|
+ if (mqtt.connect(clientId.c_str())) {
|
|
307
|
+ // Once connected, publish an announcement...
|
|
308
|
+ //mqtt.publish("outTopic", "hello world");
|
|
309
|
+ // ... and resubscribe
|
|
310
|
+ mqtt.subscribe(sensor_location);
|
|
311
|
+ } else {
|
|
312
|
+ // Wait 5 seconds before retrying
|
|
313
|
+ delay(5000);
|
|
314
|
+ }
|
|
315
|
+ //}
|
|
316
|
+}
|
|
317
|
+#endif // ENABLE_MQTT
|
|
318
|
+
|
|
319
|
+void setup() {
|
|
320
|
+ pinMode(BUILTIN_LED_PIN, OUTPUT);
|
|
321
|
+
|
|
322
|
+ relais_init();
|
|
323
|
+
|
|
324
|
+ for (int i = 0; i < RELAIS_COUNT; i++) {
|
|
325
|
+ relais_state[i] = false;
|
|
326
|
+ }
|
|
327
|
+
|
|
328
|
+ Serial.println();
|
|
329
|
+ Serial.println("Hello World From Relais Test");
|
|
330
|
+
|
|
331
|
+ // Blink LED for init
|
|
332
|
+ for (int i = 0; i < 2; i++) {
|
|
333
|
+ digitalWrite(BUILTIN_LED_PIN, LOW); // LED on
|
|
334
|
+ delay(LED_INIT_BLINK_INTERVAL);
|
|
335
|
+ digitalWrite(BUILTIN_LED_PIN, HIGH); // LED off
|
|
336
|
+ delay(LED_INIT_BLINK_INTERVAL);
|
|
337
|
+ }
|
|
338
|
+
|
|
339
|
+ // Build hostname string
|
|
340
|
+ String hostname = SENSOR_HOSTNAME_PREFIX;
|
|
341
|
+ hostname += sensor_location;
|
|
342
|
+
|
|
343
|
+#if defined(ARDUINO_ARCH_ESP8266)
|
|
344
|
+
|
|
345
|
+ // Connect to WiFi AP
|
|
346
|
+ Serial.print("Connecting to WiFi");
|
|
347
|
+ WiFi.hostname(hostname);
|
|
348
|
+ WiFi.mode(WIFI_STA);
|
|
349
|
+ WiFi.begin(ssid, password);
|
|
350
|
+ while (WiFi.status() != WL_CONNECTED) {
|
|
351
|
+ Serial.print(".");
|
|
352
|
+ delay(LED_CONNECT_BLINK_INTERVAL);
|
|
353
|
+ digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
|
|
354
|
+ }
|
|
355
|
+ Serial.println();
|
|
356
|
+
|
|
357
|
+#elif defined(ARDUINO_ARCH_ESP32)
|
|
358
|
+
|
|
359
|
+ // Set hostname workaround
|
|
360
|
+ WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
|
|
361
|
+ WiFi.setHostname(hostname.c_str());
|
|
362
|
+
|
|
363
|
+ // Workaround for WiFi connecting only every 2nd reset
|
|
364
|
+ // https://github.com/espressif/arduino-esp32/issues/2501#issuecomment-513602522
|
|
365
|
+ WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info) {
|
|
366
|
+ if (info.disconnected.reason == 202) {
|
|
367
|
+ esp_sleep_enable_timer_wakeup(10);
|
|
368
|
+ esp_deep_sleep_start();
|
|
369
|
+ delay(100);
|
|
370
|
+ }
|
|
371
|
+ }, WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
|
|
372
|
+
|
|
373
|
+ // Connect to WiFi AP
|
|
374
|
+ WiFi.mode(WIFI_STA);
|
|
375
|
+ WiFi.begin(ssid, password);
|
|
376
|
+ while (WiFi.status() != WL_CONNECTED) {
|
|
377
|
+ delay(LED_CONNECT_BLINK_INTERVAL);
|
|
378
|
+ digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
|
|
379
|
+ }
|
|
380
|
+
|
|
381
|
+ // Set hostname workaround
|
|
382
|
+ WiFi.setHostname(hostname.c_str());
|
|
383
|
+
|
|
384
|
+#endif
|
|
385
|
+
|
|
386
|
+ Serial.println("Seeding");
|
|
387
|
+ randomSeed(micros());
|
|
388
|
+
|
|
389
|
+#ifdef ENABLE_MQTT
|
|
390
|
+ Serial.println("MQTT");
|
|
391
|
+ mqtt.setServer(MQTT_HOST, MQTT_PORT);
|
|
392
|
+ mqtt.setCallback(mqttCallback);
|
|
393
|
+#endif // ENABLE_MQTT
|
|
394
|
+
|
|
395
|
+#ifdef ENABLE_INFLUXDB_LOGGING
|
|
396
|
+ // Setup InfluxDB Client
|
|
397
|
+ influx.setDb(INFLUXDB_DATABASE);
|
|
398
|
+#endif // ENABLE_INFLUXDB_LOGGING
|
|
399
|
+
|
|
400
|
+#ifdef ENABLE_NTP
|
|
401
|
+ timeClient.begin();
|
|
402
|
+#endif // ENABLE_NTP
|
|
403
|
+
|
|
404
|
+ // Setup HTTP Server
|
|
405
|
+ Serial.println("Server");
|
|
406
|
+ MDNS.begin(hostname.c_str());
|
|
407
|
+ updater.setup(&server);
|
|
408
|
+ server.on("/", handleRoot);
|
|
409
|
+ server.on("/on", handleOn);
|
|
410
|
+ server.on("/off", handleOff);
|
|
411
|
+ server.begin();
|
|
412
|
+ MDNS.addService("http", "tcp", 80);
|
|
413
|
+
|
|
414
|
+ Serial.println("Done");
|
|
415
|
+}
|
|
416
|
+
|
|
417
|
+void handleServers() {
|
|
418
|
+ server.handleClient();
|
|
419
|
+
|
|
420
|
+#if defined(ARDUINO_ARCH_ESP8266)
|
|
421
|
+ MDNS.update();
|
|
422
|
+#endif
|
|
423
|
+}
|
|
424
|
+
|
|
425
|
+#ifdef ENABLE_INFLUXDB_LOGGING
|
|
426
|
+static boolean writeMeasurement(InfluxData &measurement) {
|
|
427
|
+ boolean success = influx.write(measurement);
|
|
428
|
+ if (!success) {
|
|
429
|
+ error_count++;
|
|
430
|
+ for (int i = 0; i < 10; i++) {
|
|
431
|
+ digitalWrite(BUILTIN_LED_PIN, LOW); // LED on
|
|
432
|
+ delay(LED_ERROR_BLINK_INTERVAL);
|
|
433
|
+ digitalWrite(BUILTIN_LED_PIN, HIGH); // LED off
|
|
434
|
+ delay(LED_ERROR_BLINK_INTERVAL);
|
|
435
|
+ }
|
|
436
|
+ }
|
|
437
|
+ return success;
|
|
438
|
+}
|
|
439
|
+
|
|
440
|
+void writeDatabase() {
|
|
441
|
+ if (found_bme1) {
|
|
442
|
+ InfluxData measurement("environment");
|
|
443
|
+ measurement.addTag("location", sensor_location);
|
|
444
|
+ measurement.addTag("placement", "1");
|
|
445
|
+ measurement.addTag("device", WiFi.macAddress());
|
|
446
|
+ measurement.addTag("sensor", "bme280");
|
|
447
|
+
|
|
448
|
+ measurement.addValue("temperature", bme1_temp());
|
|
449
|
+ measurement.addValue("pressure", bme1_pressure());
|
|
450
|
+ measurement.addValue("humidity", bme1_humid());
|
|
451
|
+
|
|
452
|
+ writeMeasurement(measurement);
|
|
453
|
+ }
|
|
454
|
+ if (found_bme2) {
|
|
455
|
+ InfluxData measurement("environment");
|
|
456
|
+ measurement.addTag("location", sensor_location);
|
|
457
|
+ measurement.addTag("placement", "2");
|
|
458
|
+ measurement.addTag("device", WiFi.macAddress());
|
|
459
|
+ measurement.addTag("sensor", "bme280");
|
|
460
|
+
|
|
461
|
+ measurement.addValue("temperature", bme2_temp());
|
|
462
|
+ measurement.addValue("pressure", bme2_pressure());
|
|
463
|
+ measurement.addValue("humidity", bme2_humid());
|
|
464
|
+
|
|
465
|
+ writeMeasurement(measurement);
|
|
466
|
+ }
|
|
467
|
+
|
|
468
|
+ if (found_sht) {
|
|
469
|
+ InfluxData measurement("environment");
|
|
470
|
+ measurement.addTag("location", sensor_location);
|
|
471
|
+ measurement.addTag("device", WiFi.macAddress());
|
|
472
|
+ measurement.addTag("sensor", "sht21");
|
|
473
|
+
|
|
474
|
+ measurement.addValue("temperature", sht_temp());
|
|
475
|
+ measurement.addValue("humidity", sht_humid());
|
|
476
|
+
|
|
477
|
+ writeMeasurement(measurement);
|
|
478
|
+ }
|
|
479
|
+
|
|
480
|
+ for (int i = 0; i < moisture_count(); i++) {
|
|
481
|
+ int moisture = moisture_read(i);
|
|
482
|
+ if (moisture < moisture_max()) {
|
|
483
|
+ InfluxData measurement("moisture");
|
|
484
|
+ measurement.addTag("location", sensor_location);
|
|
485
|
+ measurement.addTag("device", WiFi.macAddress());
|
|
486
|
+ measurement.addTag("sensor", String(i + 1));
|
|
487
|
+
|
|
488
|
+ measurement.addValue("value", moisture);
|
|
489
|
+ measurement.addValue("maximum", moisture_max());
|
|
490
|
+
|
|
491
|
+ writeMeasurement(measurement);
|
|
492
|
+ }
|
|
493
|
+ }
|
|
494
|
+}
|
|
495
|
+#endif // ENABLE_INFLUXDB_LOGGING
|
|
496
|
+
|
|
497
|
+void loop() {
|
|
498
|
+ if ((millis() - last_server_handle_time) >= SERVER_HANDLE_INTERVAL) {
|
|
499
|
+ last_server_handle_time = millis();
|
|
500
|
+ handleServers();
|
|
501
|
+ }
|
|
502
|
+
|
|
503
|
+#ifdef ENABLE_NTP
|
|
504
|
+ timeClient.update();
|
|
505
|
+
|
|
506
|
+ if (timeClient.isTimeSet()) {
|
|
507
|
+ if (((millis() - last_timing_check_time) >= AUTO_TIMING_INTERVAL) || (last_timing_check_time == 0)) {
|
|
508
|
+ last_timing_check_time = millis();
|
|
509
|
+ handleAutoTimingCheck();
|
|
510
|
+ }
|
|
511
|
+ }
|
|
512
|
+#endif // ENABLE_NTP
|
|
513
|
+
|
|
514
|
+#ifdef ENABLE_MQTT
|
|
515
|
+ if (!mqtt.connected()) {
|
|
516
|
+ mqttReconnect();
|
|
517
|
+ }
|
|
518
|
+ mqtt.loop();
|
|
519
|
+#endif // ENABLE_MQTT
|
|
520
|
+
|
|
521
|
+#ifdef ENABLE_INFLUXDB_LOGGING
|
|
522
|
+ if ((millis() - last_db_write_time) >= DB_WRITE_INTERVAL) {
|
|
523
|
+ last_db_write_time = millis();
|
|
524
|
+ writeDatabase();
|
|
525
|
+ }
|
|
526
|
+
|
|
527
|
+#ifdef INFLUX_MAX_ERRORS_RESET
|
|
528
|
+ if (error_count >= INFLUX_MAX_ERRORS_RESET) {
|
|
529
|
+ ESP.restart();
|
|
530
|
+ }
|
|
531
|
+#endif // INFLUX_MAX_ERRORS_RESET
|
|
532
|
+#endif // ENABLE_INFLUXDB_LOGGING
|
|
533
|
+
|
|
534
|
+ // blink heartbeat LED
|
|
535
|
+ if ((millis() - last_led_blink_time) >= LED_BLINK_INTERVAL) {
|
|
536
|
+ last_led_blink_time = millis();
|
|
537
|
+ digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
|
|
538
|
+ }
|
|
539
|
+
|
|
540
|
+ // reset ESP every 6h to be safe
|
|
541
|
+ if (millis() >= (6 * 60 * 60 * 1000)) {
|
|
542
|
+ ESP.restart();
|
|
543
|
+ }
|
|
544
|
+}
|
|
545
|
+
|