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.

59 lines
1.6 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. template<typename T> bool influxDBSend(const char * topic, T payload) {
  10. if (!influxDBEnabled) return true;
  11. SyncClient client;
  12. if (!client.connect(getSetting("idbHost").c_str(), getSetting("idbPort", INFLUXDB_PORT).toInt())) {
  13. DEBUG_MSG_P(("[INFLUXDB] Connection failed\n"));
  14. return false;
  15. }
  16. client.setTimeout(2);
  17. char data[64];
  18. sprintf(data, "%s,device=%s value=%s", topic, getSetting("hostname", HOSTNAME).c_str(), String(payload).c_str());
  19. DEBUG_MSG_P(("[INFLUXDB] Data: %s\n"), data);
  20. char request[250];
  21. 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",
  22. getSetting("idbDatabase").c_str(), getSetting("idbUsername").c_str(), getSetting("idbPassword").c_str(),
  23. getSetting("idbHost").c_str(), getSetting("idbPort", INFLUXDB_PORT).toInt(),
  24. strlen(data), data);
  25. if (client.printf(request) > 0) {
  26. while (client.connected() && client.available() == 0) delay(1);
  27. while (client.available()) client.read();
  28. if (client.connected()) client.stop();
  29. return true;
  30. }
  31. client.stop();
  32. DEBUG_MSG_P(("[INFLUXDB] Sent failed\n"));
  33. while (client.connected()) delay(0);
  34. return false;
  35. }
  36. void influxDBConfigure() {
  37. influxDBEnabled = getSetting("idbHost").length() > 0;
  38. }
  39. void influxDBSetup() {
  40. influxDBConfigure();
  41. }
  42. #endif