interface_influxdb.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. // Format: #define PREVALUE_TIME_FORMAT_OUTPUT "%Y-%m-%dT%H:%M:%S%z"
  40. char nowTimestamp[21];
  41. std::string payload;
  42. if (_timestamp.length() > 0)
  43. {
  44. struct tm tm;
  45. strptime(_timestamp.c_str(), PREVALUE_TIME_FORMAT_OUTPUT, &tm);
  46. time_t t = mktime(&tm); // Time in Localtime (looks like timezone is not used by strptime)
  47. struct tm * ptm;
  48. ptm = gmtime ( &t );
  49. time_t utc = mktime(ptm);
  50. utc = 2*t - utc; // Take care of timezone (looks difficult, but is easy: t = t + (t - utc), weil t-utc = timezone)
  51. sprintf(nowTimestamp,"%ld000000000", (long) utc); // UTC
  52. payload = _influxDB_V2_Measurement + " " + _key + "=" + _content + " " + nowTimestamp;
  53. // payload = _influxDB_V2_Measurement + " " + _key + "=774 " + nowTimestamp;
  54. }
  55. else
  56. {
  57. payload = _influxDB_V2_Measurement + " " + _key + "=" + _content;
  58. // payload = _influxDB_V2_Measurement + " " + _key + "=774";
  59. }
  60. payload.shrink_to_fit();
  61. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "sending line to influxdb:" + payload);
  62. std::string apiURI = _influxDB_V2_URI + "/api/v2/write?org=" + _influxDB_V2_Org + "&bucket=" + _influxDB_V2_Database;
  63. apiURI.shrink_to_fit();
  64. http_config.url = apiURI.c_str();
  65. ESP_LOGI(TAG, "http_config: %s", http_config.url); // Add mark on log to see when it restarted
  66. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "API URI: " + apiURI);
  67. esp_http_client_handle_t http_client = esp_http_client_init(&http_config);
  68. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "client is initialized");
  69. esp_http_client_set_header(http_client, "Content-Type", "text/plain");
  70. std::string _zw = "Token " + _influxDB_V2_Token;
  71. // LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Tokenheader: %s\n", _zw.c_str());
  72. esp_http_client_set_header(http_client, "Authorization", _zw.c_str());
  73. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "header is set");
  74. ESP_ERROR_CHECK(esp_http_client_set_post_field(http_client, payload.c_str(), payload.length()));
  75. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "post payload is set");
  76. esp_err_t err = ESP_ERROR_CHECK_WITHOUT_ABORT(esp_http_client_perform(http_client));
  77. if( err == ESP_OK ) {
  78. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP request was performed");
  79. int status_code = esp_http_client_get_status_code(http_client);
  80. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP status code" + std::to_string(status_code));
  81. } else {
  82. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP request failed");
  83. }
  84. esp_http_client_cleanup(http_client);
  85. }
  86. static esp_err_t http_event_handler(esp_http_client_event_t *evt)
  87. {
  88. switch(evt->event_id)
  89. {
  90. case HTTP_EVENT_ERROR:
  91. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client Error encountered");
  92. break;
  93. case HTTP_EVENT_ON_CONNECTED:
  94. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client Error encountered");
  95. ESP_LOGI(TAG, "HTTP Client Connected");
  96. break;
  97. case HTTP_EVENT_HEADERS_SENT:
  98. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client sent all request headers");
  99. break;
  100. case HTTP_EVENT_ON_HEADER:
  101. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Header: key=" + std::string(evt->header_key) + ", value=" + std::string(evt->header_value));
  102. break;
  103. case HTTP_EVENT_ON_DATA:
  104. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client data recevied: len=" + std::to_string(evt->data_len));
  105. break;
  106. case HTTP_EVENT_ON_FINISH:
  107. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client finished");
  108. break;
  109. case HTTP_EVENT_DISCONNECTED:
  110. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client Disconnected");
  111. break;
  112. }
  113. return ESP_OK;
  114. }
  115. void InfluxDBPublish(std::string _key, std::string _content, std::string _timestamp) {
  116. char response_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0};
  117. esp_http_client_config_t http_config = {
  118. .user_agent = "ESP32 Meter reader",
  119. .method = HTTP_METHOD_POST,
  120. .event_handler = http_event_handler,
  121. .buffer_size = MAX_HTTP_OUTPUT_BUFFER,
  122. .user_data = response_buffer
  123. };
  124. if (_influxDBUser.length() && _influxDBPassword.length()){
  125. http_config.username = _influxDBUser.c_str();
  126. http_config.password = _influxDBPassword.c_str();
  127. http_config.auth_type = HTTP_AUTH_TYPE_BASIC;
  128. }
  129. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "InfluxDBPublish - Key: " + _key + ", Content: " + _content + ", Timestamp: " + _timestamp);
  130. char nowTimestamp[21];
  131. std::string payload;
  132. if (_timestamp.length() > 0)
  133. {
  134. struct tm tm;
  135. strptime(_timestamp.c_str(), PREVALUE_TIME_FORMAT_OUTPUT, &tm);
  136. time_t t = mktime(&tm); // Time in Localtime (looks like timezone is not used by strptime)
  137. struct tm * ptm;
  138. ptm = gmtime ( &t );
  139. time_t utc = mktime(ptm);
  140. utc = 2*t - utc; // Take care of timezone (looks difficult, but is easy: t = t + (t - utc), weil t-utc = timezone)
  141. sprintf(nowTimestamp,"%ld000000000", (long) utc); // UTC
  142. payload = _influxDB_V2_Measurement + " " + _key + "=" + _content + " " + nowTimestamp;
  143. // payload = _influxDB_V2_Measurement + " " + _key + "=774 " + nowTimestamp;
  144. }
  145. else
  146. {
  147. payload = _influxDBMeasurement + " " + _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 + "/api/v2/write?bucket=" + _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