Browse Source

Fixed influxdb write

klaute 1 year ago
parent
commit
29c52d0a34
1 changed files with 63 additions and 29 deletions
  1. 63
    29
      firmware/influxdb.ino

+ 63
- 29
firmware/influxdb.ino View File

@@ -1,37 +1,71 @@
1 1
 
2 2
 void pushToInfluxDB(String device, float sensorValues[]) {
3 3
 
4
-  #ifdef DEBUG
5
-  String msg = "weather,device=" + device + " temperature=" + String(sensorValues[SENSOR_TEMPERATURE])
6
-               + ",humidity=" + String(sensorValues[SENSOR_HUMIDITY])
7
-               + ",light=" + String(sensorValues[SENSOR_LIGHT])
8
-               + ",windspeed=" + String(sensorValues[SENSOR_WINDSPEED])
9
-               + ",pressure=" + String(sensorValues[SENSOR_PRESSURE])
10
-               + ",batvoltage=" + String(sensorValues[SENSOR_BAT_VOLTAGE])
11
-               + ",esavemode=" + String(sensorValues[SENSOR_ESAVEMODE])
12
-               + ",batchargestate=" + String(sensorValues[SENSOR_BATCHARGESTATE]);
4
+  uint8_t tries = 0;
5
+  boolean addComma = false;
13 6
 
7
+  tries++;
8
+  String msg = "weather,device=" + device + " ";
9
+  if (!(isnan(sensorValues[SENSOR_TEMPERATURE])))
10
+  {
11
+    msg += "temperature=" + String(sensorValues[SENSOR_TEMPERATURE]);
12
+    addComma = true;
13
+  }
14
+  if (!(isnan(sensorValues[SENSOR_HUMIDITY])))
15
+  {
16
+    if (true == addComma)
17
+      msg += ",";
18
+    msg += "humidity=" + String(sensorValues[SENSOR_HUMIDITY]);
19
+    addComma = true;
20
+  }
21
+  if (!(isnan(sensorValues[SENSOR_LIGHT])))
22
+  {
23
+    if (true == addComma)
24
+      msg += ",";
25
+    msg += "light=" + String(sensorValues[SENSOR_LIGHT]);
26
+    addComma = true;
27
+  }
28
+  if (!(isnan(sensorValues[SENSOR_WINDSPEED])))
29
+  {
30
+    if (true == addComma)
31
+      msg += ",";
32
+    msg += "windspeed=" + String(sensorValues[SENSOR_WINDSPEED]);
33
+    addComma = true;
34
+  }
35
+  if (!(isnan(sensorValues[SENSOR_PRESSURE])))
36
+  {
37
+    if (true == addComma)
38
+      msg += ",";
39
+    msg += "pressure=" + String(sensorValues[SENSOR_PRESSURE]);
40
+    addComma = true;
41
+  }
42
+  if (!(isnan(sensorValues[SENSOR_BAT_VOLTAGE])))
43
+  {
44
+    if (true == addComma)
45
+      msg += ",";
46
+    msg += "batvoltage=" + String(sensorValues[SENSOR_BAT_VOLTAGE]);
47
+    addComma = true;
48
+  }
49
+  if (!(isnan(sensorValues[SENSOR_ESAVEMODE])))
50
+  {
51
+    if (true == addComma)
52
+      msg += ",";
53
+    msg += "esavemode=" + String(sensorValues[SENSOR_ESAVEMODE]);
54
+    addComma = true;
55
+  }
56
+  if (!(isnan(sensorValues[SENSOR_BATCHARGESTATE])))
57
+  {
58
+    if (true == addComma)
59
+      msg += ",";
60
+    msg += "batchargestate=" + String(sensorValues[SENSOR_BATCHARGESTATE]);
61
+    addComma = true;
62
+  }
63
+
64
+#ifdef DEBUG
14 65
   Serial.println(msg);
15
-  #endif
66
+#endif
16 67
 
17
- uint8_t tries = 0;
18 68
   do {
19
-    tries++;
20
-    if (!(isnan(sensorValues[SENSOR_TEMPERATURE])))
21
-      influxdb.write("weather,device=" + device + " temperature=" + String(sensorValues[SENSOR_TEMPERATURE]));
22
-    if (!(isnan(sensorValues[SENSOR_HUMIDITY])))
23
-      influxdb.write("weather,device=" + device + " humidity=" + String(sensorValues[SENSOR_HUMIDITY]));
24
-    if (!(isnan(sensorValues[SENSOR_LIGHT])))
25
-      influxdb.write("weather,device=" + device + " light=" + String(sensorValues[SENSOR_LIGHT]));
26
-    if (!(isnan(sensorValues[SENSOR_WINDSPEED])))
27
-      influxdb.write("weather,device=" + device + " windspeed=" + String(sensorValues[SENSOR_WINDSPEED]));
28
-    if (!(isnan(sensorValues[SENSOR_PRESSURE])))
29
-      influxdb.write("weather,device=" + device + " pressure=" + String(sensorValues[SENSOR_PRESSURE]));
30
-    if (!(isnan(sensorValues[SENSOR_BAT_VOLTAGE])))
31
-      influxdb.write("weather,device=" + device + " batvoltage=" + String(sensorValues[SENSOR_BAT_VOLTAGE]));
32
-    if (!(isnan(sensorValues[SENSOR_ESAVEMODE])))
33
-      influxdb.write("weather,device=" + device + " esavemode=" + String(sensorValues[SENSOR_ESAVEMODE]));
34
-    if (!(isnan(sensorValues[SENSOR_BATCHARGESTATE])))
35
-      influxdb.write("weather,device=" + device + " batchargestate=" + String(sensorValues[SENSOR_BATCHARGESTATE]));
69
+    influxdb.write(msg);
36 70
   } while (influxdb.response() != DB_SUCCESS and tries < 5);
37
-}
71
+}

Loading…
Cancel
Save