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.

123 lines
3.2 KiB

  1. /*
  2. DS18B20 MODULE
  3. Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #if DS18B20_SUPPORT
  6. #include <OneWire.h>
  7. #include <DallasTemperature.h>
  8. OneWire oneWire(DS18B20_PIN);
  9. DallasTemperature ds18b20(&oneWire);
  10. bool _dsIsConnected = false;
  11. double _dsTemperature = 0;
  12. char _dsTemperatureStr[6];
  13. // -----------------------------------------------------------------------------
  14. // DS18B20
  15. // -----------------------------------------------------------------------------
  16. double getDSTemperature() {
  17. return _dsTemperature;
  18. }
  19. const char* getDSTemperatureStr() {
  20. if (!_dsIsConnected)
  21. return "NOT CONNECTED";
  22. return _dsTemperatureStr;
  23. }
  24. void dsSetup() {
  25. ds18b20.begin();
  26. ds18b20.setWaitForConversion(false);
  27. #if WEB_SUPPORT
  28. apiRegister(DS18B20_TEMPERATURE_TOPIC, DS18B20_TEMPERATURE_TOPIC, [](char * buffer, size_t len) {
  29. dtostrf(_dsTemperature, len-1, 1, buffer);
  30. });
  31. #endif
  32. }
  33. void dsLoop() {
  34. // Check if we should read new data
  35. static unsigned long last_update = 0;
  36. static bool requested = false;
  37. if ((millis() - last_update > DS18B20_UPDATE_INTERVAL) || (last_update == 0)) {
  38. if (!requested) {
  39. ds18b20.requestTemperatures();
  40. requested = true;
  41. /* Requesting takes time,
  42. * so data will probably not be available in this round */
  43. return;
  44. }
  45. /* Check if requested data is already available */
  46. if (!ds18b20.isConversionComplete()) {
  47. return;
  48. }
  49. requested = false;
  50. last_update = millis();
  51. unsigned char tmpUnits = getSetting("tmpUnits", TMP_UNITS).toInt();
  52. // Read sensor data
  53. double t = (tmpUnits == TMP_CELSIUS) ? ds18b20.getTempCByIndex(0) : ds18b20.getTempFByIndex(0);
  54. // Check if readings are valid
  55. if (isnan(t) || t < -50) {
  56. DEBUG_MSG_P(PSTR("[DS18B20] Error reading sensor\n"));
  57. } else {
  58. _dsTemperature = t;
  59. if ((tmpUnits == TMP_CELSIUS && _dsTemperature == DEVICE_DISCONNECTED_C) ||
  60. (tmpUnits == TMP_FAHRENHEIT && _dsTemperature == DEVICE_DISCONNECTED_F))
  61. _dsIsConnected = false;
  62. else
  63. _dsIsConnected = true;
  64. dtostrf(t, 5, 1, _dsTemperatureStr);
  65. DEBUG_MSG_P(PSTR("[DS18B20] Temperature: %s%s\n"),
  66. getDSTemperatureStr(),
  67. (_dsIsConnected ? ((tmpUnits == TMP_CELSIUS) ? "ºC" : "ºF") : ""));
  68. // Send MQTT messages
  69. mqttSend(getSetting("dsTmpTopic", DS18B20_TEMPERATURE_TOPIC).c_str(), _dsTemperatureStr);
  70. // Send to Domoticz
  71. #if DOMOTICZ_SUPPORT
  72. domoticzSend("dczTmpIdx", 0, _dsTemperatureStr);
  73. #endif
  74. #if INFLUXDB_SUPPORT
  75. influxDBSend(getSetting("dsTmpTopic", DS18B20_TEMPERATURE_TOPIC).c_str(), _dsTemperatureStr);
  76. #endif
  77. // Update websocket clients
  78. #if WEB_SUPPORT
  79. char buffer[100];
  80. snprintf_P(buffer, strlen(buffer), PSTR("{\"dsVisible\": 1, \"dsTmp\": %s, \"tmpUnits\": %d}"), getDSTemperatureStr(), tmpUnits);
  81. wsSend(buffer);
  82. #endif
  83. }
  84. }
  85. }
  86. #endif