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.

135 lines
3.6 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. // Read sensor data
  55. double t = (tmpUnits == TMP_CELSIUS) ? ds18b20.getTempCByIndex(0) : ds18b20.getTempFByIndex(0);
  56. // Check if readings are valid
  57. if (isnan(t) || t < -50) {
  58. DEBUG_MSG_P(PSTR("[DS18B20] Error reading sensor\n"));
  59. } else {
  60. //If the new temperature is different from the last
  61. if (fabs(t - last_temperature) >= DS18B20_UPDATE_ON_CHANGE) {
  62. last_temperature = t;
  63. send_update = true;
  64. }
  65. _dsTemperature = t;
  66. if ((tmpUnits == TMP_CELSIUS && _dsTemperature == DEVICE_DISCONNECTED_C) ||
  67. (tmpUnits == TMP_FAHRENHEIT && _dsTemperature == DEVICE_DISCONNECTED_F))
  68. _dsIsConnected = false;
  69. else
  70. _dsIsConnected = true;
  71. dtostrf(t, 1-sizeof(_dsTemperatureStr), 1, _dsTemperatureStr);
  72. DEBUG_MSG_P(PSTR("[DS18B20] Temperature: %s%s\n"),
  73. getDSTemperatureStr(),
  74. (_dsIsConnected ? ((tmpUnits == TMP_CELSIUS) ? "ºC" : "ºF") : ""));
  75. if (send_update) {
  76. // Send MQTT messages
  77. mqttSend(getSetting("dsTmpTopic", DS18B20_TEMPERATURE_TOPIC).c_str(), _dsTemperatureStr);
  78. // Send to Domoticz
  79. #if DOMOTICZ_SUPPORT
  80. domoticzSend("dczTmpIdx", 0, _dsTemperatureStr);
  81. #endif
  82. #if INFLUXDB_SUPPORT
  83. influxDBSend(getSetting("dsTmpTopic", DS18B20_TEMPERATURE_TOPIC).c_str(), _dsTemperatureStr);
  84. #endif
  85. }
  86. // Update websocket clients
  87. #if WEB_SUPPORT
  88. char buffer[100];
  89. snprintf_P(buffer, sizeof(buffer), PSTR("{\"dsVisible\": 1, \"dsTmp\": %s, \"tmpUnits\": %d}"), getDSTemperatureStr(), tmpUnits);
  90. wsSend(buffer);
  91. #endif
  92. }
  93. }
  94. }
  95. #endif