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.

139 lines
3.8 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, 1-len, 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. static double last_temperature = 0.0;
  38. bool send_update = false;
  39. if ((millis() - last_update > DS18B20_UPDATE_INTERVAL) || (last_update == 0)) {
  40. if (!requested) {
  41. ds18b20.requestTemperatures();
  42. requested = true;
  43. /* Requesting takes time,
  44. * so data will probably not be available in this round */
  45. return;
  46. }
  47. /* Check if requested data is already available */
  48. if (!ds18b20.isConversionComplete()) {
  49. return;
  50. }
  51. requested = false;
  52. last_update = millis();
  53. unsigned char tmpUnits = getSetting("tmpUnits", TMP_UNITS).toInt();
  54. double tmpCorrection = getSetting("tmpCorrection", TMP_CORRECTION).toFloat();
  55. // Read sensor data
  56. double t = (tmpUnits == TMP_CELSIUS) ? ds18b20.getTempCByIndex(0) : ds18b20.getTempFByIndex(0);
  57. // apply temperature reading correction
  58. t = t + tmpCorrection;
  59. // Check if readings are valid
  60. if (isnan(t) || t < -50) {
  61. DEBUG_MSG_P(PSTR("[DS18B20] Error reading sensor\n"));
  62. } else {
  63. //If the new temperature is different from the last
  64. if (fabs(t - last_temperature) >= DS18B20_UPDATE_ON_CHANGE) {
  65. last_temperature = t;
  66. send_update = true;
  67. }
  68. _dsTemperature = t;
  69. if ((tmpUnits == TMP_CELSIUS && _dsTemperature == DEVICE_DISCONNECTED_C) ||
  70. (tmpUnits == TMP_FAHRENHEIT && _dsTemperature == DEVICE_DISCONNECTED_F))
  71. _dsIsConnected = false;
  72. else
  73. _dsIsConnected = true;
  74. dtostrf(t, 1-sizeof(_dsTemperatureStr), 1, _dsTemperatureStr);
  75. DEBUG_MSG_P(PSTR("[DS18B20] Temperature: %s%s\n"),
  76. getDSTemperatureStr(),
  77. (_dsIsConnected ? ((tmpUnits == TMP_CELSIUS) ? "ºC" : "ºF") : ""));
  78. if (send_update) {
  79. // Send MQTT messages
  80. mqttSend(getSetting("dsTmpTopic", DS18B20_TEMPERATURE_TOPIC).c_str(), _dsTemperatureStr);
  81. // Send to Domoticz
  82. #if DOMOTICZ_SUPPORT
  83. domoticzSend("dczTmpIdx", 0, _dsTemperatureStr);
  84. #endif
  85. #if INFLUXDB_SUPPORT
  86. idbSend(getSetting("dsTmpTopic", DS18B20_TEMPERATURE_TOPIC).c_str(), _dsTemperatureStr);
  87. #endif
  88. }
  89. // Update websocket clients
  90. #if WEB_SUPPORT
  91. char buffer[100];
  92. snprintf_P(buffer, sizeof(buffer), PSTR("{\"dsVisible\": 1, \"dsTmp\": %s, \"tmpUnits\": %d}"), getDSTemperatureStr(), tmpUnits);
  93. wsSend(buffer);
  94. #endif
  95. }
  96. }
  97. }
  98. #endif