Tasmota/sonoff/xsns_06_dht.ino
arendst 3403ca1e0a v5.10.0
5.10.0 20171201
 * Upgrade library ArduinoJson to 5.11.2
 * Upgrade
library IRRemoteEsp8266 to 2.2.1 + 2 commits but disabled some protocols
(code size reduction)
 * Upgrade library NeoPixelBus to 2.2.9
 * Upgrade
library OneWire to 2.3.3 + 6 commits and disabled CRC lookup-table
(#define ONEWIRE_CRC8_TABLE 0) (code size reduction)
 * Update library
PubSubClient to 2.6 + 9 commits and additional delay (#790)
 * Update
core_esp8266_wiring_digital.c to latest (staged) level
 * Patch library
I2Cdevlib-Core for esp8266-core 2.4.0-rc2 compatibility
 * Remove
command EnergyReset 1..3 now replaced by ENergyReset1 to EnergyReset3
 *
Remove spaces in JSON messages (code size reduction)
 * Renamed
xsns_05_ds18x20.ino to xsns_05_ds18x20_legacy.ino still using library
OneWire and providing dynamic sensor scan
 * Fix possible iram1_0_seg
compile error by shrinking ICACHE_RAM_ATTR code usage
 * Fix PWM
watchdog timeout if Dimmer is set to 100 or Color set to 0xFF (#1146)
 *
Fix Sonoff Bridge Learn Mode hang caused by unrecognised RF code
(#1181)
 * Fix blank console log window by using XML character encoding
(#1187)
 * Fix wrong response name for command HlwISet (#1214)
 * Fix
DHT type sensor timeout recognition by distinguish "signal already
there" from "timeout" (#1233)
 * Add fixed color options 1..12 to
command Color
 * Add + (plus) and - (minus) to commands Dimmer
(+10/-10), Speed and Scheme
 * Add + (plus) and - (minus) to command
Color to select 1 out of 12 preset colors
 * Add + (plus) and - (minus)
to command Ct to control ColdWarm led ColorTemperature (+34/-34)
 * Add
commands EnergyReset1 0..42500, EnergyReset2 0..42500 and EnergyReset3
0..42500000
 *  to (Re)set Energy Today, Yesterday or Total respectively
in Wh (#406, #685, #1202)
 * Add optional ADS1115 driver as alternative
for unsupported I2Cdevlib in esp8266-core 2.4.0-rc2
 * Add support for
INA219 Voltage and Current sensor to be enabled in user_config.h with
define USE_INA219
 * Add support for Arilux LC11 (Clearing RF home code
when selecting no Arilux module)
 * Add support for WS2812 RGBW
ledstrips to be enabled in user_config.h with define USE_WS2812_CTYPE
(#1156)
 * Add SettingsSaveAll routine to command SaveData to be used
before controlled power down (#1202)
 * Add option PUSHBUTTON_TOGGLE
(SwitchMode 7) to allow toggling on any switch change (#1221)
 * Add new
xdrv_05_ds18x20.ino free from library OneWire and add the following
features:
 *  Add support for DS1822
 *  Add forced setting of 12-bit
resolution for selected device types (#1222)
 *  Add read temperature
retry counter (#1215)
 *  Fix lost sensors by performing sensor probe at
restart only thereby removing dynamic sensor probe (#1215)
 *  Fix
sensor address sorting using ascending sort on sensor type followed by
sensor address
 *  Rewrite JSON resulting in shorter message allowing
more sensors in default firmware image:
 *
"DS18B20-1":{"Id":"00000483C23A","Temperature":19.5},"DS18B20-2":{"Id":"0000048EC44C","Temperature":19.6}

* Add additional define in user_config.h to select either single sensor
(defines disabled), new multi sensor (USE_DS18X20) or legacy multi
sensor (USE_DS18X20_LEGACY)
 * Add clock support for more different
pixel counts (#1226)
 * Add support for Sonoff Dual R2 (#1249)
 * Add
FriendlyName to web page tab and add program information to web page
footer (#1275)
2017-12-01 14:42:22 +01:00

277 lines
7.3 KiB
C++

/*
xsns_06_dht.ino - DHTxx and AM23xx temperature and humidity sensor support for Sonoff-Tasmota
Copyright (C) 2017 Theo Arends
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef USE_DHT
/*********************************************************************************************\
* DHT11, DHT21 (AM2301), DHT22 (AM2302, AM2321) - Temperature and Humidy
*
* Reading temperature or humidity takes about 250 milliseconds!
* Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
* Source: Adafruit Industries https://github.com/adafruit/DHT-sensor-library
\*********************************************************************************************/
#define DHT_MAX_SENSORS 3
#define DHT_MAX_RETRY 8
#define MIN_INTERVAL 2000
uint32_t dht_max_cycles;
uint8_t dht_data[5];
byte dht_sensors = 0;
struct DHTSTRUCT {
byte pin;
byte type;
char stype[12];
uint32_t lastreadtime;
uint8_t lastresult;
float t;
float h = 0;
} Dht[DHT_MAX_SENSORS];
void DhtReadPrep()
{
for (byte i = 0; i < dht_sensors; i++) {
digitalWrite(Dht[i].pin, HIGH);
}
}
int32_t DhtExpectPulse(byte sensor, bool level)
{
int32_t count = 0;
while (digitalRead(Dht[sensor].pin) == level) {
if (count++ >= dht_max_cycles) {
return -1; // Timeout
}
}
return count;
}
void DhtRead(byte sensor)
{
int32_t cycles[80];
uint32_t currenttime = millis();
if ((currenttime - Dht[sensor].lastreadtime) < MIN_INTERVAL) {
return;
}
Dht[sensor].lastreadtime = currenttime;
dht_data[0] = dht_data[1] = dht_data[2] = dht_data[3] = dht_data[4] = 0;
// digitalWrite(Dht[sensor].pin, HIGH);
// delay(250);
if (Dht[sensor].lastresult > DHT_MAX_RETRY) {
Dht[sensor].lastresult = 0;
digitalWrite(Dht[sensor].pin, HIGH); // Retry read prep
delay(250);
}
pinMode(Dht[sensor].pin, OUTPUT);
digitalWrite(Dht[sensor].pin, LOW);
delay(20);
noInterrupts();
digitalWrite(Dht[sensor].pin, HIGH);
delayMicroseconds(40);
pinMode(Dht[sensor].pin, INPUT_PULLUP);
delayMicroseconds(10);
if (-1 == DhtExpectPulse(sensor, LOW)) {
AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DHT D_TIMEOUT_WAITING_FOR " " D_START_SIGNAL_LOW " " D_PULSE));
Dht[sensor].lastresult++;
return;
}
if (-1 == DhtExpectPulse(sensor, HIGH)) {
AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DHT D_TIMEOUT_WAITING_FOR " " D_START_SIGNAL_HIGH " " D_PULSE));
Dht[sensor].lastresult++;
return;
}
for (int i = 0; i < 80; i += 2) {
cycles[i] = DhtExpectPulse(sensor, LOW);
cycles[i+1] = DhtExpectPulse(sensor, HIGH);
}
interrupts();
for (int i = 0; i < 40; ++i) {
int32_t lowCycles = cycles[2*i];
int32_t highCycles = cycles[2*i+1];
if ((-1 == lowCycles) || (-1 == highCycles)) {
AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DHT D_TIMEOUT_WAITING_FOR " " D_PULSE));
Dht[sensor].lastresult++;
return;
}
dht_data[i/8] <<= 1;
if (highCycles > lowCycles) {
dht_data[i / 8] |= 1;
}
}
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_DHT D_RECEIVED " %02X, %02X, %02X, %02X, %02X =? %02X"),
dht_data[0], dht_data[1], dht_data[2], dht_data[3], dht_data[4], (dht_data[0] + dht_data[1] + dht_data[2] + dht_data[3]) & 0xFF);
AddLog(LOG_LEVEL_DEBUG);
if (dht_data[4] == ((dht_data[0] + dht_data[1] + dht_data[2] + dht_data[3]) & 0xFF)) {
Dht[sensor].lastresult = 0;
} else {
AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DHT D_CHECKSUM_FAILURE));
Dht[sensor].lastresult++;
}
}
boolean DhtReadTempHum(byte sensor, float &t, float &h)
{
if (!Dht[sensor].h) {
t = NAN;
h = NAN;
} else {
if (Dht[sensor].lastresult > DHT_MAX_RETRY) { // Reset after 8 misses
Dht[sensor].t = NAN;
Dht[sensor].h = NAN;
}
t = Dht[sensor].t;
h = Dht[sensor].h;
}
DhtRead(sensor);
if (!Dht[sensor].lastresult) {
switch (Dht[sensor].type) {
case GPIO_DHT11:
h = dht_data[0];
t = ConvertTemp(dht_data[2]);
break;
case GPIO_DHT22:
case GPIO_DHT21:
h = dht_data[0];
h *= 256;
h += dht_data[1];
h *= 0.1;
t = dht_data[2] & 0x7F;
t *= 256;
t += dht_data[3];
t *= 0.1;
if (dht_data[2] & 0x80) {
t *= -1;
}
t = ConvertTemp(t);
break;
}
if (!isnan(t)) {
Dht[sensor].t = t;
}
if (!isnan(h)) {
Dht[sensor].h = h;
}
}
return (!isnan(t) && !isnan(h));
}
boolean DhtSetup(byte pin, byte type)
{
boolean success = false;
if (dht_sensors < DHT_MAX_SENSORS) {
Dht[dht_sensors].pin = pin;
Dht[dht_sensors].type = type;
dht_sensors++;
success = true;
}
return success;
}
/********************************************************************************************/
void DhtInit()
{
dht_max_cycles = microsecondsToClockCycles(1000); // 1 millisecond timeout for reading pulses from DHT sensor.
for (byte i = 0; i < dht_sensors; i++) {
pinMode(Dht[i].pin, INPUT_PULLUP);
Dht[i].lastreadtime = 0;
Dht[i].lastresult = 0;
strcpy_P(Dht[i].stype, kSensors[Dht[i].type]);
if (dht_sensors > 1) {
snprintf_P(Dht[i].stype, sizeof(Dht[i].stype), PSTR("%s-%02d"), Dht[i].stype, Dht[i].pin);
}
}
}
void DhtShow(boolean json)
{
char temperature[10];
char humidity[10];
float t;
float h;
byte dsxflg = 0;
for (byte i = 0; i < dht_sensors; i++) {
if (DhtReadTempHum(i, t, h)) { // Read temperature
dtostrfd(t, Settings.flag2.temperature_resolution, temperature);
dtostrfd(h, Settings.flag2.humidity_resolution, humidity);
if (json) {
snprintf_P(mqtt_data, sizeof(mqtt_data), JSON_SNS_TEMPHUM, mqtt_data, Dht[i].stype, temperature, humidity);
#ifdef USE_DOMOTICZ
if (!dsxflg) {
DomoticzTempHumSensor(temperature, humidity);
dsxflg++;
}
#endif // USE_DOMOTICZ
#ifdef USE_WEBSERVER
} else {
snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_TEMP, mqtt_data, Dht[i].stype, temperature, TempUnit());
snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_HUM, mqtt_data, Dht[i].stype, humidity);
#endif // USE_WEBSERVER
}
}
}
}
/*********************************************************************************************\
* Interface
\*********************************************************************************************/
#define XSNS_06
boolean Xsns06(byte function)
{
boolean result = false;
if (dht_flg) {
switch (function) {
case FUNC_XSNS_INIT:
DhtInit();
break;
case FUNC_XSNS_PREP:
DhtReadPrep();
break;
case FUNC_XSNS_JSON_APPEND:
DhtShow(1);
break;
#ifdef USE_WEBSERVER
case FUNC_XSNS_WEB:
DhtShow(0);
break;
#endif // USE_WEBSERVER
}
}
return result;
}
#endif // USE_DHT