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.
// Debug output on the serial console
#define DEBUG 0
#define DEBUG
// Enable/Disable features
#define WEBUPDATER_FEATURE 0
#define BATTERY_POWERED 1
#define POWERSAVING 1
//#define WEBUPDATER_FEATURE
#define BATTERY_POWERED
// InfluxDB credentials
const char *INFLUXDB_HOST = "hostname";

View file

@ -1,4 +1,3 @@
#include <WiFiClient.h>
#include <ESP8266mDNS.h>
#include <ESP8266HTTPUpdateServer.h>
@ -45,8 +44,9 @@ Influxdb influxdb(INFLUXDB_HOST, INFLUXDB_PORT);
Adafruit_APDS9960 apds;
Adafruit_BME280 bme;
#ifdef WEBUPDATER_FEATURE
String localIP = "127.0.0.1";
#endif
//*************************************************************************//
void setup() {
@ -63,10 +63,11 @@ void setup() {
digitalWrite(STATUS_LED_PIN, LOW);
#ifdef BATTERY_POWERED
criticalBatCheck();
#endif
// Establish WiFi connection
String wifiName = "oko-weather-" + String(ESP.getChipId());
String wifiName = "oko-weather-" + DEVICE_NAME;
wifiManager.setMinimumSignalQuality(15);
wifiManager.setConnectTimeout(60); // the time in seconds to wait for the known wifi connection
@ -76,7 +77,7 @@ void setup() {
#ifdef DEBUG
Serial.println("WiFi connection failed, we reboot ...");
#endif
// If autoconnect to WLAN failed and no client connected, go to deep sleep
ESP.deepSleep(POWERSAVING_SLEEP_S * 1000000, WAKE_RF_DEFAULT);
delay(100);
}
@ -85,31 +86,34 @@ void setup() {
Serial.println("Connected!");
#endif
// Open the InfluxDB session
// Init variables to influxdb config - doesn't talk to database
influxdb.opendb(INFLUXDB_DB, INFLUXDB_USER, INFLUXDB_PASS);
// Initialize and configure the sensors
//Light Sensor
apds.begin();
apds.enableColor(true);
//Temperature + pressure
bool status = bme.begin(0x76);
if (!status) {
#ifdef DEBUG
Serial.println("Could not find a valid BME280 sensor, check wiring!");
#endif
while (1);
//#warning TODO: FIXME while (1);
}
#ifdef WEBUPDATER_FEATURE
#ifndef POWERSAVING
#ifndef BATTERY_POWERED
setupWebUpdater();
localIP = WiFi.localIP().toString();
#endif
#endif
//It's magic! leave in
delay(100);
#ifdef POWERSAVING
#ifdef BATTERY_POWERED
_loop();
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
ESP.deepSleep(POWERSAVING_SLEEP_S * 1000000, WAKE_RF_DEFAULT);
delay(100);
#endif
}
//*************************************************************************//
#ifdef BATTERY_POWERED
void criticalBatCheck() {
float volt = getBatteryVoltage();
if (volt <= BAT_EMERGENCY_DEEPSLEEP_VOLTAGE) {
@ -139,7 +143,9 @@ void criticalBatCheck() {
delay(100);
}
}
#endif
#ifdef BATTERY_POWERED
int energySavingMode() {
// Give the solar panel some time to load the cell to prevent
// flapping.
@ -158,11 +164,10 @@ int energySavingMode() {
return 0;
}
#endif
void loop() {
criticalBatCheck();
#ifdef POWERSAVING
#ifdef BATTERY_POWERED
delay(50);
return;
#endif
@ -176,9 +181,9 @@ void loop() {
_loop();
//Needed to give WIFI time to function properly
delay(DELAY_LOOP_MS);
update_sensor_cnt++;
#ifdef WEBUPDATER_FEATURE
@ -188,21 +193,24 @@ void loop() {
void _loop() {
#ifndef POWERSAVING
#ifndef BATTERY_POWERED
if (UPDATE_SENSOR_INTERVAL_S * 1000 / DELAY_LOOP_MS <= update_sensor_cnt) {
update_sensor_cnt = 0;
#endif
currentSensorData[SENSOR_TEMPERATURE] = fetchTemperature();
currentSensorData[SENSOR_HUMIDITY] = fetchHumidity();
currentSensorData[SENSOR_LIGHT] = fetchLight();
currentSensorData[SENSOR_PRESSURE] = fetchPressure();
#ifdef BATTERY_POWERED
currentSensorData[SENSOR_BAT_VOLTAGE] = getBatteryVoltage();
#else
currentSensorData[SENSOR_BAT_VOLTAGE] = 0xFFFFFFFF;
#endif
// Disable expensive tasks
if (energySavingMode() == 0) {
currentSensorData[SENSOR_WINDSPEED] = fetchWindspeed();
} else {
currentSensorData[SENSOR_WINDSPEED] = -1;
currentSensorData[SENSOR_WINDSPEED] = 0xFFFFFFFF;
}
#ifdef DEBUG
@ -210,7 +218,7 @@ void _loop() {
//Serial.println("Current readings:");
Serial.println("Temperature: " + String(currentSensorData[SENSOR_TEMPERATURE]) + " °C");
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("Pressure: " + String(currentSensorData[SENSOR_PRESSURE]) + " hPa");
Serial.println("Bat Voltage: " + String(currentSensorData[SENSOR_BAT_VOLTAGE]) + " V");
@ -221,7 +229,7 @@ void _loop() {
pushToInfluxDB(DEVICE_NAME, currentSensorData);
#ifdef WEBUPDATER_FEATURE
#ifndef POWERSAVING
#ifndef BATTERY_POWERED
setSensorData(DEVICE_NAME, localIP, currentSensorData);
}
#endif

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

@ -1,3 +1,4 @@
#include "config_user.h"
int anemometerRotations = 0;
unsigned long currentTime = 0;
@ -17,14 +18,17 @@ float fetchHumidity() {
}
float fetchLight() {
uint16_t red, green, blue, white;
//TODO read values
uint16_t red, green, blue, white, lux;
while(!apds.colorDataReady()) {
delay(5);
}
apds.getColorData(&red, &green, &blue, &white);
return white;
//calculate lux
lux = apds.calculateLux(red, green, blue);
return lux;
}
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/
#ifdef BATTERY_POWERED
float getBatteryVoltage() {
uint16_t raw = analogRead(A0);
float volt = raw / 1023.0;
return volt * 4.2;
}
#endif