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.4 KiB

  1. /*
  2. ANALOG MODULE
  3. Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #if ENABLE_ANALOG
  6. // -----------------------------------------------------------------------------
  7. // ANALOG
  8. // -----------------------------------------------------------------------------
  9. unsigned int getAnalog() {
  10. return analogRead(ANALOG_PIN);
  11. }
  12. void analogSetup() {
  13. pinMode(ANALOG_PIN, INPUT);
  14. apiRegister(ANALOG_TOPIC, ANALOG_TOPIC, [](char * buffer, size_t len) {
  15. snprintf(buffer, len, "%d", getAnalog());
  16. });
  17. }
  18. void analogLoop() {
  19. // Check if we should read new data
  20. static unsigned long last_update = 0;
  21. if ((millis() - last_update > ANALOG_UPDATE_INTERVAL) || (last_update == 0)) {
  22. last_update = millis();
  23. unsigned int analog = getAnalog();
  24. DEBUG_MSG_P(PSTR("[ANALOG] Value: %d\n"), analog);
  25. // Send MQTT messages
  26. mqttSend(getSetting("analogTopic", ANALOG_TOPIC).c_str(), String(analog).c_str());
  27. // Send to Domoticz
  28. #if ENABLE_DOMOTICZ
  29. domoticzSend("dczAnaIdx", 0, String(analog).c_str());
  30. #endif
  31. // Send to InfluxDB
  32. #if ENABLE_INFLUXDB
  33. influxDBSend(MQTT_TOPIC_ANALOG, analog);
  34. #endif
  35. // Update websocket clients
  36. char buffer[100];
  37. sprintf_P(buffer, PSTR("{\"analogVisible\": 1, \"analogValue\": %d}"), analog);
  38. wsSend(buffer);
  39. }
  40. }
  41. #endif