Add influxdb and push the sensor data to the database

This commit is contained in:
Aaron Fischer 2017-11-20 20:15:14 +01:00
parent dd0e2b2e40
commit f6d8abc723
3 changed files with 28 additions and 13 deletions

View File

@ -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";

View File

@ -3,12 +3,12 @@
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#include <ESP8266Influxdb.h>
#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);
}

View File

@ -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]));
}