2017-01-28 13:41:01 +00:00
|
|
|
/*
|
2017-12-03 13:06:15 +00:00
|
|
|
xsns_06_dht.ino - DHTxx, AM23xx and SI7021 temperature and humidity sensor support for Sonoff-Tasmota
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2019-01-01 12:55:01 +00:00
|
|
|
Copyright (C) 2019 Theo Arends
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-05-13 12:02:10 +01:00
|
|
|
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.
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-05-13 12:02:10 +01:00
|
|
|
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/>.
|
2017-01-28 13:41:01 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef USE_DHT
|
|
|
|
/*********************************************************************************************\
|
2017-12-03 13:06:15 +00:00
|
|
|
* DHT11, AM2301 (DHT21, DHT22, AM2302, AM2321), SI7021 - Temperature and Humidy
|
2017-01-28 13:41:01 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2018-11-06 16:33:51 +00:00
|
|
|
#define XSNS_06 6
|
|
|
|
|
2017-05-03 17:19:13 +01:00
|
|
|
#define DHT_MAX_SENSORS 3
|
2017-10-27 11:13:36 +01:00
|
|
|
#define DHT_MAX_RETRY 8
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
uint32_t dht_max_cycles;
|
2017-05-03 17:19:13 +01:00
|
|
|
uint8_t dht_data[5];
|
2019-01-28 13:08:33 +00:00
|
|
|
uint8_t dht_sensors = 0;
|
2017-05-03 17:19:13 +01:00
|
|
|
|
|
|
|
struct DHTSTRUCT {
|
2019-01-28 13:08:33 +00:00
|
|
|
uint8_t pin;
|
|
|
|
uint8_t type;
|
2017-11-11 11:33:30 +00:00
|
|
|
char stype[12];
|
2017-05-03 17:19:13 +01:00
|
|
|
uint32_t lastreadtime;
|
2017-10-27 11:13:36 +01:00
|
|
|
uint8_t lastresult;
|
2018-02-06 09:06:22 +00:00
|
|
|
float t = NAN;
|
|
|
|
float h = NAN;
|
2017-10-27 11:13:36 +01:00
|
|
|
} Dht[DHT_MAX_SENSORS];
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void DhtReadPrep(void)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t i = 0; i < dht_sensors; i++) {
|
2017-10-27 11:13:36 +01:00
|
|
|
digitalWrite(Dht[i].pin, HIGH);
|
2017-05-03 17:19:13 +01:00
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
int32_t DhtExpectPulse(uint8_t sensor, bool level)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
2017-11-22 10:02:23 +00:00
|
|
|
int32_t count = 0;
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-10-27 11:13:36 +01:00
|
|
|
while (digitalRead(Dht[sensor].pin) == level) {
|
2018-02-03 22:25:05 +00:00
|
|
|
if (count++ >= (int32_t)dht_max_cycles) {
|
2017-11-22 10:02:23 +00:00
|
|
|
return -1; // Timeout
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
bool DhtRead(uint8_t sensor)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
2017-11-22 10:02:23 +00:00
|
|
|
int32_t cycles[80];
|
2018-07-09 16:59:56 +01:00
|
|
|
uint8_t error = 0;
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-05-03 17:19:13 +01:00
|
|
|
dht_data[0] = dht_data[1] = dht_data[2] = dht_data[3] = dht_data[4] = 0;
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-10-27 11:13:36 +01:00
|
|
|
// digitalWrite(Dht[sensor].pin, HIGH);
|
2017-01-28 13:41:01 +00:00
|
|
|
// delay(250);
|
|
|
|
|
2017-10-27 11:13:36 +01:00
|
|
|
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);
|
2017-12-03 13:06:15 +00:00
|
|
|
|
|
|
|
if (GPIO_SI7021 == Dht[sensor].type) {
|
|
|
|
delayMicroseconds(500);
|
|
|
|
} else {
|
|
|
|
delay(20);
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
|
|
|
|
noInterrupts();
|
2017-10-27 11:13:36 +01:00
|
|
|
digitalWrite(Dht[sensor].pin, HIGH);
|
2017-01-28 13:41:01 +00:00
|
|
|
delayMicroseconds(40);
|
2017-10-27 11:13:36 +01:00
|
|
|
pinMode(Dht[sensor].pin, INPUT_PULLUP);
|
2017-01-28 13:41:01 +00:00
|
|
|
delayMicroseconds(10);
|
2017-11-22 10:02:23 +00:00
|
|
|
if (-1 == DhtExpectPulse(sensor, LOW)) {
|
2017-10-18 17:22:34 +01:00
|
|
|
AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DHT D_TIMEOUT_WAITING_FOR " " D_START_SIGNAL_LOW " " D_PULSE));
|
2018-07-09 16:59:56 +01:00
|
|
|
error = 1;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2018-07-09 16:59:56 +01:00
|
|
|
else if (-1 == DhtExpectPulse(sensor, HIGH)) {
|
2017-10-18 17:22:34 +01:00
|
|
|
AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DHT D_TIMEOUT_WAITING_FOR " " D_START_SIGNAL_HIGH " " D_PULSE));
|
2018-07-09 16:59:56 +01:00
|
|
|
error = 1;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2018-07-09 16:59:56 +01:00
|
|
|
else {
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t i = 0; i < 80; i += 2) {
|
2018-07-09 16:59:56 +01:00
|
|
|
cycles[i] = DhtExpectPulse(sensor, LOW);
|
|
|
|
cycles[i+1] = DhtExpectPulse(sensor, HIGH);
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
interrupts();
|
2018-07-09 16:59:56 +01:00
|
|
|
if (error) { return false; }
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t i = 0; i < 40; ++i) {
|
2017-11-22 10:02:23 +00:00
|
|
|
int32_t lowCycles = cycles[2*i];
|
|
|
|
int32_t highCycles = cycles[2*i+1];
|
|
|
|
if ((-1 == lowCycles) || (-1 == highCycles)) {
|
2017-10-18 17:22:34 +01:00
|
|
|
AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DHT D_TIMEOUT_WAITING_FOR " " D_PULSE));
|
2018-07-09 16:59:56 +01:00
|
|
|
return false;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-05-03 17:19:13 +01:00
|
|
|
dht_data[i/8] <<= 1;
|
2017-04-25 17:24:42 +01:00
|
|
|
if (highCycles > lowCycles) {
|
2017-11-22 10:02:23 +00:00
|
|
|
dht_data[i / 8] |= 1;
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2018-07-10 21:12:16 +01:00
|
|
|
uint8_t checksum = (dht_data[0] + dht_data[1] + dht_data[2] + dht_data[3]) & 0xFF;
|
|
|
|
if (dht_data[4] != checksum) {
|
2019-03-08 14:15:42 +00:00
|
|
|
AddLog_P2(LOG_LEVEL_DEBUG, PSTR(D_LOG_DHT D_CHECKSUM_FAILURE " %02X, %02X, %02X, %02X, %02X =? %02X"),
|
2018-07-10 21:12:16 +01:00
|
|
|
dht_data[0], dht_data[1], dht_data[2], dht_data[3], dht_data[4], checksum);
|
2018-07-09 16:59:56 +01:00
|
|
|
return false;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2018-07-09 16:59:56 +01:00
|
|
|
|
|
|
|
return true;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
void DhtReadTempHum(uint8_t sensor)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
2018-07-09 15:39:24 +01:00
|
|
|
if ((NAN == Dht[sensor].h) || (Dht[sensor].lastresult > DHT_MAX_RETRY)) { // Reset after 8 misses
|
|
|
|
Dht[sensor].t = NAN;
|
|
|
|
Dht[sensor].h = NAN;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2018-07-09 16:59:56 +01:00
|
|
|
if (DhtRead(sensor)) {
|
2017-10-27 11:13:36 +01:00
|
|
|
switch (Dht[sensor].type) {
|
2017-02-11 14:06:23 +00:00
|
|
|
case GPIO_DHT11:
|
2018-07-09 15:39:24 +01:00
|
|
|
Dht[sensor].h = dht_data[0];
|
|
|
|
Dht[sensor].t = dht_data[2] + ((float)dht_data[3] * 0.1f); // Issue #3164
|
2017-01-28 13:41:01 +00:00
|
|
|
break;
|
2017-02-11 14:06:23 +00:00
|
|
|
case GPIO_DHT22:
|
2017-12-03 13:06:15 +00:00
|
|
|
case GPIO_SI7021:
|
2018-07-09 15:39:24 +01:00
|
|
|
Dht[sensor].h = ((dht_data[0] << 8) | dht_data[1]) * 0.1;
|
|
|
|
Dht[sensor].t = (((dht_data[2] & 0x7F) << 8 ) | dht_data[3]) * 0.1;
|
2017-05-03 17:19:13 +01:00
|
|
|
if (dht_data[2] & 0x80) {
|
2018-07-09 15:39:24 +01:00
|
|
|
Dht[sensor].t *= -1;
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-07-09 15:39:24 +01:00
|
|
|
Dht[sensor].t = ConvertTemp(Dht[sensor].t);
|
2019-04-15 17:12:42 +01:00
|
|
|
Dht[sensor].h = ConvertHumidity(Dht[sensor].h);
|
2018-07-09 16:59:56 +01:00
|
|
|
Dht[sensor].lastresult = 0;
|
|
|
|
} else {
|
|
|
|
Dht[sensor].lastresult++;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
bool DhtSetup(uint8_t pin, uint8_t type)
|
2017-05-03 17:19:13 +01:00
|
|
|
{
|
2019-01-28 13:08:33 +00:00
|
|
|
bool success = false;
|
2017-09-02 13:37:02 +01:00
|
|
|
|
2017-05-03 17:19:13 +01:00
|
|
|
if (dht_sensors < DHT_MAX_SENSORS) {
|
2017-10-27 11:13:36 +01:00
|
|
|
Dht[dht_sensors].pin = pin;
|
|
|
|
Dht[dht_sensors].type = type;
|
2017-05-03 17:19:13 +01:00
|
|
|
dht_sensors++;
|
|
|
|
success = true;
|
|
|
|
}
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2017-11-04 15:36:51 +00:00
|
|
|
/********************************************************************************************/
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void DhtInit(void)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
2017-10-18 17:22:34 +01:00
|
|
|
dht_max_cycles = microsecondsToClockCycles(1000); // 1 millisecond timeout for reading pulses from DHT sensor.
|
2017-05-03 17:19:13 +01:00
|
|
|
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t i = 0; i < dht_sensors; i++) {
|
2017-10-27 11:13:36 +01:00
|
|
|
pinMode(Dht[i].pin, INPUT_PULLUP);
|
|
|
|
Dht[i].lastreadtime = 0;
|
|
|
|
Dht[i].lastresult = 0;
|
2018-02-01 15:18:00 +00:00
|
|
|
GetTextIndexed(Dht[i].stype, sizeof(Dht[i].stype), Dht[i].type, kSensorNames);
|
2017-05-10 13:19:36 +01:00
|
|
|
if (dht_sensors > 1) {
|
2019-05-17 13:23:21 +01:00
|
|
|
snprintf_P(Dht[i].stype, sizeof(Dht[i].stype), PSTR("%s%c%02d"), Dht[i].stype, IndexSeparator(), Dht[i].pin);
|
2017-05-03 17:19:13 +01:00
|
|
|
}
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void DhtEverySecond(void)
|
2018-07-09 15:39:24 +01:00
|
|
|
{
|
|
|
|
if (uptime &1) {
|
2018-07-12 11:19:08 +01:00
|
|
|
// <1mS
|
2018-07-09 15:39:24 +01:00
|
|
|
DhtReadPrep();
|
|
|
|
} else {
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t i = 0; i < dht_sensors; i++) {
|
2018-07-12 11:19:08 +01:00
|
|
|
// DHT11 and AM2301 25mS per sensor, SI7021 5mS per sensor
|
2018-07-09 15:39:24 +01:00
|
|
|
DhtReadTempHum(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
void DhtShow(bool json)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t i = 0; i < dht_sensors; i++) {
|
2018-12-21 15:17:06 +00:00
|
|
|
char temperature[33];
|
2018-07-09 15:39:24 +01:00
|
|
|
dtostrfd(Dht[i].t, Settings.flag2.temperature_resolution, temperature);
|
2018-12-21 15:17:06 +00:00
|
|
|
char humidity[33];
|
2018-07-09 15:39:24 +01:00
|
|
|
dtostrfd(Dht[i].h, Settings.flag2.humidity_resolution, humidity);
|
|
|
|
|
|
|
|
if (json) {
|
2019-03-23 16:57:31 +00:00
|
|
|
ResponseAppend_P(JSON_SNS_TEMPHUM, Dht[i].stype, temperature, humidity);
|
2017-01-28 13:41:01 +00:00
|
|
|
#ifdef USE_DOMOTICZ
|
2018-07-09 16:59:56 +01:00
|
|
|
if ((0 == tele_period) && (0 == i)) {
|
2018-07-09 15:39:24 +01:00
|
|
|
DomoticzTempHumSensor(temperature, humidity);
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
#endif // USE_DOMOTICZ
|
2018-04-18 16:28:45 +01:00
|
|
|
#ifdef USE_KNX
|
2018-07-09 16:59:56 +01:00
|
|
|
if ((0 == tele_period) && (0 == i)) {
|
2018-07-09 15:39:24 +01:00
|
|
|
KnxSensor(KNX_TEMPERATURE, Dht[i].t);
|
|
|
|
KnxSensor(KNX_HUMIDITY, Dht[i].h);
|
|
|
|
}
|
2018-04-18 16:28:45 +01:00
|
|
|
#endif // USE_KNX
|
2017-01-28 13:41:01 +00:00
|
|
|
#ifdef USE_WEBSERVER
|
2018-07-09 15:39:24 +01:00
|
|
|
} else {
|
2019-03-19 16:31:43 +00:00
|
|
|
WSContentSend_PD(HTTP_SNS_TEMP, Dht[i].stype, temperature, TempUnit());
|
|
|
|
WSContentSend_PD(HTTP_SNS_HUM, Dht[i].stype, humidity);
|
2017-11-04 15:36:51 +00:00
|
|
|
#endif // USE_WEBSERVER
|
2017-05-03 17:19:13 +01:00
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-03 17:07:25 +00:00
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
bool Xsns06(uint8_t function)
|
2017-11-03 17:07:25 +00:00
|
|
|
{
|
2019-01-28 13:08:33 +00:00
|
|
|
bool result = false;
|
2017-11-03 17:07:25 +00:00
|
|
|
|
|
|
|
if (dht_flg) {
|
|
|
|
switch (function) {
|
2017-12-25 16:41:12 +00:00
|
|
|
case FUNC_INIT:
|
2017-11-03 17:07:25 +00:00
|
|
|
DhtInit();
|
|
|
|
break;
|
2018-07-09 15:39:24 +01:00
|
|
|
case FUNC_EVERY_SECOND:
|
|
|
|
DhtEverySecond();
|
2017-11-03 17:07:25 +00:00
|
|
|
break;
|
2017-12-25 16:41:12 +00:00
|
|
|
case FUNC_JSON_APPEND:
|
2017-11-04 15:36:51 +00:00
|
|
|
DhtShow(1);
|
2017-11-03 17:07:25 +00:00
|
|
|
break;
|
|
|
|
#ifdef USE_WEBSERVER
|
2019-03-19 16:31:43 +00:00
|
|
|
case FUNC_WEB_SENSOR:
|
2017-11-04 15:36:51 +00:00
|
|
|
DhtShow(0);
|
2017-11-03 17:07:25 +00:00
|
|
|
break;
|
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-01-28 13:41:01 +00:00
|
|
|
#endif // USE_DHT
|