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.

68 lines
1.6 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. 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. DEBUG_MSG_P(PSTR("[ANALOG] Monitoring analog values\n"));
  20. }
  21. void analogLoop() {
  22. // Check if we should read new data
  23. static unsigned long last_update = 0;
  24. if ((millis() - last_update > ANALOG_UPDATE_INTERVAL) || (last_update == 0)) {
  25. last_update = millis();
  26. unsigned int analog = getAnalog();
  27. DEBUG_MSG_P(PSTR("[ANALOG] Value: %d\n"), analog);
  28. // Send MQTT messages
  29. mqttSend(getSetting("analogTopic", ANALOG_TOPIC).c_str(), String(analog).c_str());
  30. // Send to Domoticz
  31. #if DOMOTICZ_SUPPORT
  32. domoticzSend("dczAnaIdx", 0, String(analog).c_str());
  33. #endif
  34. // Send to InfluxDB
  35. #if INFLUXDB_SUPPORT
  36. influxDBSend(MQTT_TOPIC_ANALOG, analog);
  37. #endif
  38. // Update websocket clients
  39. #if WEB_SUPPORT
  40. char buffer[100];
  41. snprintf_P(buffer, sizeof(buffer), PSTR("{\"analogVisible\": 1, \"analogValue\": %d}"), analog);
  42. wsSend(buffer);
  43. #endif
  44. }
  45. }
  46. #endif