weatherstation/firmware/firmware.ino

195 lines
5.2 KiB
Arduino
Raw Normal View History

2017-10-20 00:13:40 +02:00
#include <WiFiClient.h>
#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 <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
2017-11-12 22:34:08 +01:00
2017-11-07 22:20:14 +01:00
#include "config.h"
//*************************************************************************//
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?
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
*
* TODO:
* - https://sminghub.github.io/Sming/about/
* - https://github.com/marvinroger/homie-esp8266
2017-11-07 22:20:14 +01:00
*/
//*************************************************************************//
2018-07-11 11:12:53 +02:00
float currentSensorData[6] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
2017-11-07 22:20:14 +01:00
2018-06-24 16:54:57 +02:00
uint16_t update_sensor_cnt = 0;
uint16_t update_webserver_cnt = 0;
2017-11-12 22:34:08 +01:00
WiFiManager wifiManager;
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
2018-07-11 11:49:58 +02:00
String localIP = "127.0.0.1";
//*************************************************************************//
void setup() {
//WiFi.disconnect(); // erase the wifi credentials
#ifdef DEBUG
Serial.begin(115200);
#endif
2017-11-12 22:34:08 +01:00
// Pin settings
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
digitalWrite(STATUS_LED_PIN, HIGH);
// Establish WiFi connection
2017-11-12 22:34:08 +01:00
String wifiName = "oko-weather-" + String(ESP.getChipId());
wifiManager.setMinimumSignalQuality(15);
2018-06-27 19:43:34 +02:00
wifiManager.setConnectTimeout(60);
2017-11-12 22:34:08 +01:00
if (!wifiManager.autoConnect(wifiName.c_str(), "DEADBEEF")) {
#ifdef DEBUG
Serial.println("WiFi connection failed, we reboot ...");
#endif
ESP.reset();
delay(1000);
2017-11-12 22:34:08 +01:00
}
#ifdef DEBUG
Serial.println("Connected!");
#endif
// Open the InfluxDB session
influxdb.opendb(INFLUXDB_DB, INFLUXDB_USER, INFLUXDB_PASS);
2017-11-20 20:42:02 +01:00
// Initialize and configure the sensors
apds.begin();
apds.enableColor(true);
2017-12-09 18:08:37 +01:00
bool status = bme.begin(0x76);
if (!status) {
#ifdef DEBUG
Serial.println("Could not find a valid BME280 sensor, check wiring!");
#endif
while (1);
}
2017-11-20 20:42:02 +01:00
#ifndef POWERSAVING
setupWebUpdater();
#endif
2018-07-11 11:49:58 +02:00
localIP = WiFi.localIP().toString();
2017-11-20 20:42:02 +01:00
delay(100);
#ifdef POWERSAVING
_loop();
digitalWrite(STATUS_LED_PIN, LOW);
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
}
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
2017-10-20 00:13:40 +02:00
}
//*************************************************************************//
2017-10-20 00:13:40 +02:00
void loop() {
2017-12-09 18:08:37 +01:00
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
}
#ifdef POWERSAVING
delay(50);
return;
#endif
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();
}
2017-12-09 18:08:37 +01:00
_loop();
delay(DELAY_LOOP_MS);
update_sensor_cnt++;
update_webserver_cnt++;
}
void _loop() {
#ifndef POWERSAVING
2018-06-24 16:54:57 +02:00
if (UPDATE_SENSOR_INTERVAL_S * 1000 / DELAY_LOOP_MS <= update_sensor_cnt) {
update_sensor_cnt = 0;
#endif
2018-06-24 16:54:57 +02:00
currentSensorData[SENSOR_TEMPERATURE] = fetchTemperature();
currentSensorData[SENSOR_HUMIDITY] = fetchHumidity();
currentSensorData[SENSOR_LIGHT] = fetchLight();
currentSensorData[SENSOR_WINDSPEED] = fetchWindspeed();
currentSensorData[SENSOR_PRESSURE] = fetchPressure();
2018-07-11 11:12:53 +02:00
currentSensorData[SENSOR_BAT_VOLTAGE] = getBatteryVoltage();
2018-07-11 11:49:58 +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]) + " %");
Serial.println("Light: " + String(currentSensorData[SENSOR_LIGHT]) + " Lumen");
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");
#endif
2017-10-20 00:13:40 +02:00
delay(100);
2018-06-24 16:54:57 +02:00
pushToInfluxDB(DEVICE_NAME, currentSensorData);
#ifndef POWERSAVING
2018-07-11 11:49:58 +02:00
setSensorData(DEVICE_NAME, localIP, currentSensorData);
2018-06-24 16:54:57 +02:00
}
#endif
}
//*************************************************************************//