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.

75 lines
1.7 KiB

  1. /*
  2. DS18B20 MODULE
  3. Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #if ENABLE_DS18B20
  6. #include <OneWire.h>
  7. #include <DallasTemperature.h>
  8. OneWire oneWire(DS_PIN);
  9. DallasTemperature ds18b20(&oneWire);
  10. char dsTemperature[6];
  11. // -----------------------------------------------------------------------------
  12. // DS18B20
  13. // -----------------------------------------------------------------------------
  14. char * getDSTemperature() {
  15. return dsTemperature;
  16. }
  17. void dsSetup() {
  18. ds18b20.begin();
  19. apiRegister("/api/temperature", "temperature", getDSTemperature);
  20. }
  21. void dsLoop() {
  22. if (!mqttConnected()) return;
  23. // Check if we should read new data
  24. static unsigned long last_update = 0;
  25. if ((millis() - last_update > DS_UPDATE_INTERVAL) || (last_update == 0)) {
  26. last_update = millis();
  27. // Read sensor data
  28. ds18b20.requestTemperatures();
  29. double t = ds18b20.getTempCByIndex(0);
  30. // Check if readings are valid
  31. if (isnan(t)) {
  32. DEBUG_MSG("[DS18B20] Error reading sensor\n");
  33. } else {
  34. dtostrf(t, 4, 1, dsTemperature);
  35. DEBUG_MSG("[DS18B20] Temperature: %s\n", dsTemperature);
  36. // Send MQTT messages
  37. mqttSend(getSetting("dsTmpTopic", DS_TEMPERATURE_TOPIC).c_str(), dsTemperature);
  38. // Send to Domoticz
  39. #if ENABLE_DOMOTICZ
  40. domoticzSend("dczTmpIdx", dsTemperature);
  41. #endif
  42. // Update websocket clients
  43. char buffer[100];
  44. sprintf_P(buffer, PSTR("{\"dsVisible\": 1, \"dsTmp\": %s}"), dsTemperature);
  45. wsSend(buffer);
  46. }
  47. }
  48. }
  49. #endif