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.

64 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. 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(ANALOG_TOPIC, ANALOG_TOPIC, [](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. // Send to InfluxDB
  33. #if ENABLE_INFLUXDB
  34. influxDBSend(MQTT_TOPIC_ANALOG, analog);
  35. #endif
  36. // Update websocket clients
  37. char buffer[100];
  38. sprintf_P(buffer, PSTR("{\"analogVisible\": 1, \"analogValue\": %d}"), analog);
  39. wsSend(buffer);
  40. }
  41. }
  42. #endif