151 lines
4.7 KiB
C++
151 lines
4.7 KiB
C++
|
|
#include <WiFiClient.h>
|
|
#include <ESP8266mDNS.h>
|
|
#include <ESP8266HTTPUpdateServer.h>
|
|
#include <ESP8266WiFi.h>
|
|
#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
|
|
|
|
#include "config.h"
|
|
|
|
//*************************************************************************//
|
|
|
|
/**
|
|
* Whishlist:
|
|
* - Webserver for /metrics endpoint (Prometheus)
|
|
* - Show current sensor values over simple webpage
|
|
* - Push sensor values to various 3rd party services (https://openweathermap.org/)
|
|
* - MQTT?
|
|
*
|
|
* - Buffer sensor values if there is no WIFI connection
|
|
* - Configure weather station over http webinterface
|
|
* - OTA firmware update
|
|
*
|
|
* TODO:
|
|
* - https://sminghub.github.io/Sming/about/
|
|
* - https://github.com/marvinroger/homie-esp8266
|
|
*/
|
|
|
|
//*************************************************************************//
|
|
|
|
float currentSensorData[6] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
|
|
|
|
uint16_t update_sensor_cnt = 0;
|
|
uint16_t update_webserver_cnt = 0;
|
|
|
|
WiFiManager wifiManager;
|
|
Influxdb influxdb(INFLUXDB_HOST, INFLUXDB_PORT);
|
|
Adafruit_APDS9960 apds;
|
|
Adafruit_BME280 bme;
|
|
|
|
String localIP = "127.0.0.1";
|
|
|
|
//*************************************************************************//
|
|
|
|
void setup() {
|
|
|
|
//WiFi.disconnect(); // erase the wifi credentials
|
|
|
|
#ifdef DEBUG
|
|
Serial.begin(115200);
|
|
#endif
|
|
|
|
// Pin settings
|
|
pinMode(STATUS_LED_PIN, OUTPUT);
|
|
pinMode(ANEMOMETER_PIN, INPUT_PULLUP);
|
|
pinMode(A0, INPUT);
|
|
|
|
// Establish WiFi connection
|
|
String wifiName = "oko-weather-" + String(ESP.getChipId());
|
|
wifiManager.setMinimumSignalQuality(15);
|
|
wifiManager.setConnectTimeout(60);
|
|
if (!wifiManager.autoConnect(wifiName.c_str(), "DEADBEEF")) {
|
|
#ifdef DEBUG
|
|
Serial.println("WiFi connection failed, we reboot ...");
|
|
#endif
|
|
ESP.reset();
|
|
delay(1000);
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
Serial.println("Connected!");
|
|
#endif
|
|
|
|
// Open the InfluxDB session
|
|
influxdb.opendb(INFLUXDB_DB, INFLUXDB_USER, INFLUXDB_PASS);
|
|
|
|
// Initialize and configure the sensors
|
|
apds.begin();
|
|
apds.enableColor(true);
|
|
|
|
bool status = bme.begin(0x76);
|
|
if (!status) {
|
|
Serial.println("Could not find a valid BME280 sensor, check wiring!");
|
|
while (1);
|
|
}
|
|
|
|
setupWebUpdater();
|
|
|
|
localIP = WiFi.localIP().toString();
|
|
|
|
delay(100);
|
|
|
|
digitalWrite(STATUS_LED_PIN, LOW);
|
|
}
|
|
|
|
//*************************************************************************//
|
|
|
|
void loop() {
|
|
|
|
if (UPDATE_WEBSERVER_INTVERVAL_S * 1000 / DELAY_LOOP_MS <= update_webserver_cnt) {
|
|
update_webserver_cnt = 0;
|
|
doWebUpdater();
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
digitalWrite(STATUS_LED_PIN, HIGH);
|
|
#endif
|
|
|
|
if (UPDATE_SENSOR_INTERVAL_S * 1000 / DELAY_LOOP_MS <= update_sensor_cnt) {
|
|
update_sensor_cnt = 0;
|
|
currentSensorData[SENSOR_TEMPERATURE] = fetchTemperature();
|
|
currentSensorData[SENSOR_HUMIDITY] = fetchHumidity();
|
|
currentSensorData[SENSOR_LIGHT] = fetchLight();
|
|
currentSensorData[SENSOR_WINDSPEED] = fetchWindspeed();
|
|
currentSensorData[SENSOR_PRESSURE] = fetchPressure();
|
|
currentSensorData[SENSOR_BAT_VOLTAGE] = getBatteryVoltage();
|
|
|
|
if (currentSensorData[SENSOR_BAT_VOLTAGE] <= 3.7)
|
|
{
|
|
ESP.deepSleep(4294967295); // battery low, shutting down
|
|
}
|
|
#ifdef DEBUG
|
|
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");
|
|
Serial.println("Bat Voltage: " + String(currentSensorData[SENSOR_BAT_VOLTAGE]) + " V");
|
|
#endif
|
|
|
|
pushToInfluxDB(DEVICE_NAME, currentSensorData);
|
|
setSensorData(DEVICE_NAME, localIP, currentSensorData);
|
|
}
|
|
|
|
//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++;
|
|
}
|
|
|
|
//*************************************************************************//
|
|
|