weatherstation/firmware/firmware.ino

236 lines
5.8 KiB
Arduino
Raw Normal View History

// Standard ESP8266 libs
#include <ESP8266mDNS.h>
#include <ESP8266HTTPUpdateServer.h>
2017-11-07 22:20:14 +01:00
#include <ESP8266WiFi.h>
2017-11-12 22:34:08 +01:00
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiClient.h> // WiFiClient
#include <WiFiManager.h> // WiFiManager
// Project includes
2017-11-07 22:20:14 +01:00
#include "config.h"
2019-01-08 22:41:24 +01:00
#include "config_user.h"
2017-11-07 22:20:14 +01:00
//*************************************************************************//
const uint8_t VALUES = 8;
float currentSensorData[VALUES] = {nanf("no value"), nanf("no value"), nanf("no value"), nanf("no value"), nanf("no value"), nanf("no value"), nanf("no value"), nanf("no value")};
float (*sensors[VALUES])() = {};
2017-11-07 22:20:14 +01:00
2019-02-02 10:09:00 +01:00
uint16_t update_sensor_cnt = 0;
uint16_t update_webserver_cnt = 0;
2018-06-24 16:54:57 +02:00
2017-11-12 22:34:08 +01:00
WiFiManager wifiManager;
2019-03-31 17:01:04 +02:00
//*************************************************************************//
void debug(String x) {
#ifdef DEBUG
Serial.println(x);
#endif
}
void setup() {
2019-02-06 21:26:13 +01:00
// Erase WiFi Credentials (maybe this will work ...)
//WiFi.disconnect(true);
//delay(2000);
//ESP.reset();
#if defined(DEBUG) || defined(SERIAL_FEATURE)
Serial.begin(115200);
#endif
2017-11-12 22:34:08 +01:00
// Pin settings
2019-02-02 13:31:21 +01:00
pinMode(BAT_CHARGED_PIN, INPUT_PULLUP);
pinMode(BAT_CHARGING_PIN, INPUT_PULLUP);
2017-11-12 21:37:55 +01:00
pinMode(STATUS_LED_PIN, OUTPUT);
pinMode(ANEMOMETER_PIN, INPUT_PULLUP);
2018-07-11 11:12:53 +02:00
pinMode(A0, INPUT);
2017-11-12 22:34:08 +01:00
2018-12-02 17:06:06 +01:00
digitalWrite(STATUS_LED_PIN, LOW);
2019-01-27 18:49:28 +01:00
#ifdef BATTERY_POWERED
2019-01-06 15:21:03 +01:00
criticalBatCheck();
2019-01-27 18:49:28 +01:00
#endif
// Establish WiFi connection
2019-01-27 18:49:28 +01:00
String wifiName = "oko-weather-" + DEVICE_NAME;
2019-02-02 11:58:44 +01:00
wifiManager.setMinimumSignalQuality(16);
wifiManager.setConnectTimeout(WIFI_AUTOCONNECT_TIMEOUT_S); // the time in seconds to wait for the known wifi connection
wifiManager.setTimeout(WIFI_CONFIG_PORTAL_TIMEOUT_S); // the time in seconds to wait for the user to configure the device
2017-11-12 22:34:08 +01:00
if (!wifiManager.autoConnect(wifiName.c_str(), "DEADBEEF")) {
debug("WiFi connection failed, going into deep sleep ...");
2019-01-27 18:49:28 +01:00
// If autoconnect to WLAN failed and no client connected, go to deep sleep
ESP.deepSleep(POWERSAVING_SLEEP_S * 1000000, WAKE_RF_DEFAULT);
delay(100);
2017-11-12 22:34:08 +01:00
}
debug("Connected!");
#ifdef INFLUXDB_FEATURE
influxdb_begin();
#endif
2017-11-20 20:42:02 +01:00
// Initialize and configure the sensors
#ifdef SENSOR_APDS9930
if (sensor_apds9930_begin()) {
sensors[SENSOR_LIGHT] = &apds9930_light;
2019-03-31 17:01:04 +02:00
}
#endif
#ifdef SENSOR_APDS9960
if (sensor_apds9960_begin()) {
sensors[SENSOR_LIGHT] = &apds9930_light;
}
#endif
#ifdef SENSOR_BME280
2019-01-27 18:49:28 +01:00
//Temperature + pressure
if (sensor_bme280_begin(BME_ADDRESS)) {
sensors[SENSOR_TEMPERATURE] = &bme280_temperature;
sensors[SENSOR_HUMIDITY] = &bme280_humidity;
sensors[SENSOR_PRESSURE] = &bme280_pressure;
}
#endif
#ifdef SENSOR_WIND
sensors[SENSOR_WINDSPEED] = &wind_speed;
#endif
#ifdef SENSOR_BATTERY
sensors[SENSOR_BAT_VOLTAGE] = &battery_voltage;
sensors[SENSOR_BATCHARGESTATE] = &battery_charging;
sensors[SENSOR_ESAVEMODE] = &isEnergySavingMode;
#endif
2017-11-20 20:42:02 +01:00
2018-12-02 17:06:06 +01:00
#ifdef WEBUPDATER_FEATURE
2019-01-27 18:49:28 +01:00
#ifndef BATTERY_POWERED
setupWebUpdater(DEVICE_NAME, WiFi.localIP().toString());
2018-12-02 17:06:06 +01:00
#endif
#endif
2018-07-11 11:49:58 +02:00
2019-01-27 18:49:28 +01:00
//It's magic! leave in
2017-11-20 20:42:02 +01:00
delay(100);
2019-01-27 18:49:28 +01:00
#ifdef BATTERY_POWERED
debug("battery powered");
_loop();
digitalWrite(STATUS_LED_PIN, LOW);
2018-12-02 17:06:06 +01:00
criticalBatCheck();
WiFi.mode(WIFI_OFF);
WiFi.forceSleepBegin();
debug("deep sleep");
// 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
2017-10-20 00:13:40 +02:00
}
//*************************************************************************//
2019-01-27 18:49:28 +01:00
#ifdef BATTERY_POWERED
2018-12-02 17:06:06 +01:00
void criticalBatCheck() {
float volt = battery_voltage();
2019-01-06 15:21:03 +01:00
if (volt <= BAT_EMERGENCY_DEEPSLEEP_VOLTAGE) {
debug("Bat Voltage: " + String(volt) + " V");
debug("Low battery, going into deep sleep.");
2019-02-03 19:10:38 +01:00
// Casting to an unsigned int, so it fits into the integer range
ESP.deepSleep(1U * EMERGENCY_SLEEP_S * 1000000); // battery low, shutting down
delay(100);
}
}
2019-01-27 18:49:28 +01:00
#endif
2018-12-02 17:06:06 +01:00
void loop() {
2019-01-27 18:49:28 +01:00
#ifdef BATTERY_POWERED
delay(50);
return;
#endif
2018-12-02 17:06:06 +01:00
#ifdef WEBUPDATER_FEATURE
2018-06-24 16:54:57 +02:00
if (UPDATE_WEBSERVER_INTVERVAL_S * 1000 / DELAY_LOOP_MS <= update_webserver_cnt) {
update_webserver_cnt = 0;
doWebUpdater();
}
2018-12-02 17:06:06 +01:00
#endif
2017-12-09 18:08:37 +01:00
2019-02-02 10:09:00 +01:00
_loop();
2019-01-27 18:49:28 +01:00
//Needed to give WIFI time to function properly
delay(DELAY_LOOP_MS);
update_sensor_cnt++;
2018-12-02 17:06:06 +01:00
#ifdef WEBUPDATER_FEATURE
update_webserver_cnt++;
2018-12-02 17:06:06 +01:00
#endif
}
void _loop() {
2019-01-27 18:49:28 +01:00
#ifndef BATTERY_POWERED
if (UPDATE_SENSOR_INTERVAL_S * 1000 / DELAY_LOOP_MS > update_sensor_cnt) {
return;
}
#endif
#ifdef defined(BATTERY_POWERED) && defined(SENSOR_WIND)
if (energySavingMode() == 1) {
2018-12-02 17:06:06 +01:00
// Disable expensive tasks
sensors[SENSOR_WINDSPEED] = 0;
} else {
sensors[SENSOR_WINDSPEED] = &wind_speed;
}
2019-03-31 16:17:57 +02:00
#endif
update_sensor_cnt = 0;
for (uint8_t i = 0; i < VALUES; i++) {
if (sensors[i]) {
currentSensorData[i] = sensors[i]();
2018-12-02 17:06:06 +01:00
} else {
currentSensorData[i] = nan("no value");
2018-12-02 17:06:06 +01:00
}
}
#ifdef SERIAL_FEATURE
logToSerial(currentSensorData);
#endif
2017-10-20 00:13:40 +02:00
delay(100);
#ifdef INFLUXDB_FEATURE
pushToInfluxDB(DEVICE_NAME, currentSensorData);
#endif
2019-01-27 18:49:28 +01:00
2018-12-02 17:06:06 +01:00
#ifdef WEBUPDATER_FEATURE
setSensorData(currentSensorData);
#endif
}
void logToSerial(float sensorValues[]) {
Serial.println("");
Serial.println("Current readings:");
Serial.println("Temperature: " + String(sensorValues[SENSOR_TEMPERATURE]) + " °C");
Serial.println("Humidity: " + String(sensorValues[SENSOR_HUMIDITY]) + " %");
Serial.println("Light: " + String(sensorValues[SENSOR_LIGHT]) + " Lux");
Serial.println("Windspeed: " + String(sensorValues[SENSOR_WINDSPEED]) + " km/h");
Serial.println("Pressure: " + String(sensorValues[SENSOR_PRESSURE]) + " hPa");
Serial.println("Bat Voltage: " + String(sensorValues[SENSOR_BAT_VOLTAGE]) + " V");
Serial.println("Bat charge state: " + String(sensorValues[SENSOR_BATCHARGESTATE]));
Serial.println("Energy saving: " + String(sensorValues[SENSOR_ESAVEMODE]));
}