Browse Source

Merged in izikd/espurna/ds18b20 (pull request #12)

Ds18b20
fastled
Izik D 7 years ago
committed by Xose Pérez
parent
commit
1aca16b168
2 changed files with 42 additions and 8 deletions
  1. +41
    -7
      code/espurna/ds18b20.ino
  2. +1
    -1
      code/espurna/web.ino

+ 41
- 7
code/espurna/ds18b20.ino View File

@ -14,7 +14,9 @@ Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
OneWire oneWire(DS_PIN);
DallasTemperature ds18b20(&oneWire);
bool _dsIsConnected = false;
double _dsTemperature = 0;
char _dsTemperatureStr[6];
// -----------------------------------------------------------------------------
// DS18B20
@ -24,8 +26,17 @@ double getDSTemperature() {
return _dsTemperature;
}
const char* getDSTemperatureStr() {
if (!_dsIsConnected)
return "NOT CONNECTED";
return _dsTemperatureStr;
}
void dsSetup() {
ds18b20.begin();
ds18b20.setWaitForConversion(false);
apiRegister("/api/temperature", "temperature", [](char * buffer, size_t len) {
dtostrf(_dsTemperature, len-1, 1, buffer);
});
@ -35,13 +46,28 @@ void dsLoop() {
// Check if we should read new data
static unsigned long last_update = 0;
static bool requested = false;
if ((millis() - last_update > DS_UPDATE_INTERVAL) || (last_update == 0)) {
if (!requested) {
ds18b20.requestTemperatures();
requested = true;
/* Requesting takes time,
* so data will probably not be available in this round */
return;
}
/* Check if requested data is already available */
if (!ds18b20.isConversionComplete()) {
return;
}
requested = false;
last_update = millis();
unsigned char tmpUnits = getSetting("tmpUnits", TMP_UNITS).toInt();
// Read sensor data
ds18b20.requestTemperatures();
double t = (tmpUnits == TMP_CELSIUS) ? ds18b20.getTempCByIndex(0) : ds18b20.getTempFByIndex(0);
// Check if readings are valid
@ -53,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);
}


+ 1
- 1
code/espurna/web.ino View File

@ -467,7 +467,7 @@ void _wsStart(uint32_t client_id) {
#if ENABLE_DS18B20
root["dsVisible"] = 1;
root["dsTmp"] = getDSTemperature();
root["dsTmp"] = getDSTemperatureStr();
#endif
#if ENABLE_DHT


Loading…
Cancel
Save