Skip to content

Commit 4c40749

Browse files
committed
Update Influxdb
1 parent b97d808 commit 4c40749

File tree

3 files changed

+316
-60
lines changed

3 files changed

+316
-60
lines changed
Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
#ifdef ENABLE_INFLUXDB
2+
#include "interface_influxdb.h"
3+
4+
#include "esp_log.h"
5+
#include <time.h>
6+
#include "ClassLogFile.h"
7+
#include "esp_http_client.h"
8+
#include "../../include/defines.h"
9+
10+
11+
static const char *TAG = "INFLUXDB";
12+
13+
std::string _influxDBURI;
14+
std::string _influxDBDatabase;
15+
std::string _influxDBMeasurement;
16+
std::string _influxDBUser;
17+
std::string _influxDBPassword;
18+
19+
static esp_err_t http_event_handler(esp_http_client_event_t *evt)
20+
{
21+
switch(evt->event_id)
22+
{
23+
case HTTP_EVENT_ERROR:
24+
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client Error encountered");
25+
break;
26+
case HTTP_EVENT_ON_CONNECTED:
27+
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client Error encountered");
28+
ESP_LOGI(TAG, "HTTP Client Connected");
29+
break;
30+
case HTTP_EVENT_HEADERS_SENT:
31+
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client sent all request headers");
32+
break;
33+
case HTTP_EVENT_ON_HEADER:
34+
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Header: key=" + std::string(evt->header_key) + ", value=" + std::string(evt->header_value));
35+
break;
36+
case HTTP_EVENT_ON_DATA:
37+
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client data recevied: len=" + std::to_string(evt->data_len));
38+
break;
39+
case HTTP_EVENT_ON_FINISH:
40+
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client finished");
41+
break;
42+
case HTTP_EVENT_DISCONNECTED:
43+
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client Disconnected");
44+
break;
45+
}
46+
return ESP_OK;
47+
}
48+
49+
void InfluxDBPublish(std::string _key, std::string _content, std::string _timestamp) {
50+
char response_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0};
51+
esp_http_client_config_t http_config = {
52+
.user_agent = "ESP32 Meter reader",
53+
.method = HTTP_METHOD_POST,
54+
.event_handler = http_event_handler,
55+
.buffer_size = MAX_HTTP_OUTPUT_BUFFER,
56+
.user_data = response_buffer
57+
};
58+
59+
if (_influxDBUser.length() && _influxDBPassword.length()){
60+
http_config.username = _influxDBUser.c_str();
61+
http_config.password = _influxDBPassword.c_str();
62+
http_config.auth_type = HTTP_AUTH_TYPE_BASIC;
63+
}
64+
65+
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "InfluxDBPublish - Key: " + _key + ", Content: " + _content + ", Timestamp: " + _timestamp);
66+
67+
char nowTimestamp[21];
68+
std::string payload;
69+
70+
if (_timestamp.length() > 0)
71+
{
72+
struct tm tm;
73+
strptime(_timestamp.c_str(), PREVALUE_TIME_FORMAT_OUTPUT, &tm);
74+
time_t t = mktime(&tm); // Time in Localtime (looks like timezone is not used by strptime)
75+
76+
struct tm * ptm;
77+
ptm = gmtime ( &t );
78+
time_t utc = mktime(ptm);
79+
utc = 2*t - utc; // Take care of timezone (looks difficult, but is easy: t = t + (t - utc), weil t-utc = timezone)
80+
81+
sprintf(nowTimestamp,"%ld000000000", (long) utc); // UTC
82+
83+
payload = _influxDBMeasurement + " " + _key + "=" + _content + " " + nowTimestamp;
84+
// payload = _influxDBMeasurement + " " + _key + "=774 " + nowTimestamp;
85+
}
86+
else
87+
{
88+
payload = _influxDBMeasurement + " " + _key + "=" + _content;
89+
}
90+
91+
payload.shrink_to_fit();
92+
93+
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "sending line to influxdb:" + payload);
94+
95+
96+
// use the default retention policy of the database
97+
std::string apiURI = _influxDBURI + "/api/v2/write?bucket=" + _influxDBDatabase + "/";
98+
apiURI.shrink_to_fit();
99+
http_config.url = apiURI.c_str();
100+
101+
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "API URI: " + apiURI);
102+
103+
esp_http_client_handle_t http_client = esp_http_client_init(&http_config);
104+
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "client is initialized");
105+
106+
esp_http_client_set_header(http_client, "Content-Type", "text/plain");
107+
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "header is set");
108+
109+
ESP_ERROR_CHECK(esp_http_client_set_post_field(http_client, payload.c_str(), payload.length()));
110+
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "post payload is set");
111+
112+
esp_err_t err = ESP_ERROR_CHECK_WITHOUT_ABORT(esp_http_client_perform(http_client));
113+
114+
if( err == ESP_OK ) {
115+
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP request was performed");
116+
int status_code = esp_http_client_get_status_code(http_client);
117+
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP status code" + std::to_string(status_code));
118+
} else {
119+
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP request failed");
120+
}
121+
esp_http_client_cleanup(http_client);
122+
}
123+
124+
125+
126+
/*
127+
void InfluxDBPublish(std::string _key, std::string _content, std::string _timestamp) {
128+
char response_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0};
129+
esp_http_client_config_t http_config = {
130+
.user_agent = "ESP32 Meter reader",
131+
.method = HTTP_METHOD_POST,
132+
.event_handler = http_event_handler,
133+
.buffer_size = MAX_HTTP_OUTPUT_BUFFER,
134+
.user_data = response_buffer
135+
};
136+
137+
if (_influxDBUser.length() && _influxDBPassword.length()){
138+
http_config.username = _influxDBUser.c_str();
139+
http_config.password = _influxDBPassword.c_str();
140+
http_config.auth_type = HTTP_AUTH_TYPE_BASIC;
141+
}
142+
143+
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "InfluxDBPublish - Key: " + _key + ", Content: " + _content + ", Timestamp: " + _timestamp);
144+
145+
// Format: #define PREVALUE_TIME_FORMAT_OUTPUT "%Y-%m-%dT%H:%M:%S%z"
146+
struct tm tm;
147+
strptime(_timestamp.c_str(), PREVALUE_TIME_FORMAT_OUTPUT, &tm);
148+
time_t t = mktime(&tm); // t is now your desired time_t
149+
150+
struct tm * ptm;
151+
ptm = gmtime ( &t );
152+
time_t utc = mktime(ptm);
153+
154+
// time_t now;
155+
// time(&now);
156+
char nowTimestamp[21];
157+
// pad with zeroes to get nanoseconds
158+
// sprintf(nowTimestamp,"%ld000000000", (long) now);
159+
// sprintf(nowTimestamp,"%ld000000000", (long) t); // Localtime
160+
sprintf(nowTimestamp,"%ld000000000", (long) utc); // UTC
161+
162+
163+
// LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Test Time Conversion - t: " + std::to_string(t) + ", utc: " + std::to_string(utc));
164+
// LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Test Time Conversion - now: " + std::to_string(now) + ", timestamp: " + std::to_string(t) + "(correct time not used yet)");
165+
166+
std::string payload = _influxDBMeasurement + " " + _key + "=" + _content + " " + nowTimestamp;
167+
payload.shrink_to_fit();
168+
169+
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "sending line to influxdb:" + payload);
170+
171+
172+
// use the default retention policy of the database
173+
std::string apiURI = _influxDBURI + "/api/v2/write?bucket=" + _influxDBDatabase + "/";
174+
apiURI.shrink_to_fit();
175+
http_config.url = apiURI.c_str();
176+
177+
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "API URI: " + apiURI);
178+
179+
esp_http_client_handle_t http_client = esp_http_client_init(&http_config);
180+
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "client is initialized");
181+
182+
esp_http_client_set_header(http_client, "Content-Type", "text/plain");
183+
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "header is set");
184+
185+
ESP_ERROR_CHECK(esp_http_client_set_post_field(http_client, payload.c_str(), payload.length()));
186+
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "post payload is set");
187+
188+
esp_err_t err = ESP_ERROR_CHECK_WITHOUT_ABORT(esp_http_client_perform(http_client));
189+
190+
if( err == ESP_OK ) {
191+
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP request was performed");
192+
int status_code = esp_http_client_get_status_code(http_client);
193+
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP status code" + std::to_string(status_code));
194+
} else {
195+
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP request failed");
196+
}
197+
esp_http_client_cleanup(http_client);
198+
}
199+
*/
200+
201+
202+
void InfluxDBInit(std::string _uri, std::string _database, std::string _measurement, std::string _user, std::string _password){
203+
_influxDBURI = _uri;
204+
_influxDBDatabase = _database;
205+
_influxDBMeasurement = _measurement;
206+
_influxDBUser = _user;
207+
_influxDBPassword = _password;
208+
209+
}
210+
211+
void InfluxDBdestroy() {
212+
}
213+
214+
#endif //ENABLE_INFLUXDB
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifdef ENABLE_INFLUXDB
2+
3+
#pragma once
4+
#ifndef INTERFACE_INFLUXDB_H
5+
#define INTERFACE_INFLUXDB_H
6+
7+
#include <string>
8+
#include <map>
9+
#include <functional>
10+
11+
// Interface to InfluxDB v1.x
12+
void InfluxDBInit(std::string _influxDBURI, std::string _database, std::string _measurement, std::string _user, std::string _password);
13+
void InfluxDBPublish(std::string _key, std::string _content, std::string _timestamp);
14+
15+
// Interface to InfluxDB v2.x
16+
void InfluxDB_V2_Init(std::string _uri, std::string _database, std::string _measurement, std::string _org, std::string _token);
17+
void InfluxDB_V2_Publish(std::string _key, std::string _content, std::string _timestamp);
18+
19+
20+
21+
void InfluxDBdestroy();
22+
23+
#endif //INTERFACE_INFLUXDB_H
24+
#endif //ENABLE_INFLUXDB

0 commit comments

Comments
 (0)