interface_influxdb.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. // Format: #define PREVALUE_TIME_FORMAT_OUTPUT "%Y-%m-%dT%H:%M:%S%z"
  59. struct tm tm;
  60. strptime(_timestamp.c_str(), PREVALUE_TIME_FORMAT_OUTPUT, &tm);
  61. time_t t = mktime(&tm); // Time in Localtime (looks like timezone is not used by strptime)
  62. struct tm * ptm;
  63. ptm = gmtime ( &t );
  64. time_t utc = mktime(ptm);
  65. utc = 2*t - utc;
  66. char nowTimestamp[21];
  67. sprintf(nowTimestamp,"%ld000000000", (long) utc); // UTC
  68. // LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Test Time Conversion - t: " + std::to_string(t) + ", utc: " + std::to_string(utc) + ", now: " + std::to_string(now) + ", utc_local: " + std::to_string(utc_local));
  69. std::string payload = _influxDBMeasurement + " " + _key + "=" + _content + " " + nowTimestamp;
  70. payload.shrink_to_fit();
  71. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "sending line to influxdb:" + payload);
  72. // use the default retention policy of the database
  73. std::string apiURI = _influxDBURI + "/api/v2/write?bucket=" + _influxDBDatabase + "/";
  74. apiURI.shrink_to_fit();
  75. http_config.url = apiURI.c_str();
  76. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "API URI: " + apiURI);
  77. esp_http_client_handle_t http_client = esp_http_client_init(&http_config);
  78. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "client is initialized");
  79. esp_http_client_set_header(http_client, "Content-Type", "text/plain");
  80. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "header is set");
  81. ESP_ERROR_CHECK(esp_http_client_set_post_field(http_client, payload.c_str(), payload.length()));
  82. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "post payload is set");
  83. esp_err_t err = ESP_ERROR_CHECK_WITHOUT_ABORT(esp_http_client_perform(http_client));
  84. if( err == ESP_OK ) {
  85. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP request was performed");
  86. int status_code = esp_http_client_get_status_code(http_client);
  87. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP status code" + std::to_string(status_code));
  88. } else {
  89. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP request failed");
  90. }
  91. esp_http_client_cleanup(http_client);
  92. }
  93. void InfluxDBInit(std::string _uri, std::string _database, std::string _measurement, std::string _user, std::string _password){
  94. _influxDBURI = _uri;
  95. _influxDBDatabase = _database;
  96. _influxDBMeasurement = _measurement;
  97. _influxDBUser = _user;
  98. _influxDBPassword = _password;
  99. }
  100. void InfluxDBdestroy() {
  101. }
  102. #endif //ENABLE_INFLUXDB