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.

145 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. // -----------------------------------------------------------------------------
  13. // Private
  14. // -----------------------------------------------------------------------------
  15. void _dsWebSocketOnSend(JsonObject& root) {
  16. root["dsVisible"] = 1;
  17. root["dsConnected"] = getDSIsConnected();
  18. if (getDSIsConnected()) {
  19. root["dsTmp"] = getDSTemperature();
  20. }
  21. root["tmpUnits"] = getSetting("tmpUnits", TMP_UNITS).toInt();
  22. }
  23. // -----------------------------------------------------------------------------
  24. // DS18B20
  25. // -----------------------------------------------------------------------------
  26. bool getDSIsConnected() {
  27. return _dsIsConnected;
  28. }
  29. double getDSTemperature(bool celsius) {
  30. double value = celsius ? _dsTemperature : _dsTemperature * 1.8 + 32;
  31. double correction = getSetting("tmpCorrection", TEMPERATURE_CORRECTION).toFloat();
  32. return roundTo(value + correction, TEMPERATURE_DECIMALS);
  33. }
  34. double getDSTemperature() {
  35. bool celsius = getSetting("tmpUnits", TMP_UNITS).toInt() == TMP_CELSIUS;
  36. return getDSTemperature(celsius);
  37. }
  38. void dsSetup() {
  39. #if DS18B20_PULLUP
  40. pinMode(DS18B20_PIN, INPUT_PULLUP);
  41. #endif
  42. ds18b20.begin();
  43. ds18b20.setWaitForConversion(false);
  44. #if WEB_SUPPORT
  45. wsOnSendRegister(_dsWebSocketOnSend);
  46. apiRegister(DS18B20_TEMPERATURE_TOPIC, DS18B20_TEMPERATURE_TOPIC, [](char * buffer, size_t len) {
  47. dtostrf(getDSTemperature(), 1-len, 1, buffer);
  48. });
  49. #endif
  50. }
  51. void dsLoop() {
  52. static unsigned long last_update = 0;
  53. static double last_temperature = 0.0;
  54. static bool requested = false;
  55. if ((millis() - last_update > DS18B20_UPDATE_INTERVAL) || (last_update == 0)) {
  56. if (!requested) {
  57. ds18b20.requestTemperatures();
  58. requested = true;
  59. // Requesting takes time, so data will probably not be available in this round
  60. return;
  61. }
  62. // Check if requested data is already available
  63. if (!ds18b20.isConversionComplete()) return;
  64. requested = false;
  65. last_update = millis();
  66. // Read sensor data
  67. double t = ds18b20.getTempCByIndex(0);
  68. // Check returned value
  69. if (t == DEVICE_DISCONNECTED_C) {
  70. _dsIsConnected = false;
  71. DEBUG_MSG_P(PSTR("[DS18B20] Not connected\n"));
  72. return;
  73. } else {
  74. _dsIsConnected = true;
  75. }
  76. // Save & convert
  77. _dsTemperature = t;
  78. bool celsius = getSetting("tmpUnits", TMP_UNITS).toInt() == TMP_CELSIUS;
  79. t = getDSTemperature(celsius);
  80. // Build string
  81. char temperature[6];
  82. dtostrf(getDSTemperature(celsius), 1-sizeof(temperature), 1, temperature);
  83. // Debug
  84. DEBUG_MSG_P(PSTR("[DS18B20] Temperature: %s%s\n"), temperature, celsius ? "ºC" : "ºF");
  85. // If the new temperature is different from the last
  86. if (fabs(_dsTemperature - last_temperature) >= TEMPERATURE_MIN_CHANGE) {
  87. last_temperature = _dsTemperature;
  88. // Send MQTT messages
  89. mqttSend(getSetting("dsTmpTopic", DS18B20_TEMPERATURE_TOPIC).c_str(), temperature);
  90. // Send to Domoticz
  91. #if DOMOTICZ_SUPPORT
  92. domoticzSend("dczTmpIdx", 0, temperature);
  93. #endif
  94. #if INFLUXDB_SUPPORT
  95. idbSend(getSetting("dsTmpTopic", DS18B20_TEMPERATURE_TOPIC).c_str(), temperature);
  96. #endif
  97. }
  98. // Update websocket clients
  99. #if WEB_SUPPORT
  100. wsSend(_dsWebSocketOnSend);
  101. #endif
  102. }
  103. }
  104. #endif