Browse Source

Clean DHT and DS18B20 modules

fastled
Xose Pérez 7 years ago
parent
commit
5275f1e70c
8 changed files with 782 additions and 732 deletions
  1. +1
    -1
      code/espurna/config/hardware.h
  2. +23
    -7
      code/espurna/config/sensors.h
  3. BIN
      code/espurna/data/index.html.gz
  4. +55
    -25
      code/espurna/dht.ino
  5. +63
    -69
      code/espurna/ds18b20.ino
  6. +630
    -627
      code/espurna/static/index.html.gz.h
  7. +8
    -1
      code/html/custom.js
  8. +2
    -2
      code/html/index.html

+ 1
- 1
code/espurna/config/hardware.h View File

@ -1034,7 +1034,7 @@
#define DS18B20_SUPPORT 1
#define DS18B20_PIN 2
#define DS18B20_UPDATE_INTERVAL 5000
#define DS18B20_UPDATE_ON_CHANGE 1.0
#define TEMPERATURE_MIN_CHANGE 1.0
// -----------------------------------------------------------------------------
// QuinLED


+ 23
- 7
code/espurna/config/sensors.h View File

@ -15,6 +15,21 @@
#define RF_CHANNEL 31
#define RF_DEVICE 1
//--------------------------------------------------------------------------------
// General
//--------------------------------------------------------------------------------
#ifndef TEMPERATURE_MIN_CHANGE
#define TEMPERATURE_MIN_CHANGE 0.0 // Minimum temperature change to report
#endif
#ifndef HUMIDITY_MIN_CHANGE
#define HUMIDITY_MIN_CHANGE 0 // Minimum humidity change to report
#endif
#define TEMPERATURE_CORRECTION 0.0 // This is both for DHT and DS18B20
#define TEMPERATURE_DECIMALS 1 // Decimals for temperature values
//--------------------------------------------------------------------------------
// DHTXX temperature/humidity sensor
// Enable support by passing DHT_SUPPORT=1 build flag
@ -32,21 +47,22 @@
#define DHT_TYPE DHT22
#endif
#ifndef DHT_PULLUP
#define DHT_PULLUP 0
#endif
#ifndef DHT_UPDATE_INTERVAL
#define DHT_UPDATE_INTERVAL 60000
#endif
#define DHT_TEMPERATURE_TOPIC "temperature"
#define DHT_HUMIDITY_TOPIC "humidity"
#define DHT_TEMPERATURE_DECIMALS 1 // Decimals for temperature values
#define HUMIDITY_NORMAL 0
#define HUMIDITY_COMFORTABLE 1
#define HUMIDITY_DRY 2
#define HUMIDITY_WET 3
#define TEMPERATURE_CORRECTION 0.0 // This is both for DHT and DS18B20
//--------------------------------------------------------------------------------
// Analog sensor
// Enable support by passing ANALOG_SUPPORT=1 build flag
@ -113,6 +129,10 @@
#define DS18B20_PIN 14
#endif
#ifndef DS18B20_PULLUP
#define DS18B20_PULLUP 1
#endif
#ifndef DS18B20_UPDATE_INTERVAL
#define DS18B20_UPDATE_INTERVAL 60000
#endif
@ -121,10 +141,6 @@
#define DS18B20_TEMPERATURE_TOPIC "temperature"
#endif
#ifndef DS18B20_UPDATE_ON_CHANGE
#define DS18B20_UPDATE_ON_CHANGE 0.0
#endif
//--------------------------------------------------------------------------------
// Internal power montior
// Enable support by passing ADC_VCC_ENABLED=1 build flag


BIN
code/espurna/data/index.html.gz View File


+ 55
- 25
code/espurna/dht.ino View File

@ -10,6 +10,7 @@ Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
double _dhtTemperature = 0;
unsigned int _dhtHumidity = 0;
bool _dhtIsConnected = false;
// -----------------------------------------------------------------------------
// HAL
@ -40,7 +41,7 @@ unsigned long _getSignalLevel(unsigned char gpio, int usTimeOut, bool state) {
int readDHT(unsigned char gpio, unsigned char type) {
static unsigned long last_ok = 0;
if (millis() - last_ok < DHT_MIN_INTERVAL) return DHT_OK;
if ((last_ok > 0) && (millis() - last_ok < DHT_MIN_INTERVAL)) return DHT_OK;
unsigned long low = 0;
unsigned long high = 0;
@ -137,22 +138,31 @@ int readDHT() {
void _dhtWebSocketOnSend(JsonObject& root) {
root["dhtVisible"] = 1;
root["dhtTmp"] = getDHTTemperature();
root["dhtHum"] = getDHTHumidity();
root["dhtConnected"] = getDHTIsConnected();
if (getDHTIsConnected()) {
root["dhtTmp"] = getDHTTemperature();
root["dhtHum"] = getDHTHumidity();
}
root["tmpUnits"] = getSetting("tmpUnits", TMP_UNITS).toInt();
}
// -----------------------------------------------------------------------------
// Values
// -----------------------------------------------------------------------------
bool getDHTIsConnected() {
return _dhtIsConnected;
}
double getDHTTemperature(bool celsius) {
double value = celsius ? _dhtTemperature : _dhtTemperature * 1.8 + 32;
double correction = getSetting("tmpCorrection", TEMPERATURE_CORRECTION).toFloat();
return roundTo(value + correction, DHT_TEMPERATURE_DECIMALS);
return roundTo(value + correction, TEMPERATURE_DECIMALS);
}
double getDHTTemperature() {
return getDHTTemperature(true);
bool celsius = getSetting("tmpUnits", TMP_UNITS).toInt() == TMP_CELSIUS;
return getDHTTemperature(celsius);
}
unsigned int getDHTHumidity() {
@ -161,16 +171,20 @@ unsigned int getDHTHumidity() {
void dhtSetup() {
#if DHT_PULLUP
pinMode(DHT_PIN, INPUT_PULLUP);
#endif
#if WEB_SUPPORT
// Websockets
wsOnSendRegister(_dhtWebSocketOnSend);
apiRegister(DHT_TEMPERATURE_TOPIC, DHT_TEMPERATURE_TOPIC, [](char * buffer, size_t len) {
dtostrf(_dhtTemperature, 1-len, 1, buffer);
dtostrf(getDHTTemperature(), 1-len, 1, buffer);
});
apiRegister(DHT_HUMIDITY_TOPIC, DHT_HUMIDITY_TOPIC, [](char * buffer, size_t len) {
snprintf_P(buffer, len, PSTR("%d"), _dhtHumidity);
snprintf_P(buffer, len, PSTR("%d"), getDHTHumidity());
});
#endif
@ -179,25 +193,43 @@ void dhtSetup() {
void dhtLoop() {
// Check if we should read new data
static unsigned long last_update = 0;
static double last_temperature = 0.0;
static unsigned int last_humidity = 0;
// Check if we should read new data
if ((millis() - last_update > DHT_UPDATE_INTERVAL) || (last_update == 0)) {
last_update = millis();
// Read sensor data
if (readDHT(DHT_PIN, DHT_TYPE) == DHT_OK) {
int response = readDHT(DHT_PIN, DHT_TYPE);
if (response != DHT_OK) {
DEBUG_MSG_P(PSTR("[DHT] Error: %d\n"), response);
return;
}
_dhtIsConnected = true;
// Get values
bool celsius = getSetting("tmpUnits", TMP_UNITS).toInt() == TMP_CELSIUS;
double t = getDHTTemperature(celsius);
unsigned int h = getDHTHumidity();
// Build strings
char temperature[6];
char humidity[6];
dtostrf(t, 1-sizeof(temperature), 1, temperature);
itoa((unsigned int) h, humidity, 10);
unsigned char tmpUnits = getSetting("tmpUnits", TMP_UNITS).toInt();
double t = getDHTTemperature(tmpUnits == TMP_CELSIUS);
unsigned int h = getDHTHumidity();
// Debug
DEBUG_MSG_P(PSTR("[DHT] Temperature: %s%s\n"), temperature, celsius ? "ºC" : "ºF");
DEBUG_MSG_P(PSTR("[DHT] Humidity: %s\n"), humidity);
char temperature[6];
char humidity[6];
dtostrf(t, 1-sizeof(temperature), 1, temperature);
itoa((unsigned int) h, humidity, 10);
// If the new temperature & humidity are different from the last
if ((fabs(t - last_temperature) >= TEMPERATURE_MIN_CHANGE)
|| (abs(h - last_humidity) >= HUMIDITY_MIN_CHANGE)) {
DEBUG_MSG_P(PSTR("[DHT] Temperature: %s%s\n"), temperature, (tmpUnits == TMP_CELSIUS) ? "ºC" : "ºF");
DEBUG_MSG_P(PSTR("[DHT] Humidity: %s\n"), humidity);
last_temperature = t;
last_humidity = h;
// Send MQTT messages
mqttSend(getSetting("dhtTmpTopic", DHT_TEMPERATURE_TOPIC).c_str(), temperature);
@ -228,15 +260,13 @@ void dhtLoop() {
idbSend(getSetting("dhtHumTopic", DHT_HUMIDITY_TOPIC).c_str(), humidity);
#endif
// Update websocket clients
#if WEB_SUPPORT
char buffer[100];
snprintf_P(buffer, sizeof(buffer), PSTR("{\"dhtVisible\": 1, \"dhtTmp\": %s, \"dhtHum\": %s, \"tmpUnits\": %d}"), temperature, humidity, tmpUnits);
wsSend(buffer);
#endif
}
// Update websocket clients
#if WEB_SUPPORT
wsSend(_dhtWebSocketOnSend);
#endif
}
}


+ 63
- 69
code/espurna/ds18b20.ino View File

@ -16,7 +16,6 @@ DallasTemperature ds18b20(&oneWire);
bool _dsIsConnected = false;
double _dsTemperature = 0;
char _dsTemperatureStr[6];
// -----------------------------------------------------------------------------
// Private
@ -24,26 +23,38 @@ char _dsTemperatureStr[6];
void _dsWebSocketOnSend(JsonObject& root) {
root["dsVisible"] = 1;
root["dsTmp"] = getDSTemperatureStr();
root["dsConnected"] = getDSIsConnected();
if (getDSIsConnected()) {
root["dsTmp"] = getDSTemperature();
}
root["tmpUnits"] = getSetting("tmpUnits", TMP_UNITS).toInt();
}
// -----------------------------------------------------------------------------
// DS18B20
// -----------------------------------------------------------------------------
double getDSTemperature() {
return _dsTemperature;
bool getDSIsConnected() {
return _dsIsConnected;
}
const char* getDSTemperatureStr() {
if (!_dsIsConnected)
return "NOT CONNECTED";
double getDSTemperature(bool celsius) {
double value = celsius ? _dsTemperature : _dsTemperature * 1.8 + 32;
double correction = getSetting("tmpCorrection", TEMPERATURE_CORRECTION).toFloat();
return roundTo(value + correction, TEMPERATURE_DECIMALS);
}
return _dsTemperatureStr;
double getDSTemperature() {
bool celsius = getSetting("tmpUnits", TMP_UNITS).toInt() == TMP_CELSIUS;
return getDSTemperature(celsius);
}
void dsSetup() {
#if DS18B20_PULLUP
pinMode(DS18B20_PIN, INPUT_PULLUP);
#endif
ds18b20.begin();
ds18b20.setWaitForConversion(false);
@ -52,7 +63,7 @@ void dsSetup() {
wsOnSendRegister(_dsWebSocketOnSend);
apiRegister(DS18B20_TEMPERATURE_TOPIC, DS18B20_TEMPERATURE_TOPIC, [](char * buffer, size_t len) {
dtostrf(_dsTemperature, 1-len, 1, buffer);
dtostrf(getDSTemperature(), 1-len, 1, buffer);
});
#endif
@ -61,89 +72,72 @@ void dsSetup() {
void dsLoop() {
// Check if we should read new data
static unsigned long last_update = 0;
static bool requested = false;
static double last_temperature = 0.0;
bool send_update = false;
static bool requested = false;
if ((millis() - last_update > DS18B20_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()) {
// 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();
// Read sensor data
unsigned char tmpUnits = getSetting("tmpUnits", TMP_UNITS).toInt();
double t = (tmpUnits == TMP_CELSIUS) ? ds18b20.getTempCByIndex(0) : ds18b20.getTempFByIndex(0);
double t = ds18b20.getTempCByIndex(0);
// apply temperature reading correction
double correction = getSetting("tmpCorrection", TEMPERATURE_CORRECTION).toFloat();
t = t + correction;
// Check returned value
if (t == DEVICE_DISCONNECTED_C) {
_dsIsConnected = false;
DEBUG_MSG_P(PSTR("[DS18B20] Not connected\n"));
return;
} else {
_dsIsConnected = true;
}
// Check if readings are valid
if (isnan(t) || t < -50) {
// Save & convert
_dsTemperature = t;
bool celsius = getSetting("tmpUnits", TMP_UNITS).toInt() == TMP_CELSIUS;
t = getDSTemperature(celsius);
DEBUG_MSG_P(PSTR("[DS18B20] Error reading sensor\n"));
// Build string
char temperature[6];
dtostrf(getDSTemperature(celsius), 1-sizeof(temperature), 1, temperature);
} else {
// Debug
DEBUG_MSG_P(PSTR("[DS18B20] Temperature: %s%s\n"), temperature, celsius ? "ºC" : "ºF");
//If the new temperature is different from the last
if (fabs(t - last_temperature) >= DS18B20_UPDATE_ON_CHANGE) {
last_temperature = t;
send_update = true;
}
_dsTemperature = t;
if ((tmpUnits == TMP_CELSIUS && _dsTemperature == DEVICE_DISCONNECTED_C) ||
(tmpUnits == TMP_FAHRENHEIT && _dsTemperature == DEVICE_DISCONNECTED_F))
_dsIsConnected = false;
else
_dsIsConnected = true;
dtostrf(t, 1-sizeof(_dsTemperatureStr), 1, _dsTemperatureStr);
DEBUG_MSG_P(PSTR("[DS18B20] Temperature: %s%s\n"),
getDSTemperatureStr(),
(_dsIsConnected ? ((tmpUnits == TMP_CELSIUS) ? "ºC" : "ºF") : ""));
if (send_update) {
// Send MQTT messages
mqttSend(getSetting("dsTmpTopic", DS18B20_TEMPERATURE_TOPIC).c_str(), _dsTemperatureStr);
// Send to Domoticz
#if DOMOTICZ_SUPPORT
domoticzSend("dczTmpIdx", 0, _dsTemperatureStr);
#endif
#if INFLUXDB_SUPPORT
idbSend(getSetting("dsTmpTopic", DS18B20_TEMPERATURE_TOPIC).c_str(), _dsTemperatureStr);
#endif
}
// Update websocket clients
#if WEB_SUPPORT
char buffer[100];
snprintf_P(buffer, sizeof(buffer), PSTR("{\"dsVisible\": 1, \"dsTmp\": %s, \"tmpUnits\": %d}"), getDSTemperatureStr(), tmpUnits);
wsSend(buffer);
// If the new temperature is different from the last
if (fabs(_dsTemperature - last_temperature) >= TEMPERATURE_MIN_CHANGE) {
last_temperature = _dsTemperature;
// Send MQTT messages
mqttSend(getSetting("dsTmpTopic", DS18B20_TEMPERATURE_TOPIC).c_str(), temperature);
// Send to Domoticz
#if DOMOTICZ_SUPPORT
domoticzSend("dczTmpIdx", 0, temperature);
#endif
#if INFLUXDB_SUPPORT
idbSend(getSetting("dsTmpTopic", DS18B20_TEMPERATURE_TOPIC).c_str(), temperature);
#endif
}
// Update websocket clients
#if WEB_SUPPORT
wsSend(_dsWebSocketOnSend);
#endif
}
}


+ 630
- 627
code/espurna/static/index.html.gz.h
File diff suppressed because it is too large
View File


+ 8
- 1
code/html/custom.js View File

@ -732,7 +732,14 @@ function processData(data) {
data.ntpStatus = data.ntpStatus ? "SYNC'D" : "NOT SYNC'D";
}
if (key == "tmpUnits") {
$("span#tmpUnit").html(data[key] == 1 ? "ºF" : "ºC");
$("span[name='tmpUnits']").html(data[key] == 1 ? "ºF" : "ºC");
}
if (key == "dhtConnected" && !data[key]) {
$("input[name='dhtTmp']").val("NOT CONNECTED");
$("input[name='dhtHum']").val("NOT CONNECTED");
}
if (key == "dsConnected" && !data[key]) {
$("input[name='dsTmp']").val("NOT CONNECTED");
}
// Look for INPUTs


+ 2
- 2
code/html/index.html View File

@ -171,12 +171,12 @@
</div>
<div class="pure-g module module-ds">
<label class="pure-u-1 pure-u-sm-1-4" for="dsTmp">Temperature (<span id="tmpUnit"></span>)</label>
<label class="pure-u-1 pure-u-sm-1-4" for="dsTmp">Temperature (<span name="tmpUnits"></span>)</label>
<input class="pure-u-1 pure-u-sm-3-4" type="text" name="dsTmp" readonly />
</div>
<div class="pure-g module module-dht">
<label class="pure-u-1 pure-u-sm-1-4" for="dhtTmp">Temperature (<span id="tmpUnit"></span>)</label>
<label class="pure-u-1 pure-u-sm-1-4" for="dhtTmp">Temperature (<span name="tmpUnits"></span>)</label>
<input class="pure-u-1 pure-u-sm-3-4" type="text" name="dhtTmp" readonly />
</div>


Loading…
Cancel
Save