Просмотр исходного кода

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 лет назад
Родитель
Сommit
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,7 +15,7 @@
15 15
 #define __ESP_ENV_CONFIG__
16 16
 
17 17
 // Sketch version
18
-#define ESP_ENV_VERSION "0.6.0"
18
+#define ESP_ENV_VERSION "0.6.1"
19 19
 
20 20
 // location of sensor, used in DB and hostname
21 21
 //#define SENSOR_LOCATION_LIVINGROOM

+ 34
- 2
platformio.ini Просмотреть файл

@@ -14,11 +14,13 @@ board = esp01_1m
14 14
 framework = arduino
15 15
 build_flags =
16 16
   -DSENSOR_HOSTNAME_PREFIX=\"env-\"
17
+  "-DNAME_OF_FEATURE=\"Environment Sensor\""
17 18
   -DENABLE_WEBSOCKETS
18 19
   -DENABLE_DEBUGLOG
19 20
   -DENABLE_BME280
20 21
   -DENABLE_CCS811
21 22
   -DENABLE_INFLUXDB_LOGGING
23
+  -DUSE_INFLUXDB_LIB
22 24
   -DENABLE_MQTT
23 25
 lib_deps =
24 26
     Wire
@@ -30,12 +32,35 @@ lib_deps =
30 32
     https://github.com/rlogiacco/CircularBuffer
31 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 57
 [env:esp8266smallrelais]
34 58
 platform = espressif8266
35 59
 board = esp01
36 60
 framework = arduino
37 61
 build_flags =
38 62
   -DSENSOR_HOSTNAME_PREFIX=\"relais-\"
63
+  "-DNAME_OF_FEATURE=\"Relais Switch\""
39 64
   -DENABLE_WEBSOCKETS
40 65
   -DENABLE_DEBUGLOG
41 66
   -DENABLE_BME280
@@ -57,11 +82,13 @@ board = esp01_1m
57 82
 framework = arduino
58 83
 build_flags =
59 84
   -DSENSOR_HOSTNAME_PREFIX=\"relais-\"
85
+  "-DNAME_OF_FEATURE=\"Relais Switch\""
60 86
   -DENABLE_WEBSOCKETS
61 87
   -DENABLE_DEBUGLOG
62 88
   -DENABLE_BME280
63 89
   -DRELAIS_SERIAL
64 90
   -DENABLE_INFLUXDB_LOGGING
91
+  -DUSE_INFLUXDB_LIB
65 92
   -DENABLE_MQTT
66 93
 lib_deps =
67 94
     Wire
@@ -82,11 +109,13 @@ monitor_port = /dev/ttyUSB1
82 109
 monitor_speed = 115200
83 110
 build_flags =
84 111
   -DSENSOR_HOSTNAME_PREFIX=\"env-\"
112
+  "-DNAME_OF_FEATURE=\"Environment Sensor\""
85 113
   -DENABLE_WEBSOCKETS
86 114
   -DENABLE_DEBUGLOG
87 115
   -DENABLE_BME280
88 116
   -DENABLE_CCS811
89 117
   -DENABLE_INFLUXDB_LOGGING
118
+  -DUSE_INFLUXDB_LIB
90 119
   -DENABLE_MQTT
91 120
 lib_deps =
92 121
     Wire
@@ -107,13 +136,15 @@ upload_port = /dev/ttyUSB1
107 136
 monitor_port = /dev/ttyUSB1
108 137
 monitor_speed = 115200
109 138
 build_flags =
110
-  -DSENSOR_HOSTNAME_PREFIX=\"env-\"
139
+  -DSENSOR_HOSTNAME_PREFIX=\"mst-\"
140
+  "-DNAME_OF_FEATURE=\"Moisture Sensor\""
111 141
   -DENABLE_WEBSOCKETS
112 142
   -DENABLE_DEBUGLOG
113 143
   -DMOISTURE_ADC_ESP32
114 144
   -DENABLE_BME280
115 145
   -DENABLE_CCS811
116 146
   -DENABLE_INFLUXDB_LOGGING
147
+  -DUSE_INFLUXDB_LIB
117 148
   -DENABLE_MQTT
118 149
 lib_deps =
119 150
     Wire
@@ -133,7 +164,8 @@ upload_port = /dev/ttyACM0
133 164
 monitor_port = /dev/ttyACM0
134 165
 monitor_speed = 115200
135 166
 build_flags =
136
-  -DSENSOR_HOSTNAME_PREFIX=\"env-\"
167
+  -DSENSOR_HOSTNAME_PREFIX=\"mst-\"
168
+  "-DNAME_OF_FEATURE=\"Moisture Sensor\""
137 169
   -DENABLE_BME280
138 170
   -DMOISTURE_ADC_ARDUINO
139 171
   -DENABLE_INFLUXDB_LOGGING

+ 69
- 6
src/SimpleInflux.cpp Просмотреть файл

@@ -15,12 +15,15 @@
15 15
 #include "DebugLog.h"
16 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 21
 #include <WiFiLink.h>
21
-
22 22
 WiFiClient client;
23
-
23
+#elif defined(ARDUINO_ARCH_ESP8266)
24
+#include <ESP8266WiFi.h>
25
+#include <ESP8266HTTPClient.h>
26
+#include <WiFiClient.h>
24 27
 #endif
25 28
 
26 29
 void InfluxData::addTag(const char *name, const char *value) {
@@ -42,10 +45,10 @@ void InfluxData::addValue(const char *name, double value) {
42 45
 // https://docs.influxdata.com/influxdb/v1.8/guides/write_data/
43 46
 
44 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 53
     client.stop();
51 54
 
@@ -139,9 +142,69 @@ boolean Influxdb::write(InfluxData &data) {
139 142
         debug.println(F("Error connecting"));
140 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 203
 #else
143 204
 
144 205
     return true; // success
145 206
 
146 207
 #endif
147 208
 }
209
+
210
+#endif

+ 2
- 2
src/html.cpp Просмотреть файл

@@ -73,7 +73,7 @@ void handlePage(WiFiClient &client, int mode, int id) {
73 73
     message += F("<html><head>");
74 74
     message += F("<meta charset='utf-8'/>");
75 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 77
     message += F("<style>");
78 78
     message += F(".log {\n");
79 79
     message += F(    "max-height: 300px;\n");
@@ -100,7 +100,7 @@ void handlePage(WiFiClient &client, int mode, int id) {
100 100
     message += F("}");
101 101
     message += F("</style>");
102 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 104
     message += F("\n<p>\n");
105 105
     message += F("Version: ");
106 106
     message += ESP_ENV_VERSION;

+ 16
- 18
src/influx.cpp Просмотреть файл

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

+ 40
- 14
src/main.cpp Просмотреть файл

@@ -37,11 +37,27 @@ unsigned long last_led_blink_time = 0;
37 37
 
38 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 54
 void setup() {
41 55
     pinMode(BUILTIN_LED_PIN, OUTPUT);
42 56
     
43 57
     Serial.begin(115200);
44 58
 
59
+    debug.println(F("Initializing..."));
60
+
45 61
     // Blink LED for init
46 62
     for (int i = 0; i < 2; i++) {
47 63
         digitalWrite(BUILTIN_LED_PIN, LOW); // LED on
@@ -70,6 +86,7 @@ void setup() {
70 86
     debug.print(F("Connecting WiFi"));
71 87
     WiFi.hostname(hostname);
72 88
     WiFi.mode(WIFI_STA);
89
+    WiFi.hostname(hostname);
73 90
     WiFi.begin(WIFI_SSID, WIFI_PASS);
74 91
     while (WiFi.status() != WL_CONNECTED) {
75 92
         delay(LED_CONNECT_BLINK_INTERVAL);
@@ -77,6 +94,11 @@ void setup() {
77 94
         debug.print(F("."));
78 95
     }
79 96
     debug.println(F("\nWiFi connected!"));
97
+
98
+    disconnectHandler = WiFi.onStationModeDisconnected(onDisconnected);
99
+
100
+    // Set hostname workaround
101
+    WiFi.hostname(hostname);
80 102
     
81 103
 #elif defined(ARDUINO_ARCH_ESP32)
82 104
 
@@ -84,14 +106,18 @@ void setup() {
84 106
     WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
85 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 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 121
     }, WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
96 122
 
97 123
     // Connect to WiFi AP
@@ -128,9 +154,16 @@ void setup() {
128 154
     debug.println(F("Seeding"));
129 155
     randomSeed(micros());
130 156
 
157
+    debug.println(F("MQTT"));
131 158
     initMQTT();
159
+
160
+    debug.println(F("Influx"));
132 161
     initInflux();
162
+
163
+    debug.println(F("Servers"));
133 164
     initServers(hostname);
165
+
166
+    debug.println(F("Ready! Starting..."));
134 167
 }
135 168
 
136 169
 void loop() {
@@ -146,11 +179,4 @@ void loop() {
146 179
         last_led_blink_time = time;
147 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,18 +53,23 @@ static void writeMQTT() {
53 53
         return;
54 54
     }
55 55
 
56
+    bool wrote = false;
57
+
56 58
     if (found_sht) {
57 59
         mqtt.publish(SENSOR_LOCATION "/temperature", String(sht_temp()).c_str(), true);
58 60
         mqtt.publish(SENSOR_LOCATION "/humidity", String(sht_humid()).c_str(), true);
61
+        wrote = true;
59 62
 #ifdef ENABLE_BME280
60 63
     } else if (found_bme1) {
61 64
         mqtt.publish(SENSOR_LOCATION "/temperature", String(bme1_temp()).c_str(), true);
62 65
         mqtt.publish(SENSOR_LOCATION "/humidity", String(bme1_humid()).c_str(), true);
63 66
         mqtt.publish(SENSOR_LOCATION "/pressure", String(bme1_pressure()).c_str(), true);
67
+        wrote = true;
64 68
     } else if (found_bme2) {
65 69
         mqtt.publish(SENSOR_LOCATION "/temperature", String(bme2_temp()).c_str(), true);
66 70
         mqtt.publish(SENSOR_LOCATION "/humidity", String(bme2_humid()).c_str(), true);
67 71
         mqtt.publish(SENSOR_LOCATION "/pressure", String(bme2_pressure()).c_str(), true);
72
+        wrote = true;
68 73
 #endif // ENABLE_BME280
69 74
     }
70 75
 
@@ -72,11 +77,17 @@ static void writeMQTT() {
72 77
     if (found_ccs1) {
73 78
         mqtt.publish(SENSOR_LOCATION "/eco2", String(ccs1_eco2()).c_str(), true);
74 79
         mqtt.publish(SENSOR_LOCATION "/tvoc", String(ccs1_tvoc()).c_str(), true);
80
+        wrote = true;
75 81
     } else if (found_ccs2) {
76 82
         mqtt.publish(SENSOR_LOCATION "/eco2", String(ccs2_eco2()).c_str(), true);
77 83
         mqtt.publish(SENSOR_LOCATION "/tvoc", String(ccs2_tvoc()).c_str(), true);
84
+        wrote = true;
78 85
     }
79 86
 #endif // ENABLE_CCS811
87
+
88
+    if (wrote) {
89
+        debug.println(F("Updated MQTT sensor values"));
90
+    }
80 91
 }
81 92
 
82 93
 static void mqttCallback(char* topic, byte* payload, unsigned int length) {
@@ -163,7 +174,6 @@ static void mqttReconnect() {
163 174
 }
164 175
 
165 176
 void initMQTT() {
166
-    debug.println(F("MQTT"));
167 177
     mqtt.setServer(MQTT_HOST, MQTT_PORT);
168 178
     mqtt.setCallback(mqttCallback);
169 179
 }

+ 27
- 127
src/sensors.cpp Просмотреть файл

@@ -74,137 +74,37 @@ int ccs2_error_code = 0;
74 74
 
75 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 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 109
 #ifdef ENABLE_CCS811
210 110
 

+ 0
- 1
src/servers.cpp Просмотреть файл

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

Загрузка…
Отмена
Сохранить