interface_influxdb.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. char response_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0};
  11. static esp_err_t http_event_handler(esp_http_client_event_t *evt)
  12. {
  13. switch(evt->event_id)
  14. {
  15. case HTTP_EVENT_ERROR:
  16. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client Error encountered");
  17. break;
  18. case HTTP_EVENT_ON_CONNECTED:
  19. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client connected");
  20. ESP_LOGI(TAG, "HTTP Client Connected");
  21. break;
  22. case HTTP_EVENT_HEADERS_SENT:
  23. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client sent all request headers");
  24. break;
  25. case HTTP_EVENT_ON_HEADER:
  26. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Header: key=" + std::string(evt->header_key) + ", value=" + std::string(evt->header_value));
  27. break;
  28. case HTTP_EVENT_ON_DATA:
  29. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client data recevied: len=" + std::to_string(evt->data_len));
  30. break;
  31. case HTTP_EVENT_ON_FINISH:
  32. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client finished");
  33. break;
  34. case HTTP_EVENT_DISCONNECTED:
  35. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client Disconnected");
  36. break;
  37. case HTTP_EVENT_REDIRECT:
  38. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Redirect");
  39. break;
  40. }
  41. return ESP_OK;
  42. }
  43. void InfluxDB::InfluxDBInitV1(std::string _influxDBURI, std::string _database, std::string _user, std::string _password) {
  44. version = INFLUXDB_V1;
  45. influxDBURI = _influxDBURI;
  46. database = _database;
  47. user = _user;
  48. password = _password;
  49. }
  50. void InfluxDB::InfluxDBInitV2(std::string _influxDBURI, std::string _bucket, std::string _org, std::string _token) {
  51. version = INFLUXDB_V2;
  52. influxDBURI = _influxDBURI;
  53. bucket = _bucket;
  54. org = _org;
  55. token = _token;
  56. }
  57. void InfluxDB::connectHTTP() {
  58. esp_http_client_config_t config = {};
  59. config.url = influxDBURI.c_str();
  60. config.event_handler = http_event_handler;
  61. config.buffer_size = MAX_HTTP_OUTPUT_BUFFER;
  62. config.user_data = response_buffer;
  63. switch (version) {
  64. case INFLUXDB_V1:
  65. config.auth_type = HTTP_AUTH_TYPE_BASIC;
  66. config.username = user.c_str();
  67. config.password = password.c_str();
  68. break;
  69. case INFLUXDB_V2:
  70. break;
  71. }
  72. InfluxDBdestroy();
  73. httpClient = esp_http_client_init(&config);
  74. if (!httpClient) {
  75. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to initialize HTTP client");
  76. } else {
  77. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "HTTP client initialized successfully");
  78. }
  79. }
  80. // Destroy the InfluxDB connection
  81. void InfluxDB::InfluxDBdestroy() {
  82. if (httpClient) {
  83. esp_http_client_cleanup(httpClient);
  84. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "HTTP client cleaned up");
  85. httpClient = NULL;
  86. }
  87. }
  88. // Publish data to the InfluxDB server
  89. void InfluxDB::InfluxDBPublish(std::string _measurement, std::string _key, std::string _content, long int _timeUTC) {
  90. std::string apiURI;
  91. std::string payload;
  92. char nowTimestamp[21];
  93. connectHTTP();
  94. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "InfluxDBPublish - Key: " + _key + ", Content: " + _content + ", timeUTC: " + std::to_string(_timeUTC));
  95. if (_timeUTC > 0)
  96. {
  97. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Timestamp (UTC): " + std::to_string(_timeUTC));
  98. sprintf(nowTimestamp,"%ld000000000", _timeUTC); // UTC
  99. payload = _measurement + " " + _key + "=" + _content + " " + nowTimestamp;
  100. }
  101. else
  102. {
  103. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "no timestamp given");
  104. payload = _measurement + " " + _key + "=" + _content;
  105. }
  106. payload.shrink_to_fit();
  107. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "sending line to influxdb:" + payload);
  108. esp_err_t err;
  109. switch (version) {
  110. case INFLUXDB_V1:
  111. apiURI = influxDBURI + "/write?db=" + database;
  112. apiURI.shrink_to_fit();
  113. esp_http_client_set_url(httpClient, apiURI.c_str());
  114. esp_http_client_set_method(httpClient, HTTP_METHOD_POST);
  115. esp_http_client_set_header(httpClient, "Content-Type", "text/plain");
  116. esp_http_client_set_post_field(httpClient, payload.c_str(), payload.length());
  117. err = esp_http_client_perform(httpClient);
  118. if (err == ESP_OK) {
  119. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Data published successfully: " + payload);
  120. } else {
  121. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to publish data: " + std::string(esp_err_to_name(err)));
  122. }
  123. break;
  124. case INFLUXDB_V2:
  125. apiURI = influxDBURI + "/api/v2/write?org=" + org + "&bucket=" + bucket;
  126. apiURI.shrink_to_fit();
  127. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "apiURI: " + apiURI);
  128. esp_http_client_set_url(httpClient, apiURI.c_str());
  129. esp_http_client_set_method(httpClient, HTTP_METHOD_POST);
  130. esp_http_client_set_header(httpClient, "Content-Type", "text/plain");
  131. std::string _zw = "Token " + token;
  132. esp_http_client_set_header(httpClient, "Authorization", _zw.c_str());
  133. esp_http_client_set_post_field(httpClient, payload.c_str(), payload.length());
  134. err = ESP_ERROR_CHECK_WITHOUT_ABORT(esp_http_client_perform(httpClient));
  135. if (err == ESP_OK) {
  136. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Data published successfully: " + payload);
  137. } else {
  138. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Failed to publish data: " + std::string(esp_err_to_name(err)));
  139. }
  140. break;
  141. }
  142. }
  143. #endif //ENABLE_INFLUXDB