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.

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