ソースを参照

fix for watchdog resets when reading sensors. custom influx client to save space. 512k esp8266 env version. esp8266 hostname fix. wifi disconnect reset.

Thomas Buck 2年前
コミット
1fbc4253c5
9個のファイルの変更200行の追加172行の削除
  1. 1
    1
      include/config.h
  2. 34
    2
      platformio.ini
  3. 69
    6
      src/SimpleInflux.cpp
  4. 2
    2
      src/html.cpp
  5. 16
    18
      src/influx.cpp
  6. 40
    14
      src/main.cpp
  7. 11
    1
      src/mqtt.cpp
  8. 27
    127
      src/sensors.cpp
  9. 0
    1
      src/servers.cpp

+ 1
- 1
include/config.h ファイルの表示

15
 #define __ESP_ENV_CONFIG__
15
 #define __ESP_ENV_CONFIG__
16
 
16
 
17
 // Sketch version
17
 // Sketch version
18
-#define ESP_ENV_VERSION "0.6.0"
18
+#define ESP_ENV_VERSION "0.6.1"
19
 
19
 
20
 // location of sensor, used in DB and hostname
20
 // location of sensor, used in DB and hostname
21
 //#define SENSOR_LOCATION_LIVINGROOM
21
 //#define SENSOR_LOCATION_LIVINGROOM

+ 34
- 2
platformio.ini ファイルの表示

14
 framework = arduino
14
 framework = arduino
15
 build_flags =
15
 build_flags =
16
   -DSENSOR_HOSTNAME_PREFIX=\"env-\"
16
   -DSENSOR_HOSTNAME_PREFIX=\"env-\"
17
+  "-DNAME_OF_FEATURE=\"Environment Sensor\""
17
   -DENABLE_WEBSOCKETS
18
   -DENABLE_WEBSOCKETS
18
   -DENABLE_DEBUGLOG
19
   -DENABLE_DEBUGLOG
19
   -DENABLE_BME280
20
   -DENABLE_BME280
20
   -DENABLE_CCS811
21
   -DENABLE_CCS811
21
   -DENABLE_INFLUXDB_LOGGING
22
   -DENABLE_INFLUXDB_LOGGING
23
+  -DUSE_INFLUXDB_LIB
22
   -DENABLE_MQTT
24
   -DENABLE_MQTT
23
 lib_deps =
25
 lib_deps =
24
     Wire
26
     Wire
30
     https://github.com/rlogiacco/CircularBuffer
32
     https://github.com/rlogiacco/CircularBuffer
31
     https://github.com/Links2004/arduinoWebSockets
33
     https://github.com/Links2004/arduinoWebSockets
32
 
34
 
35
+[env:esp8266smallenv]
36
+platform = espressif8266
37
+board = esp01
38
+framework = arduino
39
+build_flags =
40
+  -DSENSOR_HOSTNAME_PREFIX=\"env-\"
41
+  "-DNAME_OF_FEATURE=\"Environment Sensor\""
42
+  -DENABLE_WEBSOCKETS
43
+  -DENABLE_DEBUGLOG
44
+  -DENABLE_BME280
45
+  -DENABLE_CCS811
46
+  -DENABLE_INFLUXDB_LOGGING
47
+  -DENABLE_MQTT
48
+lib_deps =
49
+    Wire
50
+    Adafruit Unified Sensor
51
+    Adafruit BME280 Library
52
+    https://github.com/adafruit/Adafruit_CCS811
53
+    https://github.com/knolleary/pubsubclient.git#2d228f2f862a95846c65a8518c79f48dfc8f188c
54
+    https://github.com/rlogiacco/CircularBuffer
55
+    https://github.com/Links2004/arduinoWebSockets
56
+
33
 [env:esp8266smallrelais]
57
 [env:esp8266smallrelais]
34
 platform = espressif8266
58
 platform = espressif8266
35
 board = esp01
59
 board = esp01
36
 framework = arduino
60
 framework = arduino
37
 build_flags =
61
 build_flags =
38
   -DSENSOR_HOSTNAME_PREFIX=\"relais-\"
62
   -DSENSOR_HOSTNAME_PREFIX=\"relais-\"
63
+  "-DNAME_OF_FEATURE=\"Relais Switch\""
39
   -DENABLE_WEBSOCKETS
64
   -DENABLE_WEBSOCKETS
40
   -DENABLE_DEBUGLOG
65
   -DENABLE_DEBUGLOG
41
   -DENABLE_BME280
66
   -DENABLE_BME280
57
 framework = arduino
82
 framework = arduino
58
 build_flags =
83
 build_flags =
59
   -DSENSOR_HOSTNAME_PREFIX=\"relais-\"
84
   -DSENSOR_HOSTNAME_PREFIX=\"relais-\"
85
+  "-DNAME_OF_FEATURE=\"Relais Switch\""
60
   -DENABLE_WEBSOCKETS
86
   -DENABLE_WEBSOCKETS
61
   -DENABLE_DEBUGLOG
87
   -DENABLE_DEBUGLOG
62
   -DENABLE_BME280
88
   -DENABLE_BME280
63
   -DRELAIS_SERIAL
89
   -DRELAIS_SERIAL
64
   -DENABLE_INFLUXDB_LOGGING
90
   -DENABLE_INFLUXDB_LOGGING
91
+  -DUSE_INFLUXDB_LIB
65
   -DENABLE_MQTT
92
   -DENABLE_MQTT
66
 lib_deps =
93
 lib_deps =
67
     Wire
94
     Wire
82
 monitor_speed = 115200
109
 monitor_speed = 115200
83
 build_flags =
110
 build_flags =
84
   -DSENSOR_HOSTNAME_PREFIX=\"env-\"
111
   -DSENSOR_HOSTNAME_PREFIX=\"env-\"
112
+  "-DNAME_OF_FEATURE=\"Environment Sensor\""
85
   -DENABLE_WEBSOCKETS
113
   -DENABLE_WEBSOCKETS
86
   -DENABLE_DEBUGLOG
114
   -DENABLE_DEBUGLOG
87
   -DENABLE_BME280
115
   -DENABLE_BME280
88
   -DENABLE_CCS811
116
   -DENABLE_CCS811
89
   -DENABLE_INFLUXDB_LOGGING
117
   -DENABLE_INFLUXDB_LOGGING
118
+  -DUSE_INFLUXDB_LIB
90
   -DENABLE_MQTT
119
   -DENABLE_MQTT
91
 lib_deps =
120
 lib_deps =
92
     Wire
121
     Wire
107
 monitor_port = /dev/ttyUSB1
136
 monitor_port = /dev/ttyUSB1
108
 monitor_speed = 115200
137
 monitor_speed = 115200
109
 build_flags =
138
 build_flags =
110
-  -DSENSOR_HOSTNAME_PREFIX=\"env-\"
139
+  -DSENSOR_HOSTNAME_PREFIX=\"mst-\"
140
+  "-DNAME_OF_FEATURE=\"Moisture Sensor\""
111
   -DENABLE_WEBSOCKETS
141
   -DENABLE_WEBSOCKETS
112
   -DENABLE_DEBUGLOG
142
   -DENABLE_DEBUGLOG
113
   -DMOISTURE_ADC_ESP32
143
   -DMOISTURE_ADC_ESP32
114
   -DENABLE_BME280
144
   -DENABLE_BME280
115
   -DENABLE_CCS811
145
   -DENABLE_CCS811
116
   -DENABLE_INFLUXDB_LOGGING
146
   -DENABLE_INFLUXDB_LOGGING
147
+  -DUSE_INFLUXDB_LIB
117
   -DENABLE_MQTT
148
   -DENABLE_MQTT
118
 lib_deps =
149
 lib_deps =
119
     Wire
150
     Wire
133
 monitor_port = /dev/ttyACM0
164
 monitor_port = /dev/ttyACM0
134
 monitor_speed = 115200
165
 monitor_speed = 115200
135
 build_flags =
166
 build_flags =
136
-  -DSENSOR_HOSTNAME_PREFIX=\"env-\"
167
+  -DSENSOR_HOSTNAME_PREFIX=\"mst-\"
168
+  "-DNAME_OF_FEATURE=\"Moisture Sensor\""
137
   -DENABLE_BME280
169
   -DENABLE_BME280
138
   -DMOISTURE_ADC_ARDUINO
170
   -DMOISTURE_ADC_ARDUINO
139
   -DENABLE_INFLUXDB_LOGGING
171
   -DENABLE_INFLUXDB_LOGGING

+ 69
- 6
src/SimpleInflux.cpp ファイルの表示

15
 #include "DebugLog.h"
15
 #include "DebugLog.h"
16
 #include "SimpleInflux.h"
16
 #include "SimpleInflux.h"
17
 
17
 
18
-#if defined(ARDUINO_ARCH_AVR)
18
+#ifndef USE_INFLUXDB_LIB
19
 
19
 
20
+#if defined(ARDUINO_ARCH_AVR)
20
 #include <WiFiLink.h>
21
 #include <WiFiLink.h>
21
-
22
 WiFiClient client;
22
 WiFiClient client;
23
-
23
+#elif defined(ARDUINO_ARCH_ESP8266)
24
+#include <ESP8266WiFi.h>
25
+#include <ESP8266HTTPClient.h>
26
+#include <WiFiClient.h>
24
 #endif
27
 #endif
25
 
28
 
26
 void InfluxData::addTag(const char *name, const char *value) {
29
 void InfluxData::addTag(const char *name, const char *value) {
42
 // https://docs.influxdata.com/influxdb/v1.8/guides/write_data/
45
 // https://docs.influxdata.com/influxdb/v1.8/guides/write_data/
43
 
46
 
44
 boolean Influxdb::write(InfluxData &data) {
47
 boolean Influxdb::write(InfluxData &data) {
45
-#if defined(ARDUINO_ARCH_AVR)
48
+    //debug.print(F("Writing "));
49
+    //debug.println(data.dataName());
46
 
50
 
47
-    debug.print(F("Writing "));
48
-    debug.println(data.dataName());
51
+#if defined(ARDUINO_ARCH_AVR)
49
 
52
 
50
     client.stop();
53
     client.stop();
51
 
54
 
139
         debug.println(F("Error connecting"));
142
         debug.println(F("Error connecting"));
140
         return false; // failed
143
         return false; // failed
141
     }
144
     }
145
+
146
+#elif defined(ARDUINO_ARCH_ESP8266)
147
+
148
+    String content = data.dataName();
149
+    for (int i = 0; i < data.tagCount(); i++) {
150
+        content += F(",");
151
+        content += data.tagName(i);
152
+        content += F("=");
153
+        //content += F("\"");
154
+        content += data.tagValue(i);
155
+        //content += F("\"");
156
+    }
157
+    content += F(" ");
158
+    for (int i = 0; i < data.valueCount(); i++) {
159
+        if (i > 0) {
160
+            content += F(",");
161
+        }
162
+        content += data.valueName(i);
163
+        content += F("=");
164
+        content += data.valueValue(i);
165
+    }
166
+    // we're leaving out the timestamp, it's optional
167
+
168
+    WiFiClient client;
169
+    HTTPClient http;
170
+
171
+    http.setReuse(false);
172
+    http.setTimeout(1500); // ms
173
+
174
+    String uri("/write?db=");
175
+    uri += db_name;
176
+
177
+    http.begin(client, db_host, db_port, uri, false);
178
+
179
+    //debug.print(F("Sending to Influx: "));
180
+    //debug.println(content);
181
+
182
+    int httpResponseCode = http.POST(content);
183
+    String payload = http.getString();
184
+
185
+    String compare_to(F("X-Influxdb-Error"));
186
+    bool result = false; // error
187
+
188
+    if ((httpResponseCode >= 200) && (httpResponseCode <= 299)
189
+            && (payload.indexOf(compare_to) < 0)) {
190
+        result = true; // success
191
+    } else {
192
+        debug.print(F("Got "));
193
+        debug.print(httpResponseCode);
194
+        debug.print(F(" response from Influx: "));
195
+        debug.println(payload);
196
+    }
197
+
198
+    http.end();
199
+    return result;
200
+
201
+#elif defined(ARDUINO_ARCH_ESP32)
202
+#error Not implemented for ESP32 yet
142
 #else
203
 #else
143
 
204
 
144
     return true; // success
205
     return true; // success
145
 
206
 
146
 #endif
207
 #endif
147
 }
208
 }
209
+
210
+#endif

+ 2
- 2
src/html.cpp ファイルの表示

73
     message += F("<html><head>");
73
     message += F("<html><head>");
74
     message += F("<meta charset='utf-8'/>");
74
     message += F("<meta charset='utf-8'/>");
75
     message += F("<meta name='viewport' content='width=device-width, initial-scale=1'/>");
75
     message += F("<meta name='viewport' content='width=device-width, initial-scale=1'/>");
76
-    message += F("<title>" ESP_PLATFORM_NAME " Environment Sensor</title>");
76
+    message += F("<title>" ESP_PLATFORM_NAME " " NAME_OF_FEATURE "</title>");
77
     message += F("<style>");
77
     message += F("<style>");
78
     message += F(".log {\n");
78
     message += F(".log {\n");
79
     message += F(    "max-height: 300px;\n");
79
     message += F(    "max-height: 300px;\n");
100
     message += F("}");
100
     message += F("}");
101
     message += F("</style>");
101
     message += F("</style>");
102
     message += F("</head><body>");
102
     message += F("</head><body>");
103
-    message += F("<h1>" ESP_PLATFORM_NAME " Environment Sensor</h1>");
103
+    message += F("<h1>" ESP_PLATFORM_NAME " " NAME_OF_FEATURE "</h1>");
104
     message += F("\n<p>\n");
104
     message += F("\n<p>\n");
105
     message += F("Version: ");
105
     message += F("Version: ");
106
     message += ESP_ENV_VERSION;
106
     message += ESP_ENV_VERSION;

+ 16
- 18
src/influx.cpp ファイルの表示

41
 
41
 
42
 #endif
42
 #endif
43
 
43
 
44
-#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
44
+#ifdef USE_INFLUXDB_LIB
45
 #include <InfluxDb.h>
45
 #include <InfluxDb.h>
46
 #else
46
 #else
47
 #include "SimpleInflux.h"
47
 #include "SimpleInflux.h"
52
 static unsigned long last_db_write_time = 0;
52
 static unsigned long last_db_write_time = 0;
53
 
53
 
54
 void initInflux() {
54
 void initInflux() {
55
-    debug.println(F("Influx"));
56
     influx.setDb(INFLUXDB_DATABASE);
55
     influx.setDb(INFLUXDB_DATABASE);
57
 }
56
 }
58
 
57
 
67
 #ifdef INFLUX_MAX_ERRORS_RESET
66
 #ifdef INFLUX_MAX_ERRORS_RESET
68
     if (error_count >= INFLUX_MAX_ERRORS_RESET) {
67
     if (error_count >= INFLUX_MAX_ERRORS_RESET) {
69
 #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
68
 #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
69
+        debug.println(F("Resetting due to too many Influx errors"));
70
         ESP.restart();
70
         ESP.restart();
71
 #else
71
 #else
72
         // TODO implement watchdog reset for AVR
72
         // TODO implement watchdog reset for AVR
90
 }
90
 }
91
 
91
 
92
 void writeDatabase() {
92
 void writeDatabase() {
93
-#if defined(ARDUINO_ARCH_AVR)
94
-    debug.println(F("Writing to InfluxDB"));
95
-
93
+#ifndef USE_INFLUXDB_LIB
96
     InfluxData measurement("");
94
     InfluxData measurement("");
97
 #endif
95
 #endif
98
 
96
 
99
 #ifdef ENABLE_BME280
97
 #ifdef ENABLE_BME280
100
 
98
 
101
     if (found_bme1) {
99
     if (found_bme1) {
102
-#if defined(ARDUINO_ARCH_AVR)
100
+#ifndef USE_INFLUXDB_LIB
103
         measurement.clear();
101
         measurement.clear();
104
         measurement.setName("environment");
102
         measurement.setName("environment");
105
 #else
103
 #else
112
         measurement.addTag("sensor", "bme280");
110
         measurement.addTag("sensor", "bme280");
113
 
111
 
114
 #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
112
 #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
115
-        measurement.addTag("device", WiFi.macAddress());
113
+        measurement.addTag("device", WiFi.macAddress().c_str());
116
 #endif
114
 #endif
117
 
115
 
118
         measurement.addValue("temperature", bme1_temp());
116
         measurement.addValue("temperature", bme1_temp());
125
     }
123
     }
126
 
124
 
127
     if (found_bme2) {
125
     if (found_bme2) {
128
-#if defined(ARDUINO_ARCH_AVR)
126
+#ifndef USE_INFLUXDB_LIB
129
         measurement.clear();
127
         measurement.clear();
130
         measurement.setName("environment");
128
         measurement.setName("environment");
131
 #else
129
 #else
138
         measurement.addTag("sensor", "bme280");
136
         measurement.addTag("sensor", "bme280");
139
 
137
 
140
 #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
138
 #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
141
-        measurement.addTag("device", WiFi.macAddress());
139
+        measurement.addTag("device", WiFi.macAddress().c_str());
142
 #endif
140
 #endif
143
 
141
 
144
         measurement.addValue("temperature", bme2_temp());
142
         measurement.addValue("temperature", bme2_temp());
153
 #endif // ENABLE_BME280
151
 #endif // ENABLE_BME280
154
 
152
 
155
     if (found_sht) {
153
     if (found_sht) {
156
-#if defined(ARDUINO_ARCH_AVR)
154
+#ifndef USE_INFLUXDB_LIB
157
         measurement.clear();
155
         measurement.clear();
158
         measurement.setName("environment");
156
         measurement.setName("environment");
159
 #else
157
 #else
165
         measurement.addTag("sensor", "sht21");
163
         measurement.addTag("sensor", "sht21");
166
 
164
 
167
 #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
165
 #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
168
-        measurement.addTag("device", WiFi.macAddress());
166
+        measurement.addTag("device", WiFi.macAddress().c_str());
169
 #endif
167
 #endif
170
 
168
 
171
         measurement.addValue("temperature", sht_temp());
169
         measurement.addValue("temperature", sht_temp());
179
 #ifdef ENABLE_CCS811
177
 #ifdef ENABLE_CCS811
180
 
178
 
181
     if (found_ccs1) {
179
     if (found_ccs1) {
182
-#if defined(ARDUINO_ARCH_AVR)
180
+#ifndef USE_INFLUXDB_LIB
183
         measurement.clear();
181
         measurement.clear();
184
         measurement.setName("environment");
182
         measurement.setName("environment");
185
 #else
183
 #else
195
         measurement.addTag("error", err);
193
         measurement.addTag("error", err);
196
 
194
 
197
 #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
195
 #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
198
-        measurement.addTag("device", WiFi.macAddress());
196
+        measurement.addTag("device", WiFi.macAddress().c_str());
199
 #endif
197
 #endif
200
 
198
 
201
         measurement.addValue("eco2", ccs1_eco2());
199
         measurement.addValue("eco2", ccs1_eco2());
207
     }
205
     }
208
 
206
 
209
     if (found_ccs2) {
207
     if (found_ccs2) {
210
-#if defined(ARDUINO_ARCH_AVR)
208
+#ifndef USE_INFLUXDB_LIB
211
         measurement.clear();
209
         measurement.clear();
212
         measurement.setName("environment");
210
         measurement.setName("environment");
213
 #else
211
 #else
223
         measurement.addTag("error", err);
221
         measurement.addTag("error", err);
224
 
222
 
225
 #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
223
 #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
226
-        measurement.addTag("device", WiFi.macAddress());
224
+        measurement.addTag("device", WiFi.macAddress().c_str());
227
 #endif
225
 #endif
228
 
226
 
229
         measurement.addValue("eco2", ccs2_eco2());
227
         measurement.addValue("eco2", ccs2_eco2());
240
     for (int i = 0; i < moisture_count(); i++) {
238
     for (int i = 0; i < moisture_count(); i++) {
241
         int moisture = moisture_read(i);
239
         int moisture = moisture_read(i);
242
         if (moisture < moisture_max()) {
240
         if (moisture < moisture_max()) {
243
-#if defined(ARDUINO_ARCH_AVR)
241
+#ifndef USE_INFLUXDB_LIB
244
             measurement.clear();
242
             measurement.clear();
245
             measurement.setName("moisture");
243
             measurement.setName("moisture");
246
 #else
244
 #else
253
             measurement.addTag("sensor", sensor);
251
             measurement.addTag("sensor", sensor);
254
 
252
 
255
 #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
253
 #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
256
-            measurement.addTag("device", WiFi.macAddress());
254
+            measurement.addTag("device", WiFi.macAddress().c_str());
257
 #endif
255
 #endif
258
 
256
 
259
             measurement.addValue("value", moisture);
257
             measurement.addValue("value", moisture);
276
         measurement.addTag("name", relais_name(i));
274
         measurement.addTag("name", relais_name(i));
277
 
275
 
278
 #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
276
 #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
279
-        measurement.addTag("device", WiFi.macAddress());
277
+        measurement.addTag("device", WiFi.macAddress().c_str());
280
 #endif
278
 #endif
281
 
279
 
282
         measurement.addValue("state", relais_get(i));
280
         measurement.addValue("state", relais_get(i));

+ 40
- 14
src/main.cpp ファイルの表示

37
 
37
 
38
 ConfigMemory config;
38
 ConfigMemory config;
39
 
39
 
40
+#if defined(ARDUINO_ARCH_ESP8266)
41
+
42
+WiFiEventHandler disconnectHandler;
43
+
44
+void onDisconnected(const WiFiEventStationModeDisconnected& event) {
45
+    /*
46
+     * simply restart in case we lose wifi connection
47
+     * we can't do anything useful without wifi anyway!
48
+     */
49
+    ESP.restart();
50
+}
51
+
52
+#endif // ARDUINO_ARCH_ESP8266
53
+
40
 void setup() {
54
 void setup() {
41
     pinMode(BUILTIN_LED_PIN, OUTPUT);
55
     pinMode(BUILTIN_LED_PIN, OUTPUT);
42
     
56
     
43
     Serial.begin(115200);
57
     Serial.begin(115200);
44
 
58
 
59
+    debug.println(F("Initializing..."));
60
+
45
     // Blink LED for init
61
     // Blink LED for init
46
     for (int i = 0; i < 2; i++) {
62
     for (int i = 0; i < 2; i++) {
47
         digitalWrite(BUILTIN_LED_PIN, LOW); // LED on
63
         digitalWrite(BUILTIN_LED_PIN, LOW); // LED on
70
     debug.print(F("Connecting WiFi"));
86
     debug.print(F("Connecting WiFi"));
71
     WiFi.hostname(hostname);
87
     WiFi.hostname(hostname);
72
     WiFi.mode(WIFI_STA);
88
     WiFi.mode(WIFI_STA);
89
+    WiFi.hostname(hostname);
73
     WiFi.begin(WIFI_SSID, WIFI_PASS);
90
     WiFi.begin(WIFI_SSID, WIFI_PASS);
74
     while (WiFi.status() != WL_CONNECTED) {
91
     while (WiFi.status() != WL_CONNECTED) {
75
         delay(LED_CONNECT_BLINK_INTERVAL);
92
         delay(LED_CONNECT_BLINK_INTERVAL);
77
         debug.print(F("."));
94
         debug.print(F("."));
78
     }
95
     }
79
     debug.println(F("\nWiFi connected!"));
96
     debug.println(F("\nWiFi connected!"));
97
+
98
+    disconnectHandler = WiFi.onStationModeDisconnected(onDisconnected);
99
+
100
+    // Set hostname workaround
101
+    WiFi.hostname(hostname);
80
     
102
     
81
 #elif defined(ARDUINO_ARCH_ESP32)
103
 #elif defined(ARDUINO_ARCH_ESP32)
82
 
104
 
84
     WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
106
     WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
85
     WiFi.setHostname(hostname.c_str());
107
     WiFi.setHostname(hostname.c_str());
86
     
108
     
87
-    // Workaround for WiFi connecting only every 2nd reset
88
-    // https://github.com/espressif/arduino-esp32/issues/2501#issuecomment-513602522
89
     WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info) {
109
     WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info) {
90
-        if (info.disconnected.reason == 202) {
91
-            esp_sleep_enable_timer_wakeup(10);
92
-            esp_deep_sleep_start();
93
-            delay(100);
94
-        }
110
+        /*
111
+         * was initially: workaround for WiFi connecting only every 2nd reset
112
+         * https://github.com/espressif/arduino-esp32/issues/2501#issuecomment-513602522
113
+         *
114
+         * now simply reset on every disconnect reason - we can't do anything
115
+         * useful without wifi anyway!
116
+         */
117
+        esp_sleep_enable_timer_wakeup(10);
118
+        esp_deep_sleep_start();
119
+        delay(100);
120
+        ESP.restart();
95
     }, WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
121
     }, WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
96
 
122
 
97
     // Connect to WiFi AP
123
     // Connect to WiFi AP
128
     debug.println(F("Seeding"));
154
     debug.println(F("Seeding"));
129
     randomSeed(micros());
155
     randomSeed(micros());
130
 
156
 
157
+    debug.println(F("MQTT"));
131
     initMQTT();
158
     initMQTT();
159
+
160
+    debug.println(F("Influx"));
132
     initInflux();
161
     initInflux();
162
+
163
+    debug.println(F("Servers"));
133
     initServers(hostname);
164
     initServers(hostname);
165
+
166
+    debug.println(F("Ready! Starting..."));
134
 }
167
 }
135
 
168
 
136
 void loop() {
169
 void loop() {
146
         last_led_blink_time = time;
179
         last_led_blink_time = time;
147
         digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
180
         digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
148
     }
181
     }
149
-    
150
-#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
151
-    // reset ESP every 3d to be safe
152
-    if (time >= (3UL * 24UL * 60UL * 60UL * 1000UL)) {
153
-        ESP.restart();
154
-    }
155
-#endif
156
 }
182
 }

+ 11
- 1
src/mqtt.cpp ファイルの表示

53
         return;
53
         return;
54
     }
54
     }
55
 
55
 
56
+    bool wrote = false;
57
+
56
     if (found_sht) {
58
     if (found_sht) {
57
         mqtt.publish(SENSOR_LOCATION "/temperature", String(sht_temp()).c_str(), true);
59
         mqtt.publish(SENSOR_LOCATION "/temperature", String(sht_temp()).c_str(), true);
58
         mqtt.publish(SENSOR_LOCATION "/humidity", String(sht_humid()).c_str(), true);
60
         mqtt.publish(SENSOR_LOCATION "/humidity", String(sht_humid()).c_str(), true);
61
+        wrote = true;
59
 #ifdef ENABLE_BME280
62
 #ifdef ENABLE_BME280
60
     } else if (found_bme1) {
63
     } else if (found_bme1) {
61
         mqtt.publish(SENSOR_LOCATION "/temperature", String(bme1_temp()).c_str(), true);
64
         mqtt.publish(SENSOR_LOCATION "/temperature", String(bme1_temp()).c_str(), true);
62
         mqtt.publish(SENSOR_LOCATION "/humidity", String(bme1_humid()).c_str(), true);
65
         mqtt.publish(SENSOR_LOCATION "/humidity", String(bme1_humid()).c_str(), true);
63
         mqtt.publish(SENSOR_LOCATION "/pressure", String(bme1_pressure()).c_str(), true);
66
         mqtt.publish(SENSOR_LOCATION "/pressure", String(bme1_pressure()).c_str(), true);
67
+        wrote = true;
64
     } else if (found_bme2) {
68
     } else if (found_bme2) {
65
         mqtt.publish(SENSOR_LOCATION "/temperature", String(bme2_temp()).c_str(), true);
69
         mqtt.publish(SENSOR_LOCATION "/temperature", String(bme2_temp()).c_str(), true);
66
         mqtt.publish(SENSOR_LOCATION "/humidity", String(bme2_humid()).c_str(), true);
70
         mqtt.publish(SENSOR_LOCATION "/humidity", String(bme2_humid()).c_str(), true);
67
         mqtt.publish(SENSOR_LOCATION "/pressure", String(bme2_pressure()).c_str(), true);
71
         mqtt.publish(SENSOR_LOCATION "/pressure", String(bme2_pressure()).c_str(), true);
72
+        wrote = true;
68
 #endif // ENABLE_BME280
73
 #endif // ENABLE_BME280
69
     }
74
     }
70
 
75
 
72
     if (found_ccs1) {
77
     if (found_ccs1) {
73
         mqtt.publish(SENSOR_LOCATION "/eco2", String(ccs1_eco2()).c_str(), true);
78
         mqtt.publish(SENSOR_LOCATION "/eco2", String(ccs1_eco2()).c_str(), true);
74
         mqtt.publish(SENSOR_LOCATION "/tvoc", String(ccs1_tvoc()).c_str(), true);
79
         mqtt.publish(SENSOR_LOCATION "/tvoc", String(ccs1_tvoc()).c_str(), true);
80
+        wrote = true;
75
     } else if (found_ccs2) {
81
     } else if (found_ccs2) {
76
         mqtt.publish(SENSOR_LOCATION "/eco2", String(ccs2_eco2()).c_str(), true);
82
         mqtt.publish(SENSOR_LOCATION "/eco2", String(ccs2_eco2()).c_str(), true);
77
         mqtt.publish(SENSOR_LOCATION "/tvoc", String(ccs2_tvoc()).c_str(), true);
83
         mqtt.publish(SENSOR_LOCATION "/tvoc", String(ccs2_tvoc()).c_str(), true);
84
+        wrote = true;
78
     }
85
     }
79
 #endif // ENABLE_CCS811
86
 #endif // ENABLE_CCS811
87
+
88
+    if (wrote) {
89
+        debug.println(F("Updated MQTT sensor values"));
90
+    }
80
 }
91
 }
81
 
92
 
82
 static void mqttCallback(char* topic, byte* payload, unsigned int length) {
93
 static void mqttCallback(char* topic, byte* payload, unsigned int length) {
163
 }
174
 }
164
 
175
 
165
 void initMQTT() {
176
 void initMQTT() {
166
-    debug.println(F("MQTT"));
167
     mqtt.setServer(MQTT_HOST, MQTT_PORT);
177
     mqtt.setServer(MQTT_HOST, MQTT_PORT);
168
     mqtt.setCallback(mqttCallback);
178
     mqtt.setCallback(mqttCallback);
169
 }
179
 }

+ 27
- 127
src/sensors.cpp ファイルの表示

74
 
74
 
75
 static unsigned long last_sensor_handle_time = 0;
75
 static unsigned long last_sensor_handle_time = 0;
76
 
76
 
77
-#ifdef ENABLE_BME280
78
-
79
-float bme1_temp(void) {
80
-    while (1) {
81
-        float a = bme1.readTemperature();
82
-        float b = bme1.readTemperature();
83
-        
84
-        if ((a > b) && ((a - b) < 2.0)) {
85
-            return (a + b) / 2.0;
86
-        }
87
-        
88
-        if ((a < b) && ((b - a) < 2.0)) {
89
-            return (a + b) / 2.0;
90
-        }
91
-    }
92
-    return 0.0;
93
-}
94
-
95
-float bme2_temp(void) {
96
-    while (1) {
97
-        float a = bme2.readTemperature();
98
-        float b = bme2.readTemperature();
99
-        
100
-        if ((a > b) && ((a - b) < 2.0)) {
101
-            return (a + b) / 2.0;
102
-        }
103
-        
104
-        if ((a < b) && ((b - a) < 2.0)) {
105
-            return (a + b) / 2.0;
106
-        }
107
-    }
108
-    return 0.0;
109
-}
110
-
111
-float bme1_humid(void) {
112
-    while (1) {
113
-        float a = bme1.readHumidity();
114
-        float b = bme1.readHumidity();
115
-        
116
-        if ((a > b) && ((a - b) < 2.0)) {
117
-            return (a + b) / 2.0;
118
-        }
119
-        
120
-        if ((a < b) && ((b - a) < 2.0)) {
121
-            return (a + b) / 2.0;
122
-        }
123
-    }
124
-    return 0.0;
125
-}
126
-
127
-float bme2_humid(void) {
128
-    while (1) {
129
-        float a = bme2.readHumidity();
130
-        float b = bme2.readHumidity();
131
-        
132
-        if ((a > b) && ((a - b) < 2.0)) {
133
-            return (a + b) / 2.0;
134
-        }
135
-        
136
-        if ((a < b) && ((b - a) < 2.0)) {
137
-            return (a + b) / 2.0;
138
-        }
139
-    }
140
-    return 0.0;
141
-}
142
-
143
-float bme1_pressure(void) {
144
-    while (1) {
145
-        float a = bme1.readPressure();
146
-        float b = bme1.readPressure();
147
-        
148
-        if ((a > b) && ((a - b) < 2.0)) {
149
-            return (a + b) / 2.0;
150
-        }
151
-        
152
-        if ((a < b) && ((b - a) < 2.0)) {
153
-            return (a + b) / 2.0;
154
-        }
155
-    }
156
-    return 0.0;
157
-}
158
-
159
-float bme2_pressure(void) {
160
-    while (1) {
161
-        float a = bme2.readPressure();
162
-        float b = bme2.readPressure();
163
-        
164
-        if ((a > b) && ((a - b) < 2.0)) {
165
-            return (a + b) / 2.0;
166
-        }
167
-        
168
-        if ((a < b) && ((b - a) < 2.0)) {
169
-            return (a + b) / 2.0;
170
-        }
171
-    }
172
-    return 0.0;
77
+#define DEF_SENSOR_READ_FUNC(n, v)        \
78
+float n(void) {                           \
79
+    while (1) {                           \
80
+        float a = v;                      \
81
+        float b = v;                      \
82
+                                          \
83
+        if ((a > b) && ((a - b) < 2.0)) { \
84
+            return (a + b) / 2.0;         \
85
+        }                                 \
86
+                                          \
87
+        if ((a < b) && ((b - a) < 2.0)) { \
88
+            return (a + b) / 2.0;         \
89
+        }                                 \
90
+                                          \
91
+        /* to keep wdt happy */           \
92
+        delay(1);                         \
93
+    }                                     \
94
+    return 0.0;                           \
173
 }
95
 }
174
 
96
 
97
+#ifdef ENABLE_BME280
98
+DEF_SENSOR_READ_FUNC(bme1_temp, bme1.readTemperature())
99
+DEF_SENSOR_READ_FUNC(bme2_temp, bme2.readTemperature())
100
+DEF_SENSOR_READ_FUNC(bme1_humid, bme1.readHumidity())
101
+DEF_SENSOR_READ_FUNC(bme2_humid, bme2.readHumidity())
102
+DEF_SENSOR_READ_FUNC(bme1_pressure, bme1.readPressure())
103
+DEF_SENSOR_READ_FUNC(bme2_pressure, bme2.readPressure())
175
 #endif // ENABLE_BME280
104
 #endif // ENABLE_BME280
176
 
105
 
177
-float sht_temp(void) {
178
-    while (1) {
179
-        float a = sht.GetTemperature() + config.sht_temp_off;
180
-        float b = sht.GetTemperature() + config.sht_temp_off;
181
-        
182
-        if ((a > b) && ((a - b) < 2.0)) {
183
-            return (a + b) / 2.0;
184
-        }
185
-        
186
-        if ((a < b) && ((b - a) < 2.0)) {
187
-            return (a + b) / 2.0;
188
-        }
189
-    }
190
-    return 0.0;
191
-}
192
-
193
-float sht_humid(void) {
194
-    while (1) {
195
-        float a = sht.GetHumidity();
196
-        float b = sht.GetHumidity();
197
-        
198
-        if ((a > b) && ((a - b) < 2.0)) {
199
-            return (a + b) / 2.0;
200
-        }
201
-        
202
-        if ((a < b) && ((b - a) < 2.0)) {
203
-            return (a + b) / 2.0;
204
-        }
205
-    }
206
-    return 0.0;
207
-}
106
+DEF_SENSOR_READ_FUNC(sht_temp, sht.GetTemperature() + config.sht_temp_off)
107
+DEF_SENSOR_READ_FUNC(sht_humid, sht.GetHumidity())
208
 
108
 
209
 #ifdef ENABLE_CCS811
109
 #ifdef ENABLE_CCS811
210
 
110
 

+ 0
- 1
src/servers.cpp ファイルの表示

132
 void initServers(String hostname) {
132
 void initServers(String hostname) {
133
 #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
133
 #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
134
     // Setup HTTP Server
134
     // Setup HTTP Server
135
-    debug.println(F("HTTP"));
136
     MDNS.begin(hostname.c_str());
135
     MDNS.begin(hostname.c_str());
137
     updater.setup(&server);
136
     updater.setup(&server);
138
     server.on("/", handleRoot);
137
     server.on("/", handleRoot);

読み込み中…
キャンセル
保存