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.

70 lines
1.5 KiB

  1. /*
  2. ESPurna
  3. SD18B20 MODULE
  4. Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
  5. */
  6. #if ENABLE_DS18B20
  7. #include <OneWire.h>
  8. #include <DallasTemperature.h>
  9. OneWire oneWire(DS_PIN);
  10. DallasTemperature ds18b20(&oneWire);
  11. char dsTemperature[6];
  12. // -----------------------------------------------------------------------------
  13. // DS18B20
  14. // -----------------------------------------------------------------------------
  15. char * getDSTemperature() {
  16. return dsTemperature;
  17. }
  18. void dsSetup() {
  19. ds18b20.begin();
  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. // Update websocket clients
  39. char buffer[100];
  40. sprintf_P(buffer, PSTR("{\"dsVisible\": 1, \"dsTmp\": %s}"), dsTemperature);
  41. wsSend(buffer);
  42. }
  43. }
  44. }
  45. #endif