diff --git a/code/espurna/ds18b20.ino b/code/espurna/ds18b20.ino index 8f262fc7..bf177627 100644 --- a/code/espurna/ds18b20.ino +++ b/code/espurna/ds18b20.ino @@ -14,7 +14,9 @@ Copyright (C) 2016-2017 by Xose Pérez OneWire oneWire(DS_PIN); DallasTemperature ds18b20(&oneWire); +bool _dsIsConnected = false; double _dsTemperature = 0; +char _dsTemperatureStr[6]; // ----------------------------------------------------------------------------- // DS18B20 @@ -24,6 +26,13 @@ double getDSTemperature() { return _dsTemperature; } +const char* getDSTemperatureStr() { + if (!_dsIsConnected) + return "NOT CONNECTED"; + + return _dsTemperatureStr; +} + void dsSetup() { ds18b20.begin(); ds18b20.setWaitForConversion(false); @@ -70,21 +79,29 @@ void dsLoop() { _dsTemperature = t; - char temperature[6]; - dtostrf(t, 5, 1, temperature); - DEBUG_MSG("[DS18B20] Temperature: %s%s\n", temperature, (tmpUnits == TMP_CELSIUS) ? "ºC" : "ºF"); + if ((tmpUnits == TMP_CELSIUS && _dsTemperature == DEVICE_DISCONNECTED_C) || + (tmpUnits == TMP_FAHRENHEIT && _dsTemperature == DEVICE_DISCONNECTED_F)) + _dsIsConnected = false; + else + _dsIsConnected = true; + + dtostrf(t, 5, 1, _dsTemperatureStr); + + DEBUG_MSG("[DS18B20] Temperature: %s%s\n", + getDSTemperatureStr(), + (_dsIsConnected ? ((tmpUnits == TMP_CELSIUS) ? "ºC" : "ºF") : "")); // Send MQTT messages - mqttSend(getSetting("dsTmpTopic", DS_TEMPERATURE_TOPIC).c_str(), temperature); + mqttSend(getSetting("dsTmpTopic", DS_TEMPERATURE_TOPIC).c_str(), _dsTemperatureStr); // Send to Domoticz #if ENABLE_DOMOTICZ - domoticzSend("dczTmpIdx", 0, temperature); + domoticzSend("dczTmpIdx", 0, _dsTemperatureStr); #endif // Update websocket clients char buffer[100]; - sprintf_P(buffer, PSTR("{\"dsVisible\": 1, \"dsTmp\": %s, \"tmpUnits\": %d}"), temperature, tmpUnits); + sprintf_P(buffer, PSTR("{\"dsVisible\": 1, \"dsTmp\": %s, \"tmpUnits\": %d}"), getDSTemperatureStr(), tmpUnits); wsSend(buffer); } diff --git a/code/espurna/web.ino b/code/espurna/web.ino index 4763b417..bd5894b2 100644 --- a/code/espurna/web.ino +++ b/code/espurna/web.ino @@ -451,7 +451,7 @@ void _wsStart(uint32_t client_id) { #if ENABLE_DS18B20 root["dsVisible"] = 1; - root["dsTmp"] = getDSTemperature(); + root["dsTmp"] = getDSTemperatureStr(); #endif #if ENABLE_DHT