2017-01-28 13:41:01 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2017 Theo Arends. All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
- Redistributions of source code must retain the above copyright notice,
|
|
|
|
this list of conditions and the following disclaimer.
|
|
|
|
- Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
this list of conditions and the following disclaimer in the documentation
|
|
|
|
and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2017-05-03 17:19:13 +01:00
|
|
|
#define DHT_MAX_SENSORS 3
|
|
|
|
#define MIN_INTERVAL 2000
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-05-03 17:19:13 +01:00
|
|
|
uint32_t dht_maxcycles;
|
|
|
|
uint8_t dht_data[5];
|
|
|
|
byte dht_sensors = 0;
|
|
|
|
|
|
|
|
struct DHTSTRUCT {
|
|
|
|
byte pin;
|
|
|
|
byte type;
|
|
|
|
char stype[10];
|
|
|
|
uint32_t lastreadtime;
|
|
|
|
bool lastresult;
|
|
|
|
float t;
|
|
|
|
float h = 0;
|
|
|
|
} dht[DHT_MAX_SENSORS];
|
2017-01-28 13:41:01 +00:00
|
|
|
|
|
|
|
void dht_readPrep()
|
|
|
|
{
|
2017-05-03 17:19:13 +01:00
|
|
|
for (byte i = 0; i < dht_sensors; i++) {
|
|
|
|
digitalWrite(dht[i].pin, HIGH);
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2017-05-03 17:19:13 +01:00
|
|
|
uint32_t dht_expectPulse(byte sensor, bool level)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
|
|
|
uint32_t count = 0;
|
|
|
|
|
2017-05-03 17:19:13 +01:00
|
|
|
while (digitalRead(dht[sensor].pin) == level) {
|
|
|
|
if (count++ >= dht_maxcycles) {
|
2017-04-25 17:24:42 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2017-05-03 17:19:13 +01:00
|
|
|
boolean dht_read(byte sensor)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
|
|
|
char log[LOGSZ];
|
|
|
|
uint32_t cycles[80];
|
|
|
|
uint32_t currenttime = millis();
|
|
|
|
|
2017-05-03 17:19:13 +01:00
|
|
|
if ((currenttime - dht[sensor].lastreadtime) < 2000) {
|
|
|
|
return dht[sensor].lastresult;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-05-03 17:19:13 +01:00
|
|
|
dht[sensor].lastreadtime = currenttime;
|
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-05-03 17:19:13 +01:00
|
|
|
// digitalWrite(dht[sensor].pin, HIGH);
|
2017-01-28 13:41:01 +00:00
|
|
|
// delay(250);
|
|
|
|
|
2017-05-03 17:19:13 +01:00
|
|
|
pinMode(dht[sensor].pin, OUTPUT);
|
|
|
|
digitalWrite(dht[sensor].pin, LOW);
|
2017-01-28 13:41:01 +00:00
|
|
|
delay(20);
|
|
|
|
|
|
|
|
noInterrupts();
|
2017-05-03 17:19:13 +01:00
|
|
|
digitalWrite(dht[sensor].pin, HIGH);
|
2017-01-28 13:41:01 +00:00
|
|
|
delayMicroseconds(40);
|
2017-05-03 17:19:13 +01:00
|
|
|
pinMode(dht[sensor].pin, INPUT_PULLUP);
|
2017-01-28 13:41:01 +00:00
|
|
|
delayMicroseconds(10);
|
2017-05-03 17:19:13 +01:00
|
|
|
if (0 == dht_expectPulse(sensor, LOW)) {
|
2017-01-28 13:41:01 +00:00
|
|
|
addLog_P(LOG_LEVEL_DEBUG, PSTR("DHT: Timeout waiting for start signal low pulse"));
|
2017-05-03 17:19:13 +01:00
|
|
|
dht[sensor].lastresult = false;
|
|
|
|
return dht[sensor].lastresult;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-05-03 17:19:13 +01:00
|
|
|
if (0 == dht_expectPulse(sensor, HIGH)) {
|
2017-01-28 13:41:01 +00:00
|
|
|
addLog_P(LOG_LEVEL_DEBUG, PSTR("DHT: Timeout waiting for start signal high pulse"));
|
2017-05-03 17:19:13 +01:00
|
|
|
dht[sensor].lastresult = false;
|
|
|
|
return dht[sensor].lastresult;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-04-25 17:24:42 +01:00
|
|
|
for (int i = 0; i < 80; i += 2) {
|
2017-05-03 17:19:13 +01:00
|
|
|
cycles[i] = dht_expectPulse(sensor, LOW);
|
|
|
|
cycles[i+1] = dht_expectPulse(sensor, HIGH);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
interrupts();
|
|
|
|
|
|
|
|
for (int i=0; i<40; ++i) {
|
|
|
|
uint32_t lowCycles = cycles[2*i];
|
|
|
|
uint32_t highCycles = cycles[2*i+1];
|
2017-04-25 17:24:42 +01:00
|
|
|
if ((0 == lowCycles) || (0 == highCycles)) {
|
2017-01-28 13:41:01 +00:00
|
|
|
addLog_P(LOG_LEVEL_DEBUG, PSTR("DHT: Timeout waiting for pulse"));
|
2017-05-03 17:19:13 +01:00
|
|
|
dht[sensor].lastresult = false;
|
|
|
|
return dht[sensor].lastresult;
|
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-05-03 17:19:13 +01:00
|
|
|
dht_data[i/8] |= 1;
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
snprintf_P(log, sizeof(log), PSTR("DHT: Received %02X, %02X, %02X, %02X, %02X =? %02X"),
|
2017-05-03 17:19:13 +01:00
|
|
|
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);
|
2017-01-28 13:41:01 +00:00
|
|
|
addLog(LOG_LEVEL_DEBUG, log);
|
|
|
|
|
2017-05-03 17:19:13 +01:00
|
|
|
if (dht_data[4] == ((dht_data[0] + dht_data[1] + dht_data[2] + dht_data[3]) & 0xFF)) {
|
|
|
|
dht[sensor].lastresult = true;
|
2017-01-28 13:41:01 +00:00
|
|
|
} else {
|
|
|
|
addLog_P(LOG_LEVEL_DEBUG, PSTR("DHT: Checksum failure"));
|
2017-05-03 17:19:13 +01:00
|
|
|
dht[sensor].lastresult = false;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-05-03 17:19:13 +01:00
|
|
|
return dht[sensor].lastresult;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2017-05-03 17:19:13 +01:00
|
|
|
boolean dht_readTempHum(byte sensor, float &t, float &h)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
2017-05-03 17:19:13 +01:00
|
|
|
if (!dht[sensor].h) {
|
2017-01-28 13:41:01 +00:00
|
|
|
t = NAN;
|
|
|
|
h = NAN;
|
|
|
|
} else {
|
2017-05-03 17:19:13 +01:00
|
|
|
t = dht[sensor].t;
|
|
|
|
h = dht[sensor].h;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2017-05-03 17:19:13 +01:00
|
|
|
if (dht_read(sensor)) {
|
|
|
|
switch (dht[sensor].type) {
|
2017-02-11 14:06:23 +00:00
|
|
|
case GPIO_DHT11:
|
2017-05-03 17:19:13 +01:00
|
|
|
h = dht_data[0];
|
|
|
|
t = convertTemp(dht_data[2]);
|
2017-01-28 13:41:01 +00:00
|
|
|
break;
|
2017-02-11 14:06:23 +00:00
|
|
|
case GPIO_DHT22:
|
|
|
|
case GPIO_DHT21:
|
2017-05-03 17:19:13 +01:00
|
|
|
h = dht_data[0];
|
2017-01-28 13:41:01 +00:00
|
|
|
h *= 256;
|
2017-05-03 17:19:13 +01:00
|
|
|
h += dht_data[1];
|
2017-01-28 13:41:01 +00:00
|
|
|
h *= 0.1;
|
2017-05-03 17:19:13 +01:00
|
|
|
t = dht_data[2] & 0x7F;
|
2017-01-28 13:41:01 +00:00
|
|
|
t *= 256;
|
2017-05-03 17:19:13 +01:00
|
|
|
t += dht_data[3];
|
2017-01-28 13:41:01 +00:00
|
|
|
t *= 0.1;
|
2017-05-03 17:19:13 +01:00
|
|
|
if (dht_data[2] & 0x80) {
|
2017-04-25 17:24:42 +01:00
|
|
|
t *= -1;
|
|
|
|
}
|
2017-05-03 17:19:13 +01:00
|
|
|
t = convertTemp(t);
|
2017-01-28 13:41:01 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-04-25 17:24:42 +01:00
|
|
|
if (!isnan(t)) {
|
2017-05-03 17:19:13 +01:00
|
|
|
dht[sensor].t = t;
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
|
|
|
if (!isnan(h)) {
|
2017-05-03 17:19:13 +01:00
|
|
|
dht[sensor].h = h;
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
return (!isnan(t) && !isnan(h));
|
|
|
|
}
|
|
|
|
|
2017-05-03 17:19:13 +01:00
|
|
|
boolean dht_setup(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;
|
|
|
|
}
|
|
|
|
|
2017-01-28 13:41:01 +00:00
|
|
|
void dht_init()
|
|
|
|
{
|
|
|
|
char log[LOGSZ];
|
2017-02-28 15:01:48 +00:00
|
|
|
|
2017-05-03 17:19:13 +01:00
|
|
|
dht_maxcycles = 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 = -MIN_INTERVAL;
|
|
|
|
switch (dht[i].type) {
|
|
|
|
case GPIO_DHT11:
|
2017-05-10 13:19:36 +01:00
|
|
|
strcpy_P(dht[i].stype, PSTR("DHT11"));
|
2017-05-03 17:19:13 +01:00
|
|
|
break;
|
|
|
|
case GPIO_DHT21:
|
2017-05-10 13:19:36 +01:00
|
|
|
strcpy_P(dht[i].stype, PSTR("AM2301"));
|
2017-05-03 17:19:13 +01:00
|
|
|
break;
|
|
|
|
case GPIO_DHT22:
|
2017-05-10 13:19:36 +01:00
|
|
|
strcpy_P(dht[i].stype, PSTR("DHT22"));
|
|
|
|
}
|
|
|
|
if (dht_sensors > 1) {
|
|
|
|
snprintf_P(dht[i].stype, sizeof(dht[i].stype), PSTR("%s-%02d"), dht[i].stype, dht[i].pin);
|
2017-05-03 17:19:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf_P(log, sizeof(log), PSTR("DHT: Max clock cycles %d"), dht_maxcycles);
|
2017-01-28 13:41:01 +00:00
|
|
|
addLog(LOG_LEVEL_DEBUG, log);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Presentation
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2017-02-04 16:09:54 +00:00
|
|
|
void dht_mqttPresent(char* svalue, uint16_t ssvalue, uint8_t* djson)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
2017-04-25 17:24:42 +01:00
|
|
|
char stemp1[10];
|
|
|
|
char stemp2[10];
|
|
|
|
float t;
|
|
|
|
float h;
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-05-03 17:19:13 +01:00
|
|
|
byte dsxflg = 0;
|
|
|
|
for (byte i = 0; i < dht_sensors; i++) {
|
|
|
|
if (dht_readTempHum(i, t, h)) { // Read temperature
|
|
|
|
dtostrf(t, 1, sysCfg.flag.temperature_resolution, stemp1);
|
|
|
|
dtostrf(h, 1, sysCfg.flag.humidity_resolution, stemp2);
|
|
|
|
// snprintf_P(svalue, ssvalue, PSTR("%s, \"%s\":{\"Temperature\":%s, \"Humidity\":%s}"),
|
|
|
|
// svalue, dhtstype, stemp1, stemp2);
|
|
|
|
snprintf_P(svalue, ssvalue, JSON_SNS_TEMPHUM, svalue, dht[i].stype, stemp1, stemp2);
|
|
|
|
*djson = 1;
|
2017-01-28 13:41:01 +00:00
|
|
|
#ifdef USE_DOMOTICZ
|
2017-05-03 17:19:13 +01:00
|
|
|
if (!dsxflg) {
|
|
|
|
domoticz_sensor2(stemp1, stemp2);
|
|
|
|
dsxflg++;
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
#endif // USE_DOMOTICZ
|
2017-05-03 17:19:13 +01:00
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
String dht_webPresent()
|
|
|
|
{
|
|
|
|
String page = "";
|
2017-05-03 17:19:13 +01:00
|
|
|
char stemp[10];
|
|
|
|
char sensor[80];
|
2017-04-25 17:24:42 +01:00
|
|
|
float t;
|
|
|
|
float h;
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-05-03 17:19:13 +01:00
|
|
|
for (byte i = 0; i < dht_sensors; i++) {
|
|
|
|
if (dht_readTempHum(i, t, h)) {
|
|
|
|
dtostrf(t, 1, sysCfg.flag.temperature_resolution, stemp);
|
|
|
|
snprintf_P(sensor, sizeof(sensor), HTTP_SNS_TEMP, dht[i].stype, stemp, tempUnit());
|
|
|
|
page += sensor;
|
|
|
|
dtostrf(h, 1, sysCfg.flag.humidity_resolution, stemp);
|
|
|
|
snprintf_P(sensor, sizeof(sensor), HTTP_SNS_HUM, dht[i].stype, stemp);
|
|
|
|
page += sensor;
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
return page;
|
|
|
|
}
|
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
#endif // USE_DHT
|