interface_influxdb.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 _influxDBMeasurement;
  12. std::string _influxDBUser;
  13. std::string _influxDBPassword;
  14. static esp_err_t http_event_handler(esp_http_client_event_t *evt)
  15. {
  16. switch(evt->event_id)
  17. {
  18. case HTTP_EVENT_ERROR:
  19. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client Error encountered");
  20. break;
  21. case HTTP_EVENT_ON_CONNECTED:
  22. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client Error encountered");
  23. ESP_LOGI(TAG, "HTTP Client Connected");
  24. break;
  25. case HTTP_EVENT_HEADERS_SENT:
  26. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client sent all request headers");
  27. break;
  28. case HTTP_EVENT_ON_HEADER:
  29. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Header: key=" + std::string(evt->header_key) + ", value=" + std::string(evt->header_value));
  30. break;
  31. case HTTP_EVENT_ON_DATA:
  32. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client data recevied: len=" + std::to_string(evt->data_len));
  33. break;
  34. case HTTP_EVENT_ON_FINISH:
  35. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client finished");
  36. break;
  37. case HTTP_EVENT_DISCONNECTED:
  38. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client Disconnected");
  39. break;
  40. }
  41. return ESP_OK;
  42. }
  43. void InfluxDBPublish(std::string _key, std::string _content, std::string _timestamp) {
  44. char response_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0};
  45. esp_http_client_config_t http_config = {
  46. .user_agent = "ESP32 Meter reader",
  47. .method = HTTP_METHOD_POST,
  48. .event_handler = http_event_handler,
  49. .buffer_size = MAX_HTTP_OUTPUT_BUFFER,
  50. .user_data = response_buffer
  51. };
  52. if (_influxDBUser.length() && _influxDBPassword.length()){
  53. http_config.username = _influxDBUser.c_str();
  54. http_config.password = _influxDBPassword.c_str();
  55. http_config.auth_type = HTTP_AUTH_TYPE_BASIC;
  56. }
  57. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "InfluxDBPublish - Key: " + _key + ", Content: " + _content + ", Timestamp: " + _timestamp);
  58. char nowTimestamp[21];
  59. std::string payload;
  60. if (_timestamp.length() > 0)
  61. {
  62. struct tm tm;
  63. strptime(_timestamp.c_str(), PREVALUE_TIME_FORMAT_OUTPUT, &tm);
  64. time_t t = mktime(&tm); // Time in Localtime (looks like timezone is not used by strptime)
  65. struct tm * ptm;
  66. ptm = gmtime ( &t );
  67. time_t utc = mktime(ptm);
  68. utc = 2*t - utc; // Take care of timezone (looks difficult, but is easy: t = t + (t - utc), weil t-utc = timezone)
  69. sprintf(nowTimestamp,"%ld000000000", (long) utc); // UTC
  70. payload = _influxDBMeasurement + " " + _key + "=" + _content + " " + nowTimestamp;
  71. // payload = _influxDBMeasurement + " " + _key + "=774 " + nowTimestamp;
  72. }
  73. else
  74. {
  75. payload = _influxDBMeasurement + " " + _key + "=" + _content;
  76. }
  77. payload.shrink_to_fit();
  78. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "sending line to influxdb:" + payload);
  79. // use the default retention policy of the database
  80. std::string apiURI = _influxDBURI + "/api/v2/write?bucket=" + _influxDBDatabase + "/";
  81. apiURI.shrink_to_fit();
  82. http_config.url = apiURI.c_str();
  83. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "API URI: " + apiURI);
  84. esp_http_client_handle_t http_client = esp_http_client_init(&http_config);
  85. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "client is initialized");
  86. esp_http_client_set_header(http_client, "Content-Type", "text/plain");
  87. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "header is set");
  88. ESP_ERROR_CHECK(esp_http_client_set_post_field(http_client, payload.c_str(), payload.length()));
  89. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "post payload is set");
  90. esp_err_t err = ESP_ERROR_CHECK_WITHOUT_ABORT(esp_http_client_perform(http_client));
  91. if( err == ESP_OK ) {
  92. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP request was performed");
  93. int status_code = esp_http_client_get_status_code(http_client);
  94. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP status code" + std::to_string(status_code));
  95. } else {
  96. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP request failed");
  97. }
  98. esp_http_client_cleanup(http_client);
  99. }
  100. /*
  101. void InfluxDBPublish(std::string _key, std::string _content, std::string _timestamp) {
  102. char response_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0};
  103. esp_http_client_config_t http_config = {
  104. .user_agent = "ESP32 Meter reader",
  105. .method = HTTP_METHOD_POST,
  106. .event_handler = http_event_handler,
  107. .buffer_size = MAX_HTTP_OUTPUT_BUFFER,
  108. .user_data = response_buffer
  109. };
  110. if (_influxDBUser.length() && _influxDBPassword.length()){
  111. http_config.username = _influxDBUser.c_str();
  112. http_config.password = _influxDBPassword.c_str();
  113. http_config.auth_type = HTTP_AUTH_TYPE_BASIC;
  114. }
  115. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "InfluxDBPublish - Key: " + _key + ", Content: " + _content + ", Timestamp: " + _timestamp);
  116. // Format: #define PREVALUE_TIME_FORMAT_OUTPUT "%Y-%m-%dT%H:%M:%S%z"
  117. struct tm tm;
  118. strptime(_timestamp.c_str(), PREVALUE_TIME_FORMAT_OUTPUT, &tm);
  119. time_t t = mktime(&tm); // t is now your desired time_t
  120. struct tm * ptm;
  121. ptm = gmtime ( &t );
  122. time_t utc = mktime(ptm);
  123. // time_t now;
  124. // time(&now);
  125. char nowTimestamp[21];
  126. // pad with zeroes to get nanoseconds
  127. // sprintf(nowTimestamp,"%ld000000000", (long) now);
  128. // sprintf(nowTimestamp,"%ld000000000", (long) t); // Localtime
  129. sprintf(nowTimestamp,"%ld000000000", (long) utc); // UTC
  130. // LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Test Time Conversion - t: " + std::to_string(t) + ", utc: " + std::to_string(utc));
  131. // LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Test Time Conversion - now: " + std::to_string(now) + ", timestamp: " + std::to_string(t) + "(correct time not used yet)");
  132. std::string payload = _influxDBMeasurement + " " + _key + "=" + _content + " " + nowTimestamp;
  133. payload.shrink_to_fit();
  134. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "sending line to influxdb:" + payload);
  135. // use the default retention policy of the database
  136. std::string apiURI = _influxDBURI + "/api/v2/write?bucket=" + _influxDBDatabase + "/";
  137. apiURI.shrink_to_fit();
  138. http_config.url = apiURI.c_str();
  139. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "API URI: " + apiURI);
  140. esp_http_client_handle_t http_client = esp_http_client_init(&http_config);
  141. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "client is initialized");
  142. esp_http_client_set_header(http_client, "Content-Type", "text/plain");
  143. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "header is set");
  144. ESP_ERROR_CHECK(esp_http_client_set_post_field(http_client, payload.c_str(), payload.length()));
  145. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "post payload is set");
  146. esp_err_t err = ESP_ERROR_CHECK_WITHOUT_ABORT(esp_http_client_perform(http_client));
  147. if( err == ESP_OK ) {
  148. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP request was performed");
  149. int status_code = esp_http_client_get_status_code(http_client);
  150. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP status code" + std::to_string(status_code));
  151. } else {
  152. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP request failed");
  153. }
  154. esp_http_client_cleanup(http_client);
  155. }
  156. */
  157. void InfluxDBInit(std::string _uri, std::string _database, std::string _measurement, std::string _user, std::string _password){
  158. _influxDBURI = _uri;
  159. _influxDBDatabase = _database;
  160. _influxDBMeasurement = _measurement;
  161. _influxDBUser = _user;
  162. _influxDBPassword = _password;
  163. }
  164. void InfluxDBdestroy() {
  165. }
  166. #endif //ENABLE_INFLUXDB