Make influxdb and serial feature configurable

This commit is contained in:
Florian Eitel 2019-08-04 12:08:31 +02:00
parent be334245ff
commit ed913d740e
No known key found for this signature in database
GPG key ID: 9987EAFEF6F686BB
3 changed files with 38 additions and 25 deletions

View file

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

View file

@ -17,7 +17,6 @@
// folder in order to use the libs. (File -> Preferences -> Sketchbook Location)
#include <WiFiClient.h> // WiFiClient
#include <WiFiManager.h> // WiFiManager
#include <ESP8266Influxdb.h> // 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);
#ifdef INFLUXDB_FEATURE
pushToInfluxDB(DEVICE_NAME, currentSensorData);
#endif
#ifdef WEBUPDATER_FEATURE
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]));
}

View file

@ -1,5 +1,13 @@
void pushToInfluxDB(String device, float sensorValues[]) {
#include <ESP8266Influxdb.h> // 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);
}