2019-08-04 12:08:31 +02:00
|
|
|
|
2022-05-15 22:34:36 +02:00
|
|
|
#include "config_user.h"
|
|
|
|
|
2022-05-16 22:29:28 +02:00
|
|
|
#if INFLUXDB_VERSION == 1
|
|
|
|
|
2022-05-22 16:35:10 +02:00
|
|
|
#include <ESP8266Influxdb.h> // https://github.com/hwwong/ESP8266Influxdb
|
2022-05-16 22:29:28 +02:00
|
|
|
|
|
|
|
Influxdb _influxdb(INFLUXDB_HOST, INFLUXDB_PORT);
|
|
|
|
|
2022-09-08 11:21:05 +02:00
|
|
|
const String msg = "weather,device=" + device + " ";
|
|
|
|
|
2022-05-16 22:29:28 +02:00
|
|
|
void influxdb_begin() {
|
|
|
|
// Init variables to influxdb config - doesn't talk to database
|
|
|
|
_influxdb.opendb(INFLUXDB_DB, INFLUXDB_USER, INFLUXDB_PASS);
|
|
|
|
}
|
|
|
|
|
2022-09-08 11:21:05 +02:00
|
|
|
void pushToInfluxDB(String device, float sensorValues[])
|
|
|
|
{
|
2022-05-16 22:29:28 +02:00
|
|
|
uint8_t tries = 0;
|
|
|
|
boolean addComma = false;
|
|
|
|
|
2022-09-08 11:21:05 +02:00
|
|
|
#ifndef USE_WIFIMANAGER
|
|
|
|
if (WiFi.status() != WL_CONNECTED)
|
|
|
|
{
|
|
|
|
debug("Not connected to WLAN");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-05-16 22:29:28 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-05-27 11:59:57 +02:00
|
|
|
#ifdef LOG_MILLIS_TO_INFLUXDB
|
|
|
|
if (true == addComma)
|
|
|
|
msg += ",";
|
|
|
|
msg += "timestamp=" + String(millis());
|
|
|
|
#endif
|
|
|
|
|
2022-05-16 22:29:28 +02:00
|
|
|
debug(msg);
|
|
|
|
|
|
|
|
do {
|
|
|
|
tries++;
|
|
|
|
_influxdb.write(msg);
|
|
|
|
} while (_influxdb.response() != DB_SUCCESS and tries < 5);
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif INFLUXDB_VERSION == 2
|
2022-05-16 08:46:05 +02:00
|
|
|
|
2022-05-22 16:35:10 +02:00
|
|
|
#include <InfluxDbClient.h> // from bib manager
|
|
|
|
|
2022-05-09 09:42:42 +02:00
|
|
|
// Data point
|
2022-05-15 22:34:36 +02:00
|
|
|
Point sensor(DEVICE_NAME);
|
2022-05-09 09:42:42 +02:00
|
|
|
|
|
|
|
// Init variables to influxdb config - doesn't talk to database
|
|
|
|
InfluxDBClient client(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN);
|
2018-06-23 20:13:33 +02:00
|
|
|
|
2019-08-04 12:08:31 +02:00
|
|
|
void influxdb_begin() {
|
2022-05-09 09:42:42 +02:00
|
|
|
|
|
|
|
// Check server connection
|
|
|
|
if (client.validateConnection()) {
|
|
|
|
// success
|
|
|
|
#ifdef DEBUG
|
|
|
|
Serial.print("InfluxDB connect success\n");
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
// fail
|
|
|
|
#ifdef DEBUG
|
|
|
|
Serial.print("InfluxDB connect failed\n");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-08-04 12:08:31 +02:00
|
|
|
}
|
|
|
|
|
2022-09-08 11:21:05 +02:00
|
|
|
void pushToInfluxDB(String device, float sensorValues[])
|
|
|
|
{
|
|
|
|
|
|
|
|
#ifndef USE_WIFIMANAGER
|
|
|
|
if (WiFi.status() != WL_CONNECTED)
|
|
|
|
{
|
|
|
|
debug("Not connected to WLAN");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2018-07-12 22:30:30 +02:00
|
|
|
|
2019-02-02 13:58:47 +01:00
|
|
|
if (!(isnan(sensorValues[SENSOR_TEMPERATURE])))
|
|
|
|
{
|
2022-05-09 09:42:42 +02:00
|
|
|
sensor.clearFields();
|
|
|
|
sensor.addField("temperature", sensorValues[SENSOR_TEMPERATURE]);
|
|
|
|
_writePoint();
|
2019-02-02 13:58:47 +01:00
|
|
|
}
|
|
|
|
if (!(isnan(sensorValues[SENSOR_HUMIDITY])))
|
|
|
|
{
|
2022-05-09 09:42:42 +02:00
|
|
|
sensor.clearFields();
|
|
|
|
sensor.addField("humidity", sensorValues[SENSOR_HUMIDITY]);
|
|
|
|
_writePoint();
|
2019-02-02 13:58:47 +01:00
|
|
|
}
|
|
|
|
if (!(isnan(sensorValues[SENSOR_LIGHT])))
|
|
|
|
{
|
2022-05-09 09:42:42 +02:00
|
|
|
sensor.clearFields();
|
|
|
|
sensor.addField("light", sensorValues[SENSOR_LIGHT]);
|
|
|
|
_writePoint();
|
2019-02-02 13:58:47 +01:00
|
|
|
}
|
|
|
|
if (!(isnan(sensorValues[SENSOR_WINDSPEED])))
|
|
|
|
{
|
2022-05-09 09:42:42 +02:00
|
|
|
sensor.clearFields();
|
|
|
|
sensor.addField("windspeed", sensorValues[SENSOR_WINDSPEED]);
|
|
|
|
_writePoint();
|
2019-02-02 13:58:47 +01:00
|
|
|
}
|
|
|
|
if (!(isnan(sensorValues[SENSOR_PRESSURE])))
|
|
|
|
{
|
2022-05-09 09:42:42 +02:00
|
|
|
sensor.clearFields();
|
|
|
|
sensor.addField("pressure", sensorValues[SENSOR_PRESSURE]);
|
|
|
|
_writePoint();
|
2019-02-02 13:58:47 +01:00
|
|
|
}
|
|
|
|
if (!(isnan(sensorValues[SENSOR_BAT_VOLTAGE])))
|
|
|
|
{
|
2022-05-09 09:42:42 +02:00
|
|
|
sensor.clearFields();
|
|
|
|
sensor.addField("batvoltage", sensorValues[SENSOR_BAT_VOLTAGE]);
|
|
|
|
_writePoint();
|
2019-02-02 13:58:47 +01:00
|
|
|
}
|
|
|
|
if (!(isnan(sensorValues[SENSOR_ESAVEMODE])))
|
|
|
|
{
|
2022-05-09 09:42:42 +02:00
|
|
|
sensor.clearFields();
|
|
|
|
sensor.addField("esavemode", sensorValues[SENSOR_ESAVEMODE]);
|
|
|
|
_writePoint();
|
2019-02-02 13:58:47 +01:00
|
|
|
}
|
|
|
|
if (!(isnan(sensorValues[SENSOR_BATCHARGESTATE])))
|
|
|
|
{
|
2022-05-09 09:42:42 +02:00
|
|
|
sensor.clearFields();
|
|
|
|
sensor.addField("batchargestate", sensorValues[SENSOR_BATCHARGESTATE]);
|
|
|
|
_writePoint();
|
2019-02-02 13:58:47 +01:00
|
|
|
}
|
|
|
|
|
2022-05-27 11:59:57 +02:00
|
|
|
#ifdef LOG_MILLIS_TO_INFLUXDB
|
|
|
|
sensor.clearFields();
|
|
|
|
sensor.addField("timestamp", millis());
|
|
|
|
_writePoint();
|
|
|
|
#endif
|
|
|
|
|
2022-05-09 09:42:42 +02:00
|
|
|
}
|
2019-01-27 16:29:58 +01:00
|
|
|
|
2022-09-08 11:21:05 +02:00
|
|
|
void _writePoint()
|
|
|
|
{
|
2022-05-09 09:42:42 +02:00
|
|
|
|
|
|
|
// wait unitl ready
|
2018-07-12 22:30:30 +02:00
|
|
|
do {
|
2022-05-09 09:42:42 +02:00
|
|
|
#ifdef DEBUG
|
|
|
|
Serial.print("InfluxDB: waiting for write ready\n");
|
|
|
|
#endif
|
|
|
|
} while (client.canSendRequest() == false);
|
|
|
|
|
|
|
|
// Write point
|
|
|
|
if (!client.writePoint(sensor)) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
Serial.print("InfluxDB write failed: ");
|
|
|
|
Serial.println(client.getLastErrorMessage());
|
|
|
|
Serial.print("\nErrorcode: ");
|
|
|
|
Serial.println(client.getLastStatusCode());
|
|
|
|
Serial.print("\n");
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
#ifdef DEBUG
|
|
|
|
Serial.print("InfluxDB write done\n");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-02-02 14:59:00 +01:00
|
|
|
}
|
2022-05-16 08:46:05 +02:00
|
|
|
|
|
|
|
#endif // influxdb version 2 check
|