Browse Source

did no longer work with shell oneliner due to missing newlines

Thomas Buck 3 years ago
parent
commit
be1fef30e4
3 changed files with 44 additions and 29 deletions
  1. 7
    0
      README.md
  2. 25
    26
      src/main.cpp
  3. 12
    3
      src/moisture.cpp

+ 7
- 0
README.md View File

1
+# ESP-Env
2
+
3
+Small firmware supporting different environmental sensors, as well as different microcontroller platforms.
4
+
5
+Supports SHT21, BME280, analog moisture sensors.
6
+
7
+Runs on ESP32, ESP8266, Arduino Uno Wifi Developer Edition.

+ 25
- 26
src/main.cpp View File

12
  */
12
  */
13
 
13
 
14
 #include <Arduino.h>
14
 #include <Arduino.h>
15
-#include <Wire.h>
16
 #include <Adafruit_BME280.h>
15
 #include <Adafruit_BME280.h>
17
 #include <SHT2x.h>
16
 #include <SHT2x.h>
18
 
17
 
39
 
38
 
40
 #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
39
 #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
41
 
40
 
41
+#include <Wire.h>
42
 #include "SimpleUpdater.h"
42
 #include "SimpleUpdater.h"
43
 
43
 
44
 #define BUILTIN_LED_PIN 1
44
 #define BUILTIN_LED_PIN 1
269
     message += F("<title>" ESP_PLATFORM_NAME " Environment Sensor</title>");
269
     message += F("<title>" ESP_PLATFORM_NAME " Environment Sensor</title>");
270
     message += F("</head><body>");
270
     message += F("</head><body>");
271
     message += F("<h1>" ESP_PLATFORM_NAME " Environment Sensor</h1>");
271
     message += F("<h1>" ESP_PLATFORM_NAME " Environment Sensor</h1>");
272
-    message += F("<p>");
272
+    message += F("\n<p>\n");
273
     message += F("Version: ");
273
     message += F("Version: ");
274
     message += esp_env_version;
274
     message += esp_env_version;
275
-    message += F("<br>");
275
+    message += F("\n<br>\n");
276
     message += F("Location: ");
276
     message += F("Location: ");
277
     message += sensor_location;
277
     message += sensor_location;
278
 
278
 
279
 #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
279
 #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
280
-    message += F("<br>");
280
+    message += F("\n<br>\n");
281
     message += F("MAC: ");
281
     message += F("MAC: ");
282
     message += WiFi.macAddress();
282
     message += WiFi.macAddress();
283
 #endif
283
 #endif
284
 
284
 
285
-    message += F("</p>");
285
+    message += F("\n</p>\n");
286
 
286
 
287
 #if defined(ARDUINO_ARCH_AVR)
287
 #if defined(ARDUINO_ARCH_AVR)
288
     do {
288
     do {
342
     
342
     
343
 #endif
343
 #endif
344
 
344
 
345
-    message += F("<p>");
345
+    message += F("\n<p>\n");
346
     if (found_bme1) {
346
     if (found_bme1) {
347
         message += F("BME280 Low:");
347
         message += F("BME280 Low:");
348
-        message += F("<br>");
348
+        message += F("\n<br>\n");
349
         message += F("Temperature: ");
349
         message += F("Temperature: ");
350
         message += String(bme1_temp());
350
         message += String(bme1_temp());
351
-        message += F("<br>");
351
+        message += F("\n<br>\n");
352
         message += F("Humidity: ");
352
         message += F("Humidity: ");
353
         message += String(bme1_humid());
353
         message += String(bme1_humid());
354
-        message += F("<br>");
354
+        message += F("\n<br>\n");
355
         message += F("Pressure: ");
355
         message += F("Pressure: ");
356
         message += String(bme1_pressure());
356
         message += String(bme1_pressure());
357
     } else {
357
     } else {
358
         message += F("BME280 (low) not connected!");
358
         message += F("BME280 (low) not connected!");
359
     }
359
     }
360
-    message += F("</p>");
360
+    message += F("\n</p>\n");
361
 
361
 
362
-    message += F("<p>");
362
+    message += F("\n<p>\n");
363
     if (found_bme2) {
363
     if (found_bme2) {
364
         message += F("BME280 High:");
364
         message += F("BME280 High:");
365
-        message += F("<br>");
365
+        message += F("\n<br>\n");
366
         message += F("Temperature: ");
366
         message += F("Temperature: ");
367
         message += String(bme2_temp());
367
         message += String(bme2_temp());
368
-        message += F("<br>");
368
+        message += F("\n<br>\n");
369
         message += F("Humidity: ");
369
         message += F("Humidity: ");
370
         message += String(bme2_humid());
370
         message += String(bme2_humid());
371
-        message += F("<br>");
371
+        message += F("\n<br>\n");
372
         message += F("Pressure: ");
372
         message += F("Pressure: ");
373
         message += String(bme2_pressure());
373
         message += String(bme2_pressure());
374
     } else {
374
     } else {
375
         message += F("BME280 (high) not connected!");
375
         message += F("BME280 (high) not connected!");
376
     }
376
     }
377
-    message += F("</p>");
377
+    message += F("\n</p>\n");
378
 
378
 
379
-    message += F("<p>");
379
+    message += F("\n<p>\n");
380
     if (found_sht) {
380
     if (found_sht) {
381
         message += F("SHT21:");
381
         message += F("SHT21:");
382
-        message += F("<br>");
382
+        message += F("\n<br>\n");
383
         message += F("Temperature: ");
383
         message += F("Temperature: ");
384
         message += String(sht_temp());
384
         message += String(sht_temp());
385
-        message += F("<br>");
385
+        message += F("\n<br>\n");
386
         message += F("Humidity: ");
386
         message += F("Humidity: ");
387
         message += String(sht_humid());
387
         message += String(sht_humid());
388
     } else {
388
     } else {
389
-        //message += F("SHT21 not connected!");
389
+        message += F("SHT21 not connected!");
390
     }
390
     }
391
-    message += F("</p>");
391
+    message += F("\n</p>\n");
392
 
392
 
393
 #if defined(ARDUINO_ARCH_AVR)
393
 #if defined(ARDUINO_ARCH_AVR)
394
     do {
394
     do {
409
     for (int i = 0; i < moisture_count(); i++) {
409
     for (int i = 0; i < moisture_count(); i++) {
410
         int moisture = moisture_read(i);
410
         int moisture = moisture_read(i);
411
         if (moisture < moisture_max()) {
411
         if (moisture < moisture_max()) {
412
-            message += F("<p>");
412
+            message += F("\n<p>\n");
413
             message += F("Sensor ");
413
             message += F("Sensor ");
414
             message += String(i + 1);
414
             message += String(i + 1);
415
-            message += F(":<br>");
415
+            message += F(":\n<br>\n");
416
             message += F("Moisture: ");
416
             message += F("Moisture: ");
417
             message += String(moisture);
417
             message += String(moisture);
418
             message += F(" / ");
418
             message += F(" / ");
419
             message += String(moisture_max());
419
             message += String(moisture_max());
420
-            message += F("</p>");
420
+            message += F("\n</p>\n");
421
         }
421
         }
422
     }
422
     }
423
     
423
     
424
     if (moisture_count() <= 0) {
424
     if (moisture_count() <= 0) {
425
-        message += F("<p>");
425
+        message += F("\n<p>\n");
426
         message += F("No moisture sensors configured!");
426
         message += F("No moisture sensors configured!");
427
-        message += F("</p>");
427
+        message += F("\n</p>\n");
428
     }
428
     }
429
 
429
 
430
 #if ! defined(ARDUINO_ARCH_AVR)
430
 #if ! defined(ARDUINO_ARCH_AVR)
502
 
502
 
503
 #elif defined(ARDUINO_ARCH_AVR)
503
 #elif defined(ARDUINO_ARCH_AVR)
504
 
504
 
505
-    Wire.begin();
506
     found_bme1 = (!bme1.begin(BME_I2C_ADDRESS_1, &Wire)) ? false : true;
505
     found_bme1 = (!bme1.begin(BME_I2C_ADDRESS_1, &Wire)) ? false : true;
507
     found_bme2 = (!bme2.begin(BME_I2C_ADDRESS_2, &Wire)) ? false : true;
506
     found_bme2 = (!bme2.begin(BME_I2C_ADDRESS_2, &Wire)) ? false : true;
508
 
507
 

+ 12
- 3
src/moisture.cpp View File

81
 
81
 
82
 #elif defined(ARDUINO_ARCH_AVR)
82
 #elif defined(ARDUINO_ARCH_AVR)
83
 
83
 
84
+// Hardware I2C pins A4 and A5 not usable on Arduino Uno Wifi Dev. Ed.
84
 #define SENSOR_COUNT 6
85
 #define SENSOR_COUNT 6
85
-uint8_t sensor_pins[SENSOR_COUNT] = { A0, A1, A2, A3, A4, A5 };
86
+uint8_t sensor_pins[SENSOR_COUNT] = { A0, A1, A1, A2, A3, A3 };
87
+
88
+#define OVERSAMPLE_COUNT 3
86
 
89
 
87
 int moisture_count(void) {
90
 int moisture_count(void) {
88
-    return 6;
91
+    return SENSOR_COUNT;
89
 }
92
 }
90
 
93
 
91
 int moisture_read(int sensor) {
94
 int moisture_read(int sensor) {
92
-    return (analogRead(sensor_pins[sensor]) << 2) | 3;
95
+    unsigned long val = 0;
96
+    for (int i = 0; i < OVERSAMPLE_COUNT; i++) {
97
+        val += analogRead(sensor_pins[sensor]);
98
+    }
99
+    val /= OVERSAMPLE_COUNT;
100
+    if (val >= 1010) val = 1023; // avoid noisy data in db
101
+    return (int)((val << 2) | 3);
93
 }
102
 }
94
 
103
 
95
 int moisture_max(void) {
104
 int moisture_max(void) {

Loading…
Cancel
Save