interface_influxdb.cpp 7.7 KB

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