From 87fe6c6e7a64dd441a08a70953ea53210b20375e Mon Sep 17 00:00:00 2001 From: klaute Date: Sat, 2 Feb 2019 10:09:00 +0100 Subject: [PATCH] Added battery charging states. --- firmware/config.h | 7 ++++++- firmware/firmware.ino | 17 ++++++++++++----- firmware/influxdb.ino | 9 +++++++-- firmware/sensors.ino | 13 +++++++++++++ 4 files changed, 38 insertions(+), 8 deletions(-) mode change 100755 => 100644 firmware/config.h mode change 100755 => 100644 firmware/sensors.ino diff --git a/firmware/config.h b/firmware/config.h old mode 100755 new mode 100644 index 2274575..da0ab0e --- a/firmware/config.h +++ b/firmware/config.h @@ -8,6 +8,12 @@ #define SENSOR_WINDSPEED 3 #define SENSOR_PRESSURE 4 #define SENSOR_BAT_VOLTAGE 5 +#define SENSOR_ESAVEMODE 6 +#define SENSOR_BATCHARGESTATE 7 + +#define BAT_CHARGE_STATE_CHARGED 2.0 +#define BAT_CHARGE_STATE_CHARGING 1.0 +#define BAT_CHARGE_STATE_NOTCHARGING 0.0 #define WIFI_AUTOCONNECT_TIMEOUT_S 60 #define WIFI_CONFIG_PORTAL_TIMEOUT_S 60 @@ -35,4 +41,3 @@ #define INITIAL_WEBSERVER_TIME 20 #endif - diff --git a/firmware/firmware.ino b/firmware/firmware.ino index a2f608c..bb01514 100644 --- a/firmware/firmware.ino +++ b/firmware/firmware.ino @@ -33,10 +33,10 @@ //*************************************************************************// -float currentSensorData[6] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0}; +float currentSensorData[8] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}; -uint16_t update_sensor_cnt = 0; -uint16_t update_webserver_cnt = 0; +uint16_t update_sensor_cnt = 0; +uint16_t update_webserver_cnt = 0; uint16_t energySavingIterations = 0; WiFiManager wifiManager; @@ -179,6 +179,7 @@ void loop() { } #endif + _loop(); //Needed to give WIFI time to function properly @@ -202,15 +203,19 @@ void _loop() { currentSensorData[SENSOR_LIGHT] = fetchLight(); currentSensorData[SENSOR_PRESSURE] = fetchPressure(); #ifdef BATTERY_POWERED - currentSensorData[SENSOR_BAT_VOLTAGE] = getBatteryVoltage(); + currentSensorData[SENSOR_BAT_VOLTAGE] = getBatteryVoltage(); + currentSensorData[SENSOR_BATCHARGESTATE] = isBatCharging(); #else - currentSensorData[SENSOR_BAT_VOLTAGE] = 0xFFFFFFFF; + currentSensorData[SENSOR_BAT_VOLTAGE] = 0xFFFFFFFF; + currentSensorData[SENSOR_BATCHARGESTATE] = 0xFFFFFFFF; #endif // Disable expensive tasks if (energySavingMode() == 0) { currentSensorData[SENSOR_WINDSPEED] = fetchWindspeed(); + currentSensorData[SENSOR_ESAVEMODE] = 1.0; } else { currentSensorData[SENSOR_WINDSPEED] = 0xFFFFFFFF; + currentSensorData[SENSOR_ESAVEMODE] = 0.0; } #ifdef DEBUG @@ -222,6 +227,8 @@ void _loop() { Serial.println("Windspeed: " + String(currentSensorData[SENSOR_WINDSPEED]) + " km/h"); Serial.println("Pressure: " + String(currentSensorData[SENSOR_PRESSURE]) + " hPa"); Serial.println("Bat Voltage: " + String(currentSensorData[SENSOR_BAT_VOLTAGE]) + " V"); + Serial.println("Bat charge state: " + String(currentSensorData[SENSOR_BATCHARGESTATE])); + Serial.println("Energy saving: " + String(currentSensorData[SENSOR_ESAVEMODE])); #endif delay(100); diff --git a/firmware/influxdb.ino b/firmware/influxdb.ino index e6aea1b..a26ff12 100644 --- a/firmware/influxdb.ino +++ b/firmware/influxdb.ino @@ -7,7 +7,9 @@ void pushToInfluxDB(String device, float sensorValues[]) { + ",light=" + String(sensorValues[SENSOR_LIGHT]) + ",windspeed=" + String(sensorValues[SENSOR_WINDSPEED]) + ",pressure=" + String(sensorValues[SENSOR_PRESSURE]) - + ",batvoltage=" + String(sensorValues[SENSOR_BAT_VOLTAGE]); + + ",batvoltage=" + String(sensorValues[SENSOR_BAT_VOLTAGE]) + + ",esavemode=" + String(sensorValues[SENSOR_ESAVEMODE]) + + ",batchargestate=" + String(sensorValues[SENSOR_BATCHARGESTATE]); Serial.println(msg); #endif @@ -27,6 +29,9 @@ void pushToInfluxDB(String device, float sensorValues[]) { influxdb.write("weather,device=" + device + " pressure=" + String(sensorValues[SENSOR_PRESSURE])); if (!(isnan(sensorValues[SENSOR_BAT_VOLTAGE]))) influxdb.write("weather,device=" + device + " batvoltage=" + String(sensorValues[SENSOR_BAT_VOLTAGE])); + if (!(isnan(sensorValues[SENSOR_ESAVEMODE]))) + influxdb.write("weather,device=" + device + " esavemode=" + String(sensorValues[SENSOR_ESAVEMODE])); + if (!(isnan(sensorValues[SENSOR_BATCHARGESTATE]))) + influxdb.write("weather,device=" + device + " batchargestate=" + String(sensorValues[SENSOR_BATCHARGESTATE])); } while (influxdb.response() != DB_SUCCESS and tries < 5); } - diff --git a/firmware/sensors.ino b/firmware/sensors.ino old mode 100755 new mode 100644 index 820650c..a503a3b --- a/firmware/sensors.ino +++ b/firmware/sensors.ino @@ -61,4 +61,17 @@ float getBatteryVoltage() { float volt = raw / 1023.0; return volt * 4.2; } + +float isBatCompletelyCharged() { + if (HIGH == digitalRead(BAT_CHARGED_PIN)) + { + return BAT_CHARGE_STATE_CHARGED; + } else if (HIGH == digitalRead(BAT_CHARGING_PIN)) + { + return BAT_CHARGE_STATE_CHARGING; + } + + return BAT_CHARGE_STATE_NOTCHARGING; +} + #endif