Clean up power settings logic

This commit is contained in:
Thomas Kopp 2019-01-27 18:49:28 +01:00
parent 07ed99f9a2
commit 1cef0e43c8
3 changed files with 45 additions and 34 deletions

View file

@ -1,12 +1,11 @@
// Copy this file to config_user.h and adjust it to your needs. // Copy this file to config_user.h and adjust it to your needs.
// Debug output on the serial console // Debug output on the serial console
#define DEBUG 0 #define DEBUG
// Enable/Disable features // Enable/Disable features
#define WEBUPDATER_FEATURE 0 //#define WEBUPDATER_FEATURE
#define BATTERY_POWERED 1 #define BATTERY_POWERED
#define POWERSAVING 1
// InfluxDB credentials // InfluxDB credentials
const char *INFLUXDB_HOST = "hostname"; const char *INFLUXDB_HOST = "hostname";

View file

@ -1,4 +1,3 @@
#include <WiFiClient.h> #include <WiFiClient.h>
#include <ESP8266mDNS.h> #include <ESP8266mDNS.h>
#include <ESP8266HTTPUpdateServer.h> #include <ESP8266HTTPUpdateServer.h>
@ -22,13 +21,13 @@
* - Show current sensor values over simple webpage * - Show current sensor values over simple webpage
* - Push sensor values to various 3rd party services (https://openweathermap.org/) * - Push sensor values to various 3rd party services (https://openweathermap.org/)
* - MQTT? * - MQTT?
* *
* - Buffer sensor values if there is no WIFI connection * - Buffer sensor values if there is no WIFI connection
* - Configure weather station over http webinterface * - Configure weather station over http webinterface
* - OTA firmware update * - OTA firmware update
* *
* TODO: * TODO:
* - https://sminghub.github.io/Sming/about/ * - https://sminghub.github.io/Sming/about/
* - https://github.com/marvinroger/homie-esp8266 * - https://github.com/marvinroger/homie-esp8266
*/ */
@ -45,8 +44,9 @@ Influxdb influxdb(INFLUXDB_HOST, INFLUXDB_PORT);
Adafruit_APDS9960 apds; Adafruit_APDS9960 apds;
Adafruit_BME280 bme; Adafruit_BME280 bme;
#ifdef WEBUPDATER_FEATURE
String localIP = "127.0.0.1"; String localIP = "127.0.0.1";
#endif
//*************************************************************************// //*************************************************************************//
void setup() { void setup() {
@ -63,10 +63,11 @@ void setup() {
digitalWrite(STATUS_LED_PIN, LOW); digitalWrite(STATUS_LED_PIN, LOW);
#ifdef BATTERY_POWERED
criticalBatCheck(); criticalBatCheck();
#endif
// Establish WiFi connection // Establish WiFi connection
String wifiName = "oko-weather-" + String(ESP.getChipId()); String wifiName = "oko-weather-" + DEVICE_NAME;
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
@ -76,7 +77,7 @@ void setup() {
#ifdef DEBUG #ifdef DEBUG
Serial.println("WiFi connection failed, we reboot ..."); Serial.println("WiFi connection failed, we reboot ...");
#endif #endif
// If autoconnect to WLAN failed and no client connected, go to deep sleep
ESP.deepSleep(POWERSAVING_SLEEP_S * 1000000, WAKE_RF_DEFAULT); ESP.deepSleep(POWERSAVING_SLEEP_S * 1000000, WAKE_RF_DEFAULT);
delay(100); delay(100);
} }
@ -85,31 +86,34 @@ void setup() {
Serial.println("Connected!"); Serial.println("Connected!");
#endif #endif
// Open the InfluxDB session // Init variables to influxdb config - doesn't talk to database
influxdb.opendb(INFLUXDB_DB, INFLUXDB_USER, INFLUXDB_PASS); influxdb.opendb(INFLUXDB_DB, INFLUXDB_USER, INFLUXDB_PASS);
// Initialize and configure the sensors // Initialize and configure the sensors
//Light Sensor
apds.begin(); apds.begin();
apds.enableColor(true); apds.enableColor(true);
//Temperature + pressure
bool status = bme.begin(0x76); bool status = bme.begin(0x76);
if (!status) { if (!status) {
#ifdef DEBUG #ifdef DEBUG
Serial.println("Could not find a valid BME280 sensor, check wiring!"); Serial.println("Could not find a valid BME280 sensor, check wiring!");
#endif #endif
while (1); //#warning TODO: FIXME while (1);
} }
#ifdef WEBUPDATER_FEATURE #ifdef WEBUPDATER_FEATURE
#ifndef POWERSAVING #ifndef BATTERY_POWERED
setupWebUpdater(); setupWebUpdater();
localIP = WiFi.localIP().toString(); localIP = WiFi.localIP().toString();
#endif #endif
#endif #endif
//It's magic! leave in
delay(100); delay(100);
#ifdef POWERSAVING #ifdef BATTERY_POWERED
_loop(); _loop();
digitalWrite(STATUS_LED_PIN, LOW); digitalWrite(STATUS_LED_PIN, LOW);
@ -122,12 +126,12 @@ void setup() {
// the ESP.deepSleep requires microseconds as input, after the sleep the system will run into the setup routine // the ESP.deepSleep requires microseconds as input, after the sleep the system will run into the setup routine
ESP.deepSleep(POWERSAVING_SLEEP_S * 1000000, WAKE_RF_DEFAULT); ESP.deepSleep(POWERSAVING_SLEEP_S * 1000000, WAKE_RF_DEFAULT);
delay(100); delay(100);
#endif #endif
} }
//*************************************************************************// //*************************************************************************//
#ifdef BATTERY_POWERED
void criticalBatCheck() { void criticalBatCheck() {
float volt = getBatteryVoltage(); float volt = getBatteryVoltage();
if (volt <= BAT_EMERGENCY_DEEPSLEEP_VOLTAGE) { if (volt <= BAT_EMERGENCY_DEEPSLEEP_VOLTAGE) {
@ -139,7 +143,9 @@ void criticalBatCheck() {
delay(100); delay(100);
} }
} }
#endif
#ifdef BATTERY_POWERED
int energySavingMode() { int energySavingMode() {
// Give the solar panel some time to load the cell to prevent // Give the solar panel some time to load the cell to prevent
// flapping. // flapping.
@ -155,14 +161,13 @@ int energySavingMode() {
return 1; return 1;
} }
return 0; return 0;
} }
#endif
void loop() { void loop() {
criticalBatCheck(); #ifdef BATTERY_POWERED
#ifdef POWERSAVING
delay(50); delay(50);
return; return;
#endif #endif
@ -176,9 +181,9 @@ void loop() {
_loop(); _loop();
//Needed to give WIFI time to function properly
delay(DELAY_LOOP_MS); delay(DELAY_LOOP_MS);
update_sensor_cnt++; update_sensor_cnt++;
#ifdef WEBUPDATER_FEATURE #ifdef WEBUPDATER_FEATURE
@ -188,21 +193,24 @@ void loop() {
void _loop() { void _loop() {
#ifndef POWERSAVING #ifndef BATTERY_POWERED
if (UPDATE_SENSOR_INTERVAL_S * 1000 / DELAY_LOOP_MS <= update_sensor_cnt) { if (UPDATE_SENSOR_INTERVAL_S * 1000 / DELAY_LOOP_MS <= update_sensor_cnt) {
update_sensor_cnt = 0; update_sensor_cnt = 0;
#endif #endif
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_PRESSURE] = fetchPressure(); currentSensorData[SENSOR_PRESSURE] = fetchPressure();
#ifdef BATTERY_POWERED
currentSensorData[SENSOR_BAT_VOLTAGE] = getBatteryVoltage(); currentSensorData[SENSOR_BAT_VOLTAGE] = getBatteryVoltage();
#else
currentSensorData[SENSOR_BAT_VOLTAGE] = 0xFFFFFFFF;
#endif
// Disable expensive tasks // Disable expensive tasks
if (energySavingMode() == 0) { if (energySavingMode() == 0) {
currentSensorData[SENSOR_WINDSPEED] = fetchWindspeed(); currentSensorData[SENSOR_WINDSPEED] = fetchWindspeed();
} else { } else {
currentSensorData[SENSOR_WINDSPEED] = -1; currentSensorData[SENSOR_WINDSPEED] = 0xFFFFFFFF;
} }
#ifdef DEBUG #ifdef DEBUG
@ -210,7 +218,7 @@ void _loop() {
//Serial.println("Current readings:"); //Serial.println("Current readings:");
Serial.println("Temperature: " + String(currentSensorData[SENSOR_TEMPERATURE]) + " °C"); Serial.println("Temperature: " + String(currentSensorData[SENSOR_TEMPERATURE]) + " °C");
Serial.println("Humidity: " + String(currentSensorData[SENSOR_HUMIDITY]) + " %"); Serial.println("Humidity: " + String(currentSensorData[SENSOR_HUMIDITY]) + " %");
Serial.println("Light: " + String(currentSensorData[SENSOR_LIGHT]) + " Lumen"); Serial.println("Light: " + String(currentSensorData[SENSOR_LIGHT]) + " Lux");
Serial.println("Windspeed: " + String(currentSensorData[SENSOR_WINDSPEED]) + " km/h"); Serial.println("Windspeed: " + String(currentSensorData[SENSOR_WINDSPEED]) + " km/h");
Serial.println("Pressure: " + String(currentSensorData[SENSOR_PRESSURE]) + " hPa"); Serial.println("Pressure: " + String(currentSensorData[SENSOR_PRESSURE]) + " hPa");
Serial.println("Bat Voltage: " + String(currentSensorData[SENSOR_BAT_VOLTAGE]) + " V"); Serial.println("Bat Voltage: " + String(currentSensorData[SENSOR_BAT_VOLTAGE]) + " V");
@ -219,9 +227,9 @@ void _loop() {
delay(100); delay(100);
pushToInfluxDB(DEVICE_NAME, currentSensorData); pushToInfluxDB(DEVICE_NAME, currentSensorData);
#ifdef WEBUPDATER_FEATURE #ifdef WEBUPDATER_FEATURE
#ifndef POWERSAVING #ifndef BATTERY_POWERED
setSensorData(DEVICE_NAME, localIP, currentSensorData); setSensorData(DEVICE_NAME, localIP, currentSensorData);
} }
#endif #endif

16
firmware/sensors.ino Executable file → Normal file
View file

@ -1,3 +1,4 @@
#include "config_user.h"
int anemometerRotations = 0; int anemometerRotations = 0;
unsigned long currentTime = 0; unsigned long currentTime = 0;
@ -17,14 +18,17 @@ float fetchHumidity() {
} }
float fetchLight() { float fetchLight() {
uint16_t red, green, blue, white; //TODO read values
uint16_t red, green, blue, white, lux;
while(!apds.colorDataReady()) { while(!apds.colorDataReady()) {
delay(5); delay(5);
} }
apds.getColorData(&red, &green, &blue, &white); apds.getColorData(&red, &green, &blue, &white);
return white; //calculate lux
lux = apds.calculateLux(red, green, blue);
return lux;
} }
void _anemometerInterrupt() { void _anemometerInterrupt() {
@ -47,10 +51,10 @@ float fetchWindspeed() {
} }
// Copied from https://arduinodiy.wordpress.com/2016/12/25/monitoring-lipo-battery-voltage-with-wemos-d1-minibattery-shield-and-thingspeak/ // Copied from https://arduinodiy.wordpress.com/2016/12/25/monitoring-lipo-battery-voltage-with-wemos-d1-minibattery-shield-and-thingspeak/
#ifdef BATTERY_POWERED
float getBatteryVoltage() { float getBatteryVoltage() {
uint16_t raw = analogRead(A0); uint16_t raw = analogRead(A0);
float volt = raw / 1023.0; float volt = raw / 1023.0;
return volt * 4.2; return volt * 4.2;
} }
#endif