From f6d8abc723f947ec69aeef1b4ab50fc925e077ac Mon Sep 17 00:00:00 2001 From: Aaron Fischer Date: Mon, 20 Nov 2017 20:15:14 +0100 Subject: [PATCH] Add influxdb and push the sensor data to the database --- firmware/config.h | 22 +++++++++++++++------- firmware/firmware.ino | 11 ++++++++--- firmware/influxdb.ino | 8 +++++--- 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/firmware/config.h b/firmware/config.h index 0ff6695..7459542 100644 --- a/firmware/config.h +++ b/firmware/config.h @@ -1,11 +1,19 @@ #define DEBUG -#define SENSOR_TEMPERATURE 0 -#define SENSOR_HUMIDITY 1 -#define SENSOR_LIGHT 2 -#define SENSOR_WINDSPEED 3 +#define SENSOR_TEMPERATURE 0 +#define SENSOR_HUMIDITY 1 +#define SENSOR_LIGHT 2 +#define SENSOR_WINDSPEED 3 -#define UPDATE_INTERVAL 4 +#define UPDATE_INTERVAL 4 -#define STATUS_LED_PIN BUILTIN_LED -#define ANEMOMETER_PIN D4 +#define STATUS_LED_PIN BUILTIN_LED +#define ANEMOMETER_PIN D4 + +const char *INFLUXDB_HOST = "influxdb.okoyono.de"; +const uint16_t INFLUXDB_PORT = 80; +const char *INFLUXDB_DB = "weatherstation"; +const char *INFLUXDB_USER = "oko"; +const char *INFLUXDB_PASS = "de1873a0d2f8f21f17cf4d8db4f65c59"; + +String DEVICE_NAME = "aaron"; diff --git a/firmware/firmware.ino b/firmware/firmware.ino index aff286a..543502c 100644 --- a/firmware/firmware.ino +++ b/firmware/firmware.ino @@ -3,12 +3,12 @@ #include #include #include +#include #include "config.h" /** * Whishlist: - * - Push sensor values to influxdb * - Webserver for /metrics endpoint (Prometheus) * - Show current sensor values over simple webpage * - Push sensor values to various 3rd party services (https://openweathermap.org/) @@ -27,6 +27,7 @@ float currentSensorData[4] = {0.0, 0.0, 0.0, 0.0}; WiFiManager wifiManager; +Influxdb influxdb(INFLUXDB_HOST, INFLUXDB_PORT); void setup() { #ifdef DEBUG @@ -37,7 +38,7 @@ void setup() { pinMode(STATUS_LED_PIN, OUTPUT); pinMode(ANEMOMETER_PIN, INPUT_PULLUP); - // Establish wifi connection + // Establish WiFi connection String wifiName = "oko-weather-" + String(ESP.getChipId()); wifiManager.setMinimumSignalQuality(15); if (!wifiManager.autoConnect(wifiName.c_str(), "DEADBEEF")) { @@ -50,6 +51,10 @@ void setup() { #ifdef DEBUG Serial.println("Connected!"); #endif + + // Open the InfluxDB session + influxdb.opendb(INFLUXDB_DB, INFLUXDB_USER, INFLUXDB_PASS); + delay(2000); } void loop() { @@ -69,7 +74,7 @@ void loop() { Serial.println("Windspeed: " + String(currentSensorData[SENSOR_WINDSPEED]) + " Km/h"); #endif - pushToInfluxDB(currentSensorData); + pushToInfluxDB(DEVICE_NAME, currentSensorData); delay(UPDATE_INTERVAL*1000); } diff --git a/firmware/influxdb.ino b/firmware/influxdb.ino index c1aa461..4fdee39 100644 --- a/firmware/influxdb.ino +++ b/firmware/influxdb.ino @@ -1,4 +1,6 @@ -void pushToInfluxDB(float sensorValues[]) { - +void pushToInfluxDB(String device, float sensorValues[]) { + influxdb.write("weather,device=" + device + " temperature=" + String(sensorValues[SENSOR_TEMPERATURE])); + influxdb.write("weather,device=" + device + " humidity=" + String(sensorValues[SENSOR_HUMIDITY])); + influxdb.write("weather,device=" + device + " light=" + String(sensorValues[SENSOR_LIGHT])); + influxdb.write("weather,device=" + device + " windspeed=" + String(sensorValues[SENSOR_WINDSPEED])); } -