weatherstation/firmware/sensor_battery.ino

66 lines
2.2 KiB
Arduino
Raw Normal View History

2020-02-18 20:06:03 +01:00
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/
2023-02-18 16:58:03 +01:00
#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
// The (+) pole of the battery is connected to the A0 pin of the Wemos board through a additional 100kOhm resistance.
// The battery voltage of 4.2V max is measured as 1.0V on ESP8266 ADC pin.
uint16_t raw = analogRead(A0);
float volt = raw / 1023.0;
return volt * 4.2;
}
float battery_charging() {
2020-02-20 19:49:44 +01:00
//debug("charging pin=" + String(digitalRead(BAT_CHARGING_PIN)));
//debug("charged pin=" + String(digitalRead(BAT_CHARGED_PIN)));
if (LOW == digitalRead(BAT_CHARGING_PIN)) {
return BAT_CHARGE_STATE_CHARGING;
} else if (LOW == digitalRead(BAT_CHARGED_PIN)) {
return BAT_CHARGE_STATE_CHARGED;
}
return BAT_CHARGE_STATE_NOTCHARGING;
}
int energySavingMode() {
// Give the solar panel some time to load the cell to prevent
// flapping.
if (energySavingIterations > 0) {
energySavingIterations--;
_sensor_battery_saveMode = ENERGY_SAVE_MODE_ENABLED;
return 1;
}
// Is the battery low?
if (currentSensorData[SENSOR_BAT_VOLTAGE] <= BAT_LOW_VOLTAGE) {
// Entering energy saving
energySavingIterations = ENERGY_SAVING_ITERATIONS;
_sensor_battery_saveMode = ENERGY_SAVE_MODE_ENABLED;
return 1;
}
_sensor_battery_saveMode = ENERGY_SAVE_MODE_DISABLED;
return 0;
}
float isEnergySavingMode() {
return _sensor_battery_saveMode;
2022-05-16 14:26:17 +02:00
}
2023-02-18 16:58:03 +01:00
#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