From f740973b01ac08d18db78fe6d6076a4b6c73d3a2 Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Sat, 18 Feb 2023 16:58:03 +0100 Subject: [PATCH] Added USB voltage read sensor --- firmware/config_user.h.example | 1 + firmware/constants.h | 3 ++- firmware/firmware.ino | 13 +++++++++++-- firmware/influxdb.ino | 4 ++-- firmware/sensor_battery.ino | 14 ++++++++++++++ firmware/webUpdater.ino | 14 +++++++++++--- 6 files changed, 41 insertions(+), 8 deletions(-) diff --git a/firmware/config_user.h.example b/firmware/config_user.h.example index c209cd5..d39911a 100755 --- a/firmware/config_user.h.example +++ b/firmware/config_user.h.example @@ -31,6 +31,7 @@ String DEVICE_NAME = "weatherstation"; #define SENSOR_BME280 //#define SENSOR_BMP280 #define SENSOR_BATTERY +//#define SENSOR_USB_VOLTAGE //#define BAT_PINS_D34 diff --git a/firmware/constants.h b/firmware/constants.h index cab8a7d..aea0fe1 100644 --- a/firmware/constants.h +++ b/firmware/constants.h @@ -7,7 +7,7 @@ #define SENSOR_LIGHT 2 #define SENSOR_WINDSPEED 3 #define SENSOR_PRESSURE 4 -#define SENSOR_BAT_VOLTAGE 5 +#define SENSOR_VOLTAGE 5 #define SENSOR_ESAVEMODE 6 #define SENSOR_BATCHARGESTATE 7 @@ -37,6 +37,7 @@ 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_voltage = "\"voltage\": "; const String hb_ws_msg_timestamp = "\"timestamp\": "; const String hb_ws_msg_valid = "\"valid\": "; const String hb_ws_msg_end = "}"; diff --git a/firmware/firmware.ino b/firmware/firmware.ino index e5d39b1..768c5b4 100644 --- a/firmware/firmware.ino +++ b/firmware/firmware.ino @@ -241,9 +241,12 @@ float readSensors(uint8_t s) { #endif break; - case SENSOR_BAT_VOLTAGE: + case SENSOR_VOLTAGE: #ifdef SENSOR_BATTERY ret = battery_voltage(); +#endif +#ifdef SENSOR_USB_VOLTAGE + ret = usb_voltage(); #endif break; @@ -694,8 +697,10 @@ void _fsm_loop() //*************************************************************************// + void _battery_mode_main() { +#ifdef SENSOR_BATTERY if (energySavingMode() == 1) { // Disable expensive tasks //debug("read of wind sensor because of low battery disabled"); @@ -705,6 +710,7 @@ void _battery_mode_main() { //debug("read of wind sensor because of high battery enabled"); do_not_read_windsensor = false; } +#endif for (uint8_t i = 0; i < VALUES; i++) { currentSensorData[i] = readSensors(i); @@ -736,7 +742,7 @@ void logToSerial(float sensorValues[]) { Serial.println("Light: " + String(sensorValues[SENSOR_LIGHT]) + " Lux"); Serial.println("Windspeed: " + String(sensorValues[SENSOR_WINDSPEED]) + " km/h"); Serial.println("Pressure: " + String(sensorValues[SENSOR_PRESSURE]) + " hPa"); - Serial.println("Bat Voltage: " + String(sensorValues[SENSOR_BAT_VOLTAGE]) + " V"); + Serial.println("Bat Voltage: " + String(sensorValues[SENSOR_VOLTAGE]) + " V"); Serial.println("Bat charge state: " + String(sensorValues[SENSOR_BATCHARGESTATE])); Serial.println("Energy saving: " + String(sensorValues[SENSOR_ESAVEMODE])); } @@ -765,6 +771,9 @@ String getJsonData() hb_ws_msg_pressure + String(currentSensorData[SENSOR_PRESSURE], 2) + ", " + + hb_ws_msg_voltage + + String(currentSensorData[SENSOR_VOLTAGE], 2) + + ", " + hb_ws_msg_timestamp + String(millis()) + ", " + diff --git a/firmware/influxdb.ino b/firmware/influxdb.ino index a3d2174..fd4ab62 100644 --- a/firmware/influxdb.ino +++ b/firmware/influxdb.ino @@ -150,10 +150,10 @@ void pushToInfluxDB(String device, float sensorValues[]) { sensor.addField("pressure", sensorValues[SENSOR_PRESSURE]); _writePoint(); } - if (!(isnan(sensorValues[SENSOR_BAT_VOLTAGE]))) + if (!(isnan(sensorValues[SENSOR_VOLTAGE]))) { sensor.clearFields(); - sensor.addField("batvoltage", sensorValues[SENSOR_BAT_VOLTAGE]); + sensor.addField("batvoltage", sensorValues[SENSOR_VOLTAGE]); _writePoint(); } if (!(isnan(sensorValues[SENSOR_ESAVEMODE]))) diff --git a/firmware/sensor_battery.ino b/firmware/sensor_battery.ino index a2e7745..dfff287 100644 --- a/firmware/sensor_battery.ino +++ b/firmware/sensor_battery.ino @@ -2,6 +2,7 @@ float _sensor_battery_saveMode = ENERGY_SAVE_MODE_DISABLED; uint16_t energySavingIterations = 0; // Copied from https://arduinodiy.wordpress.com/2016/12/25/monitoring-lipo-battery-voltage-with-wemos-d1-minibattery-shield-and-thingspeak/ +#ifdef SENSOR_BATTERY float battery_voltage() { // ESP8266 ADC pin input voltage range ist 0V .. 1V // The Wemos D1 mini does already contain a voltage divider circuit: A0(Wemos PCB) -- [220kOhm] -- ADC (ESP8266)-- [100kOhm] -- GND @@ -49,3 +50,16 @@ int energySavingMode() { float isEnergySavingMode() { return _sensor_battery_saveMode; } +#endif + +#ifdef SENSOR_USB_VOLTAGE +float usb_voltage() { + // ESP8266 ADC pin input voltage range ist 0V .. 1V + // The Wemos D1 mini does already contain a voltage divider circuit: A0(Wemos PCB) -- [220kOhm] -- ADC (ESP8266)-- [100kOhm] -- GND + // The (+) pole of the battery is connected to the A0 pin of the Wemos board through a additional 300kOhm resistance. + // The USB voltage of 5V max is measured as 1.0V on ESP8266 ADC pin. + uint16_t raw = analogRead(A0); + float volt = raw / 1023.0; + return volt * 6.2; +} +#endif diff --git a/firmware/webUpdater.ino b/firmware/webUpdater.ino index f3b5b08..1fe5748 100644 --- a/firmware/webUpdater.ino +++ b/firmware/webUpdater.ino @@ -128,7 +128,7 @@ void showHTMLMain(void) { #endif #ifdef SHOW_SENSOR_DATA_ON_WEBUPDATER_MAIN_PAGE "

" 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 + "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 TR_TD_START_STR + "wifi rssi" + TD_TD_MID_STR + WiFi.RSSI() + TR_TD_END_STR TR_TD_START_STR + "wifi rec cnt" + TD_TD_MID_STR + String(_wifi_reconnect_cnt) + TR_TD_END_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 + "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_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 TR_TD_START_STR + "wifi rssi" + TD_TD_MID_STR + WiFi.RSSI() + TR_TD_END_STR TR_TD_START_STR + "wifi rec cnt" + TD_TD_MID_STR + String(_wifi_reconnect_cnt) + TR_TD_END_STR "" #endif #ifndef DISABLE_WIFIMANAGER "