Ver código fonte

more cleanup.

Thomas Buck 2 anos atrás
pai
commit
650b645dac
5 arquivos alterados com 48 adições e 106 exclusões
  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 Ver arquivo

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

+ 6
- 6
src/html.cpp Ver arquivo

@@ -390,8 +390,8 @@ void handlePage(WiFiClient &client, int mode, int id) {
390 390
     message += F("</body>");
391 391
 
392 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 395
     message += F("socket.onmessage = function(e) {");
396 396
     message += F(    "var log = document.getElementById('logbuf');");
397 397
     message += F(    "var div = document.getElementsByClassName('log')[0];");
@@ -400,10 +400,10 @@ void handlePage(WiFiClient &client, int mode, int id) {
400 400
     message += F(        "log.innerHTML = log.innerHTML.substring(1024 * 1024);");
401 401
     message += F(    "}");
402 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 407
 #endif // ENABLE_WEBSOCKETS
408 408
 
409 409
     message += F("</html>");

+ 34
- 94
src/influx.cpp Ver arquivo

@@ -89,29 +89,35 @@ static boolean writeMeasurement(InfluxData &measurement) {
89 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 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 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 122
         measurement.addValue("temperature", bme1_temp());
117 123
         measurement.addValue("pressure", bme1_pressure());
@@ -123,21 +129,8 @@ void writeDatabase() {
123 129
     }
124 130
 
125 131
     if (found_bme2) {
126
-#ifndef USE_INFLUXDB_LIB
127
-        measurement.clear();
128
-        measurement.setName("environment");
129
-#else
130 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 135
         measurement.addValue("temperature", bme2_temp());
143 136
         measurement.addValue("pressure", bme2_pressure());
@@ -151,20 +144,8 @@ void writeDatabase() {
151 144
 #endif // ENABLE_BME280
152 145
 
153 146
     if (found_sht) {
154
-#ifndef USE_INFLUXDB_LIB
155
-        measurement.clear();
156
-        measurement.setName("environment");
157
-#else
158 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 150
         measurement.addValue("temperature", sht_temp());
170 151
         measurement.addValue("humidity", sht_humid());
@@ -177,25 +158,12 @@ void writeDatabase() {
177 158
 #ifdef ENABLE_CCS811
178 159
 
179 160
     if (found_ccs1) {
180
-#ifndef USE_INFLUXDB_LIB
181
-        measurement.clear();
182
-        measurement.setName("environment");
183
-#else
184 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 164
         String err(ccs1_error_code);
193 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 167
         measurement.addValue("eco2", ccs1_eco2());
200 168
         measurement.addValue("tvoc", ccs1_tvoc());
201 169
 
@@ -205,25 +173,12 @@ void writeDatabase() {
205 173
     }
206 174
 
207 175
     if (found_ccs2) {
208
-#ifndef USE_INFLUXDB_LIB
209
-        measurement.clear();
210
-        measurement.setName("environment");
211
-#else
212 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 179
         String err(ccs2_error_code);
221 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 182
         measurement.addValue("eco2", ccs2_eco2());
228 183
         measurement.addValue("tvoc", ccs2_tvoc());
229 184
 
@@ -238,21 +193,9 @@ void writeDatabase() {
238 193
     for (int i = 0; i < moisture_count(); i++) {
239 194
         int moisture = moisture_read(i);
240 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 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 200
             measurement.addValue("value", moisture);
258 201
             measurement.addValue("maximum", moisture_max());
@@ -268,17 +211,14 @@ void writeDatabase() {
268 211
 #ifdef FEATURE_RELAIS
269 212
     for (int i = 0; i < relais_count(); i++) {
270 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 216
         measurement.addValue("state", relais_get(i));
217
+
218
+        debug.print(F("Writing relais "));
219
+        debug.println(i);
281 220
         writeMeasurement(measurement);
221
+        debug.println(F("Done!"));
282 222
     }
283 223
 #endif // FEATURE_RELAIS
284 224
 }

+ 7
- 1
src/main.cpp Ver arquivo

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

+ 0
- 4
src/servers.cpp Ver arquivo

@@ -229,8 +229,4 @@ void runServers() {
229 229
 #endif // ENABLE_WEBSOCKETS
230 230
 #endif
231 231
     }
232
-    
233
-    runSensors();
234
-    runMQTT();
235
-    runInflux();
236 232
 }

Carregando…
Cancelar
Salvar