interface_influxdb.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. }
  40. return ESP_OK;
  41. }
  42. void InfluxDBPublish(std::string _key, std::string _content, std::string _timestamp) {
  43. char response_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0};
  44. esp_http_client_config_t http_config = {
  45. .user_agent = "ESP32 Meter reader",
  46. .method = HTTP_METHOD_POST,
  47. .event_handler = http_event_handler,
  48. .buffer_size = MAX_HTTP_OUTPUT_BUFFER,
  49. .user_data = response_buffer
  50. };
  51. if (_influxDBUser.length() && _influxDBPassword.length()){
  52. http_config.username = _influxDBUser.c_str();
  53. http_config.password = _influxDBPassword.c_str();
  54. http_config.auth_type = HTTP_AUTH_TYPE_BASIC;
  55. }
  56. // generate timestamp (TODO: parse result timestamp passed as string and convert it to POSIX timestamp?)
  57. time_t now = time(NULL);
  58. char nowTimestamp[21];
  59. // pad with zeroes to get nanoseconds
  60. sprintf(nowTimestamp,"%jd000000000", (intmax_t)now);
  61. std::string payload = _influxDBMeasurement + " " + _key + "=" + _content + " " + nowTimestamp;
  62. payload.shrink_to_fit();
  63. ESP_LOGI(TAG_INTERFACEINFLUXDB, "sending line to influxdb: %s\n", payload.c_str());
  64. // use the default retention policy of the database
  65. std::string apiURI = _influxDBURI + "/api/v2/write?bucket=" + _influxDBDatabase + "/";
  66. apiURI.shrink_to_fit();
  67. http_config.url = apiURI.c_str();
  68. ESP_LOGI(TAG_INTERFACEINFLUXDB, "API URI: %s", apiURI.c_str());
  69. esp_http_client_handle_t http_client = esp_http_client_init(&http_config);
  70. ESP_LOGI(TAG_INTERFACEINFLUXDB, "client is initialized%s\n", "");
  71. esp_http_client_set_header(http_client, "Content-Type", "text/plain");
  72. ESP_LOGI(TAG_INTERFACEINFLUXDB, "header is set%s\n", "");
  73. ESP_ERROR_CHECK(esp_http_client_set_post_field(http_client, payload.c_str(), payload.length()));
  74. ESP_LOGI(TAG_INTERFACEINFLUXDB, "post payload is set%s\n", "");
  75. esp_err_t err = ESP_ERROR_CHECK_WITHOUT_ABORT(esp_http_client_perform(http_client));
  76. if( err == ESP_OK ) {
  77. ESP_LOGI(TAG_INTERFACEINFLUXDB, "HTTP request was performed%s\n", "");
  78. int status_code = esp_http_client_get_status_code(http_client);
  79. ESP_LOGI(TAG_INTERFACEINFLUXDB, "HTTP status code %d\n", status_code);
  80. } else {
  81. ESP_LOGW(TAG_INTERFACEINFLUXDB, "HTTP request failed%s\n", "");
  82. }
  83. esp_http_client_cleanup(http_client);
  84. }
  85. void InfluxDBInit(std::string _uri, std::string _database, std::string _measurement, std::string _user, std::string _password){
  86. _influxDBURI = _uri;
  87. _influxDBDatabase = _database;
  88. _influxDBMeasurement = _measurement;
  89. _influxDBUser = _user;
  90. _influxDBPassword = _password;
  91. }
  92. void InfluxDBdestroy() {
  93. }