Browse Source

add feature-flags for libs, to fit 512k esp-01 version.

Thomas Buck 1 year ago
parent
commit
47716cc74b
9 changed files with 124 additions and 37 deletions
  1. 6
    6
      include/DebugLog.h
  2. 7
    3
      include/sensors.h
  3. 34
    0
      platformio.ini
  4. 4
    7
      src/DebugLog.cpp
  5. 8
    4
      src/html.cpp
  6. 4
    0
      src/influx.cpp
  7. 18
    6
      src/mqtt.cpp
  8. 33
    8
      src/sensors.cpp
  9. 10
    3
      src/servers.cpp

+ 6
- 6
include/DebugLog.h View File

@@ -16,16 +16,16 @@
16 16
 
17 17
 #include <Arduino.h>
18 18
 
19
-#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
19
+#ifdef ENABLE_DEBUGLOG
20 20
 #include <CircularBuffer.h>
21 21
 #define DEBUG_LOG_HISTORY_SIZE 1024
22
-#endif
22
+#endif // ENABLE_DEBUGLOG
23 23
 
24 24
 class DebugLog {
25 25
 public:
26
-#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
26
+#ifdef ENABLE_DEBUGLOG
27 27
     String getBuffer(void);
28
-#endif
28
+#endif // ENABLE_DEBUGLOG
29 29
     
30 30
     void write(char c);
31 31
     void print(String s);
@@ -38,11 +38,11 @@ public:
38 38
 private:
39 39
     void sendToTargets(String s);
40 40
     
41
-#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
41
+#ifdef ENABLE_DEBUGLOG
42 42
     void addToBuffer(String s);
43 43
     
44 44
     CircularBuffer<char, DEBUG_LOG_HISTORY_SIZE> buffer;
45
-#endif
45
+#endif // ENABLE_DEBUGLOG
46 46
 };
47 47
 
48 48
 extern DebugLog debug;

+ 7
- 3
include/sensors.h View File

@@ -14,15 +14,19 @@
14 14
 #ifndef __SENSORS_H__
15 15
 #define __SENSORS_H__
16 16
 
17
+float sht_temp(void);
18
+float sht_humid(void);
19
+extern bool found_sht;
20
+
21
+#ifdef ENABLE_BME280
17 22
 float bme1_temp(void);
18 23
 float bme2_temp(void);
19 24
 float bme1_humid(void);
20 25
 float bme2_humid(void);
21 26
 float bme1_pressure(void);
22 27
 float bme2_pressure(void);
23
-float sht_temp(void);
24
-float sht_humid(void);
25
-extern bool found_bme1, found_bme2, found_sht;
28
+extern bool found_bme1, found_bme2;
29
+#endif // ENABLE_BME280
26 30
 
27 31
 #ifdef ENABLE_CCS811
28 32
 float ccs1_eco2(void);

+ 34
- 0
platformio.ini View File

@@ -14,6 +14,9 @@ board = esp01_1m
14 14
 framework = arduino
15 15
 build_flags =
16 16
   -DSENSOR_HOSTNAME_PREFIX=\"env-\"
17
+  -DENABLE_WEBSOCKETS
18
+  -DENABLE_DEBUGLOG
19
+  -DENABLE_BME280
17 20
   -DENABLE_CCS811
18 21
   -DENABLE_INFLUXDB_LOGGING
19 22
   -DENABLE_MQTT
@@ -27,12 +30,36 @@ lib_deps =
27 30
     https://github.com/rlogiacco/CircularBuffer
28 31
     https://github.com/Links2004/arduinoWebSockets
29 32
 
33
+[env:esp8266smallrelais]
34
+platform = espressif8266
35
+board = esp01
36
+framework = arduino
37
+build_flags =
38
+  -DSENSOR_HOSTNAME_PREFIX=\"relais-\"
39
+  -DENABLE_WEBSOCKETS
40
+  -DENABLE_DEBUGLOG
41
+  -DENABLE_BME280
42
+  -DENABLE_CCS811
43
+  -DRELAIS_SERIAL
44
+  -DENABLE_MQTT
45
+lib_deps =
46
+    Wire
47
+    Adafruit Unified Sensor
48
+    Adafruit BME280 Library
49
+    https://github.com/adafruit/Adafruit_CCS811
50
+    https://github.com/knolleary/pubsubclient.git#2d228f2f862a95846c65a8518c79f48dfc8f188c
51
+    https://github.com/rlogiacco/CircularBuffer
52
+    https://github.com/Links2004/arduinoWebSockets
53
+
30 54
 [env:esp8266relais]
31 55
 platform = espressif8266
32 56
 board = esp01_1m
33 57
 framework = arduino
34 58
 build_flags =
35 59
   -DSENSOR_HOSTNAME_PREFIX=\"relais-\"
60
+  -DENABLE_WEBSOCKETS
61
+  -DENABLE_DEBUGLOG
62
+  -DENABLE_BME280
36 63
   -DRELAIS_SERIAL
37 64
   -DENABLE_INFLUXDB_LOGGING
38 65
   -DENABLE_MQTT
@@ -55,6 +82,9 @@ monitor_port = /dev/ttyUSB1
55 82
 monitor_speed = 115200
56 83
 build_flags =
57 84
   -DSENSOR_HOSTNAME_PREFIX=\"env-\"
85
+  -DENABLE_WEBSOCKETS
86
+  -DENABLE_DEBUGLOG
87
+  -DENABLE_BME280
58 88
   -DENABLE_CCS811
59 89
   -DENABLE_INFLUXDB_LOGGING
60 90
   -DENABLE_MQTT
@@ -78,7 +108,10 @@ monitor_port = /dev/ttyUSB1
78 108
 monitor_speed = 115200
79 109
 build_flags =
80 110
   -DSENSOR_HOSTNAME_PREFIX=\"env-\"
111
+  -DENABLE_WEBSOCKETS
112
+  -DENABLE_DEBUGLOG
81 113
   -DMOISTURE_ADC_ESP32
114
+  -DENABLE_BME280
82 115
   -DENABLE_CCS811
83 116
   -DENABLE_INFLUXDB_LOGGING
84 117
   -DENABLE_MQTT
@@ -101,6 +134,7 @@ monitor_port = /dev/ttyACM0
101 134
 monitor_speed = 115200
102 135
 build_flags =
103 136
   -DSENSOR_HOSTNAME_PREFIX=\"env-\"
137
+  -DENABLE_BME280
104 138
   -DMOISTURE_ADC_ARDUINO
105 139
   -DENABLE_INFLUXDB_LOGGING
106 140
 lib_deps =

+ 4
- 7
src/DebugLog.cpp View File

@@ -13,13 +13,12 @@
13 13
 
14 14
 #include <Arduino.h>
15 15
 
16
+#include "servers.h"
16 17
 #include "DebugLog.h"
17 18
 
18 19
 DebugLog debug;
19 20
 
20
-#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
21
-
22
-void wifi_send_websocket(String s);
21
+#ifdef ENABLE_DEBUGLOG
23 22
 
24 23
 String DebugLog::getBuffer(void) {
25 24
     String r;
@@ -40,10 +39,8 @@ void DebugLog::addToBuffer(String s) {
40 39
 void DebugLog::sendToTargets(String s) {
41 40
     Serial.print(s);
42 41
     
43
-#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
44 42
     s = "log:" + s;
45 43
     wifi_send_websocket(s);
46
-#endif
47 44
 }
48 45
 
49 46
 void DebugLog::write(char c) {
@@ -51,9 +48,9 @@ void DebugLog::write(char c) {
51 48
 }
52 49
 
53 50
 void DebugLog::print(String s) {
54
-#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
51
+#ifdef ENABLE_DEBUGLOG
55 52
     addToBuffer(s);
56
-#endif
53
+#endif // ENABLE_DEBUGLOG
57 54
     
58 55
     sendToTargets(s);
59 56
 }

+ 8
- 4
src/html.cpp View File

@@ -163,6 +163,8 @@ void handlePage(WiFiClient &client, int mode, int id) {
163 163
     
164 164
 #endif
165 165
 
166
+#ifdef ENABLE_BME280
167
+
166 168
     message += F("\n<p>\n");
167 169
     if (found_bme1) {
168 170
         message += F("BME280 Low:");
@@ -213,6 +215,8 @@ void handlePage(WiFiClient &client, int mode, int id) {
213 215
     }
214 216
     message += F("\n</p><hr>\n");
215 217
 
218
+#endif // ENABLE_BME280
219
+
216 220
     ARDUINO_SEND_PARTIAL_PAGE();
217 221
 
218 222
     message += F("\n<p>\n");
@@ -370,16 +374,16 @@ void handlePage(WiFiClient &client, int mode, int id) {
370 374
     message += String(millis() / 1000);
371 375
     message += F(" sec.</p>");
372 376
 
373
-#if ! defined(ARDUINO_ARCH_AVR)
377
+#ifdef ENABLE_DEBUGLOG
374 378
     message += F("<hr><p>Debug Log:</p>");
375 379
     message += F("<div class='log'><pre id='logbuf'>");
376 380
     message += debug.getBuffer();
377 381
     message += F("</pre></div>");
378
-#endif
382
+#endif // ENABLE_DEBUGLOG
379 383
 
380 384
     message += F("</body>");
381 385
 
382
-#if ! defined(ARDUINO_ARCH_AVR)
386
+#ifdef ENABLE_WEBSOCKETS
383 387
     message += F("<script type='text/javascript'>\n");
384 388
     message += F("var socket = new WebSocket('ws://' + window.location.hostname + ':81');\n");
385 389
     message += F("socket.onmessage = function(e) {");
@@ -394,7 +398,7 @@ void handlePage(WiFiClient &client, int mode, int id) {
394 398
     message += F("var hist = document.getElementsByClassName('log')[0];\n");
395 399
     message += F("hist.scrollTop = hist.scrollHeight;\n");
396 400
     message += F("</script>\n");
397
-#endif
401
+#endif // ENABLE_WEBSOCKETS
398 402
 
399 403
     message += F("</html>");
400 404
 

+ 4
- 0
src/influx.cpp View File

@@ -96,6 +96,8 @@ void writeDatabase() {
96 96
     InfluxData measurement("");
97 97
 #endif
98 98
 
99
+#ifdef ENABLE_BME280
100
+
99 101
     if (found_bme1) {
100 102
 #if defined(ARDUINO_ARCH_AVR)
101 103
         measurement.clear();
@@ -148,6 +150,8 @@ void writeDatabase() {
148 150
         debug.println(F("Done!"));
149 151
     }
150 152
 
153
+#endif // ENABLE_BME280
154
+
151 155
     if (found_sht) {
152 156
 #if defined(ARDUINO_ARCH_AVR)
153 157
         measurement.clear();

+ 18
- 6
src/mqtt.cpp View File

@@ -53,7 +53,11 @@ static void writeMQTT() {
53 53
         return;
54 54
     }
55 55
 
56
-    if (found_bme1) {
56
+    if (found_sht) {
57
+        mqtt.publish(SENSOR_LOCATION "/temperature", String(sht_temp()).c_str(), true);
58
+        mqtt.publish(SENSOR_LOCATION "/humidity", String(sht_humid()).c_str(), true);
59
+#ifdef ENABLE_BME280
60
+    } else if (found_bme1) {
57 61
         mqtt.publish(SENSOR_LOCATION "/temperature", String(bme1_temp()).c_str(), true);
58 62
         mqtt.publish(SENSOR_LOCATION "/humidity", String(bme1_humid()).c_str(), true);
59 63
         mqtt.publish(SENSOR_LOCATION "/pressure", String(bme1_pressure()).c_str(), true);
@@ -61,9 +65,7 @@ static void writeMQTT() {
61 65
         mqtt.publish(SENSOR_LOCATION "/temperature", String(bme2_temp()).c_str(), true);
62 66
         mqtt.publish(SENSOR_LOCATION "/humidity", String(bme2_humid()).c_str(), true);
63 67
         mqtt.publish(SENSOR_LOCATION "/pressure", String(bme2_pressure()).c_str(), true);
64
-    } else if (found_sht) {
65
-        mqtt.publish(SENSOR_LOCATION "/temperature", String(sht_temp()).c_str(), true);
66
-        mqtt.publish(SENSOR_LOCATION "/humidity", String(sht_humid()).c_str(), true);
68
+#endif // ENABLE_BME280
67 69
     }
68 70
 
69 71
 #ifdef ENABLE_CCS811
@@ -78,12 +80,22 @@ static void writeMQTT() {
78 80
 }
79 81
 
80 82
 static void mqttCallback(char* topic, byte* payload, unsigned int length) {
83
+    String ts(topic), ps;
84
+    for (unsigned int i = 0; i < length; i++) {
85
+        char c = payload[i];
86
+        ps += c;
87
+    }
88
+
89
+    debug.print(F("MQTT Rx @ \""));
90
+    debug.print(ts);
91
+    debug.print(F("\" = \""));
92
+    debug.print(ps);
93
+    debug.println(F("\""));
94
+
81 95
 #ifdef FEATURE_RELAIS
82 96
     int state = 0;
83 97
     int id = -1;
84 98
 
85
-    String ts(topic), ps((char *)payload);
86
-
87 99
     String our_topic(SENSOR_LOCATION);
88 100
     our_topic += "/";
89 101
 

+ 33
- 8
src/sensors.cpp View File

@@ -13,9 +13,12 @@
13 13
 
14 14
 #include <Arduino.h>
15 15
 
16
-#include <Adafruit_BME280.h>
17 16
 #include <SHT2x.h>
18 17
 
18
+#ifdef ENABLE_BME280
19
+#include <Adafruit_BME280.h>
20
+#endif // ENABLE_BME280
21
+
19 22
 #ifdef ENABLE_CCS811
20 23
 #include <Adafruit_CCS811.h>
21 24
 #endif // ENABLE_CCS811
@@ -51,10 +54,12 @@ static SHT2x sht(SHT_I2C_ADDRESS, &Wire);
51 54
 
52 55
 #endif
53 56
 
57
+#ifdef ENABLE_BME280
54 58
 static Adafruit_BME280 bme1, bme2;
55
-
56 59
 bool found_bme1 = false;
57 60
 bool found_bme2 = false;
61
+#endif // ENABLE_BME280
62
+
58 63
 bool found_sht = false;
59 64
 
60 65
 #ifdef ENABLE_CCS811
@@ -69,6 +74,8 @@ int ccs2_error_code = 0;
69 74
 
70 75
 static unsigned long last_sensor_handle_time = 0;
71 76
 
77
+#ifdef ENABLE_BME280
78
+
72 79
 float bme1_temp(void) {
73 80
     while (1) {
74 81
         float a = bme1.readTemperature();
@@ -165,6 +172,8 @@ float bme2_pressure(void) {
165 172
     return 0.0;
166 173
 }
167 174
 
175
+#endif // ENABLE_BME280
176
+
168 177
 float sht_temp(void) {
169 178
     while (1) {
170 179
         float a = sht.GetTemperature() + config.sht_temp_off;
@@ -218,12 +227,14 @@ float ccs2_tvoc(void) {
218 227
 static void ccs_update() {
219 228
     if (found_ccs1) {
220 229
         if (ccs1.available()) {
221
-            if (found_bme1) {
230
+            if (found_sht) {
231
+                ccs1.setEnvironmentalData(sht_humid(), sht_temp());
232
+#ifdef ENABLE_BME280
233
+            } else if (found_bme1) {
222 234
                 ccs1.setEnvironmentalData(bme1_humid(), bme1_temp());
223 235
             } else if (found_bme2) {
224 236
                 ccs1.setEnvironmentalData(bme2_humid(), bme2_temp());
225
-            } else if (found_sht) {
226
-                ccs1.setEnvironmentalData(sht_humid(), sht_temp());
237
+#endif // ENABLE_BME280
227 238
             }
228 239
 
229 240
             ccs1_error_code = ccs1.readData();
@@ -233,12 +244,14 @@ static void ccs_update() {
233 244
 
234 245
     if (found_ccs2) {
235 246
         if (ccs2.available()) {
236
-            if (found_bme1) {
247
+            if (found_sht) {
248
+                ccs2.setEnvironmentalData(sht_humid(), sht_temp());
249
+#ifdef ENABLE_BME280
250
+            } else if (found_bme1) {
237 251
                 ccs2.setEnvironmentalData(bme1_humid(), bme1_temp());
238 252
             } else if (found_bme2) {
239 253
                 ccs2.setEnvironmentalData(bme2_humid(), bme2_temp());
240
-            } else if (found_sht) {
241
-                ccs2.setEnvironmentalData(sht_humid(), sht_temp());
254
+#endif // ENABLE_BME280
242 255
             }
243 256
 
244 257
             ccs2_error_code = ccs2.readData();
@@ -253,6 +266,8 @@ static void ccs_update() {
253 266
 void handleCalibrate() {
254 267
     bool diff = false;
255 268
 
269
+#ifdef ENABLE_BME280
270
+
256 271
     if (server.hasArg("bme1")) {
257 272
         diff = true;
258 273
         String off_string = server.arg("bme1");
@@ -269,6 +284,8 @@ void handleCalibrate() {
269 284
         config.bme2_temp_off = real_temp - meas_temp;
270 285
     }
271 286
 
287
+#endif // ENABLE_BME280
288
+
272 289
     if (server.hasArg("sht")) {
273 290
         diff = true;
274 291
         String off_string = server.arg("sht");
@@ -278,6 +295,7 @@ void handleCalibrate() {
278 295
     }
279 296
 
280 297
     if (diff) {
298
+#ifdef ENABLE_BME280
281 299
         if (found_bme1) {
282 300
             bme1.setTemperatureCompensation(config.bme1_temp_off);
283 301
         }
@@ -285,6 +303,7 @@ void handleCalibrate() {
285 303
         if (found_bme2) {
286 304
             bme2.setTemperatureCompensation(config.bme2_temp_off);
287 305
         }
306
+#endif // ENABLE_BME280
288 307
 
289 308
         mem_write(config);
290 309
         handlePage(42);
@@ -301,9 +320,11 @@ void initSensors() {
301 320
     debug.println(F("Wire2"));
302 321
     Wire2.begin(I2C_SDA_PIN, I2C_SCL_PIN);
303 322
 
323
+#ifdef ENABLE_BME280
304 324
     debug.println(F("BME"));
305 325
     found_bme1 = (!bme1.begin(BME_I2C_ADDRESS_1, &Wire2)) ? false : true;
306 326
     found_bme2 = (!bme2.begin(BME_I2C_ADDRESS_2, &Wire2)) ? false : true;
327
+#endif // ENABLE_BME280
307 328
 
308 329
 #ifdef ENABLE_CCS811
309 330
     debug.println(F("CCS"));
@@ -318,9 +339,11 @@ void initSensors() {
318 339
     Wire.begin();
319 340
 #endif
320 341
 
342
+#ifdef ENABLE_BME280
321 343
     debug.println(F("BME"));
322 344
     found_bme1 = (!bme1.begin(BME_I2C_ADDRESS_1, &Wire)) ? false : true;
323 345
     found_bme2 = (!bme2.begin(BME_I2C_ADDRESS_2, &Wire)) ? false : true;
346
+#endif // ENABLE_BME280
324 347
 
325 348
 #ifdef ENABLE_CCS811
326 349
     debug.println(F("CCS"));
@@ -333,6 +356,7 @@ void initSensors() {
333 356
     debug.println(F("SHT"));
334 357
     found_sht = sht.GetAlive();
335 358
 
359
+#ifdef ENABLE_BME280
336 360
     // initialize temperature offsets
337 361
     if (found_bme1) {
338 362
         bme1.setTemperatureCompensation(config.bme1_temp_off);
@@ -340,6 +364,7 @@ void initSensors() {
340 364
     if (found_bme2) {
341 365
         bme2.setTemperatureCompensation(config.bme2_temp_off);
342 366
     }
367
+#endif // ENABLE_BME280
343 368
 }
344 369
 
345 370
 void runSensors() {

+ 10
- 3
src/servers.cpp View File

@@ -29,12 +29,15 @@
29 29
 
30 30
 #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
31 31
 
32
-#include <WebSocketsServer.h>
33 32
 #include "SimpleUpdater.h"
34 33
 
35 34
 UPDATE_WEB_SERVER server(80);
36 35
 SimpleUpdater updater;
36
+
37
+#ifdef ENABLE_WEBSOCKETS
38
+#include <WebSocketsServer.h>
37 39
 WebSocketsServer socket(81);
40
+#endif // ENABLE_WEBSOCKETS
38 41
 
39 42
 #elif defined(ARDUINO_ARCH_AVR)
40 43
 
@@ -57,11 +60,11 @@ WiFiServer server(80);
57 60
 
58 61
 static unsigned long last_server_handle_time = 0;
59 62
 
60
-#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
61 63
 void wifi_send_websocket(String s) {
64
+#ifdef ENABLE_WEBSOCKETS
62 65
     socket.broadcastTXT(s);
66
+#endif // ENABLE_WEBSOCKETS
63 67
 }
64
-#endif
65 68
 
66 69
 #ifdef FEATURE_RELAIS
67 70
 
@@ -143,7 +146,9 @@ void initServers(String hostname) {
143 146
 
144 147
     MDNS.addService("http", "tcp", 80);
145 148
 
149
+#ifdef ENABLE_WEBSOCKETS
146 150
     socket.begin();
151
+#endif // ENABLE_WEBSOCKETS
147 152
 #endif
148 153
 
149 154
     server.begin();
@@ -220,7 +225,9 @@ void runServers() {
220 225
         handleServers();
221 226
 
222 227
 #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
228
+#ifdef ENABLE_WEBSOCKETS
223 229
         socket.loop();
230
+#endif // ENABLE_WEBSOCKETS
224 231
 #endif
225 232
     }
226 233
     

Loading…
Cancel
Save