Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.8 KiB

  1. /*
  2. I2C MODULE
  3. Copyright (C) 2017 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #if ENABLE_INFLUXDB
  6. #include "ESPAsyncTCP.h"
  7. #include "SyncClient.h"
  8. bool influxDBEnabled = false;
  9. SyncClient _influxClient;
  10. template<typename T> bool influxDBSend(const char * topic, T payload) {
  11. if (!influxDBEnabled) return true;
  12. if (!wifiConnected() || (WiFi.getMode() != WIFI_STA)) return true;
  13. DEBUG_MSG_P(("[INFLUXDB] Sending\n"));
  14. _influxClient.setTimeout(2);
  15. if (!_influxClient.connect(getSetting("idbHost").c_str(), getSetting("idbPort", INFLUXDB_PORT).toInt())) {
  16. DEBUG_MSG_P(("[INFLUXDB] Connection failed\n"));
  17. return false;
  18. }
  19. char data[128];
  20. sprintf(data, "%s,device=%s value=%s", topic, getSetting("hostname", HOSTNAME).c_str(), String(payload).c_str());
  21. DEBUG_MSG_P(("[INFLUXDB] Data: %s\n"), data);
  22. char request[256];
  23. sprintf(request, "POST /write?db=%s&u=%s&p=%s HTTP/1.1\r\nHost: %s:%d\r\nContent-Length: %d\r\n\r\n%s",
  24. getSetting("idbDatabase").c_str(), getSetting("idbUsername").c_str(), getSetting("idbPassword").c_str(),
  25. getSetting("idbHost").c_str(), getSetting("idbPort", INFLUXDB_PORT).toInt(),
  26. strlen(data), data);
  27. if (_influxClient.printf(request) > 0) {
  28. while (_influxClient.connected() && _influxClient.available() == 0) delay(1);
  29. while (_influxClient.available()) _influxClient.read();
  30. if (_influxClient.connected()) _influxClient.stop();
  31. return true;
  32. }
  33. _influxClient.stop();
  34. DEBUG_MSG_P(("[INFLUXDB] Sent failed\n"));
  35. while (_influxClient.connected()) delay(0);
  36. return false;
  37. }
  38. void influxDBConfigure() {
  39. influxDBEnabled = getSetting("idbHost").length() > 0;
  40. }
  41. void influxDBSetup() {
  42. influxDBConfigure();
  43. }
  44. #endif