interface_influxdb.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. std::string _influxDB_V2_URI;
  15. std::string _influxDB_V2_Database;
  16. std::string _influxDB_V2_Measurement;
  17. std::string _influxDB_V2_Token;
  18. std::string _influxDB_V2_Org;
  19. static esp_err_t http_event_handler(esp_http_client_event_t *evt);
  20. void InfluxDB_V2_Init(std::string _uri, std::string _database, std::string _measurement, std::string _org, std::string _token)
  21. {
  22. _influxDB_V2_URI = _uri;
  23. _influxDB_V2_Database = _database;
  24. _influxDB_V2_Measurement = _measurement;
  25. _influxDB_V2_Org = _org;
  26. _influxDB_V2_Token = _token;
  27. }
  28. void InfluxDB_V2_Publish(std::string _key, std::string _content, std::string _timestamp)
  29. {
  30. char response_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0};
  31. esp_http_client_config_t http_config = {
  32. .user_agent = "ESP32 Meter reader",
  33. .method = HTTP_METHOD_POST,
  34. .event_handler = http_event_handler,
  35. .buffer_size = MAX_HTTP_OUTPUT_BUFFER,
  36. .user_data = response_buffer
  37. };
  38. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "InfluxDB_V2_Publish - Key: " + _key + ", Content: " + _content + ", Timestamp: " + _timestamp);
  39. std::string payload;
  40. char nowTimestamp[21];
  41. if (_timestamp.length() > 0)
  42. {
  43. struct tm tm;
  44. strptime(_timestamp.c_str(), PREVALUE_TIME_FORMAT_OUTPUT, &tm);
  45. time_t t = mktime(&tm); // Time in Localtime (looks like timezone is not used by strptime)
  46. // struct tm * ptm;
  47. // ptm = gmtime ( &t );
  48. // time_t utc = mktime(ptm);
  49. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Use handover timestamp: " + _timestamp + " converted GMT timestamp: " + std::to_string(t));
  50. // utc = 2*t - utc; // Take care of timezone (looks difficult, but is easy: t = t + (t - utc), weil t-utc = timezone)
  51. // LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "time conversion utc after: " + std::to_string(utc));
  52. sprintf(nowTimestamp,"%ld000000000", (long) t); // UTC
  53. payload = _influxDB_V2_Measurement + " " + _key + "=" + _content + " " + nowTimestamp;
  54. }
  55. else
  56. {
  57. payload = _influxDB_V2_Measurement + " " + _key + "=" + _content;
  58. }
  59. payload.shrink_to_fit();
  60. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "sending line to influxdb:" + payload);
  61. std::string apiURI = _influxDB_V2_URI + "/api/v2/write?org=" + _influxDB_V2_Org + "&bucket=" + _influxDB_V2_Database;
  62. apiURI.shrink_to_fit();
  63. http_config.url = apiURI.c_str();
  64. ESP_LOGI(TAG, "http_config: %s", http_config.url); // Add mark on log to see when it restarted
  65. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "API URI: " + apiURI);
  66. esp_http_client_handle_t http_client = esp_http_client_init(&http_config);
  67. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "client is initialized");
  68. esp_http_client_set_header(http_client, "Content-Type", "text/plain");
  69. std::string _zw = "Token " + _influxDB_V2_Token;
  70. // LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Tokenheader: %s\n", _zw.c_str());
  71. esp_http_client_set_header(http_client, "Authorization", _zw.c_str());
  72. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "header is set");
  73. ESP_ERROR_CHECK(esp_http_client_set_post_field(http_client, payload.c_str(), payload.length()));
  74. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "post payload is set");
  75. esp_err_t err = ESP_ERROR_CHECK_WITHOUT_ABORT(esp_http_client_perform(http_client));
  76. if( err == ESP_OK ) {
  77. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP request was performed");
  78. int status_code = esp_http_client_get_status_code(http_client);
  79. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP status code" + std::to_string(status_code));
  80. } else {
  81. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP request failed");
  82. }
  83. esp_http_client_cleanup(http_client);
  84. }
  85. static esp_err_t http_event_handler(esp_http_client_event_t *evt)
  86. {
  87. switch(evt->event_id)
  88. {
  89. case HTTP_EVENT_ERROR:
  90. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client Error encountered");
  91. break;
  92. case HTTP_EVENT_ON_CONNECTED:
  93. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client Error encountered");
  94. ESP_LOGI(TAG, "HTTP Client Connected");
  95. break;
  96. case HTTP_EVENT_HEADERS_SENT:
  97. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client sent all request headers");
  98. break;
  99. case HTTP_EVENT_ON_HEADER:
  100. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Header: key=" + std::string(evt->header_key) + ", value=" + std::string(evt->header_value));
  101. break;
  102. case HTTP_EVENT_ON_DATA:
  103. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client data recevied: len=" + std::to_string(evt->data_len));
  104. break;
  105. case HTTP_EVENT_ON_FINISH:
  106. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client finished");
  107. break;
  108. case HTTP_EVENT_DISCONNECTED:
  109. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client Disconnected");
  110. break;
  111. }
  112. return ESP_OK;
  113. }
  114. void InfluxDBPublish(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 = _influxDBMeasurement + " " + _key + "=" + _content + " " + nowTimestamp;
  144. }
  145. else
  146. {
  147. payload = _influxDB_V2_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. apiURI.shrink_to_fit();
  154. http_config.url = apiURI.c_str();
  155. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "API URI: " + apiURI);
  156. esp_http_client_handle_t http_client = esp_http_client_init(&http_config);
  157. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "client is initialized");
  158. esp_http_client_set_header(http_client, "Content-Type", "text/plain");
  159. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "header is set");
  160. ESP_ERROR_CHECK(esp_http_client_set_post_field(http_client, payload.c_str(), payload.length()));
  161. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "post payload is set");
  162. esp_err_t err = ESP_ERROR_CHECK_WITHOUT_ABORT(esp_http_client_perform(http_client));
  163. if( err == ESP_OK ) {
  164. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP request was performed");
  165. int status_code = esp_http_client_get_status_code(http_client);
  166. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP status code" + std::to_string(status_code));
  167. } else {
  168. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP request failed");
  169. }
  170. esp_http_client_cleanup(http_client);
  171. }
  172. void InfluxDBInit(std::string _uri, std::string _database, std::string _measurement, std::string _user, std::string _password){
  173. _influxDBURI = _uri;
  174. _influxDBDatabase = _database;
  175. _influxDBMeasurement = _measurement;
  176. _influxDBUser = _user;
  177. _influxDBPassword = _password;
  178. }
  179. void InfluxDBdestroy() {
  180. }
  181. #endif //ENABLE_INFLUXDB