interface_influxdb.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #ifdef ENABLE_INFLUXDB
  2. #include "interface_influxdb.h"
  3. #include "esp_log.h"
  4. #include <time.h>
  5. #include "ClassLogFile.h"
  6. #include "esp_http_client.h"
  7. #include "../../include/defines.h"
  8. static const char *TAG = "INFLUXDB";
  9. std::string _influxDBURI;
  10. std::string _influxDBDatabase;
  11. std::string _influxDBUser;
  12. std::string _influxDBPassword;
  13. std::string _influxDB_V2_URI;
  14. std::string _influxDB_V2_Database;
  15. std::string _influxDB_V2_Token;
  16. std::string _influxDB_V2_Org;
  17. static esp_err_t http_event_handler(esp_http_client_event_t *evt);
  18. void InfluxDB_V2_Init(std::string _uri, std::string _database, std::string _org, std::string _token)
  19. {
  20. _influxDB_V2_URI = _uri;
  21. _influxDB_V2_Database = _database;
  22. _influxDB_V2_Org = _org;
  23. _influxDB_V2_Token = _token;
  24. }
  25. void InfluxDB_V2_Publish(std::string _measurement, std::string _key, std::string _content, std::string _timestamp)
  26. {
  27. char response_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0};
  28. esp_http_client_config_t http_config = {
  29. .user_agent = "ESP32 Meter reader",
  30. .method = HTTP_METHOD_POST,
  31. .event_handler = http_event_handler,
  32. .buffer_size = MAX_HTTP_OUTPUT_BUFFER,
  33. .user_data = response_buffer
  34. };
  35. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "InfluxDB_V2_Publish - Key: " + _key + ", Content: " + _content + ", Timestamp: " + _timestamp);
  36. std::string payload;
  37. char nowTimestamp[21];
  38. if (_timestamp.length() > 0)
  39. {
  40. struct tm tm;
  41. time_t t;
  42. time(&t);
  43. localtime_r(&t, &tm); // Extract DST setting from actual time to consider it for timestamp evaluation
  44. strptime(_timestamp.c_str(), PREVALUE_TIME_FORMAT_OUTPUT, &tm);
  45. t = mktime(&tm);
  46. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Timestamp: " + _timestamp + ", Timestamp (UTC): " + std::to_string(t));
  47. sprintf(nowTimestamp,"%ld000000000", (long) t); // UTC
  48. payload = _measurement + " " + _key + "=" + _content + " " + nowTimestamp;
  49. }
  50. else
  51. {
  52. payload = _measurement + " " + _key + "=" + _content;
  53. }
  54. payload.shrink_to_fit();
  55. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "sending line to influxdb:" + payload);
  56. std::string apiURI = _influxDB_V2_URI + "/api/v2/write?org=" + _influxDB_V2_Org + "&bucket=" + _influxDB_V2_Database;
  57. apiURI.shrink_to_fit();
  58. http_config.url = apiURI.c_str();
  59. ESP_LOGI(TAG, "http_config: %s", http_config.url); // Add mark on log to see when it restarted
  60. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "API URI: " + apiURI);
  61. esp_http_client_handle_t http_client = esp_http_client_init(&http_config);
  62. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "client is initialized");
  63. esp_http_client_set_header(http_client, "Content-Type", "text/plain");
  64. std::string _zw = "Token " + _influxDB_V2_Token;
  65. // LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Tokenheader: %s\n", _zw.c_str());
  66. esp_http_client_set_header(http_client, "Authorization", _zw.c_str());
  67. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "header is set");
  68. ESP_ERROR_CHECK(esp_http_client_set_post_field(http_client, payload.c_str(), payload.length()));
  69. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "post payload is set");
  70. esp_err_t err = ESP_ERROR_CHECK_WITHOUT_ABORT(esp_http_client_perform(http_client));
  71. if( err == ESP_OK ) {
  72. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP request was performed");
  73. int status_code = esp_http_client_get_status_code(http_client);
  74. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP status code" + std::to_string(status_code));
  75. } else {
  76. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP request failed");
  77. }
  78. esp_http_client_cleanup(http_client);
  79. }
  80. static esp_err_t http_event_handler(esp_http_client_event_t *evt)
  81. {
  82. switch(evt->event_id)
  83. {
  84. case HTTP_EVENT_ERROR:
  85. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client Error encountered");
  86. break;
  87. case HTTP_EVENT_ON_CONNECTED:
  88. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client connected");
  89. ESP_LOGI(TAG, "HTTP Client Connected");
  90. break;
  91. case HTTP_EVENT_HEADERS_SENT:
  92. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client sent all request headers");
  93. break;
  94. case HTTP_EVENT_ON_HEADER:
  95. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Header: key=" + std::string(evt->header_key) + ", value=" + std::string(evt->header_value));
  96. break;
  97. case HTTP_EVENT_ON_DATA:
  98. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client data recevied: len=" + std::to_string(evt->data_len));
  99. break;
  100. case HTTP_EVENT_ON_FINISH:
  101. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client finished");
  102. break;
  103. case HTTP_EVENT_DISCONNECTED:
  104. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client Disconnected");
  105. break;
  106. case HTTP_EVENT_REDIRECT:
  107. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Redirect");
  108. break;
  109. }
  110. return ESP_OK;
  111. }
  112. void InfluxDBPublish(std::string _measurement, std::string _key, std::string _content, std::string _timestamp) {
  113. char response_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0};
  114. esp_http_client_config_t http_config = {
  115. .user_agent = "ESP32 Meter reader",
  116. .method = HTTP_METHOD_POST,
  117. .event_handler = http_event_handler,
  118. .buffer_size = MAX_HTTP_OUTPUT_BUFFER,
  119. .user_data = response_buffer
  120. };
  121. if (_influxDBUser.length() && _influxDBPassword.length()){
  122. http_config.username = _influxDBUser.c_str();
  123. http_config.password = _influxDBPassword.c_str();
  124. http_config.auth_type = HTTP_AUTH_TYPE_BASIC;
  125. }
  126. std::string payload;
  127. char nowTimestamp[21];
  128. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "InfluxDBPublish - Key: " + _key + ", Content: " + _content + ", Timestamp: " + _timestamp);
  129. if (_timestamp.length() > 0)
  130. {
  131. struct tm tm;
  132. time_t t;
  133. time(&t);
  134. localtime_r(&t, &tm); // Extract DST setting from actual time to consider it for timestamp evaluation
  135. strptime(_timestamp.c_str(), PREVALUE_TIME_FORMAT_OUTPUT, &tm);
  136. t = mktime(&tm);
  137. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Timestamp: " + _timestamp + ", Timestamp (UTC): " + std::to_string(t));
  138. sprintf(nowTimestamp,"%ld000000000", (long) t); // UTC
  139. payload = _measurement + " " + _key + "=" + _content + " " + nowTimestamp;
  140. }
  141. else
  142. {
  143. payload = _measurement + " " + _key + "=" + _content;
  144. }
  145. payload.shrink_to_fit();
  146. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "sending line to influxdb:" + payload);
  147. // use the default retention policy of the database
  148. std::string apiURI = _influxDBURI + "/write?db=" + _influxDBDatabase;
  149. // std::string apiURI = _influxDBURI + "/api/v2/write?bucket=" + _influxDBDatabase + "/";
  150. apiURI.shrink_to_fit();
  151. http_config.url = apiURI.c_str();
  152. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "API URI: " + apiURI);
  153. esp_http_client_handle_t http_client = esp_http_client_init(&http_config);
  154. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "client is initialized");
  155. esp_http_client_set_header(http_client, "Content-Type", "text/plain");
  156. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "header is set");
  157. ESP_ERROR_CHECK(esp_http_client_set_post_field(http_client, payload.c_str(), payload.length()));
  158. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "post payload is set");
  159. esp_err_t err = ESP_ERROR_CHECK_WITHOUT_ABORT(esp_http_client_perform(http_client));
  160. if( err == ESP_OK ) {
  161. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP request was performed");
  162. int status_code = esp_http_client_get_status_code(http_client);
  163. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP status code" + std::to_string(status_code));
  164. } else {
  165. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP request failed");
  166. }
  167. esp_http_client_cleanup(http_client);
  168. }
  169. void InfluxDBInit(std::string _uri, std::string _database, std::string _user, std::string _password){
  170. _influxDBURI = _uri;
  171. _influxDBDatabase = _database;
  172. _influxDBUser = _user;
  173. _influxDBPassword = _password;
  174. }
  175. void InfluxDBdestroy() {
  176. }
  177. #endif //ENABLE_INFLUXDB