Browse Source

more cleanup.

Thomas Buck 1 year ago
parent
commit
650b645dac
5 changed files with 48 additions and 106 deletions
  1. 1
    1
      include/config.h
  2. 6
    6
      src/html.cpp
  3. 34
    94
      src/influx.cpp
  4. 7
    1
      src/main.cpp
  5. 0
    4
      src/servers.cpp

+ 1
- 1
include/config.h View File

58
 #define SENSOR_ID SENSOR_LOCATION
58
 #define SENSOR_ID SENSOR_LOCATION
59
 #elif defined(SENSOR_LOCATION_LIVINGROOM_WORKSPACE)
59
 #elif defined(SENSOR_LOCATION_LIVINGROOM_WORKSPACE)
60
 #define SENSOR_LOCATION "livingroom"
60
 #define SENSOR_LOCATION "livingroom"
61
-#define SENSOR_ID "livingroom-workspace"
61
+#define SENSOR_ID "livingroom-ws"
62
 #elif defined(SENSOR_LOCATION_LIVINGROOM_TV)
62
 #elif defined(SENSOR_LOCATION_LIVINGROOM_TV)
63
 #define SENSOR_LOCATION "livingroom"
63
 #define SENSOR_LOCATION "livingroom"
64
 #define SENSOR_ID "livingroom-tv"
64
 #define SENSOR_ID "livingroom-tv"

+ 6
- 6
src/html.cpp View File

390
     message += F("</body>");
390
     message += F("</body>");
391
 
391
 
392
 #ifdef ENABLE_WEBSOCKETS
392
 #ifdef ENABLE_WEBSOCKETS
393
-    message += F("<script type='text/javascript'>\n");
394
-    message += F("var socket = new WebSocket('ws://' + window.location.hostname + ':81');\n");
393
+    message += F("<script type='text/javascript'>");
394
+    message += F("var socket = new WebSocket('ws://' + window.location.hostname + ':81');");
395
     message += F("socket.onmessage = function(e) {");
395
     message += F("socket.onmessage = function(e) {");
396
     message += F(    "var log = document.getElementById('logbuf');");
396
     message += F(    "var log = document.getElementById('logbuf');");
397
     message += F(    "var div = document.getElementsByClassName('log')[0];");
397
     message += F(    "var div = document.getElementsByClassName('log')[0];");
400
     message += F(        "log.innerHTML = log.innerHTML.substring(1024 * 1024);");
400
     message += F(        "log.innerHTML = log.innerHTML.substring(1024 * 1024);");
401
     message += F(    "}");
401
     message += F(    "}");
402
     message += F(    "div.scrollTop = div.scrollHeight;");
402
     message += F(    "div.scrollTop = div.scrollHeight;");
403
-    message += F("};\n");
404
-    message += F("var hist = document.getElementsByClassName('log')[0];\n");
405
-    message += F("hist.scrollTop = hist.scrollHeight;\n");
406
-    message += F("</script>\n");
403
+    message += F("};");
404
+    message += F("var hist = document.getElementsByClassName('log')[0];");
405
+    message += F("hist.scrollTop = hist.scrollHeight;");
406
+    message += F("</script>");
407
 #endif // ENABLE_WEBSOCKETS
407
 #endif // ENABLE_WEBSOCKETS
408
 
408
 
409
     message += F("</html>");
409
     message += F("</html>");

+ 34
- 94
src/influx.cpp View File

89
     return success;
89
     return success;
90
 }
90
 }
91
 
91
 
92
-void writeDatabase() {
93
-#ifndef USE_INFLUXDB_LIB
94
-    InfluxData measurement("");
92
+static void addTagsGeneric(InfluxData &measurement) {
93
+    measurement.addTag("location", SENSOR_LOCATION);
94
+    measurement.addTag("location-id", SENSOR_ID);
95
+
96
+#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
97
+    measurement.addTag("device", WiFi.macAddress().c_str());
95
 #endif
98
 #endif
99
+}
96
 
100
 
97
-#ifdef ENABLE_BME280
101
+static void addTagsSensor(InfluxData &measurement, String sensor, String placement) {
102
+    addTagsGeneric(measurement);
103
+    measurement.addTag("sensor", sensor);
104
+    measurement.addTag("placement", placement);
105
+}
98
 
106
 
99
-    if (found_bme1) {
100
-#ifndef USE_INFLUXDB_LIB
101
-        measurement.clear();
102
-        measurement.setName("environment");
103
-#else
104
-        InfluxData measurement("environment");
107
+#ifdef FEATURE_RELAIS
108
+static void addTagsRelais(InfluxData &measurement, String id, String name) {
109
+    addTagsGeneric(measurement);
110
+    measurement.addTag("id", id);
111
+    measurement.addTag("name", name);
112
+}
105
 #endif
113
 #endif
106
 
114
 
107
-        measurement.addTag("location", SENSOR_LOCATION);
108
-        measurement.addTag("location-id", SENSOR_ID);
109
-        measurement.addTag("placement", "1");
110
-        measurement.addTag("sensor", "bme280");
115
+void writeDatabase() {
116
+#ifdef ENABLE_BME280
111
 
117
 
112
-#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
113
-        measurement.addTag("device", WiFi.macAddress().c_str());
114
-#endif
118
+    if (found_bme1) {
119
+        InfluxData measurement("environment");
120
+        addTagsSensor(measurement, F("bme280"), F("1"));
115
 
121
 
116
         measurement.addValue("temperature", bme1_temp());
122
         measurement.addValue("temperature", bme1_temp());
117
         measurement.addValue("pressure", bme1_pressure());
123
         measurement.addValue("pressure", bme1_pressure());
123
     }
129
     }
124
 
130
 
125
     if (found_bme2) {
131
     if (found_bme2) {
126
-#ifndef USE_INFLUXDB_LIB
127
-        measurement.clear();
128
-        measurement.setName("environment");
129
-#else
130
         InfluxData measurement("environment");
132
         InfluxData measurement("environment");
131
-#endif
132
-
133
-        measurement.addTag("location", SENSOR_LOCATION);
134
-        measurement.addTag("location-id", SENSOR_ID);
135
-        measurement.addTag("placement", "2");
136
-        measurement.addTag("sensor", "bme280");
137
-
138
-#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
139
-        measurement.addTag("device", WiFi.macAddress().c_str());
140
-#endif
133
+        addTagsSensor(measurement, F("bme280"), F("2"));
141
 
134
 
142
         measurement.addValue("temperature", bme2_temp());
135
         measurement.addValue("temperature", bme2_temp());
143
         measurement.addValue("pressure", bme2_pressure());
136
         measurement.addValue("pressure", bme2_pressure());
151
 #endif // ENABLE_BME280
144
 #endif // ENABLE_BME280
152
 
145
 
153
     if (found_sht) {
146
     if (found_sht) {
154
-#ifndef USE_INFLUXDB_LIB
155
-        measurement.clear();
156
-        measurement.setName("environment");
157
-#else
158
         InfluxData measurement("environment");
147
         InfluxData measurement("environment");
159
-#endif
160
-
161
-        measurement.addTag("location", SENSOR_LOCATION);
162
-        measurement.addTag("location-id", SENSOR_ID);
163
-        measurement.addTag("sensor", "sht21");
164
-
165
-#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
166
-        measurement.addTag("device", WiFi.macAddress().c_str());
167
-#endif
148
+        addTagsSensor(measurement, F("sht21"), F("1"));
168
 
149
 
169
         measurement.addValue("temperature", sht_temp());
150
         measurement.addValue("temperature", sht_temp());
170
         measurement.addValue("humidity", sht_humid());
151
         measurement.addValue("humidity", sht_humid());
177
 #ifdef ENABLE_CCS811
158
 #ifdef ENABLE_CCS811
178
 
159
 
179
     if (found_ccs1) {
160
     if (found_ccs1) {
180
-#ifndef USE_INFLUXDB_LIB
181
-        measurement.clear();
182
-        measurement.setName("environment");
183
-#else
184
         InfluxData measurement("environment");
161
         InfluxData measurement("environment");
185
-#endif
186
-
187
-        measurement.addTag("location", SENSOR_LOCATION);
188
-        measurement.addTag("location-id", SENSOR_ID);
189
-        measurement.addTag("placement", "1");
190
-        measurement.addTag("sensor", "ccs811");
162
+        addTagsSensor(measurement, F("ccs811"), F("1"));
191
 
163
 
192
         String err(ccs1_error_code);
164
         String err(ccs1_error_code);
193
         measurement.addTag("error", err);
165
         measurement.addTag("error", err);
194
 
166
 
195
-#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
196
-        measurement.addTag("device", WiFi.macAddress().c_str());
197
-#endif
198
-
199
         measurement.addValue("eco2", ccs1_eco2());
167
         measurement.addValue("eco2", ccs1_eco2());
200
         measurement.addValue("tvoc", ccs1_tvoc());
168
         measurement.addValue("tvoc", ccs1_tvoc());
201
 
169
 
205
     }
173
     }
206
 
174
 
207
     if (found_ccs2) {
175
     if (found_ccs2) {
208
-#ifndef USE_INFLUXDB_LIB
209
-        measurement.clear();
210
-        measurement.setName("environment");
211
-#else
212
         InfluxData measurement("environment");
176
         InfluxData measurement("environment");
213
-#endif
214
-
215
-        measurement.addTag("location", SENSOR_LOCATION);
216
-        measurement.addTag("location-id", SENSOR_ID);
217
-        measurement.addTag("placement", "2");
218
-        measurement.addTag("sensor", "ccs811");
177
+        addTagsSensor(measurement, F("ccs811"), F("2"));
219
 
178
 
220
         String err(ccs2_error_code);
179
         String err(ccs2_error_code);
221
         measurement.addTag("error", err);
180
         measurement.addTag("error", err);
222
 
181
 
223
-#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
224
-        measurement.addTag("device", WiFi.macAddress().c_str());
225
-#endif
226
-
227
         measurement.addValue("eco2", ccs2_eco2());
182
         measurement.addValue("eco2", ccs2_eco2());
228
         measurement.addValue("tvoc", ccs2_tvoc());
183
         measurement.addValue("tvoc", ccs2_tvoc());
229
 
184
 
238
     for (int i = 0; i < moisture_count(); i++) {
193
     for (int i = 0; i < moisture_count(); i++) {
239
         int moisture = moisture_read(i);
194
         int moisture = moisture_read(i);
240
         if (moisture < moisture_max()) {
195
         if (moisture < moisture_max()) {
241
-#ifndef USE_INFLUXDB_LIB
242
-            measurement.clear();
243
-            measurement.setName("moisture");
244
-#else
245
-            InfluxData measurement("moisture");
246
-#endif
247
-
248
-            measurement.addTag("location", SENSOR_LOCATION);
249
-            measurement.addTag("location-id", SENSOR_ID);
250
             String sensor(i + 1, DEC);
196
             String sensor(i + 1, DEC);
251
-            measurement.addTag("sensor", sensor);
252
-
253
-#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
254
-            measurement.addTag("device", WiFi.macAddress().c_str());
255
-#endif
197
+            InfluxData measurement("moisture");
198
+            addTagsSensor(measurement, sensor, sensor);
256
 
199
 
257
             measurement.addValue("value", moisture);
200
             measurement.addValue("value", moisture);
258
             measurement.addValue("maximum", moisture_max());
201
             measurement.addValue("maximum", moisture_max());
268
 #ifdef FEATURE_RELAIS
211
 #ifdef FEATURE_RELAIS
269
     for (int i = 0; i < relais_count(); i++) {
212
     for (int i = 0; i < relais_count(); i++) {
270
         InfluxData measurement("relais");
213
         InfluxData measurement("relais");
271
-        measurement.addTag("location", SENSOR_LOCATION);
272
-        measurement.addTag("location-id", SENSOR_ID);
273
-        measurement.addTag("id", String(i));
274
-        measurement.addTag("name", relais_name(i));
275
-
276
-#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
277
-        measurement.addTag("device", WiFi.macAddress().c_str());
278
-#endif
214
+        addTagsRelais(measurement, String(i), relais_name(i));
279
 
215
 
280
         measurement.addValue("state", relais_get(i));
216
         measurement.addValue("state", relais_get(i));
217
+
218
+        debug.print(F("Writing relais "));
219
+        debug.println(i);
281
         writeMeasurement(measurement);
220
         writeMeasurement(measurement);
221
+        debug.println(F("Done!"));
282
     }
222
     }
283
 #endif // FEATURE_RELAIS
223
 #endif // FEATURE_RELAIS
284
 }
224
 }

+ 7
- 1
src/main.cpp View File

68
 
68
 
69
     config = mem_read();
69
     config = mem_read();
70
 
70
 
71
+#ifdef FEATURE_RELAIS
71
     debug.println(F("Relais"));
72
     debug.println(F("Relais"));
72
     relais_init();
73
     relais_init();
73
-    
74
+#endif // FEATURE_RELAIS
75
+
76
+#ifdef FEATURE_MOISTURE
74
     debug.println(F("Moisture"));
77
     debug.println(F("Moisture"));
75
     moisture_init();
78
     moisture_init();
79
+#endif // FEATURE_MOISTURE
76
 
80
 
81
+    debug.println(F("Sensors"));
77
     initSensors();
82
     initSensors();
78
 
83
 
79
     // Build hostname string
84
     // Build hostname string
123
     // Connect to WiFi AP
128
     // Connect to WiFi AP
124
     debug.print(F("Connecting WiFi"));
129
     debug.print(F("Connecting WiFi"));
125
     WiFi.mode(WIFI_STA);
130
     WiFi.mode(WIFI_STA);
131
+    WiFi.setHostname(hostname.c_str());
126
     WiFi.begin(WIFI_SSID, WIFI_PASS);
132
     WiFi.begin(WIFI_SSID, WIFI_PASS);
127
     while (WiFi.status() != WL_CONNECTED) {
133
     while (WiFi.status() != WL_CONNECTED) {
128
         delay(LED_CONNECT_BLINK_INTERVAL);
134
         delay(LED_CONNECT_BLINK_INTERVAL);

+ 0
- 4
src/servers.cpp View File

229
 #endif // ENABLE_WEBSOCKETS
229
 #endif // ENABLE_WEBSOCKETS
230
 #endif
230
 #endif
231
     }
231
     }
232
-    
233
-    runSensors();
234
-    runMQTT();
235
-    runInflux();
236
 }
232
 }

Loading…
Cancel
Save