|
@@ -16,25 +16,37 @@ std::string _influxDBMeasurement;
|
|
|
std::string _influxDBUser;
|
|
std::string _influxDBUser;
|
|
|
std::string _influxDBPassword;
|
|
std::string _influxDBPassword;
|
|
|
|
|
|
|
|
-std::string _influxDB_V2_URI;
|
|
|
|
|
-std::string _influxDB_V2_Database;
|
|
|
|
|
-std::string _influxDB_V2_Measurement;
|
|
|
|
|
-std::string _influxDB_V2_Token;
|
|
|
|
|
-std::string _influxDB_V2_Org;
|
|
|
|
|
-
|
|
|
|
|
-static esp_err_t http_event_handler(esp_http_client_event_t *evt);
|
|
|
|
|
-
|
|
|
|
|
-void InfluxDB_V2_Init(std::string _uri, std::string _database, std::string _measurement, std::string _org, std::string _token)
|
|
|
|
|
|
|
+static esp_err_t http_event_handler(esp_http_client_event_t *evt)
|
|
|
{
|
|
{
|
|
|
- _influxDB_V2_URI = _uri;
|
|
|
|
|
- _influxDB_V2_Database = _database;
|
|
|
|
|
- _influxDB_V2_Measurement = _measurement;
|
|
|
|
|
- _influxDB_V2_Org = _org;
|
|
|
|
|
- _influxDB_V2_Token = _token;
|
|
|
|
|
|
|
+ switch(evt->event_id)
|
|
|
|
|
+ {
|
|
|
|
|
+ case HTTP_EVENT_ERROR:
|
|
|
|
|
+ LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client Error encountered");
|
|
|
|
|
+ break;
|
|
|
|
|
+ case HTTP_EVENT_ON_CONNECTED:
|
|
|
|
|
+ LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client Error encountered");
|
|
|
|
|
+ ESP_LOGI(TAG, "HTTP Client Connected");
|
|
|
|
|
+ break;
|
|
|
|
|
+ case HTTP_EVENT_HEADERS_SENT:
|
|
|
|
|
+ LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client sent all request headers");
|
|
|
|
|
+ break;
|
|
|
|
|
+ case HTTP_EVENT_ON_HEADER:
|
|
|
|
|
+ LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Header: key=" + std::string(evt->header_key) + ", value=" + std::string(evt->header_value));
|
|
|
|
|
+ break;
|
|
|
|
|
+ case HTTP_EVENT_ON_DATA:
|
|
|
|
|
+ LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client data recevied: len=" + std::to_string(evt->data_len));
|
|
|
|
|
+ break;
|
|
|
|
|
+ case HTTP_EVENT_ON_FINISH:
|
|
|
|
|
+ LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client finished");
|
|
|
|
|
+ break;
|
|
|
|
|
+ case HTTP_EVENT_DISCONNECTED:
|
|
|
|
|
+ LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client Disconnected");
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ return ESP_OK;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void InfluxDB_V2_Publish(std::string _key, std::string _content, std::string _timestamp)
|
|
|
|
|
-{
|
|
|
|
|
|
|
+void InfluxDBPublish(std::string _key, std::string _content, std::string _timestamp) {
|
|
|
char response_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0};
|
|
char response_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0};
|
|
|
esp_http_client_config_t http_config = {
|
|
esp_http_client_config_t http_config = {
|
|
|
.user_agent = "ESP32 Meter reader",
|
|
.user_agent = "ESP32 Meter reader",
|
|
@@ -44,9 +56,13 @@ void InfluxDB_V2_Publish(std::string _key, std::string _content, std::string _ti
|
|
|
.user_data = response_buffer
|
|
.user_data = response_buffer
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
- LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "InfluxDB_V2_Publish - Key: " + _key + ", Content: " + _content + ", Timestamp: " + _timestamp);
|
|
|
|
|
|
|
+ if (_influxDBUser.length() && _influxDBPassword.length()){
|
|
|
|
|
+ http_config.username = _influxDBUser.c_str();
|
|
|
|
|
+ http_config.password = _influxDBPassword.c_str();
|
|
|
|
|
+ http_config.auth_type = HTTP_AUTH_TYPE_BASIC;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- // Format: #define PREVALUE_TIME_FORMAT_OUTPUT "%Y-%m-%dT%H:%M:%S%z"
|
|
|
|
|
|
|
+ LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "InfluxDBPublish - Key: " + _key + ", Content: " + _content + ", Timestamp: " + _timestamp);
|
|
|
|
|
|
|
|
char nowTimestamp[21];
|
|
char nowTimestamp[21];
|
|
|
std::string payload;
|
|
std::string payload;
|
|
@@ -64,23 +80,23 @@ void InfluxDB_V2_Publish(std::string _key, std::string _content, std::string _ti
|
|
|
|
|
|
|
|
sprintf(nowTimestamp,"%ld000000000", (long) utc); // UTC
|
|
sprintf(nowTimestamp,"%ld000000000", (long) utc); // UTC
|
|
|
|
|
|
|
|
- payload = _influxDB_V2_Measurement + " " + _key + "=" + _content + " " + nowTimestamp;
|
|
|
|
|
-// payload = _influxDB_V2_Measurement + " " + _key + "=774 " + nowTimestamp;
|
|
|
|
|
|
|
+ payload = _influxDBMeasurement + " " + _key + "=" + _content + " " + nowTimestamp;
|
|
|
|
|
+// payload = _influxDBMeasurement + " " + _key + "=774 " + nowTimestamp;
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|
|
|
{
|
|
{
|
|
|
- payload = _influxDB_V2_Measurement + " " + _key + "=" + _content;
|
|
|
|
|
-// payload = _influxDB_V2_Measurement + " " + _key + "=774";
|
|
|
|
|
|
|
+ payload = _influxDBMeasurement + " " + _key + "=" + _content;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
payload.shrink_to_fit();
|
|
payload.shrink_to_fit();
|
|
|
|
|
|
|
|
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "sending line to influxdb:" + payload);
|
|
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "sending line to influxdb:" + payload);
|
|
|
|
|
|
|
|
- std::string apiURI = _influxDB_V2_URI + "/api/v2/write?org=" + _influxDB_V2_Org + "&bucket=" + _influxDB_V2_Database;
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // use the default retention policy of the database
|
|
|
|
|
+ std::string apiURI = _influxDBURI + "/api/v2/write?bucket=" + _influxDBDatabase + "/";
|
|
|
apiURI.shrink_to_fit();
|
|
apiURI.shrink_to_fit();
|
|
|
http_config.url = apiURI.c_str();
|
|
http_config.url = apiURI.c_str();
|
|
|
- ESP_LOGI(TAG, "http_config: %s", http_config.url); // Add mark on log to see when it restarted
|
|
|
|
|
|
|
|
|
|
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "API URI: " + apiURI);
|
|
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "API URI: " + apiURI);
|
|
|
|
|
|
|
@@ -88,10 +104,6 @@ void InfluxDB_V2_Publish(std::string _key, std::string _content, std::string _ti
|
|
|
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "client is initialized");
|
|
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "client is initialized");
|
|
|
|
|
|
|
|
esp_http_client_set_header(http_client, "Content-Type", "text/plain");
|
|
esp_http_client_set_header(http_client, "Content-Type", "text/plain");
|
|
|
- std::string _zw = "Token " + _influxDB_V2_Token;
|
|
|
|
|
- // LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Tokenheader: %s\n", _zw.c_str());
|
|
|
|
|
- esp_http_client_set_header(http_client, "Authorization", _zw.c_str());
|
|
|
|
|
-
|
|
|
|
|
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "header is set");
|
|
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "header is set");
|
|
|
|
|
|
|
|
ESP_ERROR_CHECK(esp_http_client_set_post_field(http_client, payload.c_str(), payload.length()));
|
|
ESP_ERROR_CHECK(esp_http_client_set_post_field(http_client, payload.c_str(), payload.length()));
|
|
@@ -111,36 +123,7 @@ void InfluxDB_V2_Publish(std::string _key, std::string _content, std::string _ti
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-static esp_err_t http_event_handler(esp_http_client_event_t *evt)
|
|
|
|
|
-{
|
|
|
|
|
- switch(evt->event_id)
|
|
|
|
|
- {
|
|
|
|
|
- case HTTP_EVENT_ERROR:
|
|
|
|
|
- LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client Error encountered");
|
|
|
|
|
- break;
|
|
|
|
|
- case HTTP_EVENT_ON_CONNECTED:
|
|
|
|
|
- LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client Error encountered");
|
|
|
|
|
- ESP_LOGI(TAG, "HTTP Client Connected");
|
|
|
|
|
- break;
|
|
|
|
|
- case HTTP_EVENT_HEADERS_SENT:
|
|
|
|
|
- LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client sent all request headers");
|
|
|
|
|
- break;
|
|
|
|
|
- case HTTP_EVENT_ON_HEADER:
|
|
|
|
|
- LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Header: key=" + std::string(evt->header_key) + ", value=" + std::string(evt->header_value));
|
|
|
|
|
- break;
|
|
|
|
|
- case HTTP_EVENT_ON_DATA:
|
|
|
|
|
- LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client data recevied: len=" + std::to_string(evt->data_len));
|
|
|
|
|
- break;
|
|
|
|
|
- case HTTP_EVENT_ON_FINISH:
|
|
|
|
|
- LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client finished");
|
|
|
|
|
- break;
|
|
|
|
|
- case HTTP_EVENT_DISCONNECTED:
|
|
|
|
|
- LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client Disconnected");
|
|
|
|
|
- break;
|
|
|
|
|
- }
|
|
|
|
|
- return ESP_OK;
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
|
|
+/*
|
|
|
void InfluxDBPublish(std::string _key, std::string _content, std::string _timestamp) {
|
|
void InfluxDBPublish(std::string _key, std::string _content, std::string _timestamp) {
|
|
|
char response_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0};
|
|
char response_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0};
|
|
|
esp_http_client_config_t http_config = {
|
|
esp_http_client_config_t http_config = {
|
|
@@ -159,30 +142,28 @@ void InfluxDBPublish(std::string _key, std::string _content, std::string _timest
|
|
|
|
|
|
|
|
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "InfluxDBPublish - Key: " + _key + ", Content: " + _content + ", Timestamp: " + _timestamp);
|
|
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "InfluxDBPublish - Key: " + _key + ", Content: " + _content + ", Timestamp: " + _timestamp);
|
|
|
|
|
|
|
|
- char nowTimestamp[21];
|
|
|
|
|
- std::string payload;
|
|
|
|
|
-
|
|
|
|
|
- if (_timestamp.length() > 0)
|
|
|
|
|
- {
|
|
|
|
|
- struct tm tm;
|
|
|
|
|
- strptime(_timestamp.c_str(), PREVALUE_TIME_FORMAT_OUTPUT, &tm);
|
|
|
|
|
- time_t t = mktime(&tm); // Time in Localtime (looks like timezone is not used by strptime)
|
|
|
|
|
|
|
+ // Format: #define PREVALUE_TIME_FORMAT_OUTPUT "%Y-%m-%dT%H:%M:%S%z"
|
|
|
|
|
+ struct tm tm;
|
|
|
|
|
+ strptime(_timestamp.c_str(), PREVALUE_TIME_FORMAT_OUTPUT, &tm);
|
|
|
|
|
+ time_t t = mktime(&tm); // t is now your desired time_t
|
|
|
|
|
|
|
|
- struct tm * ptm;
|
|
|
|
|
- ptm = gmtime ( &t );
|
|
|
|
|
- time_t utc = mktime(ptm);
|
|
|
|
|
- utc = 2*t - utc; // Take care of timezone (looks difficult, but is easy: t = t + (t - utc), weil t-utc = timezone)
|
|
|
|
|
|
|
+ struct tm * ptm;
|
|
|
|
|
+ ptm = gmtime ( &t );
|
|
|
|
|
+ time_t utc = mktime(ptm);
|
|
|
|
|
|
|
|
- sprintf(nowTimestamp,"%ld000000000", (long) utc); // UTC
|
|
|
|
|
|
|
+// time_t now;
|
|
|
|
|
+// time(&now);
|
|
|
|
|
+ char nowTimestamp[21];
|
|
|
|
|
+ // pad with zeroes to get nanoseconds
|
|
|
|
|
+// sprintf(nowTimestamp,"%ld000000000", (long) now);
|
|
|
|
|
+// sprintf(nowTimestamp,"%ld000000000", (long) t); // Localtime
|
|
|
|
|
+ sprintf(nowTimestamp,"%ld000000000", (long) utc); // UTC
|
|
|
|
|
+
|
|
|
|
|
|
|
|
- payload = _influxDBMeasurement + " " + _key + "=" + _content + " " + nowTimestamp;
|
|
|
|
|
-// payload = _influxDBMeasurement + " " + _key + "=774 " + nowTimestamp;
|
|
|
|
|
- }
|
|
|
|
|
- else
|
|
|
|
|
- {
|
|
|
|
|
- payload = _influxDBMeasurement + " " + _key + "=" + _content;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+// LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Test Time Conversion - t: " + std::to_string(t) + ", utc: " + std::to_string(utc));
|
|
|
|
|
+// LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Test Time Conversion - now: " + std::to_string(now) + ", timestamp: " + std::to_string(t) + "(correct time not used yet)");
|
|
|
|
|
|
|
|
|
|
+ std::string payload = _influxDBMeasurement + " " + _key + "=" + _content + " " + nowTimestamp;
|
|
|
payload.shrink_to_fit();
|
|
payload.shrink_to_fit();
|
|
|
|
|
|
|
|
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "sending line to influxdb:" + payload);
|
|
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "sending line to influxdb:" + payload);
|
|
@@ -215,6 +196,7 @@ void InfluxDBPublish(std::string _key, std::string _content, std::string _timest
|
|
|
}
|
|
}
|
|
|
esp_http_client_cleanup(http_client);
|
|
esp_http_client_cleanup(http_client);
|
|
|
}
|
|
}
|
|
|
|
|
+*/
|
|
|
|
|
|
|
|
|
|
|
|
|
void InfluxDBInit(std::string _uri, std::string _database, std::string _measurement, std::string _user, std::string _password){
|
|
void InfluxDBInit(std::string _uri, std::string _database, std::string _measurement, std::string _user, std::string _password){
|