weatherstation/firmware/firmware.ino
2017-12-09 18:08:37 +01:00

108 lines
3.1 KiB
C++
Executable file

#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#include <ESP8266Influxdb.h> // https://github.com/hwwong/ESP8266Influxdb auchecken und code+header in das firmware verzeichnis kopieren
#include <Adafruit_Sensor.h> // Adafruit Unified Sensor
#include <Adafruit_APDS9960.h> // https://github.com/adafruit/Adafruit_APDS9960
#include <Adafruit_BME280.h> // https://github.com/adafruit/Adafruit_BME280_Library
#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[4] = {0.0, 0.0, 0.0, 0.0};
WiFiManager wifiManager;
Influxdb influxdb(INFLUXDB_HOST, INFLUXDB_PORT);
Adafruit_APDS9960 apds;
Adafruit_BME280 bme;
void setup() {
#ifdef DEBUG
Serial.begin(115200);
#endif
// Pin settings
pinMode(STATUS_LED_PIN, OUTPUT);
pinMode(ANEMOMETER_PIN, INPUT_PULLUP);
// Establish WiFi connection
String wifiName = "oko-weather-" + String(ESP.getChipId());
wifiManager.setMinimumSignalQuality(15);
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);
}
delay(100);
}
void loop() {
digitalWrite(STATUS_LED_PIN, LOW);
#ifdef DEBUG
Serial.println("---");
digitalWrite(STATUS_LED_PIN, HIGH);
#endif
currentSensorData[SENSOR_TEMPERATURE] = fetchTemperature();
Serial.print("*");
currentSensorData[SENSOR_HUMIDITY] = fetchHumidity();
Serial.print("*");
//currentSensorData[SENSOR_LIGHT] = fetchLight();
Serial.print("*");
currentSensorData[SENSOR_WINDSPEED] = fetchWindspeed();
Serial.print("*");
currentSensorData[SENSOR_PRESSURE] = fetchPressure();
Serial.println("*");
#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");
#endif
pushToInfluxDB(DEVICE_NAME, currentSensorData);
delay(UPDATE_INTERVAL*1000);
}