From ed913d740e1ca95f40d2b65018b6f2aa6ecd333f Mon Sep 17 00:00:00 2001 From: Florian Eitel Date: Sun, 4 Aug 2019 12:08:31 +0200 Subject: [PATCH] Make influxdb and serial feature configurable --- firmware/config_user.h.example | 2 ++ firmware/firmware.ino | 43 +++++++++++++++++++--------------- firmware/influxdb.ino | 18 +++++++++----- 3 files changed, 38 insertions(+), 25 deletions(-) diff --git a/firmware/config_user.h.example b/firmware/config_user.h.example index 7bdc598..d5e2ad3 100644 --- a/firmware/config_user.h.example +++ b/firmware/config_user.h.example @@ -9,6 +9,8 @@ // Enable/Disable features //#define WEBUPDATER_FEATURE +#define INFLUXDB_FEATURE +#define SERIAL_FEATURE #define BATTERY_POWERED #define SENSOR_WIND #define SENSOR_APDS9960 diff --git a/firmware/firmware.ino b/firmware/firmware.ino index 3111d48..e53531a 100644 --- a/firmware/firmware.ino +++ b/firmware/firmware.ino @@ -17,7 +17,6 @@ // folder in order to use the libs. (File -> Preferences -> Sketchbook Location) #include // WiFiClient #include // WiFiManager -#include // https://github.com/hwwong/ESP8266Influxdb auchecken und den ordner in das arduino\library verzeichnis kopieren // Project includes #include "config.h" @@ -55,7 +54,6 @@ uint16_t update_sensor_cnt = 0; uint16_t update_webserver_cnt = 0; WiFiManager wifiManager; -Influxdb influxdb(INFLUXDB_HOST, INFLUXDB_PORT); #ifdef WEBUPDATER_FEATURE String localIP = "127.0.0.1"; @@ -76,7 +74,7 @@ void setup() { //delay(2000); //ESP.reset(); -#ifdef DEBUG +#if defined(DEBUG) || defined(SERIAL_FEATURE) Serial.begin(115200); #endif @@ -108,8 +106,9 @@ void setup() { debug("Connected!"); - // Init variables to influxdb config - doesn't talk to database - influxdb.opendb(INFLUXDB_DB, INFLUXDB_USER, INFLUXDB_PASS); +#ifdef INFLUXDB_FEATURE + influxdb_begin(); +#endif // Initialize and configure the sensors #ifdef SENSOR_APDS9930 @@ -242,24 +241,30 @@ void _loop() { } } - debug(""); - debug("Current readings:"); - debug("Temperature: " + String(currentSensorData[SENSOR_TEMPERATURE]) + " °C"); - debug("Humidity: " + String(currentSensorData[SENSOR_HUMIDITY]) + " %"); - debug("Light: " + String(currentSensorData[SENSOR_LIGHT]) + " Lux"); - debug("Windspeed: " + String(currentSensorData[SENSOR_WINDSPEED]) + " km/h"); - debug("Pressure: " + String(currentSensorData[SENSOR_PRESSURE]) + " hPa"); - debug("Bat Voltage: " + String(currentSensorData[SENSOR_BAT_VOLTAGE]) + " V"); - debug("Bat charge state: " + String(currentSensorData[SENSOR_BATCHARGESTATE])); - debug("Energy saving: " + String(currentSensorData[SENSOR_ESAVEMODE])); +#ifdef SERIAL_FEATURE + logToSerial(currentSensorData); +#endif - delay(100); + delay(100); - pushToInfluxDB(DEVICE_NAME, currentSensorData); +#ifdef INFLUXDB_FEATURE + pushToInfluxDB(DEVICE_NAME, currentSensorData); +#endif #ifdef WEBUPDATER_FEATURE - setSensorData(DEVICE_NAME, localIP, currentSensorData); + setSensorData(DEVICE_NAME, localIP, currentSensorData); #endif } -//*************************************************************************// +void logToSerial(float sensorValues[]) { + Serial.println(""); + Serial.println("Current readings:"); + Serial.println("Temperature: " + String(sensorValues[SENSOR_TEMPERATURE]) + " °C"); + Serial.println("Humidity: " + String(sensorValues[SENSOR_HUMIDITY]) + " %"); + Serial.println("Light: " + String(sensorValues[SENSOR_LIGHT]) + " Lux"); + Serial.println("Windspeed: " + String(sensorValues[SENSOR_WINDSPEED]) + " km/h"); + Serial.println("Pressure: " + String(sensorValues[SENSOR_PRESSURE]) + " hPa"); + Serial.println("Bat Voltage: " + String(sensorValues[SENSOR_BAT_VOLTAGE]) + " V"); + Serial.println("Bat charge state: " + String(sensorValues[SENSOR_BATCHARGESTATE])); + Serial.println("Energy saving: " + String(sensorValues[SENSOR_ESAVEMODE])); +} diff --git a/firmware/influxdb.ino b/firmware/influxdb.ino index d63e166..8d1ebde 100644 --- a/firmware/influxdb.ino +++ b/firmware/influxdb.ino @@ -1,5 +1,13 @@ -void pushToInfluxDB(String device, float sensorValues[]) { +#include // https://github.com/hwwong/ESP8266Influxdb +Influxdb _influxdb(INFLUXDB_HOST, INFLUXDB_PORT); + +void influxdb_begin() { + // Init variables to influxdb config - doesn't talk to database + _influxdb.opendb(INFLUXDB_DB, INFLUXDB_USER, INFLUXDB_PASS); +} + +void pushToInfluxDB(String device, float sensorValues[]) { uint8_t tries = 0; boolean addComma = false; @@ -59,12 +67,10 @@ void pushToInfluxDB(String device, float sensorValues[]) { addComma = true; } -#ifdef DEBUG - Serial.println(msg); -#endif + debug(msg); do { tries++; - influxdb.write(msg); - } while (influxdb.response() != DB_SUCCESS and tries < 5); + _influxdb.write(msg); + } while (_influxdb.response() != DB_SUCCESS and tries < 5); }