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.

81 lines
1.8 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 _analogWebSocketOnSend(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. wsOnSendRegister(_analogWebSocketOnSend);
  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. #if MQTT_SUPPORT
  38. mqttSend(getSetting("analogTopic", ANALOG_TOPIC).c_str(), String(analog).c_str());
  39. #endif
  40. // Send to Domoticz
  41. #if DOMOTICZ_SUPPORT
  42. domoticzSend("dczAnaIdx", 0, String(analog).c_str());
  43. #endif
  44. // Send to InfluxDB
  45. #if INFLUXDB_SUPPORT
  46. idbSend(MQTT_TOPIC_ANALOG, analog);
  47. #endif
  48. // Update websocket clients
  49. #if WEB_SUPPORT
  50. wsSend(_analogWebSocketOnSend);
  51. #endif
  52. }
  53. }
  54. #endif