More energy saving features

This commit is contained in:
Aaron Fischer 2018-12-02 17:06:06 +01:00
parent 3c2eb795ff
commit 82e450708a
2 changed files with 53 additions and 21 deletions

View file

@ -16,8 +16,11 @@
#define UPDATE_WEBSERVER_INTVERVAL_S 1
#define DELAY_LOOP_MS 50
#define POWERSAVING_SLEEP_S 600
#define EMERGENCY_SLEEP_S 172800 // Sleep for 2 days to recover
#define ENERGY_SAVING_ITERATIONS 30
#define BAT_LOW_VOLTAGE 3.4
#define BAT_LOW_VOLTAGE 3.6
#define BAT_EMERGENCY_DEEPSLEEP_VOLTAGE 3.5
#define STATUS_LED_PIN BUILTIN_LED
#define ANEMOMETER_PIN D7
@ -28,6 +31,7 @@
#define BME_CS 10
#define INITIAL_WEBSERVER_TIME 20
#define WEBUPDATER_FEATURE 0
#define SEALEVELPRESSURE_HPA (1013.25)
@ -40,6 +44,4 @@ const char *INFLUXDB_DB = "weatherstation";
const char *INFLUXDB_USER = "oko";
const char *INFLUXDB_PASS = "de1873a0d2f8f21f17cf4d8db4f65c59";
String DEVICE_NAME = "klaute";
String DEVICE_NAME = "aaron";

View file

@ -37,6 +37,7 @@ float currentSensorData[6] = {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 energySavingIterations = 0;
WiFiManager wifiManager;
Influxdb influxdb(INFLUXDB_HOST, INFLUXDB_PORT);
@ -48,7 +49,6 @@ String localIP = "127.0.0.1";
//*************************************************************************//
void setup() {
//WiFi.disconnect(); // erase the wifi credentials
#ifdef DEBUG
@ -60,13 +60,12 @@ void setup() {
pinMode(ANEMOMETER_PIN, INPUT_PULLUP);
pinMode(A0, INPUT);
digitalWrite(STATUS_LED_PIN, HIGH);
digitalWrite(STATUS_LED_PIN, LOW);
// Establish WiFi connection
String wifiName = "oko-weather-" + String(ESP.getChipId());
wifiManager.setMinimumSignalQuality(15);
wifiManager.setConnectTimeout(60); // the time in seconds to wait for the known wifi connection
wifiManager.setTimeout(60); // the time in seconds to wait for the user to configure the device
@ -98,11 +97,12 @@ void setup() {
while (1);
}
#ifdef WEBUPDATER_FEATURE
#ifndef POWERSAVING
setupWebUpdater();
#endif
localIP = WiFi.localIP().toString();
#endif
#endif
delay(100);
@ -111,7 +111,7 @@ void setup() {
digitalWrite(STATUS_LED_PIN, LOW);
lowBatCheck();
criticalBatCheck();
WiFi.mode(WIFI_OFF);
WiFi.forceSleepBegin();
@ -121,43 +121,66 @@ void setup() {
delay(100);
#endif
}
//*************************************************************************//
void lowBatCheck()
{
if (currentSensorData[SENSOR_BAT_VOLTAGE] <= BAT_LOW_VOLTAGE)
{
void criticalBatCheck() {
if (currentSensorData[SENSOR_BAT_VOLTAGE] <= BAT_EMERGENCY_DEEPSLEEP_VOLTAGE) {
#ifdef DEBUG
Serial.println("Low battery, going into deep sleep.");
#endif
ESP.deepSleep(4294967295); // battery low, shutting down
ESP.deepSleep(EMERGENCY_SLEEP_S * 1000000); // battery low, shutting down
delay(100);
}
}
void loop() {
int energySavingMode() {
// Give the solar panel some time to load the cell to prevent
// flapping.
if (energySavingIterations > 0) {
energySavingIterations--;
return 1;
}
lowBatCheck();
// Is the battery low?
if (currentSensorData[SENSOR_BAT_VOLTAGE] <= BAT_LOW_VOLTAGE) {
// Entering energy saving
if (energySavingIterations == 0) {
energySavingIterations = ENERGY_SAVING_ITERATIONS;
}
return 1;
}
return 0;
}
void loop() {
criticalBatCheck();
#ifdef POWERSAVING
delay(50);
return;
#endif
#ifdef WEBUPDATER_FEATURE
if (UPDATE_WEBSERVER_INTVERVAL_S * 1000 / DELAY_LOOP_MS <= update_webserver_cnt) {
update_webserver_cnt = 0;
doWebUpdater();
}
#endif
_loop();
delay(DELAY_LOOP_MS);
update_sensor_cnt++;
#ifdef WEBUPDATER_FEATURE
update_webserver_cnt++;
#endif
}
void _loop() {
@ -170,7 +193,13 @@ void _loop() {
currentSensorData[SENSOR_TEMPERATURE] = fetchTemperature();
currentSensorData[SENSOR_HUMIDITY] = fetchHumidity();
currentSensorData[SENSOR_LIGHT] = fetchLight();
currentSensorData[SENSOR_WINDSPEED] = fetchWindspeed();
// Disable expensive tasks
if (energySavingMode() == 0) {
currentSensorData[SENSOR_WINDSPEED] = fetchWindspeed();
} else {
currentSensorData[SENSOR_WINDSPEED] = -1;
}
currentSensorData[SENSOR_PRESSURE] = fetchPressure();
currentSensorData[SENSOR_BAT_VOLTAGE] = getBatteryVoltage();
@ -188,13 +217,14 @@ void _loop() {
delay(100);
pushToInfluxDB(DEVICE_NAME, currentSensorData);
#ifdef WEBUPDATER_FEATURE
#ifndef POWERSAVING
setSensorData(DEVICE_NAME, localIP, currentSensorData);
}
#endif
#endif
}
//*************************************************************************//