236 lines
6.2 KiB
C++
236 lines
6.2 KiB
C++
|
|
#include "config_user.h"
|
|
|
|
#ifdef INFLUXDB_FEATURE
|
|
|
|
//*************************************************************************//
|
|
|
|
#if INFLUXDB_VERSION == 1
|
|
|
|
#include <ESP8266Influxdb.h> // https://github.com/hwwong/ESP8266Influxdb
|
|
|
|
Influxdb _influxdb(INFLUXDB_HOST, INFLUXDB_PORT);
|
|
|
|
const String msg = "weather,device=" + device + " ";
|
|
|
|
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;
|
|
|
|
if (!(isnan(sensorValues[SENSOR_TEMPERATURE])))
|
|
{
|
|
msg += "temperature=" + String(sensorValues[SENSOR_TEMPERATURE]);
|
|
addComma = true;
|
|
}
|
|
if (!(isnan(sensorValues[SENSOR_HUMIDITY])))
|
|
{
|
|
if (true == addComma)
|
|
msg += ",";
|
|
msg += "humidity=" + String(sensorValues[SENSOR_HUMIDITY]);
|
|
addComma = true;
|
|
}
|
|
if (!(isnan(sensorValues[SENSOR_LIGHT])))
|
|
{
|
|
if (true == addComma)
|
|
msg += ",";
|
|
msg += "light=" + String(sensorValues[SENSOR_LIGHT]);
|
|
addComma = true;
|
|
}
|
|
if (!(isnan(sensorValues[SENSOR_WINDSPEED])))
|
|
{
|
|
if (true == addComma)
|
|
msg += ",";
|
|
msg += "windspeed=" + String(sensorValues[SENSOR_WINDSPEED]);
|
|
addComma = true;
|
|
}
|
|
if (!(isnan(sensorValues[SENSOR_PRESSURE])))
|
|
{
|
|
if (true == addComma)
|
|
msg += ",";
|
|
msg += "pressure=" + String(sensorValues[SENSOR_PRESSURE]);
|
|
addComma = true;
|
|
}
|
|
if (!(isnan(sensorValues[SENSOR_BAT_VOLTAGE])))
|
|
{
|
|
if (true == addComma)
|
|
msg += ",";
|
|
msg += "batvoltage=" + String(sensorValues[SENSOR_BAT_VOLTAGE]);
|
|
addComma = true;
|
|
}
|
|
if (!(isnan(sensorValues[SENSOR_ESAVEMODE])))
|
|
{
|
|
if (true == addComma)
|
|
msg += ",";
|
|
msg += "esavemode=" + String(sensorValues[SENSOR_ESAVEMODE]);
|
|
addComma = true;
|
|
}
|
|
if (!(isnan(sensorValues[SENSOR_BATCHARGESTATE])))
|
|
{
|
|
if (true == addComma)
|
|
msg += ",";
|
|
msg += "batchargestate=" + String(sensorValues[SENSOR_BATCHARGESTATE]);
|
|
addComma = true;
|
|
}
|
|
|
|
#ifdef LOG_MILLIS_TO_INFLUXDB
|
|
if (true == addComma)
|
|
msg += ",";
|
|
msg += "timestamp=" + String(millis());
|
|
#endif
|
|
|
|
debug(msg);
|
|
|
|
do {
|
|
tries++;
|
|
_influxdb.write(msg);
|
|
} while (_influxdb.response() != DB_SUCCESS and tries < 5);
|
|
}
|
|
|
|
//*************************************************************************//
|
|
|
|
#elif INFLUXDB_VERSION == 2
|
|
|
|
#include <InfluxDbClient.h> // from bib manager
|
|
|
|
// Data point
|
|
Point sensor(DEVICE_NAME);
|
|
|
|
// Init variables to influxdb config - doesn't talk to database
|
|
InfluxDBClient client(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN);
|
|
|
|
void influxdb_begin() {
|
|
|
|
// Check server connection
|
|
if (client.validateConnection()) {
|
|
// success
|
|
#ifdef DEBUG
|
|
debug("InfluxDB connect success\n");
|
|
#endif
|
|
} else {
|
|
// fail
|
|
#ifdef DEBUG
|
|
debug("InfluxDB connect failed\n");
|
|
#endif
|
|
}
|
|
|
|
}
|
|
|
|
void pushToInfluxDB(String device, float sensorValues[]) {
|
|
|
|
if (!(isnan(sensorValues[SENSOR_TEMPERATURE])))
|
|
{
|
|
sensor.clearFields();
|
|
sensor.addField("temperature", sensorValues[SENSOR_TEMPERATURE]);
|
|
_writePoint();
|
|
}
|
|
if (!(isnan(sensorValues[SENSOR_HUMIDITY])))
|
|
{
|
|
sensor.clearFields();
|
|
sensor.addField("humidity", sensorValues[SENSOR_HUMIDITY]);
|
|
_writePoint();
|
|
}
|
|
if (!(isnan(sensorValues[SENSOR_LIGHT])))
|
|
{
|
|
sensor.clearFields();
|
|
sensor.addField("light", sensorValues[SENSOR_LIGHT]);
|
|
_writePoint();
|
|
}
|
|
if (!(isnan(sensorValues[SENSOR_WINDSPEED])))
|
|
{
|
|
sensor.clearFields();
|
|
sensor.addField("windspeed", sensorValues[SENSOR_WINDSPEED]);
|
|
_writePoint();
|
|
}
|
|
if (!(isnan(sensorValues[SENSOR_PRESSURE])))
|
|
{
|
|
sensor.clearFields();
|
|
sensor.addField("pressure", sensorValues[SENSOR_PRESSURE]);
|
|
_writePoint();
|
|
}
|
|
if (!(isnan(sensorValues[SENSOR_VOLTAGE])))
|
|
{
|
|
sensor.clearFields();
|
|
sensor.addField("batvoltage", sensorValues[SENSOR_VOLTAGE]);
|
|
_writePoint();
|
|
}
|
|
if (!(isnan(sensorValues[SENSOR_ESAVEMODE])))
|
|
{
|
|
sensor.clearFields();
|
|
sensor.addField("esavemode", sensorValues[SENSOR_ESAVEMODE]);
|
|
_writePoint();
|
|
}
|
|
if (!(isnan(sensorValues[SENSOR_BATCHARGESTATE])))
|
|
{
|
|
sensor.clearFields();
|
|
sensor.addField("batchargestate", sensorValues[SENSOR_BATCHARGESTATE]);
|
|
_writePoint();
|
|
}
|
|
|
|
#ifdef LOG_MILLIS_TO_INFLUXDB
|
|
sensor.clearFields();
|
|
sensor.addField("timestamp", millis());
|
|
_writePoint();
|
|
#endif
|
|
|
|
}
|
|
|
|
void _writePoint() {
|
|
const uint8_t maxRetries = 3; // Maximale Anzahl der Versuche
|
|
uint8_t attempt = 0;
|
|
bool success = false;
|
|
|
|
while (attempt < maxRetries) {
|
|
attempt++;
|
|
|
|
// Timeout für Verbindung prüfen
|
|
uint32_t startTime = millis();
|
|
while (!client.validateConnection()) {
|
|
if (millis() - startTime > INFLUXDB_TIMEOUT_MS) {
|
|
debug("Timeout validating connection");
|
|
break; // Timeout erreicht, aus Schleife ausbrechen
|
|
}
|
|
}
|
|
|
|
if (!client.validateConnection()) {
|
|
debug("Can't write to InfluxDB, connection validation failed on attempt " + String(attempt));
|
|
continue; // Zum nächsten Versuch übergehen
|
|
}
|
|
|
|
// Timeout für canSendRequest prüfen
|
|
startTime = millis();
|
|
while (!client.canSendRequest()) {
|
|
if (millis() - startTime > INFLUXDB_TIMEOUT_MS) {
|
|
debug("Timeout waiting for canSendRequest");
|
|
break; // Timeout erreicht, aus Schleife ausbrechen
|
|
}
|
|
}
|
|
|
|
if (!client.canSendRequest()) {
|
|
debug("Can't write to InfluxDB, canSendRequest failed on attempt " + String(attempt));
|
|
continue; // Zum nächsten Versuch übergehen
|
|
}
|
|
|
|
// Datenpunkt schreiben
|
|
if (client.writePoint(sensor)) {
|
|
success = true;
|
|
break; // Schreiben erfolgreich, Schleife verlassen
|
|
} else {
|
|
debug("InfluxDB write failed on attempt " + String(attempt) + ": " + client.getLastErrorMessage() + " Err: " + String(client.getLastStatusCode()));
|
|
}
|
|
}
|
|
|
|
if (!success) {
|
|
debug("Failed to write point to InfluxDB after " + String(maxRetries) + " attempts.");
|
|
}
|
|
}
|
|
|
|
#endif // influxdb version 2 check
|
|
|
|
#endif // INFLUXDB_FEATURE
|
|
|
|
//*************************************************************************//
|