interface_influxdb.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. strptime(_timestamp.c_str(), PREVALUE_TIME_FORMAT_OUTPUT, &tm);
  42. time_t t = mktime(&tm); // Time in Localtime (looks like timezone is not used by strptime)
  43. // struct tm * ptm;
  44. // ptm = gmtime ( &t );
  45. // time_t utc = mktime(ptm);
  46. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Use handover timestamp: " + _timestamp + " converted GMT timestamp: " + std::to_string(t));
  47. // utc = 2*t - utc; // Take care of timezone (looks difficult, but is easy: t = t + (t - utc), weil t-utc = timezone)
  48. // LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "time conversion utc after: " + std::to_string(utc));
  49. sprintf(nowTimestamp,"%ld000000000", (long) t); // UTC
  50. payload = _measurement + " " + _key + "=" + _content + " " + nowTimestamp;
  51. }
  52. else
  53. {
  54. payload = _measurement + " " + _key + "=" + _content;
  55. }
  56. payload.shrink_to_fit();
  57. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "sending line to influxdb:" + payload);
  58. std::string apiURI = _influxDB_V2_URI + "/api/v2/write?org=" + _influxDB_V2_Org + "&bucket=" + _influxDB_V2_Database;
  59. apiURI.shrink_to_fit();
  60. http_config.url = apiURI.c_str();
  61. ESP_LOGI(TAG, "http_config: %s", http_config.url); // Add mark on log to see when it restarted
  62. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "API URI: " + apiURI);
  63. esp_http_client_handle_t http_client = esp_http_client_init(&http_config);
  64. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "client is initialized");
  65. esp_http_client_set_header(http_client, "Content-Type", "text/plain");
  66. std::string _zw = "Token " + _influxDB_V2_Token;
  67. // LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Tokenheader: %s\n", _zw.c_str());
  68. esp_http_client_set_header(http_client, "Authorization", _zw.c_str());
  69. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "header is set");
  70. ESP_ERROR_CHECK(esp_http_client_set_post_field(http_client, payload.c_str(), payload.length()));
  71. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "post payload is set");
  72. esp_err_t err = ESP_ERROR_CHECK_WITHOUT_ABORT(esp_http_client_perform(http_client));
  73. if( err == ESP_OK ) {
  74. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP request was performed");
  75. int status_code = esp_http_client_get_status_code(http_client);
  76. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP status code" + std::to_string(status_code));
  77. } else {
  78. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP request failed");
  79. }
  80. esp_http_client_cleanup(http_client);
  81. }
  82. static esp_err_t http_event_handler(esp_http_client_event_t *evt)
  83. {
  84. switch(evt->event_id)
  85. {
  86. case HTTP_EVENT_ERROR:
  87. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client Error encountered");
  88. break;
  89. case HTTP_EVENT_ON_CONNECTED:
  90. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client Error encountered");
  91. ESP_LOGI(TAG, "HTTP Client Connected");
  92. break;
  93. case HTTP_EVENT_HEADERS_SENT:
  94. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client sent all request headers");
  95. break;
  96. case HTTP_EVENT_ON_HEADER:
  97. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Header: key=" + std::string(evt->header_key) + ", value=" + std::string(evt->header_value));
  98. break;
  99. case HTTP_EVENT_ON_DATA:
  100. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client data recevied: len=" + std::to_string(evt->data_len));
  101. break;
  102. case HTTP_EVENT_ON_FINISH:
  103. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client finished");
  104. break;
  105. case HTTP_EVENT_DISCONNECTED:
  106. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client Disconnected");
  107. break;
  108. case HTTP_EVENT_REDIRECT:
  109. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Redirect");
  110. break;
  111. }
  112. return ESP_OK;
  113. }
  114. void InfluxDBPublish(std::string _measurement, std::string _key, std::string _content, std::string _timestamp) {
  115. char response_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0};
  116. esp_http_client_config_t http_config = {
  117. .user_agent = "ESP32 Meter reader",
  118. .method = HTTP_METHOD_POST,
  119. .event_handler = http_event_handler,
  120. .buffer_size = MAX_HTTP_OUTPUT_BUFFER,
  121. .user_data = response_buffer
  122. };
  123. if (_influxDBUser.length() && _influxDBPassword.length()){
  124. http_config.username = _influxDBUser.c_str();
  125. http_config.password = _influxDBPassword.c_str();
  126. http_config.auth_type = HTTP_AUTH_TYPE_BASIC;
  127. }
  128. std::string payload;
  129. char nowTimestamp[21];
  130. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "InfluxDBPublish - Key: " + _key + ", Content: " + _content + ", Timestamp: " + _timestamp);
  131. if (_timestamp.length() > 0)
  132. {
  133. struct tm tm;
  134. strptime(_timestamp.c_str(), PREVALUE_TIME_FORMAT_OUTPUT, &tm);
  135. time_t t = mktime(&tm); // Time in Localtime (looks like timezone is not used by strptime)
  136. // struct tm * ptm;
  137. // ptm = gmtime ( &t );
  138. // time_t utc = mktime(ptm);
  139. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Use handover timestamp: " + _timestamp + " converted GMT timestamp: " + std::to_string(t));
  140. // utc = 2*t - utc; // Take care of timezone (looks difficult, but is easy: t = t + (t - utc), weil t-utc = timezone)
  141. // LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "time conversion utc after: " + std::to_string(utc));
  142. sprintf(nowTimestamp,"%ld000000000", (long) t); // UTC
  143. payload = _measurement + " " + _key + "=" + _content + " " + nowTimestamp;
  144. }
  145. else
  146. {
  147. payload = _measurement + " " + _key + "=" + _content;
  148. }
  149. payload.shrink_to_fit();
  150. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "sending line to influxdb:" + payload);
  151. // use the default retention policy of the database
  152. std::string apiURI = _influxDBURI + "/write?db=" + _influxDBDatabase;
  153. // std::string apiURI = _influxDBURI + "/api/v2/write?bucket=" + _influxDBDatabase + "/";
  154. apiURI.shrink_to_fit();
  155. http_config.url = apiURI.c_str();
  156. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "API URI: " + apiURI);
  157. esp_http_client_handle_t http_client = esp_http_client_init(&http_config);
  158. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "client is initialized");
  159. esp_http_client_set_header(http_client, "Content-Type", "text/plain");
  160. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "header is set");
  161. ESP_ERROR_CHECK(esp_http_client_set_post_field(http_client, payload.c_str(), payload.length()));
  162. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "post payload is set");
  163. esp_err_t err = ESP_ERROR_CHECK_WITHOUT_ABORT(esp_http_client_perform(http_client));
  164. if( err == ESP_OK ) {
  165. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP request was performed");
  166. int status_code = esp_http_client_get_status_code(http_client);
  167. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP status code" + std::to_string(status_code));
  168. } else {
  169. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP request failed");
  170. }
  171. esp_http_client_cleanup(http_client);
  172. }
  173. void InfluxDBInit(std::string _uri, std::string _database, std::string _user, std::string _password){
  174. _influxDBURI = _uri;
  175. _influxDBDatabase = _database;
  176. _influxDBUser = _user;
  177. _influxDBPassword = _password;
  178. }
  179. void InfluxDBdestroy() {
  180. }
  181. #endif //ENABLE_INFLUXDB