Added USB voltage read sensor

This commit is contained in:
Kai Lauterbach 2023-02-18 16:58:03 +01:00
parent 36b0c72f9b
commit f740973b01
6 changed files with 41 additions and 8 deletions

View file

@ -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

View file

@ -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 = "}";

View file

@ -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()) +
", " +

View file

@ -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])))

View file

@ -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

View file

@ -128,7 +128,7 @@ void showHTMLMain(void) {
#endif
#ifdef SHOW_SENSOR_DATA_ON_WEBUPDATER_MAIN_PAGE
"<br><br><table>" 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 "</table>"
+ "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 "</table>"
#endif
#ifndef DISABLE_WIFIMANAGER
"<br><br><br><br><br><a href=\"http://"
@ -144,8 +144,16 @@ void showHTMLMain(void) {
#ifdef HOMEBRIDGE_WEBSTAT
void hb_webstat_send(void) {
String msg = hb_ws_msg_start + hb_ws_msg_temp + String(_webUpdater_sensValues[SENSOR_TEMPERATURE], 2) + ", " + hb_ws_msg_humi + String(_webUpdater_sensValues[SENSOR_HUMIDITY], 2) + ", " + hb_ws_msg_light + String(_webUpdater_sensValues[SENSOR_LIGHT], 0) + // The light level for the homebridge-http-lux2 plugin is only able to parse integer values
", " + hb_ws_msg_windspeed + String(_webUpdater_sensValues[SENSOR_WINDSPEED], 2) + ", " + hb_ws_msg_pressure + String(_webUpdater_sensValues[SENSOR_PRESSURE], 2) + ", " + hb_ws_msg_timestamp + String(millis()) + ", " + hb_ws_msg_valid + String(wuValidData) + hb_ws_msg_end;
String msg = hb_ws_msg_start +
hb_ws_msg_temp + String(_webUpdater_sensValues[SENSOR_TEMPERATURE], 2) + ", " +
hb_ws_msg_humi + String(_webUpdater_sensValues[SENSOR_HUMIDITY], 2) + ", " +
hb_ws_msg_light + String(_webUpdater_sensValues[SENSOR_LIGHT], 0) + ", " + // The light level for the homebridge-http-lux2 plugin is only able to parse integer values
hb_ws_msg_windspeed + String(_webUpdater_sensValues[SENSOR_WINDSPEED], 2) + ", " +
hb_ws_msg_pressure + String(_webUpdater_sensValues[SENSOR_PRESSURE], 2) + ", " +
hb_ws_msg_voltage + String(_webUpdater_sensValues[SENSOR_VOLTAGE], 2) + ", " +
hb_ws_msg_timestamp + String(millis()) + ", " +
hb_ws_msg_valid + String(wuValidData) +
hb_ws_msg_end;
httpServer.send(200, "text/html", msg);
}