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.

66 lines
1.5 KiB

  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. unsigned int getAnalog() {
  10. return analogRead(ANALOG_PIN);
  11. }
  12. void analogSetup() {
  13. pinMode(ANALOG_PIN, INPUT);
  14. #if WEB_SUPPORT
  15. apiRegister(ANALOG_TOPIC, ANALOG_TOPIC, [](char * buffer, size_t len) {
  16. snprintf_P(buffer, len, PSTR("%d"), getAnalog());
  17. });
  18. #endif
  19. }
  20. void analogLoop() {
  21. // Check if we should read new data
  22. static unsigned long last_update = 0;
  23. if ((millis() - last_update > ANALOG_UPDATE_INTERVAL) || (last_update == 0)) {
  24. last_update = millis();
  25. unsigned int analog = getAnalog();
  26. DEBUG_MSG_P(PSTR("[ANALOG] Value: %d\n"), analog);
  27. // Send MQTT messages
  28. mqttSend(getSetting("analogTopic", ANALOG_TOPIC).c_str(), String(analog).c_str());
  29. // Send to Domoticz
  30. #if DOMOTICZ_SUPPORT
  31. domoticzSend("dczAnaIdx", 0, String(analog).c_str());
  32. #endif
  33. // Send to InfluxDB
  34. #if INFLUXDB_SUPPORT
  35. influxDBSend(MQTT_TOPIC_ANALOG, analog);
  36. #endif
  37. // Update websocket clients
  38. #if WEB_SUPPORT
  39. char buffer[100];
  40. snprintf_P(buffer, strlen(buffer), PSTR("{\"analogVisible\": 1, \"analogValue\": %d}"), analog);
  41. wsSend(buffer);
  42. #endif
  43. }
  44. }
  45. #endif