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.

69 lines
1.5 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. }
  20. void dsLoop() {
  21. if (!mqttConnected()) return;
  22. // Check if we should read new data
  23. static unsigned long last_update = 0;
  24. if ((millis() - last_update > DS_UPDATE_INTERVAL) || (last_update == 0)) {
  25. last_update = millis();
  26. // Read sensor data
  27. ds18b20.requestTemperatures();
  28. double t = ds18b20.getTempCByIndex(0);
  29. // Check if readings are valid
  30. if (isnan(t)) {
  31. DEBUG_MSG("[DS18B20] Error reading sensor\n");
  32. } else {
  33. dtostrf(t, 4, 1, dsTemperature);
  34. DEBUG_MSG("[DS18B20] Temperature: %s\n", dsTemperature);
  35. // Send MQTT messages
  36. mqttSend(getSetting("dsTmpTopic", DS_TEMPERATURE_TOPIC).c_str(), dsTemperature);
  37. // Update websocket clients
  38. char buffer[100];
  39. sprintf_P(buffer, PSTR("{\"dsVisible\": 1, \"dsTmp\": %s}"), dsTemperature);
  40. wsSend(buffer);
  41. }
  42. }
  43. }
  44. #endif