Powersaving implemented. The web update mechanism is disabled.
This commit is contained in:
parent
f52e7afc87
commit
10977e14b7
4 changed files with 72 additions and 20 deletions
|
@ -1,5 +1,7 @@
|
||||||
|
|
||||||
#define DEBUG 1
|
//#define DEBUG 1
|
||||||
|
|
||||||
|
#define POWERSAVING 1
|
||||||
|
|
||||||
#define SENSOR_TEMPERATURE 0
|
#define SENSOR_TEMPERATURE 0
|
||||||
#define SENSOR_HUMIDITY 1
|
#define SENSOR_HUMIDITY 1
|
||||||
|
@ -11,6 +13,7 @@
|
||||||
#define UPDATE_SENSOR_INTERVAL_S 10
|
#define UPDATE_SENSOR_INTERVAL_S 10
|
||||||
#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 10
|
||||||
|
|
||||||
#define STATUS_LED_PIN BUILTIN_LED
|
#define STATUS_LED_PIN BUILTIN_LED
|
||||||
#define ANEMOMETER_PIN D7
|
#define ANEMOMETER_PIN D7
|
||||||
|
|
|
@ -60,6 +60,8 @@ void setup() {
|
||||||
pinMode(ANEMOMETER_PIN, INPUT_PULLUP);
|
pinMode(ANEMOMETER_PIN, INPUT_PULLUP);
|
||||||
pinMode(A0, INPUT);
|
pinMode(A0, INPUT);
|
||||||
|
|
||||||
|
digitalWrite(STATUS_LED_PIN, HIGH);
|
||||||
|
|
||||||
// Establish WiFi connection
|
// Establish WiFi connection
|
||||||
String wifiName = "oko-weather-" + String(ESP.getChipId());
|
String wifiName = "oko-weather-" + String(ESP.getChipId());
|
||||||
wifiManager.setMinimumSignalQuality(15);
|
wifiManager.setMinimumSignalQuality(15);
|
||||||
|
@ -85,34 +87,72 @@ void setup() {
|
||||||
|
|
||||||
bool status = bme.begin(0x76);
|
bool status = bme.begin(0x76);
|
||||||
if (!status) {
|
if (!status) {
|
||||||
|
#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
|
||||||
while (1);
|
while (1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifndef POWERSAVING
|
||||||
setupWebUpdater();
|
setupWebUpdater();
|
||||||
|
#endif
|
||||||
|
|
||||||
localIP = WiFi.localIP().toString();
|
localIP = WiFi.localIP().toString();
|
||||||
|
|
||||||
delay(100);
|
delay(100);
|
||||||
|
|
||||||
|
#ifdef POWERSAVING
|
||||||
|
_loop();
|
||||||
|
|
||||||
digitalWrite(STATUS_LED_PIN, LOW);
|
digitalWrite(STATUS_LED_PIN, LOW);
|
||||||
|
|
||||||
|
WiFi.mode(WIFI_OFF);
|
||||||
|
WiFi.forceSleepBegin();
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//*************************************************************************//
|
//*************************************************************************//
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
|
||||||
|
#ifdef POWERSAVING
|
||||||
|
delay(50);
|
||||||
|
return;
|
||||||
|
#endif
|
||||||
|
|
||||||
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef DEBUG
|
_loop();
|
||||||
digitalWrite(STATUS_LED_PIN, HIGH);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
delay(DELAY_LOOP_MS);
|
||||||
|
|
||||||
|
if (currentSensorData[SENSOR_BAT_VOLTAGE] <= 3.4)
|
||||||
|
{
|
||||||
|
#ifdef DEBUG
|
||||||
|
Serial.println("Low battery, going into deep sleep.");
|
||||||
|
#endif
|
||||||
|
ESP.deepSleep(4294967295); // battery low, shutting down
|
||||||
|
}
|
||||||
|
|
||||||
|
update_sensor_cnt++;
|
||||||
|
update_webserver_cnt++;
|
||||||
|
}
|
||||||
|
|
||||||
|
void _loop() {
|
||||||
|
|
||||||
|
#ifndef POWERSAVING
|
||||||
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
|
||||||
|
|
||||||
currentSensorData[SENSOR_TEMPERATURE] = fetchTemperature();
|
currentSensorData[SENSOR_TEMPERATURE] = fetchTemperature();
|
||||||
currentSensorData[SENSOR_HUMIDITY] = fetchHumidity();
|
currentSensorData[SENSOR_HUMIDITY] = fetchHumidity();
|
||||||
currentSensorData[SENSOR_LIGHT] = fetchLight();
|
currentSensorData[SENSOR_LIGHT] = fetchLight();
|
||||||
|
@ -120,10 +160,6 @@ void loop() {
|
||||||
currentSensorData[SENSOR_PRESSURE] = fetchPressure();
|
currentSensorData[SENSOR_PRESSURE] = fetchPressure();
|
||||||
currentSensorData[SENSOR_BAT_VOLTAGE] = getBatteryVoltage();
|
currentSensorData[SENSOR_BAT_VOLTAGE] = getBatteryVoltage();
|
||||||
|
|
||||||
if (currentSensorData[SENSOR_BAT_VOLTAGE] <= 3.7)
|
|
||||||
{
|
|
||||||
ESP.deepSleep(4294967295); // battery low, shutting down
|
|
||||||
}
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
Serial.println("");
|
Serial.println("");
|
||||||
//Serial.println("Current readings:");
|
//Serial.println("Current readings:");
|
||||||
|
@ -135,16 +171,15 @@ void loop() {
|
||||||
Serial.println("Bat Voltage: " + String(currentSensorData[SENSOR_BAT_VOLTAGE]) + " V");
|
Serial.println("Bat Voltage: " + String(currentSensorData[SENSOR_BAT_VOLTAGE]) + " V");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
delay(100);
|
||||||
|
|
||||||
pushToInfluxDB(DEVICE_NAME, currentSensorData);
|
pushToInfluxDB(DEVICE_NAME, currentSensorData);
|
||||||
|
|
||||||
|
#ifndef POWERSAVING
|
||||||
setSensorData(DEVICE_NAME, localIP, currentSensorData);
|
setSensorData(DEVICE_NAME, localIP, currentSensorData);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
//ESP.deepSleep(DELAY_LOOP_MS * 1000); // the ESP.deepSleep requires microseconds as input, after the sleep the system will run into the setup routine
|
|
||||||
//delay(100);
|
|
||||||
delay(DELAY_LOOP_MS);
|
|
||||||
|
|
||||||
update_sensor_cnt++;
|
|
||||||
update_webserver_cnt++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//*************************************************************************//
|
//*************************************************************************//
|
||||||
|
|
|
@ -1,12 +1,26 @@
|
||||||
|
|
||||||
void pushToInfluxDB(String device, float sensorValues[]) {
|
void pushToInfluxDB(String device, float sensorValues[]) {
|
||||||
|
|
||||||
influxdb.write("weather,device=" + device + " temperature=" + String(sensorValues[SENSOR_TEMPERATURE]));
|
/*influxdb.write("weather,device=" + device + " temperature=" + String(sensorValues[SENSOR_TEMPERATURE]));
|
||||||
influxdb.write("weather,device=" + device + " humidity=" + String(sensorValues[SENSOR_HUMIDITY]));
|
influxdb.write("weather,device=" + device + " humidity=" + String(sensorValues[SENSOR_HUMIDITY]));
|
||||||
influxdb.write("weather,device=" + device + " light=" + String(sensorValues[SENSOR_LIGHT]));
|
influxdb.write("weather,device=" + device + " light=" + String(sensorValues[SENSOR_LIGHT]));
|
||||||
influxdb.write("weather,device=" + device + " windspeed=" + String(sensorValues[SENSOR_WINDSPEED]));
|
influxdb.write("weather,device=" + device + " windspeed=" + String(sensorValues[SENSOR_WINDSPEED]));
|
||||||
influxdb.write("weather,device=" + device + " pressure=" + String(sensorValues[SENSOR_PRESSURE]));
|
influxdb.write("weather,device=" + device + " pressure=" + String(sensorValues[SENSOR_PRESSURE]));
|
||||||
influxdb.write("weather,device=" + device + " batvoltage=" + String(sensorValues[SENSOR_BAT_VOLTAGE]));
|
influxdb.write("weather,device=" + device + " batvoltage=" + String(sensorValues[SENSOR_BAT_VOLTAGE]));*/
|
||||||
|
|
||||||
|
String msg = "weather,device=" + device + " temperature=" + String(sensorValues[SENSOR_TEMPERATURE])
|
||||||
|
+ ",humidity=" + String(sensorValues[SENSOR_HUMIDITY])
|
||||||
|
+ ",light=" + String(sensorValues[SENSOR_LIGHT])
|
||||||
|
+ ",windspeed=" + String(sensorValues[SENSOR_WINDSPEED])
|
||||||
|
+ ",pressure=" + String(sensorValues[SENSOR_PRESSURE])
|
||||||
|
+ ",batvoltage=" + String(sensorValues[SENSOR_BAT_VOLTAGE]);
|
||||||
|
|
||||||
|
uint8_t tries = 0;
|
||||||
|
do {
|
||||||
|
tries++;
|
||||||
|
influxdb.write(msg);
|
||||||
|
} while (influxdb.response() != DB_SUCCESS and tries < 5);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,7 @@ float fetchHumidity() {
|
||||||
float fetchLight() {
|
float fetchLight() {
|
||||||
uint16_t red, green, blue, white;
|
uint16_t red, green, blue, white;
|
||||||
|
|
||||||
while(!apds.colorDataReady()){
|
while(!apds.colorDataReady()) {
|
||||||
delay(5);
|
delay(5);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue