manual merge

This commit is contained in:
klaute 2019-01-05 18:58:01 +01:00
commit 845e637c59
2 changed files with 53 additions and 19 deletions

View file

@ -17,8 +17,11 @@
#define UPDATE_WEBSERVER_INTVERVAL_S 1 #define UPDATE_WEBSERVER_INTVERVAL_S 1
#define DELAY_LOOP_MS 50 #define DELAY_LOOP_MS 50
#define POWERSAVING_SLEEP_S 600 #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 STATUS_LED_PIN BUILTIN_LED
#define ANEMOMETER_PIN D7 #define ANEMOMETER_PIN D7
@ -29,6 +32,7 @@
#define BME_CS 10 #define BME_CS 10
#define INITIAL_WEBSERVER_TIME 20 #define INITIAL_WEBSERVER_TIME 20
#define WEBUPDATER_FEATURE 0
#define SEALEVELPRESSURE_HPA (1013.25) #define SEALEVELPRESSURE_HPA (1013.25)
@ -41,6 +45,4 @@ const char *INFLUXDB_DB = "weatherstation";
const char *INFLUXDB_USER = "oko"; const char *INFLUXDB_USER = "oko";
const char *INFLUXDB_PASS = "de1873a0d2f8f21f17cf4d8db4f65c59"; 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_sensor_cnt = 0;
uint16_t update_webserver_cnt = 0; uint16_t update_webserver_cnt = 0;
uint16_t energySavingIterations = 0;
WiFiManager wifiManager; WiFiManager wifiManager;
Influxdb influxdb(INFLUXDB_HOST, INFLUXDB_PORT); Influxdb influxdb(INFLUXDB_HOST, INFLUXDB_PORT);
@ -48,7 +49,6 @@ String localIP = "127.0.0.1";
//*************************************************************************// //*************************************************************************//
void setup() { void setup() {
//WiFi.disconnect(); // erase the wifi credentials //WiFi.disconnect(); // erase the wifi credentials
#ifdef DEBUG #ifdef DEBUG
@ -60,7 +60,7 @@ void setup() {
pinMode(ANEMOMETER_PIN, INPUT_PULLUP); pinMode(ANEMOMETER_PIN, INPUT_PULLUP);
pinMode(A0, INPUT); pinMode(A0, INPUT);
digitalWrite(STATUS_LED_PIN, HIGH); digitalWrite(STATUS_LED_PIN, LOW);
lowBatCheck(getBatteryVoltage()); lowBatCheck(getBatteryVoltage());
@ -68,7 +68,6 @@ void setup() {
String wifiName = "oko-weather-" + String(ESP.getChipId()); String wifiName = "oko-weather-" + String(ESP.getChipId());
wifiManager.setMinimumSignalQuality(15); wifiManager.setMinimumSignalQuality(15);
wifiManager.setConnectTimeout(60); // the time in seconds to wait for the known wifi connection 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 wifiManager.setTimeout(60); // the time in seconds to wait for the user to configure the device
@ -100,11 +99,12 @@ void setup() {
while (1); while (1);
} }
#ifdef WEBUPDATER_FEATURE
#ifndef POWERSAVING #ifndef POWERSAVING
setupWebUpdater(); setupWebUpdater();
#endif
localIP = WiFi.localIP().toString(); localIP = WiFi.localIP().toString();
#endif
#endif
delay(100); delay(100);
@ -113,7 +113,7 @@ void setup() {
digitalWrite(STATUS_LED_PIN, LOW); digitalWrite(STATUS_LED_PIN, LOW);
lowBatCheck(currentSensorData[SENSOR_BAT_VOLTAGE]); criticalBatCheck();
WiFi.mode(WIFI_OFF); WiFi.mode(WIFI_OFF);
WiFi.forceSleepBegin(); WiFi.forceSleepBegin();
@ -123,41 +123,66 @@ void setup() {
delay(100); delay(100);
#endif #endif
} }
//*************************************************************************// //*************************************************************************//
void lowBatCheck(float v) void criticalBatCheck() {
{ if (currentSensorData[SENSOR_BAT_VOLTAGE] <= BAT_EMERGENCY_DEEPSLEEP_VOLTAGE) {
if (v <= BAT_LOW_VOLTAGE)
{
#ifdef DEBUG #ifdef DEBUG
Serial.println("Low battery, going into deep sleep."); Serial.println("Low battery, going into deep sleep.");
#endif #endif
ESP.deepSleep(4294967295); // battery low, shutting down ESP.deepSleep(EMERGENCY_SLEEP_S * 1000000); // battery low, shutting down
delay(100); delay(100);
} }
} }
int energySavingMode() {
// Give the solar panel some time to load the cell to prevent
// flapping.
if (energySavingIterations > 0) {
energySavingIterations--;
return 1;
}
// 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() { void loop() {
criticalBatCheck();
#ifdef POWERSAVING #ifdef POWERSAVING
delay(50); delay(50);
return; return;
#endif #endif
#ifdef WEBUPDATER_FEATURE
if (UPDATE_WEBSERVER_INTVERVAL_S * 1000 / DELAY_LOOP_MS <= update_webserver_cnt) { if (UPDATE_WEBSERVER_INTVERVAL_S * 1000 / DELAY_LOOP_MS <= update_webserver_cnt) {
update_webserver_cnt = 0; update_webserver_cnt = 0;
doWebUpdater(); doWebUpdater();
} }
#endif
_loop(); _loop();
delay(DELAY_LOOP_MS); delay(DELAY_LOOP_MS);
update_sensor_cnt++; update_sensor_cnt++;
#ifdef WEBUPDATER_FEATURE
update_webserver_cnt++; update_webserver_cnt++;
#endif
} }
void _loop() { void _loop() {
@ -170,7 +195,13 @@ void _loop() {
currentSensorData[SENSOR_TEMPERATURE] = fetchTemperature(); currentSensorData[SENSOR_TEMPERATURE] = fetchTemperature();
currentSensorData[SENSOR_HUMIDITY] = fetchHumidity(); currentSensorData[SENSOR_HUMIDITY] = fetchHumidity();
currentSensorData[SENSOR_LIGHT] = fetchLight(); 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_PRESSURE] = fetchPressure();
currentSensorData[SENSOR_BAT_VOLTAGE] = getBatteryVoltage(); currentSensorData[SENSOR_BAT_VOLTAGE] = getBatteryVoltage();
@ -188,13 +219,14 @@ void _loop() {
delay(100); delay(100);
pushToInfluxDB(DEVICE_NAME, currentSensorData); pushToInfluxDB(DEVICE_NAME, currentSensorData);
#ifdef WEBUPDATER_FEATURE
#ifndef POWERSAVING #ifndef POWERSAVING
setSensorData(DEVICE_NAME, localIP, currentSensorData); setSensorData(DEVICE_NAME, localIP, currentSensorData);
} }
#endif #endif
#endif
} }
//*************************************************************************// //*************************************************************************//