interface_influxdb.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include "interface_influxdb.h"
  2. //#define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
  3. #include "esp_log.h"
  4. #include <time.h>
  5. #include "ClassLogFile.h"
  6. #include "esp_http_client.h"
  7. #define MAX_HTTP_OUTPUT_BUFFER 2048
  8. static const char *TAG_INTERFACEINFLUXDB = "interface_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. ESP_LOGE(TAG_INTERFACEINFLUXDB, "HTTP Client Error encountered");
  20. break;
  21. case HTTP_EVENT_ON_CONNECTED:
  22. ESP_LOGI(TAG_INTERFACEINFLUXDB, "HTTP Client Connected");
  23. break;
  24. case HTTP_EVENT_HEADERS_SENT:
  25. ESP_LOGV(TAG_INTERFACEINFLUXDB, "HTTP Client sent all request headers");
  26. break;
  27. case HTTP_EVENT_ON_HEADER:
  28. ESP_LOGV(TAG_INTERFACEINFLUXDB, "Header: key=%s, value=%s", evt->header_key, evt->header_value);
  29. break;
  30. case HTTP_EVENT_ON_DATA:
  31. ESP_LOGV(TAG_INTERFACEINFLUXDB, "HTTP Client data recevied: len=%d", evt->data_len);
  32. break;
  33. case HTTP_EVENT_ON_FINISH:
  34. ESP_LOGI(TAG_INTERFACEINFLUXDB, "HTTP Client finished");
  35. break;
  36. case HTTP_EVENT_DISCONNECTED:
  37. ESP_LOGI(TAG_INTERFACEINFLUXDB, "HTTP Client Disconnected");
  38. break;
  39. #if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0))
  40. case HTTP_EVENT_REDIRECT:
  41. ESP_LOGI(TAG_INTERFACEINFLUXDB, "HTTP Client Redirect");
  42. break;
  43. #endif
  44. }
  45. return ESP_OK;
  46. }
  47. void InfluxDBPublish(std::string _key, std::string _content, std::string _timestamp) {
  48. char response_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0};
  49. esp_http_client_config_t http_config;
  50. http_config.user_agent = "ESP32 Meter reader";
  51. http_config.method = HTTP_METHOD_POST;
  52. http_config.event_handler = http_event_handler;
  53. http_config.buffer_size = MAX_HTTP_OUTPUT_BUFFER;
  54. http_config.user_data = response_buffer;
  55. if (_influxDBUser.length() && _influxDBPassword.length()){
  56. http_config.username = _influxDBUser.c_str();
  57. http_config.password = _influxDBPassword.c_str();
  58. http_config.auth_type = HTTP_AUTH_TYPE_BASIC;
  59. }
  60. // generate timestamp (TODO: parse result timestamp passed as string and convert it to POSIX timestamp?)
  61. time_t now = time(NULL);
  62. char nowTimestamp[21];
  63. // pad with zeroes to get nanoseconds
  64. sprintf(nowTimestamp,"%jd000000000", (intmax_t)now);
  65. std::string payload = _influxDBMeasurement + " " + _key + "=" + _content + " " + nowTimestamp;
  66. payload.shrink_to_fit();
  67. ESP_LOGI(TAG_INTERFACEINFLUXDB, "sending line to influxdb: %s\n", payload.c_str());
  68. // use the default retention policy of the database
  69. std::string apiURI = _influxDBURI + "/api/v2/write?bucket=" + _influxDBDatabase + "/";
  70. apiURI.shrink_to_fit();
  71. http_config.url = apiURI.c_str();
  72. ESP_LOGI(TAG_INTERFACEINFLUXDB, "API URI: %s", apiURI.c_str());
  73. esp_http_client_handle_t http_client = esp_http_client_init(&http_config);
  74. ESP_LOGI(TAG_INTERFACEINFLUXDB, "client is initialized%s\n", "");
  75. esp_http_client_set_header(http_client, "Content-Type", "text/plain");
  76. ESP_LOGI(TAG_INTERFACEINFLUXDB, "header is set%s\n", "");
  77. ESP_ERROR_CHECK(esp_http_client_set_post_field(http_client, payload.c_str(), payload.length()));
  78. ESP_LOGI(TAG_INTERFACEINFLUXDB, "post payload is set%s\n", "");
  79. esp_err_t err = ESP_ERROR_CHECK_WITHOUT_ABORT(esp_http_client_perform(http_client));
  80. if( err == ESP_OK ) {
  81. ESP_LOGI(TAG_INTERFACEINFLUXDB, "HTTP request was performed%s\n", "");
  82. int status_code = esp_http_client_get_status_code(http_client);
  83. ESP_LOGI(TAG_INTERFACEINFLUXDB, "HTTP status code %d\n", status_code);
  84. } else {
  85. ESP_LOGW(TAG_INTERFACEINFLUXDB, "HTTP request failed%s\n", "");
  86. }
  87. esp_http_client_cleanup(http_client);
  88. }
  89. void InfluxDBInit(std::string _uri, std::string _database, std::string _measurement, std::string _user, std::string _password){
  90. _influxDBURI = _uri;
  91. _influxDBDatabase = _database;
  92. _influxDBMeasurement = _measurement;
  93. _influxDBUser = _user;
  94. _influxDBPassword = _password;
  95. }
  96. void InfluxDBdestroy() {
  97. }