weatherstation/firmware/firmware.ino

95 lines
2.7 KiB
Arduino
Raw Normal View History

2017-10-20 00:13:40 +02:00
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>
#include <ESP8266Influxdb.h>
2017-11-20 20:42:02 +01:00
#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
2017-11-12 22:34:08 +01:00
2017-11-07 22:20:14 +01:00
#include "config.h"
/**
* 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
*/
2017-11-12 21:37:55 +01:00
float currentSensorData[4] = {0.0, 0.0, 0.0, 0.0};
2017-11-07 22:20:14 +01:00
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
2017-11-12 21:37:55 +01:00
void setup() {
2017-11-07 22:20:14 +01:00
#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);
2017-11-12 22:34:08 +01:00
// Establish WiFi connection
2017-11-12 22:34:08 +01:00
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);
delay(2000);
2017-11-20 20:42:02 +01:00
// Initialize and configure the sensors
//apds.begin();
//apds.enableColor(true);
//bme.begin();
2017-11-20 20:42:02 +01:00
delay(100);
2017-10-20 00:13:40 +02:00
}
void loop() {
2017-11-12 21:37:55 +01:00
digitalWrite(STATUS_LED_PIN, LOW);
2017-11-07 22:20:14 +01:00
currentSensorData[SENSOR_TEMPERATURE] = fetchTemperature();
//currentSensorData[SENSOR_HUMIDITY] = fetchHumidity();
2017-12-09 18:07:46 +01:00
//currentSensorData[SENSOR_LIGHT] = fetchLight();
//currentSensorData[SENSOR_WINDSPEED] = fetchWindspeed();
//currentSensorData[SENSOR_PRESSURE] = fetchPressure();
2017-11-07 22:20:14 +01:00
#ifdef DEBUG
Serial.println("");
Serial.println("Current readings:");
2017-11-12 21:37:55 +01:00
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");
2017-11-07 22:20:14 +01:00
#endif
2017-10-20 00:13:40 +02:00
pushToInfluxDB(DEVICE_NAME, currentSensorData);
2017-11-07 22:20:14 +01:00
delay(UPDATE_INTERVAL*1000);
}