From 0b9b627b6b7c601906b292a31ca6fce673e71c50 Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Thu, 8 Sep 2022 11:21:05 +0200 Subject: [PATCH] Some optical changes done in the code. Checks added to prevent influxdb write activity if no wlan connection is applied (influxdb 2 only). --- firmware/firmware.ino | 13 ++++++------ firmware/influxdb.ino | 28 ++++++++++++++++++++---- firmware/webUpdater.ino | 47 ++++++++++++++++++++++++++--------------- 3 files changed, 61 insertions(+), 27 deletions(-) diff --git a/firmware/firmware.ino b/firmware/firmware.ino index a7730ef..1478ccb 100644 --- a/firmware/firmware.ino +++ b/firmware/firmware.ino @@ -60,11 +60,11 @@ void setup() #endif // Pin settings - pinMode(BAT_CHARGED_PIN, INPUT); - pinMode(BAT_CHARGING_PIN, INPUT); - pinMode(STATUS_LED_PIN, OUTPUT); - pinMode(ANEMOMETER_PIN, INPUT_PULLUP); - pinMode(A0, INPUT); + pinMode(BAT_CHARGED_PIN, INPUT); + pinMode(BAT_CHARGING_PIN, INPUT); + pinMode(STATUS_LED_PIN, OUTPUT); + pinMode(ANEMOMETER_PIN, INPUT_PULLUP); + pinMode(A0, INPUT); digitalWrite(STATUS_LED_PIN, LOW); @@ -182,10 +182,11 @@ void wifiConnectionCheck() #endif } -#else // else of USE_WIFIMANAGER +#else // else of USE_WIFIMANAGER, it is not defined if (WiFi.status() == WL_CONNECTED) { + Serial.println("WLAN connection already OK"); return; } diff --git a/firmware/influxdb.ino b/firmware/influxdb.ino index 0e0b881..a401ef4 100644 --- a/firmware/influxdb.ino +++ b/firmware/influxdb.ino @@ -7,16 +7,26 @@ Influxdb _influxdb(INFLUXDB_HOST, INFLUXDB_PORT); +const String msg = "weather,device=" + device + " "; + void influxdb_begin() { // Init variables to influxdb config - doesn't talk to database _influxdb.opendb(INFLUXDB_DB, INFLUXDB_USER, INFLUXDB_PASS); } -void pushToInfluxDB(String device, float sensorValues[]) { +void pushToInfluxDB(String device, float sensorValues[]) +{ uint8_t tries = 0; boolean addComma = false; - String msg = "weather,device=" + device + " "; +#ifndef USE_WIFIMANAGER + if (WiFi.status() != WL_CONNECTED) + { + debug("Not connected to WLAN"); + return; + } +#endif + if (!(isnan(sensorValues[SENSOR_TEMPERATURE]))) { msg += "temperature=" + String(sensorValues[SENSOR_TEMPERATURE]); @@ -113,7 +123,16 @@ void influxdb_begin() { } -void pushToInfluxDB(String device, float sensorValues[]) { +void pushToInfluxDB(String device, float sensorValues[]) +{ + +#ifndef USE_WIFIMANAGER + if (WiFi.status() != WL_CONNECTED) + { + debug("Not connected to WLAN"); + return; + } +#endif if (!(isnan(sensorValues[SENSOR_TEMPERATURE]))) { @@ -172,7 +191,8 @@ void pushToInfluxDB(String device, float sensorValues[]) { } -void _writePoint() { +void _writePoint() +{ // wait unitl ready do { diff --git a/firmware/webUpdater.ino b/firmware/webUpdater.ino index 4514cdd..c3ed46f 100644 --- a/firmware/webUpdater.ino +++ b/firmware/webUpdater.ino @@ -26,15 +26,19 @@ String _webUpdater_ip = "127.0.0.1"; String _webUpdater_dev = "unknown"; float _webUpdater_sensValues[VALUES]; -String hb_ws_msg_start = "{"; -String hb_ws_msg_temp = "\"temperature\": "; -String hb_ws_msg_humi = "\"humidity\": "; -String hb_ws_msg_light = "\"lightlevel\": "; -String hb_ws_msg_windspeed = "\"windspeed\": "; -String hb_ws_msg_pressure = "\"pressure\": "; -String hb_ws_msg_timestamp = "\"timestamp\": "; -String hb_ws_msg_valid = "\"valid\": "; -String hb_ws_msg_end = "}"; +const String hb_ws_msg_start = "{"; +const String hb_ws_msg_temp = "\"temperature\": "; +const String hb_ws_msg_humi = "\"humidity\": "; +const String hb_ws_msg_light = "\"lightlevel\": "; +const String hb_ws_msg_windspeed = "\"windspeed\": "; +const String hb_ws_msg_pressure = "\"pressure\": "; +const String hb_ws_msg_timestamp = "\"timestamp\": "; +const String hb_ws_msg_valid = "\"valid\": "; +const String hb_ws_msg_end = "}"; + +#define TR_TD_START_STR "" +#define TR_TD_END_STR "" +#define TD_TD_MID_STR "" boolean wuValidData = false; @@ -54,7 +58,9 @@ void setupWebUpdater(String device, String ip) httpServer.on("/hbWebstat", hb_webstat_send); #endif #ifdef DEBUG_WINDSPEED_MEASUREMENT +#ifdef SENSOR_WIND httpServer.on("/measWind", measureWindspeed); +#endif #endif httpServer.begin(); @@ -82,6 +88,7 @@ void setSensorData(float sensorValues[]) for (uint8_t i = 0; i < VALUES; i++) { _webUpdater_sensValues[i] = sensorValues[i]; } + } void showHTMLMain(void) @@ -92,20 +99,21 @@ void showHTMLMain(void) "" "
firmware update

" "" -"" -"" -"" -"" -"" -"" -"" -"" +TR_TD_START_STR + "temperature" + TD_TD_MID_STR + String(_webUpdater_sensValues[SENSOR_TEMPERATURE]) + TR_TD_END_STR +TR_TD_START_STR + "humidity" + TD_TD_MID_STR + String(_webUpdater_sensValues[SENSOR_HUMIDITY]) + TR_TD_END_STR +TR_TD_START_STR + "
temperature" + String(_webUpdater_sensValues[SENSOR_TEMPERATURE]) + "
humidity" + String(_webUpdater_sensValues[SENSOR_HUMIDITY]) + "
light" + String(_webUpdater_sensValues[SENSOR_LIGHT]) + "
windspeed" + String(_webUpdater_sensValues[SENSOR_WINDSPEED]) + "
pressure" + String(_webUpdater_sensValues[SENSOR_PRESSURE]) + "
batvoltage" + String(_webUpdater_sensValues[SENSOR_BAT_VOLTAGE]) + "
millis" + String(millis()) + "
valid" + String(wuValidData) + "
light" + TD_TD_MID_STR + String(_webUpdater_sensValues[SENSOR_LIGHT]) + TR_TD_END_STR +TR_TD_START_STR + "
windspeed" + TD_TD_MID_STR + String(_webUpdater_sensValues[SENSOR_WINDSPEED]) + TR_TD_END_STR +TR_TD_START_STR + "
pressure" + TD_TD_MID_STR + String(_webUpdater_sensValues[SENSOR_PRESSURE]) + TR_TD_END_STR +TR_TD_START_STR + "
batvoltage" + TD_TD_MID_STR + String(_webUpdater_sensValues[SENSOR_BAT_VOLTAGE]) + TR_TD_END_STR +TR_TD_START_STR + "
millis" + TD_TD_MID_STR + String(millis()) + TR_TD_END_STR +TR_TD_START_STR + "
valid" + TD_TD_MID_STR + String(wuValidData) + TR_TD_END_STR "
" ""; httpServer.send(200, "text/html", message); } +#ifdef HOMEBRIDGE_WEBSTAT void hb_webstat_send(void) { String msg = hb_ws_msg_start + @@ -133,6 +141,7 @@ void hb_webstat_send(void) httpServer.send(200, "text/html", msg); } +#endif #ifdef USE_WIFIMANAGER void resetWifiManager() @@ -158,6 +167,8 @@ void resetWifiManager() } #endif +#ifdef DEBUG_WINDSPEED_MEASUREMENT +#ifdef SENSOR_WIND void measureWindspeed() { @@ -175,3 +186,5 @@ void measureWindspeed() httpServer.send(200, "text/html", message); } +#endif +#endif