weatherstation/firmware/influxdb.ino

103 lines
2.4 KiB
C++
Executable file

#include <InfluxDbClient.h> // from bib manager
// Data point
Point sensor("weatherstation");
// 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
Serial.print("InfluxDB connect success\n");
#endif
} else {
// fail
#ifdef DEBUG
Serial.print("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_BAT_VOLTAGE])))
{
sensor.clearFields();
sensor.addField("batvoltage", sensorValues[SENSOR_BAT_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();
}
}
void _writePoint() {
// wait unitl ready
do {
#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
}
}