-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinflux.cpp
67 lines (62 loc) · 1.87 KB
/
influx.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "influx.h"
#include "credentials.h"
InfluxDBClient client(INFLUXDB_URL, INFLUXDB_DB);
/* Use a mutex to prevent both tasks from writing at the same time */
/* which causes a lock up */
SemaphoreHandle_t mutex = xSemaphoreCreateMutex();
void setupInfluxOptions() {
// TODO this breaks submission - need to set timestamp manually?
// client.setWriteOptions(WriteOptions().writePrecision(WritePrecision::MS));
client.setHTTPOptions(HTTPOptions().connectionReuse(true));
}
bool validateInfluxConnection() {
xSemaphoreTake(mutex, portMAX_DELAY);
Serial.print(F("[ INFLUX ] Validating connection to: "));
Serial.println(INFLUXDB_URL);
bool influxOk = client.validateConnection();
xSemaphoreGive(mutex);
if (influxOk) {
Serial.print(F("[ INFLUX ] Connected to: "));
Serial.println(client.getServerUrl());
return true;
} else {
Serial.print(F("[ INFLUX ] Connection failed: "));
Serial.println(client.getLastErrorMessage());
return false;
}
}
uint8_t failedCount = 0;
bool logPoint(Point &point) {
Serial.print(F("[ INFLUX ] Writing: "));
Serial.println(client.pointToLineProtocol(point));
xSemaphoreTake(mutex, portMAX_DELAY);
#ifdef TEST_MODE
bool writeOk = 1;
#else
bool writeOk = client.writePoint(point);
#endif
xSemaphoreGive(mutex);
if (!writeOk) {
int16_t lastStatusCode = client.getLastStatusCode();
if (lastStatusCode == 204) {
// We didn't send any data!
return true;
}
if (lastStatusCode == 200) {
// We good
return true;
}
Serial.print(F("[ INFLUX ] Write failed! Status Code: "));
Serial.print(lastStatusCode);
Serial.print(F(", "));
Serial.println(client.getLastErrorMessage());
if (++failedCount > 5) {
Serial.println(F("[ INFLUX ] Failed too often, restarting"));
ESP.restart();
}
return false;
} else {
failedCount = 0;
return true;
}
}