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