2019-02-03 16:12:19 +01:00
|
|
|
// Standard ESP8266 libs
|
2018-06-23 21:49:11 +02:00
|
|
|
#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>
|
|
|
|
|
2019-02-03 16:12:19 +01:00
|
|
|
// External libs
|
|
|
|
// We decided to put these libs inside the project folder, because we have a lot
|
|
|
|
// of issues with finding the right libs (some libs need a specific version, some
|
|
|
|
// libs have no unique name, some libs are broken, ...). This is not a fine
|
|
|
|
// software engineering practice, but it was too much of a hassle to get this right,
|
|
|
|
// so we decided to put everything in version control. This way, we also can control
|
|
|
|
// the version of the lib, which makes the build process of this project more
|
|
|
|
// robust to changes.
|
|
|
|
// IMPORTANT: You need to set the sketch location of your Arduino IDE to the firmware
|
|
|
|
// folder in order to use the libs. (File -> Preferences -> Sketchbook Location)
|
|
|
|
#include <WiFiClient.h> // WiFiClient
|
|
|
|
#include <WiFiManager.h> // WiFiManager
|
|
|
|
#include <ESP8266Influxdb.h> // https://github.com/hwwong/ESP8266Influxdb auchecken und den ordner in das arduino\library verzeichnis kopieren
|
|
|
|
#include <Adafruit_Sensor.h> // Package Adafruit Unified Sensor
|
|
|
|
#include <Adafruit_APDS9960.h> // Adafruit APDS9960 - https://www.makerfabs.com/index.php?route=product/product&product_id=281
|
|
|
|
#include <Adafruit_BME280.h> // BME280 - https://www.roboter-bausatz.de/1704/bmp280-barometer-luftdrucksensor?gclid=EAIaIQobChMIlpumj8Hp2wIVFWYbCh01PgmFEAQYAyABEgIwBvD_BwE
|
|
|
|
|
|
|
|
// Project includes
|
2017-11-07 22:20:14 +01:00
|
|
|
#include "config.h"
|
2019-02-03 16:12:19 +01:00
|
|
|
|
|
|
|
// IMPORTANT: If you compile the sourcecode and it can't find this file, this is
|
|
|
|
// indended :) You need to create a config_user.h with your own settings. Use the
|
|
|
|
// config_user.h.example as a starting point.
|
2019-01-08 22:41:24 +01:00
|
|
|
#include "config_user.h"
|
2017-11-07 22:20:14 +01:00
|
|
|
|
2018-06-24 12:00:51 +02:00
|
|
|
//*************************************************************************//
|
|
|
|
|
2017-11-07 22:20:14 +01:00
|
|
|
/**
|
|
|
|
* Whishlist:
|
|
|
|
* - Webserver for /metrics endpoint (Prometheus)
|
|
|
|
* - Show current sensor values over simple webpage
|
2017-11-17 16:57:09 +01:00
|
|
|
* - Push sensor values to various 3rd party services (https://openweathermap.org/)
|
2017-11-07 22:36:18 +01:00
|
|
|
* - MQTT?
|
2019-01-27 18:49:28 +01:00
|
|
|
*
|
2017-11-07 22:20:14 +01:00
|
|
|
* - Buffer sensor values if there is no WIFI connection
|
|
|
|
* - Configure weather station over http webinterface
|
2017-11-07 22:36:18 +01:00
|
|
|
* - OTA firmware update
|
2019-01-27 18:49:28 +01:00
|
|
|
*
|
2017-11-07 22:36:18 +01:00
|
|
|
* TODO:
|
2019-01-27 18:49:28 +01:00
|
|
|
* - https://sminghub.github.io/Sming/about/
|
2017-11-07 22:36:18 +01:00
|
|
|
* - https://github.com/marvinroger/homie-esp8266
|
2017-11-07 22:20:14 +01:00
|
|
|
*/
|
|
|
|
|
2018-06-24 12:00:51 +02:00
|
|
|
//*************************************************************************//
|
|
|
|
|
2019-02-02 10:09:00 +01:00
|
|
|
float currentSensorData[8] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
|
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-12-02 17:06:06 +01:00
|
|
|
uint16_t energySavingIterations = 0;
|
2018-06-24 16:54:57 +02:00
|
|
|
|
2017-11-12 22:34:08 +01:00
|
|
|
WiFiManager wifiManager;
|
2017-11-20 20:15:14 +01:00
|
|
|
Influxdb influxdb(INFLUXDB_HOST, INFLUXDB_PORT);
|
2017-11-20 20:42:02 +01:00
|
|
|
Adafruit_APDS9960 apds;
|
|
|
|
Adafruit_BME280 bme;
|
2017-11-07 22:20:14 +01:00
|
|
|
|
2019-01-27 18:49:28 +01:00
|
|
|
#ifdef WEBUPDATER_FEATURE
|
2018-07-11 11:49:58 +02:00
|
|
|
String localIP = "127.0.0.1";
|
2019-01-27 18:49:28 +01:00
|
|
|
#endif
|
2018-06-24 12:00:51 +02:00
|
|
|
//*************************************************************************//
|
2018-06-23 21:49:11 +02:00
|
|
|
|
2018-06-23 20:13:33 +02:00
|
|
|
void setup() {
|
2018-06-24 12:00:51 +02:00
|
|
|
//WiFi.disconnect(); // erase the wifi credentials
|
|
|
|
|
2018-06-23 20:13:33 +02:00
|
|
|
#ifdef DEBUG
|
|
|
|
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);
|
2018-06-24 12:00:51 +02:00
|
|
|
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);
|
2018-07-12 22:30:30 +02:00
|
|
|
|
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
|
2017-11-20 20:15:14 +01:00
|
|
|
// Establish WiFi connection
|
2019-01-27 18:49:28 +01:00
|
|
|
String wifiName = "oko-weather-" + DEVICE_NAME;
|
2018-07-19 20:13:08 +02:00
|
|
|
|
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
|
2018-07-19 20:13:08 +02:00
|
|
|
|
2017-11-12 22:34:08 +01:00
|
|
|
if (!wifiManager.autoConnect(wifiName.c_str(), "DEADBEEF")) {
|
2018-06-23 20:13:33 +02:00
|
|
|
#ifdef DEBUG
|
2019-02-02 11:15:27 +01:00
|
|
|
Serial.println("WiFi connection failed, going into deep sleep ...");
|
2018-06-23 20:13:33 +02:00
|
|
|
#endif
|
2019-01-27 18:49:28 +01:00
|
|
|
// If autoconnect to WLAN failed and no client connected, go to deep sleep
|
2018-07-19 20:13:08 +02:00
|
|
|
ESP.deepSleep(POWERSAVING_SLEEP_S * 1000000, WAKE_RF_DEFAULT);
|
|
|
|
delay(100);
|
2017-11-12 22:34:08 +01:00
|
|
|
}
|
2018-06-23 20:13:33 +02:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
Serial.println("Connected!");
|
|
|
|
#endif
|
2017-11-20 20:15:14 +01:00
|
|
|
|
2019-01-27 18:49:28 +01:00
|
|
|
// Init variables to influxdb config - doesn't talk to database
|
2017-11-20 20:15:14 +01:00
|
|
|
influxdb.opendb(INFLUXDB_DB, INFLUXDB_USER, INFLUXDB_PASS);
|
2017-11-20 20:42:02 +01:00
|
|
|
|
|
|
|
// Initialize and configure the sensors
|
2019-01-27 18:49:28 +01:00
|
|
|
//Light Sensor
|
2017-11-20 20:42:02 +01:00
|
|
|
apds.begin();
|
|
|
|
apds.enableColor(true);
|
2017-12-09 18:08:37 +01:00
|
|
|
|
2019-01-27 18:49:28 +01:00
|
|
|
//Temperature + pressure
|
2017-12-09 18:08:37 +01:00
|
|
|
bool status = bme.begin(0x76);
|
2018-06-23 20:13:33 +02:00
|
|
|
if (!status) {
|
2018-07-12 22:30:30 +02:00
|
|
|
#ifdef DEBUG
|
2018-06-23 20:13:33 +02:00
|
|
|
Serial.println("Could not find a valid BME280 sensor, check wiring!");
|
2018-07-12 22:30:30 +02:00
|
|
|
#endif
|
2019-01-27 18:49:28 +01:00
|
|
|
//#warning TODO: FIXME while (1);
|
2018-06-23 20:13:33 +02:00
|
|
|
}
|
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
|
2018-06-24 15:53:58 +02:00
|
|
|
setupWebUpdater();
|
2018-07-11 11:49:58 +02:00
|
|
|
localIP = 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);
|
2018-06-24 15:53:58 +02:00
|
|
|
|
2019-01-27 18:49:28 +01:00
|
|
|
#ifdef BATTERY_POWERED
|
2018-07-12 22:30:30 +02:00
|
|
|
_loop();
|
|
|
|
|
2018-06-24 15:53:58 +02:00
|
|
|
digitalWrite(STATUS_LED_PIN, LOW);
|
2018-07-12 22:30:30 +02:00
|
|
|
|
2018-12-02 17:06:06 +01:00
|
|
|
criticalBatCheck();
|
2018-07-13 10:21:59 +02:00
|
|
|
|
2018-07-12 22:30:30 +02:00
|
|
|
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);
|
2018-07-19 20:13:08 +02:00
|
|
|
delay(100);
|
2018-07-12 22:30:30 +02:00
|
|
|
#endif
|
2017-10-20 00:13:40 +02:00
|
|
|
}
|
|
|
|
|
2018-06-24 12:00:51 +02:00
|
|
|
//*************************************************************************//
|
|
|
|
|
2019-01-27 18:49:28 +01:00
|
|
|
#ifdef BATTERY_POWERED
|
2018-12-02 17:06:06 +01:00
|
|
|
void criticalBatCheck() {
|
2019-01-06 15:21:03 +01:00
|
|
|
float volt = getBatteryVoltage();
|
|
|
|
if (volt <= BAT_EMERGENCY_DEEPSLEEP_VOLTAGE) {
|
2018-07-13 10:02:50 +02:00
|
|
|
#ifdef DEBUG
|
2019-01-06 15:21:03 +01:00
|
|
|
Serial.println("Bat Voltage: " + String(volt) + " V");
|
2018-07-13 10:02:50 +02:00
|
|
|
Serial.println("Low battery, going into deep sleep.");
|
|
|
|
#endif
|
2018-12-02 17:06:06 +01:00
|
|
|
ESP.deepSleep(EMERGENCY_SLEEP_S * 1000000); // battery low, shutting down
|
2018-07-19 20:13:08 +02:00
|
|
|
delay(100);
|
2018-07-13 10:02:50 +02:00
|
|
|
}
|
2018-07-13 20:20:56 +02:00
|
|
|
}
|
2019-01-27 18:49:28 +01:00
|
|
|
#endif
|
2018-07-13 20:20:56 +02:00
|
|
|
|
2019-01-27 18:49:28 +01:00
|
|
|
#ifdef BATTERY_POWERED
|
2018-12-02 17:06:06 +01:00
|
|
|
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
|
2019-01-06 18:13:37 +01:00
|
|
|
energySavingIterations = ENERGY_SAVING_ITERATIONS;
|
2018-07-13 20:20:56 +02:00
|
|
|
|
2018-12-02 17:06:06 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2019-01-27 18:49:28 +01:00
|
|
|
|
2018-12-02 17:06:06 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2019-01-27 18:49:28 +01:00
|
|
|
#endif
|
2018-12-02 17:06:06 +01:00
|
|
|
|
2018-07-13 20:20:56 +02:00
|
|
|
void loop() {
|
2019-01-27 18:49:28 +01:00
|
|
|
#ifdef BATTERY_POWERED
|
2018-07-12 22:30:30 +02:00
|
|
|
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
|
|
|
|
2018-07-12 22:30:30 +02:00
|
|
|
_loop();
|
|
|
|
|
2019-01-27 18:49:28 +01:00
|
|
|
//Needed to give WIFI time to function properly
|
2018-07-12 22:30:30 +02:00
|
|
|
delay(DELAY_LOOP_MS);
|
|
|
|
|
|
|
|
update_sensor_cnt++;
|
2018-12-02 17:06:06 +01:00
|
|
|
|
|
|
|
#ifdef WEBUPDATER_FEATURE
|
2018-07-12 22:30:30 +02:00
|
|
|
update_webserver_cnt++;
|
2018-12-02 17:06:06 +01:00
|
|
|
#endif
|
2018-07-12 22:30:30 +02:00
|
|
|
}
|
2018-06-23 20:13:33 +02:00
|
|
|
|
2018-07-12 22:30:30 +02:00
|
|
|
void _loop() {
|
|
|
|
|
2019-01-27 18:49:28 +01:00
|
|
|
#ifndef BATTERY_POWERED
|
2018-06-24 16:54:57 +02:00
|
|
|
if (UPDATE_SENSOR_INTERVAL_S * 1000 / DELAY_LOOP_MS <= update_sensor_cnt) {
|
|
|
|
update_sensor_cnt = 0;
|
2018-07-12 22:30:30 +02:00
|
|
|
#endif
|
2018-06-24 16:54:57 +02:00
|
|
|
currentSensorData[SENSOR_TEMPERATURE] = fetchTemperature();
|
|
|
|
currentSensorData[SENSOR_HUMIDITY] = fetchHumidity();
|
|
|
|
currentSensorData[SENSOR_LIGHT] = fetchLight();
|
2019-01-08 21:07:16 +01:00
|
|
|
currentSensorData[SENSOR_PRESSURE] = fetchPressure();
|
2019-01-27 18:49:28 +01:00
|
|
|
#ifdef BATTERY_POWERED
|
2019-02-02 10:09:00 +01:00
|
|
|
currentSensorData[SENSOR_BAT_VOLTAGE] = getBatteryVoltage();
|
|
|
|
currentSensorData[SENSOR_BATCHARGESTATE] = isBatCharging();
|
2019-01-27 18:49:28 +01:00
|
|
|
#else
|
2019-02-02 10:09:00 +01:00
|
|
|
currentSensorData[SENSOR_BAT_VOLTAGE] = 0xFFFFFFFF;
|
|
|
|
currentSensorData[SENSOR_BATCHARGESTATE] = 0xFFFFFFFF;
|
2019-01-27 18:49:28 +01:00
|
|
|
#endif
|
2018-12-02 17:06:06 +01:00
|
|
|
// Disable expensive tasks
|
|
|
|
if (energySavingMode() == 0) {
|
2019-01-08 21:07:16 +01:00
|
|
|
currentSensorData[SENSOR_WINDSPEED] = fetchWindspeed();
|
2019-02-02 14:01:13 +01:00
|
|
|
currentSensorData[SENSOR_ESAVEMODE] = ENERGY_SAVE_MODE_DISABLED;
|
2018-12-02 17:06:06 +01:00
|
|
|
} else {
|
2019-01-27 18:49:28 +01:00
|
|
|
currentSensorData[SENSOR_WINDSPEED] = 0xFFFFFFFF;
|
2019-02-02 14:01:13 +01:00
|
|
|
currentSensorData[SENSOR_ESAVEMODE] = ENERGY_SAVE_MODE_ENABLED;
|
2018-12-02 17:06:06 +01:00
|
|
|
}
|
2018-07-11 11:49:58 +02:00
|
|
|
|
2018-06-23 20:13:33 +02:00
|
|
|
#ifdef DEBUG
|
2018-06-24 16:54:57 +02:00
|
|
|
Serial.println("");
|
|
|
|
//Serial.println("Current readings:");
|
|
|
|
Serial.println("Temperature: " + String(currentSensorData[SENSOR_TEMPERATURE]) + " °C");
|
|
|
|
Serial.println("Humidity: " + String(currentSensorData[SENSOR_HUMIDITY]) + " %");
|
2019-01-27 18:49:28 +01:00
|
|
|
Serial.println("Light: " + String(currentSensorData[SENSOR_LIGHT]) + " Lux");
|
2018-06-24 16:54:57 +02:00
|
|
|
Serial.println("Windspeed: " + String(currentSensorData[SENSOR_WINDSPEED]) + " km/h");
|
|
|
|
Serial.println("Pressure: " + String(currentSensorData[SENSOR_PRESSURE]) + " hPa");
|
2018-07-11 11:12:53 +02:00
|
|
|
Serial.println("Bat Voltage: " + String(currentSensorData[SENSOR_BAT_VOLTAGE]) + " V");
|
2019-02-02 10:09:00 +01:00
|
|
|
Serial.println("Bat charge state: " + String(currentSensorData[SENSOR_BATCHARGESTATE]));
|
|
|
|
Serial.println("Energy saving: " + String(currentSensorData[SENSOR_ESAVEMODE]));
|
2018-06-23 20:13:33 +02:00
|
|
|
#endif
|
2017-10-20 00:13:40 +02:00
|
|
|
|
2018-07-12 22:30:30 +02:00
|
|
|
delay(100);
|
|
|
|
|
2018-06-24 16:54:57 +02:00
|
|
|
pushToInfluxDB(DEVICE_NAME, currentSensorData);
|
2019-01-27 18:49:28 +01:00
|
|
|
|
2018-12-02 17:06:06 +01:00
|
|
|
#ifdef WEBUPDATER_FEATURE
|
2019-01-27 18:49:28 +01:00
|
|
|
#ifndef BATTERY_POWERED
|
2018-07-11 11:49:58 +02:00
|
|
|
setSensorData(DEVICE_NAME, localIP, currentSensorData);
|
2018-06-24 16:54:57 +02:00
|
|
|
}
|
2018-07-12 22:30:30 +02:00
|
|
|
#endif
|
2018-12-02 17:06:06 +01:00
|
|
|
#endif
|
2018-06-23 20:13:33 +02:00
|
|
|
|
2018-06-24 12:00:51 +02:00
|
|
|
}
|
|
|
|
|
2019-02-03 16:12:19 +01:00
|
|
|
//*************************************************************************//
|