slightly more sophisticated battery monitoring, with charge status
parent
83c1f2fa35
commit
e125615130
|
@ -4,16 +4,53 @@
|
|||
#include <Arduino.h>
|
||||
|
||||
const int BATT_IN_PIN = A0;
|
||||
const int BATTERY_CHECK_INTERVAL = 5000;
|
||||
|
||||
double battery_read_voltage()
|
||||
typedef struct battery_info_t
|
||||
{
|
||||
int acc = analogRead(BATT_IN_PIN);
|
||||
int i = 0;
|
||||
while (i < 10) {
|
||||
acc += analogRead(BATT_IN_PIN);
|
||||
i++;
|
||||
}
|
||||
return (double) acc * .00042; // TODO: check this against a multimeter
|
||||
uint last_raw = 0;
|
||||
int last_delta = 0;
|
||||
unsigned long last_millis = 0;
|
||||
bool is_charging = false;
|
||||
};
|
||||
|
||||
battery_info_t Battery;
|
||||
|
||||
float battery_get_voltage()
|
||||
{
|
||||
return (float) Battery.last_raw * .00042;
|
||||
}
|
||||
|
||||
void uweather_handle_battery()
|
||||
{
|
||||
unsigned long now = millis();
|
||||
if (Battery.last_millis + BATTERY_CHECK_INTERVAL > now) {
|
||||
return; // just checked, nothing to do
|
||||
}
|
||||
|
||||
// Read voltage
|
||||
unsigned int raw = 0;
|
||||
int i = 0;
|
||||
analogRead(BATT_IN_PIN); // throw away first reading
|
||||
while (i < 10) {
|
||||
raw += analogRead(BATT_IN_PIN);
|
||||
i++;
|
||||
}
|
||||
|
||||
// Calculate delta
|
||||
int delta = raw - Battery.last_raw;
|
||||
|
||||
// Determine if charging
|
||||
if (delta > 0) {
|
||||
Battery.is_charging = true;
|
||||
} else if (delta < 0) {
|
||||
Battery.is_charging = false;
|
||||
}
|
||||
|
||||
// Save values
|
||||
Battery.last_raw = raw;
|
||||
Battery.last_delta = delta;
|
||||
Battery.last_millis = now;
|
||||
}
|
||||
|
||||
#endif /** _UWEATHER_BATTERY_H_ include guard */
|
|
@ -85,14 +85,15 @@ void handle_weather_current(HttpRequest& req, HttpResponse& res)
|
|||
dtostrf(BME.readTemperature(), 2, 1, buff[0]);
|
||||
dtostrf(BME.readPressure(), 2, 1, buff[1]);
|
||||
dtostrf(BME.readHumidity(), 2, 1, buff[2]);
|
||||
dtostrf(battery_read_voltage(), 2, 1, buff[3]);
|
||||
dtostrf(battery_get_voltage(), 2, 1, buff[3]);
|
||||
Serial.printf("temp= %s, pres= %s, hum=%s\r\n", buff[0], buff[1], buff[2]);
|
||||
res.headers.set("Content-Type", "application/json");
|
||||
content.printf("{\"time\": null, \"temperature\": %s, \"pressure\": %s, \"humidity\": %s, \"battery_voltage\": %s}",
|
||||
content.printf("{\"time\": null, \"temperature\": %s, \"pressure\": %s, \"humidity\": %s, \"battery\": { \"voltage\": %s, \"is_charging\": %s}}",
|
||||
buff[0],
|
||||
buff[1],
|
||||
buff[2],
|
||||
buff[3]
|
||||
buff[3],
|
||||
(Battery.is_charging ? "true":"false")
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -287,6 +288,7 @@ void uweather_web_init(void)
|
|||
void uweather_web_handle(void)
|
||||
{
|
||||
if (web_is_shutdown) return;
|
||||
uweather_handle_battery();
|
||||
if (web_shutdown_at) {
|
||||
unsigned long now = millis();
|
||||
if (now > web_shutdown_at) {
|
||||
|
|
Loading…
Reference in New Issue