Browse Source

support Arduino Uno Wifi Developer Edition

Thomas Buck 2 years ago
parent
commit
72917b5bf4
7 changed files with 694 additions and 78 deletions
  1. 63
    0
      include/SimpleInflux.h
  2. 4
    2
      include/SimpleUpdater.h
  3. 13
    0
      platformio.ini
  4. 146
    0
      src/SimpleInflux.cpp
  5. 3
    0
      src/SimpleUpdater.cpp
  6. 444
    76
      src/main.cpp
  7. 21
    0
      src/moisture.cpp

+ 63
- 0
include/SimpleInflux.h View File

1
+/*
2
+ * SimpleInflux.h
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
+#ifndef __ESP_SIMPLE_INFLUX__
15
+#define __ESP_SIMPLE_INFLUX__
16
+
17
+#define SIMPLE_INFLUX_MAX_ELEMENTS 3
18
+
19
+class InfluxData {
20
+  public:
21
+    InfluxData(const char *name) : data_name(name), tag_count(0), value_count(0) { }
22
+    void addTag(const char *name, const char *value);
23
+    void addTag(const char *name, String &value) { addTag(name, value.c_str()); }
24
+    void addValue(const char *name, double value);
25
+
26
+    int tagCount() { return tag_count; }
27
+    const char *tagName(int tag) { return tag_name[tag < tag_count ? tag : 0]; }
28
+    const char *tagValue(int tag) { return tag_value[tag < tag_count ? tag : 0]; }
29
+
30
+    int valueCount() { return value_count; }
31
+    const char *valueName(int val) { return value_name[val < value_count ? val : 0]; }
32
+    double valueValue(int val) { return value_value[val < value_count ? val : 0]; }
33
+
34
+    const char *dataName() { return data_name; }
35
+    void setName(const char *n) { data_name = n; }
36
+
37
+    void clear() { tag_count = 0; value_count = 0;}
38
+
39
+  private:
40
+    const char *data_name;
41
+
42
+    const char *tag_name[SIMPLE_INFLUX_MAX_ELEMENTS];
43
+    const char *tag_value[SIMPLE_INFLUX_MAX_ELEMENTS];
44
+    int tag_count;
45
+
46
+    const char *value_name[SIMPLE_INFLUX_MAX_ELEMENTS];
47
+    double value_value[SIMPLE_INFLUX_MAX_ELEMENTS];
48
+    int value_count;
49
+};
50
+
51
+class Influxdb {
52
+  public:
53
+    Influxdb(const char *host, int port) : db_host(host), db_port(port) { }
54
+    void setDb(const char *db) { db_name = db; }
55
+    boolean write(InfluxData &data);
56
+
57
+  private:
58
+      const char *db_host;
59
+      int db_port;
60
+      const char *db_name;
61
+};
62
+
63
+#endif // __ESP_SIMPLE_INFLUX__

+ 4
- 2
include/SimpleUpdater.h View File

21
 #elif defined(ARDUINO_ARCH_ESP32)
21
 #elif defined(ARDUINO_ARCH_ESP32)
22
 #include <WebServer.h>
22
 #include <WebServer.h>
23
 #define UPDATE_WEB_SERVER WebServer
23
 #define UPDATE_WEB_SERVER WebServer
24
-#else
25
-#error Platform not supported!
26
 #endif
24
 #endif
27
 
25
 
26
+#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
27
+
28
 class SimpleUpdater {
28
 class SimpleUpdater {
29
 public:
29
 public:
30
     SimpleUpdater(String _uri = String("/update"));
30
     SimpleUpdater(String _uri = String("/update"));
48
     UPDATE_WEB_SERVER *server;
48
     UPDATE_WEB_SERVER *server;
49
 };
49
 };
50
 
50
 
51
+#endif
52
+
51
 #endif // __ESP_SIMPLE_UPDATER__
53
 #endif // __ESP_SIMPLE_UPDATER__
52
 
54
 

+ 13
- 0
platformio.ini View File

29
     Adafruit BME280 Library
29
     Adafruit BME280 Library
30
     https://github.com/tobiasschuerg/InfluxDB-Client-for-Arduino.git#66ed5d031caab6953cc79b407a4b49d33b1126dc
30
     https://github.com/tobiasschuerg/InfluxDB-Client-for-Arduino.git#66ed5d031caab6953cc79b407a4b49d33b1126dc
31
 
31
 
32
+[env:arduinowifideved]
33
+platform = atmelavr
34
+board = uno
35
+framework = arduino
36
+upload_port = /dev/ttyACM0
37
+monitor_port = /dev/ttyACM0
38
+monitor_speed = 115200
39
+lib_deps =
40
+    Wire
41
+    Adafruit Unified Sensor
42
+    Adafruit BME280 Library
43
+    https://github.com/jandrassy/UnoWiFiDevEdSerial1
44
+    https://github.com/jandrassy/arduino-library-wifilink

+ 146
- 0
src/SimpleInflux.cpp View File

1
+/*
2
+ * SimpleInflux.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 "SimpleInflux.h"
16
+
17
+#if defined(ARDUINO_ARCH_AVR)
18
+
19
+#include <WiFiLink.h>
20
+
21
+WiFiClient client;
22
+
23
+#endif
24
+
25
+void InfluxData::addTag(const char *name, const char *value) {
26
+    if (tag_count < SIMPLE_INFLUX_MAX_ELEMENTS) {
27
+        tag_name[tag_count] = name;
28
+        tag_value[tag_count] = value;
29
+        tag_count++;
30
+    }
31
+}
32
+
33
+void InfluxData::addValue(const char *name, double value) {
34
+    if (value_count < SIMPLE_INFLUX_MAX_ELEMENTS) {
35
+        value_name[value_count] = name;
36
+        value_value[value_count] = value;
37
+        value_count++;
38
+    }
39
+}
40
+
41
+// https://docs.influxdata.com/influxdb/v1.8/guides/write_data/
42
+
43
+boolean Influxdb::write(InfluxData &data) {
44
+#if defined(ARDUINO_ARCH_AVR)
45
+
46
+    Serial.print(F("Writing "));
47
+    Serial.println(data.dataName());
48
+
49
+    client.stop();
50
+
51
+    if (client.connect(db_host, db_port)) {
52
+        client.print(F("POST /write?db="));
53
+        client.print(db_name);
54
+        client.println(F(" HTTP/1.1"));
55
+
56
+        client.print(F("Host: "));
57
+        client.print(db_host);
58
+        client.print(F(":"));
59
+        client.println(String(db_port));
60
+
61
+        client.println(F("Connection: close"));
62
+
63
+        int len = strlen(data.dataName()) + 1;
64
+        for (int i = 0; i < data.tagCount(); i++) {
65
+            len += strlen(data.tagName(i)) + strlen(data.tagValue(i)) + 2;
66
+        }
67
+        for (int i = 0; i < data.valueCount(); i++) {
68
+            len += strlen(data.valueName(i)) + String(data.valueValue(i)).length() + 1;
69
+            if (i > 0) {
70
+                len += 1;
71
+            }
72
+        }
73
+
74
+        client.print(F("Content-Length: "));
75
+        client.println(String(len));
76
+
77
+        client.println();
78
+
79
+        client.print(data.dataName());
80
+        for (int i = 0; i < data.tagCount(); i++) {
81
+            client.print(F(","));
82
+            client.print(data.tagName(i));
83
+            client.print(F("="));
84
+            //client.print(F("\""));
85
+            client.print(data.tagValue(i));
86
+            //client.print(F("\""));
87
+        }
88
+        client.print(F(" "));
89
+        for (int i = 0; i < data.valueCount(); i++) {
90
+            if (i > 0) {
91
+                client.print(F(","));
92
+            }
93
+            client.print(data.valueName(i));
94
+            client.print(F("="));
95
+            client.print(data.valueValue(i));
96
+        }
97
+        // we're leaving out the timestamp, it's optional
98
+
99
+        boolean currentLineIsBlank = true, contains_error = false;
100
+        int compare_off = 0;
101
+        String compare_to(F("X-Influxdb-Error"));
102
+        while (client.connected()) {
103
+            if (client.available()) {
104
+                char c = client.read();
105
+                if (c != '\r') {
106
+                    Serial.write(c);
107
+                }
108
+
109
+                if (compare_off == compare_to.length()) {
110
+                    contains_error = true;
111
+                } else if ((compare_off < compare_to.length()) && (c == compare_to[compare_off])) {
112
+                    if ((compare_off > 0) || currentLineIsBlank) {
113
+                        compare_off++;
114
+                    }
115
+                } else {
116
+                    compare_off = 0;
117
+                }
118
+
119
+                if ((c == '\n') && currentLineIsBlank) {
120
+                    // http headers ended
121
+                    break;
122
+                }
123
+
124
+                if (c == '\n') {
125
+                    // you're starting a new line
126
+                    currentLineIsBlank = true;
127
+                } else if (c != '\r') {
128
+                    // you've gotten a character on the current line
129
+                    currentLineIsBlank = false;
130
+                }
131
+            }
132
+        }
133
+
134
+        client.stop();
135
+        Serial.println(contains_error ? F("Request failed") : F("Request Done"));
136
+        return !contains_error;
137
+    } else {
138
+        Serial.println(F("Error connecting"));
139
+        return false; // failed
140
+    }
141
+#else
142
+
143
+    return true; // success
144
+
145
+#endif
146
+}

+ 3
- 0
src/SimpleUpdater.cpp View File

11
  * ----------------------------------------------------------------------------
11
  * ----------------------------------------------------------------------------
12
  */
12
  */
13
 
13
 
14
+#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
15
+
14
 #if defined(ARDUINO_ARCH_ESP8266)
16
 #if defined(ARDUINO_ARCH_ESP8266)
15
 #include <ESP8266HTTPUpdateServer.h>
17
 #include <ESP8266HTTPUpdateServer.h>
16
 #elif defined(ARDUINO_ARCH_ESP32)
18
 #elif defined(ARDUINO_ARCH_ESP32)
131
     server = NULL;
133
     server = NULL;
132
 }
134
 }
133
 
135
 
136
+#endif

+ 444
- 76
src/main.cpp View File

36
 
36
 
37
 #include "config.h"
37
 #include "config.h"
38
 #include "moisture.h"
38
 #include "moisture.h"
39
-#include "relais.h"
39
+
40
+#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
41
+
40
 #include "SimpleUpdater.h"
42
 #include "SimpleUpdater.h"
41
 
43
 
42
 #define BUILTIN_LED_PIN 1
44
 #define BUILTIN_LED_PIN 1
44
 UPDATE_WEB_SERVER server(80);
46
 UPDATE_WEB_SERVER server(80);
45
 SimpleUpdater updater;
47
 SimpleUpdater updater;
46
 
48
 
49
+#elif defined(ARDUINO_ARCH_AVR)
50
+
51
+#define ESP_PLATFORM_NAME "Uno WiFi"
52
+#define BUILTIN_LED_PIN 13
53
+
54
+#endif
55
+
47
 #ifdef ENABLE_INFLUXDB_LOGGING
56
 #ifdef ENABLE_INFLUXDB_LOGGING
57
+#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
48
 #include <InfluxDb.h>
58
 #include <InfluxDb.h>
59
+#else
60
+#include "SimpleInflux.h"
61
+#endif
49
 
62
 
50
 Influxdb influx(INFLUXDB_HOST, INFLUXDB_PORT);
63
 Influxdb influx(INFLUXDB_HOST, INFLUXDB_PORT);
51
 
64
 
69
 
82
 
70
 SHT2x sht(SHT_I2C_ADDRESS, &Wire);
83
 SHT2x sht(SHT_I2C_ADDRESS, &Wire);
71
 
84
 
85
+#elif defined(ARDUINO_ARCH_AVR)
86
+
87
+#include <UnoWiFiDevEdSerial1.h>
88
+#include <WiFiLink.h>
89
+
90
+WiFiServer server(80);
91
+SHT2x sht(SHT_I2C_ADDRESS, &Wire);
92
+
72
 #endif
93
 #endif
73
 
94
 
95
+//#define ENABLE_RELAIS_TEST
96
+
74
 Adafruit_BME280 bme1, bme2;
97
 Adafruit_BME280 bme1, bme2;
75
 
98
 
76
 bool found_bme1 = false;
99
 bool found_bme1 = false;
81
 unsigned long last_db_write_time = 0;
104
 unsigned long last_db_write_time = 0;
82
 unsigned long last_led_blink_time = 0;
105
 unsigned long last_led_blink_time = 0;
83
 
106
 
107
+#ifdef ENABLE_RELAIS_TEST
108
+
109
+#include "relais.h"
110
+
84
 static void relaisTest() {
111
 static void relaisTest() {
85
     for (int i = 0; i < 10; i++) {
112
     for (int i = 0; i < 10; i++) {
86
         relais_enable(i, 400 + (i * 1000));
113
         relais_enable(i, 400 + (i * 1000));
89
 }
116
 }
90
 
117
 
91
 void handleRelaisTest() {
118
 void handleRelaisTest() {
92
-    String message = F("<html><head>\n");
93
-    message += F("<title>" ESP_PLATFORM_NAME " Environment Sensor</title>\n");
94
-    message += F("</head><body>\n");
95
-    message += F("<p>Relais Test started!</p>\n");
96
-    message += F("<p><a href=\"/\">Return to Home</a></p>\n");
97
-    message += F("</body></html>\n");
119
+    String message = F("<html><head>");
120
+    message += F("<title>" ESP_PLATFORM_NAME " Environment Sensor</title>");
121
+    message += F("</head><body>");
122
+    message += F("<p>Relais Test started!</p>");
123
+    message += F("<p><a href=\"/\">Return to Home</a></p>");
124
+    message += F("</body></html>");
98
     
125
     
99
     server.send(200, "text/html", message);
126
     server.send(200, "text/html", message);
100
     
127
     
101
     relaisTest();
128
     relaisTest();
102
 }
129
 }
103
 
130
 
131
+#endif // ENABLE_RELAIS_TEST
132
+
133
+static float bme1_temp(void) {
134
+    while (1) {
135
+        float a = bme1.readTemperature();
136
+        float b = bme1.readTemperature();
137
+        
138
+        if ((a > b) && ((a - b) < 2.0)) {
139
+            return (a + b) / 2.0;
140
+        }
141
+        
142
+        if ((a < b) && ((b - a) < 2.0)) {
143
+            return (a + b) / 2.0;
144
+        }
145
+    }
146
+    return 0.0;
147
+}
148
+
149
+static float bme2_temp(void) {
150
+    while (1) {
151
+        float a = bme2.readTemperature();
152
+        float b = bme2.readTemperature();
153
+        
154
+        if ((a > b) && ((a - b) < 2.0)) {
155
+            return (a + b) / 2.0;
156
+        }
157
+        
158
+        if ((a < b) && ((b - a) < 2.0)) {
159
+            return (a + b) / 2.0;
160
+        }
161
+    }
162
+    return 0.0;
163
+}
164
+
165
+static float bme1_humid(void) {
166
+    while (1) {
167
+        float a = bme1.readHumidity();
168
+        float b = bme1.readHumidity();
169
+        
170
+        if ((a > b) && ((a - b) < 2.0)) {
171
+            return (a + b) / 2.0;
172
+        }
173
+        
174
+        if ((a < b) && ((b - a) < 2.0)) {
175
+            return (a + b) / 2.0;
176
+        }
177
+    }
178
+    return 0.0;
179
+}
180
+
181
+static float bme2_humid(void) {
182
+    while (1) {
183
+        float a = bme2.readHumidity();
184
+        float b = bme2.readHumidity();
185
+        
186
+        if ((a > b) && ((a - b) < 2.0)) {
187
+            return (a + b) / 2.0;
188
+        }
189
+        
190
+        if ((a < b) && ((b - a) < 2.0)) {
191
+            return (a + b) / 2.0;
192
+        }
193
+    }
194
+    return 0.0;
195
+}
196
+
197
+static float bme1_pressure(void) {
198
+    while (1) {
199
+        float a = bme1.readPressure();
200
+        float b = bme1.readPressure();
201
+        
202
+        if ((a > b) && ((a - b) < 2.0)) {
203
+            return (a + b) / 2.0;
204
+        }
205
+        
206
+        if ((a < b) && ((b - a) < 2.0)) {
207
+            return (a + b) / 2.0;
208
+        }
209
+    }
210
+    return 0.0;
211
+}
212
+
213
+static float bme2_pressure(void) {
214
+    while (1) {
215
+        float a = bme2.readPressure();
216
+        float b = bme2.readPressure();
217
+        
218
+        if ((a > b) && ((a - b) < 2.0)) {
219
+            return (a + b) / 2.0;
220
+        }
221
+        
222
+        if ((a < b) && ((b - a) < 2.0)) {
223
+            return (a + b) / 2.0;
224
+        }
225
+    }
226
+    return 0.0;
227
+}
228
+
229
+static float sht_temp(void) {
230
+    while (1) {
231
+        float a = sht.GetTemperature();
232
+        float b = sht.GetTemperature();
233
+        
234
+        if ((a > b) && ((a - b) < 2.0)) {
235
+            return (a + b) / 2.0;
236
+        }
237
+        
238
+        if ((a < b) && ((b - a) < 2.0)) {
239
+            return (a + b) / 2.0;
240
+        }
241
+    }
242
+    return 0.0;
243
+}
244
+
245
+static float sht_humid(void) {
246
+    while (1) {
247
+        float a = sht.GetHumidity();
248
+        float b = sht.GetHumidity();
249
+        
250
+        if ((a > b) && ((a - b) < 2.0)) {
251
+            return (a + b) / 2.0;
252
+        }
253
+        
254
+        if ((a < b) && ((b - a) < 2.0)) {
255
+            return (a + b) / 2.0;
256
+        }
257
+    }
258
+    return 0.0;
259
+}
260
+
261
+#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
104
 void handleRoot() {
262
 void handleRoot() {
105
-    String message = F("<html><head>\n");
106
-    message += F("<title>" ESP_PLATFORM_NAME " Environment Sensor</title>\n");
107
-    message += F("</head><body>\n");
108
-    message += F("<h1>" ESP_PLATFORM_NAME " Environment Sensor</h1>\n");
109
-    message += F("\n<p>\n");
263
+#else
264
+void handleRoot(WiFiClient &client) {
265
+#endif
266
+    String message;
267
+
268
+    message += F("<html><head>");
269
+    message += F("<title>" ESP_PLATFORM_NAME " Environment Sensor</title>");
270
+    message += F("</head><body>");
271
+    message += F("<h1>" ESP_PLATFORM_NAME " Environment Sensor</h1>");
272
+    message += F("<p>");
110
     message += F("Version: ");
273
     message += F("Version: ");
111
     message += esp_env_version;
274
     message += esp_env_version;
112
-    message += F("\n<br>\n");
275
+    message += F("<br>");
113
     message += F("Location: ");
276
     message += F("Location: ");
114
     message += sensor_location;
277
     message += sensor_location;
115
-    message += F("\n<br>\n");
278
+
279
+#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
280
+    message += F("<br>");
116
     message += F("MAC: ");
281
     message += F("MAC: ");
117
     message += WiFi.macAddress();
282
     message += WiFi.macAddress();
118
-    message += F("\n</p>\n");
283
+#endif
284
+
285
+    message += F("</p>");
286
+
287
+#if defined(ARDUINO_ARCH_AVR)
288
+    do {
289
+        size_t len = message.length(), off = 0;
290
+        while (off < len) {
291
+            if ((len - off) >= 50) {
292
+                client.write(message.c_str() + off, 50);
293
+                off += 50;
294
+            } else {
295
+                client.write(message.c_str() + off, len - off);
296
+                off = len;
297
+            }
298
+        }
299
+        message = "";
300
+    } while (false);
301
+#endif
119
 
302
 
120
 #if defined(ARDUINO_ARCH_ESP8266)
303
 #if defined(ARDUINO_ARCH_ESP8266)
121
     
304
     
122
-    message += F("\n<p>\n");
305
+    message += F("<p>");
123
     message += F("Reset reason: ");
306
     message += F("Reset reason: ");
124
     message += ESP.getResetReason();
307
     message += ESP.getResetReason();
125
-    message += F("\n<br>\n");
308
+    message += F("<br>");
126
     message += F("Free heap: ");
309
     message += F("Free heap: ");
127
     message += String(ESP.getFreeHeap());
310
     message += String(ESP.getFreeHeap());
128
     message += F(" (");
311
     message += F(" (");
129
     message += String(ESP.getHeapFragmentation());
312
     message += String(ESP.getHeapFragmentation());
130
     message += F("% fragmentation)");
313
     message += F("% fragmentation)");
131
-    message += F("\n<br>\n");
314
+    message += F("<br>");
132
     message += F("Free sketch space: ");
315
     message += F("Free sketch space: ");
133
     message += String(ESP.getFreeSketchSpace());
316
     message += String(ESP.getFreeSketchSpace());
134
-    message += F("\n<br>\n");
317
+    message += F("<br>");
135
     message += F("Flash chip real size: ");
318
     message += F("Flash chip real size: ");
136
     message += String(ESP.getFlashChipRealSize());
319
     message += String(ESP.getFlashChipRealSize());
137
 
320
 
138
     if (ESP.getFlashChipSize() != ESP.getFlashChipRealSize()) {
321
     if (ESP.getFlashChipSize() != ESP.getFlashChipRealSize()) {
139
-        message += F("\n<br>\n");
322
+        message += F("<br>");
140
         message += F("WARNING: sdk chip size (");
323
         message += F("WARNING: sdk chip size (");
141
         message += (ESP.getFlashChipSize());
324
         message += (ESP.getFlashChipSize());
142
         message += F(") does not match!");
325
         message += F(") does not match!");
143
     }
326
     }
144
     
327
     
145
-    message += F("\n</p>\n");
328
+    message += F("</p>");
146
     
329
     
147
 #elif defined(ARDUINO_ARCH_ESP32)
330
 #elif defined(ARDUINO_ARCH_ESP32)
148
 
331
 
149
-    message += F("\n<p>\n");
332
+    message += F("<p>");
150
     message += F("Free heap: ");
333
     message += F("Free heap: ");
151
     message += String(ESP.getFreeHeap() / 1024.0);
334
     message += String(ESP.getFreeHeap() / 1024.0);
152
-    message += F("k\n<br>\n");
335
+    message += F("k<br>");
153
     message += F("Free sketch space: ");
336
     message += F("Free sketch space: ");
154
     message += String(ESP.getFreeSketchSpace() / 1024.0);
337
     message += String(ESP.getFreeSketchSpace() / 1024.0);
155
-    message += F("k\n<br>\n");
338
+    message += F("k<br>");
156
     message += F("Flash chip size: ");
339
     message += F("Flash chip size: ");
157
     message += String(ESP.getFlashChipSize() / 1024.0);
340
     message += String(ESP.getFlashChipSize() / 1024.0);
158
-    message += F("k\n</p>\n");
341
+    message += F("k</p>");
159
     
342
     
160
 #endif
343
 #endif
161
 
344
 
162
-    message += F("\n<p>\n");
345
+    message += F("<p>");
163
     if (found_bme1) {
346
     if (found_bme1) {
164
         message += F("BME280 Low:");
347
         message += F("BME280 Low:");
165
-        message += F("\n<br>\n");
348
+        message += F("<br>");
166
         message += F("Temperature: ");
349
         message += F("Temperature: ");
167
-        message += String(bme1.readTemperature());
168
-        message += F("\n<br>\n");
350
+        message += String(bme1_temp());
351
+        message += F("<br>");
169
         message += F("Humidity: ");
352
         message += F("Humidity: ");
170
-        message += String(bme1.readHumidity());
171
-        message += F("\n<br>\n");
353
+        message += String(bme1_humid());
354
+        message += F("<br>");
172
         message += F("Pressure: ");
355
         message += F("Pressure: ");
173
-        message += String(bme1.readPressure());
356
+        message += String(bme1_pressure());
174
     } else {
357
     } else {
175
         message += F("BME280 (low) not connected!");
358
         message += F("BME280 (low) not connected!");
176
     }
359
     }
177
-    message += F("\n</p>\n");
360
+    message += F("</p>");
178
 
361
 
179
-    message += F("\n<p>\n");
362
+    message += F("<p>");
180
     if (found_bme2) {
363
     if (found_bme2) {
181
         message += F("BME280 High:");
364
         message += F("BME280 High:");
182
-        message += F("\n<br>\n");
365
+        message += F("<br>");
183
         message += F("Temperature: ");
366
         message += F("Temperature: ");
184
-        message += String(bme2.readTemperature());
185
-        message += F("\n<br>\n");
367
+        message += String(bme2_temp());
368
+        message += F("<br>");
186
         message += F("Humidity: ");
369
         message += F("Humidity: ");
187
-        message += String(bme2.readHumidity());
188
-        message += F("\n<br>\n");
370
+        message += String(bme2_humid());
371
+        message += F("<br>");
189
         message += F("Pressure: ");
372
         message += F("Pressure: ");
190
-        message += String(bme2.readPressure());
373
+        message += String(bme2_pressure());
191
     } else {
374
     } else {
192
         message += F("BME280 (high) not connected!");
375
         message += F("BME280 (high) not connected!");
193
     }
376
     }
194
-    message += F("\n</p>\n");
377
+    message += F("</p>");
195
 
378
 
196
-    message += F("\n<p>\n");
379
+    message += F("<p>");
197
     if (found_sht) {
380
     if (found_sht) {
198
         message += F("SHT21:");
381
         message += F("SHT21:");
199
-        message += F("\n<br>\n");
382
+        message += F("<br>");
200
         message += F("Temperature: ");
383
         message += F("Temperature: ");
201
-        message += String(sht.GetTemperature());
202
-        message += F("\n<br>\n");
384
+        message += String(sht_temp());
385
+        message += F("<br>");
203
         message += F("Humidity: ");
386
         message += F("Humidity: ");
204
-        message += String(sht.GetHumidity());
387
+        message += String(sht_humid());
205
     } else {
388
     } else {
206
-        message += F("SHT21 not connected!");
389
+        //message += F("SHT21 not connected!");
207
     }
390
     }
208
-    message += F("\n</p>\n");
391
+    message += F("</p>");
392
+
393
+#if defined(ARDUINO_ARCH_AVR)
394
+    do {
395
+        size_t len = message.length(), off = 0;
396
+        while (off < len) {
397
+            if ((len - off) >= 50) {
398
+                client.write(message.c_str() + off, 50);
399
+                off += 50;
400
+            } else {
401
+                client.write(message.c_str() + off, len - off);
402
+                off = len;
403
+            }
404
+        }
405
+        message = "";
406
+    } while (false);
407
+#endif
209
 
408
 
210
     for (int i = 0; i < moisture_count(); i++) {
409
     for (int i = 0; i < moisture_count(); i++) {
211
         int moisture = moisture_read(i);
410
         int moisture = moisture_read(i);
212
         if (moisture < moisture_max()) {
411
         if (moisture < moisture_max()) {
213
-            message += F("\n<p>\n");
412
+            message += F("<p>");
214
             message += F("Sensor ");
413
             message += F("Sensor ");
215
             message += String(i + 1);
414
             message += String(i + 1);
216
-            message += F(":\n<br>\n");
415
+            message += F(":<br>");
217
             message += F("Moisture: ");
416
             message += F("Moisture: ");
218
             message += String(moisture);
417
             message += String(moisture);
219
             message += F(" / ");
418
             message += F(" / ");
220
             message += String(moisture_max());
419
             message += String(moisture_max());
221
-            message += F("\n</p>\n");
420
+            message += F("</p>");
222
         }
421
         }
223
     }
422
     }
224
     
423
     
225
     if (moisture_count() <= 0) {
424
     if (moisture_count() <= 0) {
226
-        message += F("\n<p>\n");
425
+        message += F("<p>");
227
         message += F("No moisture sensors configured!");
426
         message += F("No moisture sensors configured!");
228
-        message += F("\n</p>\n");
427
+        message += F("</p>");
229
     }
428
     }
230
 
429
 
231
-    message += F("<p>\n");
232
-    message += F("Try <a href=\"/update\">/update</a> for OTA firmware updates!\n");
233
-    message += F("</p>\n");
430
+#if ! defined(ARDUINO_ARCH_AVR)
431
+    message += F("<p>");
432
+    message += F("Try <a href=\"/update\">/update</a> for OTA firmware updates!");
433
+    message += F("</p>");
434
+#endif
234
     
435
     
235
-    message += F("<p>\n");
436
+    message += F("<p>");
236
 #ifdef ENABLE_INFLUXDB_LOGGING
437
 #ifdef ENABLE_INFLUXDB_LOGGING
237
     message += F("InfluxDB: ");
438
     message += F("InfluxDB: ");
238
     message += INFLUXDB_DATABASE;
439
     message += INFLUXDB_DATABASE;
240
     message += INFLUXDB_HOST;
441
     message += INFLUXDB_HOST;
241
     message += F(":");
442
     message += F(":");
242
     message += String(INFLUXDB_PORT);
443
     message += String(INFLUXDB_PORT);
243
-    message += F("\n");
244
 #else
444
 #else
245
-    message += F("InfluxDB logging not enabled!\n");
445
+    message += F("InfluxDB logging not enabled!");
246
 #endif
446
 #endif
247
-    message += F("</p>\n");
447
+    message += F("</p>");
248
     
448
     
249
-    message += F("<p><a href=\"/relaistest\">Relais Test</a></p>\n");
250
-    message += F("</body></html>\n");
449
+#ifdef ENABLE_RELAIS_TEST
450
+    message += F("<p><a href=\"/relaistest\">Relais Test</a></p>");
451
+#endif // ENABLE_RELAIS_TEST
251
 
452
 
453
+    message += F("</body></html>");
454
+
455
+#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
252
     server.send(200, "text/html", message);
456
     server.send(200, "text/html", message);
457
+#else
458
+    do {
459
+        size_t len = message.length(), off = 0;
460
+        while (off < len) {
461
+            if ((len - off) >= 50) {
462
+                client.write(message.c_str() + off, 50);
463
+                off += 50;
464
+            } else {
465
+                client.write(message.c_str() + off, len - off);
466
+                off = len;
467
+            }
468
+        }
469
+    } while (false);
470
+#endif
253
 }
471
 }
254
 
472
 
255
 void setup() {
473
 void setup() {
256
     pinMode(BUILTIN_LED_PIN, OUTPUT);
474
     pinMode(BUILTIN_LED_PIN, OUTPUT);
257
     
475
     
476
+#ifdef ENABLE_RELAIS_TEST
258
     relais_init();
477
     relais_init();
478
+#endif // ENABLE_RELAIS_TEST
259
 
479
 
260
     // Blink LED for init
480
     // Blink LED for init
261
     for (int i = 0; i < 2; i++) {
481
     for (int i = 0; i < 2; i++) {
280
     found_bme1 = (!bme1.begin(BME_I2C_ADDRESS_1, &Wire)) ? false : true;
500
     found_bme1 = (!bme1.begin(BME_I2C_ADDRESS_1, &Wire)) ? false : true;
281
     found_bme2 = (!bme2.begin(BME_I2C_ADDRESS_2, &Wire)) ? false : true;
501
     found_bme2 = (!bme2.begin(BME_I2C_ADDRESS_2, &Wire)) ? false : true;
282
 
502
 
503
+#elif defined(ARDUINO_ARCH_AVR)
504
+
505
+    Wire.begin();
506
+    found_bme1 = (!bme1.begin(BME_I2C_ADDRESS_1, &Wire)) ? false : true;
507
+    found_bme2 = (!bme2.begin(BME_I2C_ADDRESS_2, &Wire)) ? false : true;
508
+
283
 #endif
509
 #endif
284
 
510
 
285
     found_sht = sht.GetAlive();
511
     found_sht = sht.GetAlive();
326
     // Set hostname workaround
552
     // Set hostname workaround
327
     WiFi.setHostname(hostname.c_str());
553
     WiFi.setHostname(hostname.c_str());
328
 
554
 
555
+#elif defined(ARDUINO_ARCH_AVR)
556
+
557
+    Serial.begin(115200);
558
+    Serial1.begin(115200);
559
+
560
+    WiFi.init(&Serial1);
561
+
562
+    Serial.print(F("Connecting WiFi"));
563
+    WiFi.begin(ssid, password);
564
+    while (WiFi.status() != WL_CONNECTED) {
565
+        delay(LED_CONNECT_BLINK_INTERVAL);
566
+        digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
567
+        Serial.print(F("."));
568
+    }
569
+    Serial.println(F("\nWiFi connected!"));
570
+
329
 #endif
571
 #endif
330
 
572
 
331
 #ifdef ENABLE_INFLUXDB_LOGGING
573
 #ifdef ENABLE_INFLUXDB_LOGGING
333
     influx.setDb(INFLUXDB_DATABASE);
575
     influx.setDb(INFLUXDB_DATABASE);
334
 #endif // ENABLE_INFLUXDB_LOGGING
576
 #endif // ENABLE_INFLUXDB_LOGGING
335
 
577
 
578
+#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
336
     // Setup HTTP Server
579
     // Setup HTTP Server
337
     MDNS.begin(hostname.c_str());
580
     MDNS.begin(hostname.c_str());
338
     updater.setup(&server);
581
     updater.setup(&server);
339
     server.on("/", handleRoot);
582
     server.on("/", handleRoot);
583
+
584
+#ifdef ENABLE_RELAIS_TEST
340
     server.on("/relaistest", handleRelaisTest);
585
     server.on("/relaistest", handleRelaisTest);
341
-    server.begin();
586
+#endif
587
+
342
     MDNS.addService("http", "tcp", 80);
588
     MDNS.addService("http", "tcp", 80);
589
+#endif
590
+
591
+    server.begin();
343
 }
592
 }
344
 
593
 
594
+#if defined(ARDUINO_ARCH_AVR)
595
+void http_server() {
596
+    // listen for incoming clients
597
+    WiFiClient client = server.available();
598
+    if (client) {
599
+        Serial.println(F("new http client"));
600
+
601
+        // an http request ends with a blank line
602
+        boolean currentLineIsBlank = true;
603
+
604
+        while (client.connected()) {
605
+            if (client.available()) {
606
+                char c = client.read();
607
+                Serial.write(c);
608
+
609
+                // if you've gotten to the end of the line (received a newline
610
+                // character) and the line is blank, the http request has ended,
611
+                // so you can send a reply
612
+                if ((c == '\n') && currentLineIsBlank) {
613
+                    // send a standard http response header
614
+                    client.println(F("HTTP/1.1 200 OK"));
615
+                    client.println(F("Content-Type: text/html"));
616
+                    client.println(F("Connection: close"));
617
+                    client.println();
618
+                    handleRoot(client);
619
+                    break;
620
+                }
621
+
622
+                if (c == '\n') {
623
+                    // you're starting a new line
624
+                    currentLineIsBlank = true;
625
+                } else if (c != '\r') {
626
+                    // you've gotten a character on the current line
627
+                    currentLineIsBlank = false;
628
+                }
629
+            }
630
+        }
631
+
632
+        // give the web browser time to receive the data
633
+        delay(10);
634
+
635
+        // close the connection
636
+        client.stop();
637
+        Serial.println(F("http client disconnected"));
638
+    }
639
+}
640
+#endif
641
+
345
 void handleServers() {
642
 void handleServers() {
643
+#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
346
     server.handleClient();
644
     server.handleClient();
645
+#else
646
+    http_server();
647
+#endif
347
     
648
     
348
 #if defined(ARDUINO_ARCH_ESP8266)
649
 #if defined(ARDUINO_ARCH_ESP8266)
349
     MDNS.update();
650
     MDNS.update();
350
 #endif
651
 #endif
351
 }
652
 }
352
 
653
 
654
+#ifdef ENABLE_INFLUXDB_LOGGING
353
 static boolean writeMeasurement(InfluxData &measurement) {
655
 static boolean writeMeasurement(InfluxData &measurement) {
354
     boolean success = influx.write(measurement);
656
     boolean success = influx.write(measurement);
355
     if (!success) {
657
     if (!success) {
364
     return success;
666
     return success;
365
 }
667
 }
366
 
668
 
367
-#ifdef ENABLE_INFLUXDB_LOGGING
368
 void writeDatabase() {
669
 void writeDatabase() {
670
+#if defined(ARDUINO_ARCH_AVR)
671
+    Serial.println(F("Writing to InfluxDB"));
672
+
673
+    InfluxData measurement("");
674
+#endif
675
+
369
     if (found_bme1) {
676
     if (found_bme1) {
677
+#if defined(ARDUINO_ARCH_AVR)
678
+        measurement.clear();
679
+        measurement.setName("environment");
680
+#else
370
         InfluxData measurement("environment");
681
         InfluxData measurement("environment");
682
+#endif
683
+
371
         measurement.addTag("location", sensor_location);
684
         measurement.addTag("location", sensor_location);
372
         measurement.addTag("placement", "1");
685
         measurement.addTag("placement", "1");
373
-        measurement.addTag("device", WiFi.macAddress());
374
         measurement.addTag("sensor", "bme280");
686
         measurement.addTag("sensor", "bme280");
375
 
687
 
376
-        measurement.addValue("temperature", bme1.readTemperature());
377
-        measurement.addValue("pressure", bme1.readPressure());
378
-        measurement.addValue("humidity", bme1.readHumidity());
688
+#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
689
+        measurement.addTag("device", WiFi.macAddress());
690
+#endif
691
+
692
+        measurement.addValue("temperature", bme1_temp());
693
+        measurement.addValue("pressure", bme1_pressure());
694
+        measurement.addValue("humidity", bme1_humid());
379
 
695
 
696
+        Serial.println(F("Writing bme1"));
380
         writeMeasurement(measurement);
697
         writeMeasurement(measurement);
698
+        Serial.println(F("Done!"));
381
     }
699
     }
700
+
382
     if (found_bme2) {
701
     if (found_bme2) {
702
+#if defined(ARDUINO_ARCH_AVR)
703
+        measurement.clear();
704
+        measurement.setName("environment");
705
+#else
383
         InfluxData measurement("environment");
706
         InfluxData measurement("environment");
707
+#endif
708
+
384
         measurement.addTag("location", sensor_location);
709
         measurement.addTag("location", sensor_location);
385
         measurement.addTag("placement", "2");
710
         measurement.addTag("placement", "2");
386
-        measurement.addTag("device", WiFi.macAddress());
387
         measurement.addTag("sensor", "bme280");
711
         measurement.addTag("sensor", "bme280");
388
 
712
 
389
-        measurement.addValue("temperature", bme2.readTemperature());
390
-        measurement.addValue("pressure", bme2.readPressure());
391
-        measurement.addValue("humidity", bme2.readHumidity());
713
+#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
714
+        measurement.addTag("device", WiFi.macAddress());
715
+#endif
392
 
716
 
717
+        measurement.addValue("temperature", bme2_temp());
718
+        measurement.addValue("pressure", bme2_pressure());
719
+        measurement.addValue("humidity", bme2_humid());
720
+
721
+        Serial.println(F("Writing bme2"));
393
         writeMeasurement(measurement);
722
         writeMeasurement(measurement);
723
+        Serial.println(F("Done!"));
394
     }
724
     }
395
 
725
 
396
     if (found_sht) {
726
     if (found_sht) {
727
+#if defined(ARDUINO_ARCH_AVR)
728
+        measurement.clear();
729
+        measurement.setName("environment");
730
+#else
397
         InfluxData measurement("environment");
731
         InfluxData measurement("environment");
732
+#endif
733
+
398
         measurement.addTag("location", sensor_location);
734
         measurement.addTag("location", sensor_location);
399
-        measurement.addTag("device", WiFi.macAddress());
400
         measurement.addTag("sensor", "sht21");
735
         measurement.addTag("sensor", "sht21");
401
 
736
 
402
-        measurement.addValue("temperature", sht.GetTemperature());
403
-        measurement.addValue("humidity", sht.GetHumidity());
737
+#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
738
+        measurement.addTag("device", WiFi.macAddress());
739
+#endif
404
 
740
 
741
+        measurement.addValue("temperature", sht_temp());
742
+        measurement.addValue("humidity", sht_humid());
743
+
744
+        Serial.println(F("Writing sht"));
405
         writeMeasurement(measurement);
745
         writeMeasurement(measurement);
746
+        Serial.println(F("Done!"));
406
     }
747
     }
407
     
748
     
408
     for (int i = 0; i < moisture_count(); i++) {
749
     for (int i = 0; i < moisture_count(); i++) {
409
         int moisture = moisture_read(i);
750
         int moisture = moisture_read(i);
410
         if (moisture < moisture_max()) {
751
         if (moisture < moisture_max()) {
752
+#if defined(ARDUINO_ARCH_AVR)
753
+            measurement.clear();
754
+            measurement.setName("moisture");
755
+#else
411
             InfluxData measurement("moisture");
756
             InfluxData measurement("moisture");
757
+#endif
758
+
412
             measurement.addTag("location", sensor_location);
759
             measurement.addTag("location", sensor_location);
760
+            String sensor(i + 1, DEC);
761
+            measurement.addTag("sensor", sensor);
762
+
763
+#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
413
             measurement.addTag("device", WiFi.macAddress());
764
             measurement.addTag("device", WiFi.macAddress());
414
-            measurement.addTag("sensor", String(i + 1));
765
+#endif
415
 
766
 
416
             measurement.addValue("value", moisture);
767
             measurement.addValue("value", moisture);
417
             measurement.addValue("maximum", moisture_max());
768
             measurement.addValue("maximum", moisture_max());
418
 
769
 
770
+            Serial.print(F("Writing moisture "));
771
+            Serial.println(i);
419
             writeMeasurement(measurement);
772
             writeMeasurement(measurement);
773
+            Serial.println(F("Done!"));
420
         }
774
         }
421
     }
775
     }
776
+
777
+    Serial.println(F("All Done!"));
422
 }
778
 }
423
 #endif // ENABLE_INFLUXDB_LOGGING
779
 #endif // ENABLE_INFLUXDB_LOGGING
424
 
780
 
425
 void loop() {
781
 void loop() {
426
     unsigned long time = millis();
782
     unsigned long time = millis();
427
     
783
     
784
+#ifdef ENABLE_RELAIS_TEST
428
     relais_run();
785
     relais_run();
786
+#endif // ENABLE_RELAIS_TEST
429
 
787
 
430
     if ((time - last_server_handle_time) >= SERVER_HANDLE_INTERVAL) {
788
     if ((time - last_server_handle_time) >= SERVER_HANDLE_INTERVAL) {
431
         last_server_handle_time = time;
789
         last_server_handle_time = time;
440
     
798
     
441
 #ifdef INFLUX_MAX_ERRORS_RESET
799
 #ifdef INFLUX_MAX_ERRORS_RESET
442
     if (error_count >= INFLUX_MAX_ERRORS_RESET) {
800
     if (error_count >= INFLUX_MAX_ERRORS_RESET) {
801
+#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
443
         ESP.restart();
802
         ESP.restart();
803
+#endif
444
     }
804
     }
445
 #endif // INFLUX_MAX_ERRORS_RESET
805
 #endif // INFLUX_MAX_ERRORS_RESET
446
 #endif // ENABLE_INFLUXDB_LOGGING
806
 #endif // ENABLE_INFLUXDB_LOGGING
447
 
807
 
808
+    // blink heartbeat LED
448
     if ((time - last_led_blink_time) >= LED_BLINK_INTERVAL) {
809
     if ((time - last_led_blink_time) >= LED_BLINK_INTERVAL) {
449
         last_led_blink_time = time;
810
         last_led_blink_time = time;
450
         digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
811
         digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
451
     }
812
     }
813
+    
814
+#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
815
+    // reset ESP every 6h to be safe
816
+    if (time >= (6 * 60 * 60 * 1000)) {
817
+        ESP.restart();
818
+    }
819
+#endif
452
 }
820
 }
453
 
821
 

+ 21
- 0
src/moisture.cpp View File

79
     }
79
     }
80
 }
80
 }
81
 
81
 
82
+#elif defined(ARDUINO_ARCH_AVR)
83
+
84
+#define SENSOR_COUNT 6
85
+uint8_t sensor_pins[SENSOR_COUNT] = { A0, A1, A2, A3, A4, A5 };
86
+
87
+int moisture_count(void) {
88
+    return 6;
89
+}
90
+
91
+int moisture_read(int sensor) {
92
+    return (analogRead(sensor_pins[sensor]) << 2) | 3;
93
+}
94
+
95
+int moisture_max(void) {
96
+    return 4095;
97
+}
98
+
99
+void moisture_init(void) {
100
+
101
+}
102
+
82
 #endif
103
 #endif
83
 
104
 

Loading…
Cancel
Save