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.

79 lines
1.7 KiB

7 years ago
  1. /*
  2. ANALOG MODULE
  3. Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #if ANALOG_SUPPORT
  6. // -----------------------------------------------------------------------------
  7. // ANALOG
  8. // -----------------------------------------------------------------------------
  9. void _analogWSSend(JsonObject& root) {
  10. root["analogVisible"] = 1;
  11. root["analogValue"] = getAnalog();
  12. }
  13. // -----------------------------------------------------------------------------
  14. unsigned int getAnalog() {
  15. return analogRead(ANALOG_PIN);
  16. }
  17. void analogSetup() {
  18. pinMode(ANALOG_PIN, INPUT);
  19. #if WEB_SUPPORT
  20. // Websocket register
  21. wsRegister(_analogWSSend);
  22. // API register
  23. apiRegister(ANALOG_TOPIC, ANALOG_TOPIC, [](char * buffer, size_t len) {
  24. snprintf_P(buffer, len, PSTR("%d"), getAnalog());
  25. });
  26. #endif
  27. DEBUG_MSG_P(PSTR("[ANALOG] Monitoring analog values\n"));
  28. }
  29. void analogLoop() {
  30. // Check if we should read new data
  31. static unsigned long last_update = 0;
  32. if ((millis() - last_update > ANALOG_UPDATE_INTERVAL) || (last_update == 0)) {
  33. last_update = millis();
  34. unsigned int analog = getAnalog();
  35. DEBUG_MSG_P(PSTR("[ANALOG] Value: %d\n"), analog);
  36. // Send MQTT messages
  37. mqttSend(getSetting("analogTopic", ANALOG_TOPIC).c_str(), String(analog).c_str());
  38. // Send to Domoticz
  39. #if DOMOTICZ_SUPPORT
  40. domoticzSend("dczAnaIdx", 0, String(analog).c_str());
  41. #endif
  42. // Send to InfluxDB
  43. #if INFLUXDB_SUPPORT
  44. idbSend(MQTT_TOPIC_ANALOG, analog);
  45. #endif
  46. // Update websocket clients
  47. #if WEB_SUPPORT
  48. wsSend(_analogWSSend);
  49. #endif
  50. }
  51. }
  52. #endif