DHT12 support (single wire and I2C)

This commit is contained in:
pm-cz
2016-08-18 15:52:29 +02:00
parent ea3ace7358
commit 800cbc6819
4 changed files with 134 additions and 14 deletions
+2 -2
View File
@@ -39,7 +39,7 @@
// Analog input (ESP-7/12 only)
// Pulse counters
// Dallas OneWire DS18b20 temperature sensors
// DHT11/22 humidity sensors
// DHT11/22/12 humidity sensors
// BMP085 I2C Barometric Pressure sensor
// PCF8591 4 port Analog to Digital converter (I2C)
// RFID Wiegand-26 reader
@@ -998,4 +998,4 @@ void backgroundtasks()
statusLED(false);
yield();
}
+3
View File
@@ -1478,6 +1478,9 @@ void handle_i2cscanner() {
case 0x48:
reply += F("PCF8591 ADC");
break;
case 0x5C:
reply += F("DHT 12");
break;
case 0x68:
reply += F("DS1307 RTC");
break;
+16 -12
View File
@@ -48,14 +48,16 @@ boolean Plugin_005(byte function, struct EventStruct *event, String& string)
case PLUGIN_WEBFORM_LOAD:
{
byte choice = Settings.TaskDevicePluginConfig[event->TaskIndex][0];
String options[2];
String options[3];
options[0] = F("DHT 11");
options[1] = F("DHT 22");
int optionValues[2];
options[2] = F("DHT 12");
int optionValues[3];
optionValues[0] = 11;
optionValues[1] = 22;
optionValues[2] = 12;
string += F("<TR><TD>DHT Type:<TD><select name='plugin_005_dhttype'>");
for (byte x = 0; x < 2; x++)
for (byte x = 0; x < 3; x++)
{
string += F("<option value='");
string += optionValues[x];
@@ -128,14 +130,20 @@ boolean Plugin_005(byte function, struct EventStruct *event, String& string)
if (dht_dat[4] == dht_check_sum)
{
float temperature = 0;
float humidity = 0;
float temperature = NAN;
float humidity = NAN;
if (Par3 == 11)
{
temperature = float(dht_dat[2]); // Temperature
humidity = float(dht_dat[0]); // Humidity
}
else if (Par3 == 12)
{
temperature = float(dht_dat[2]*10 + (dht_dat[3] & 0x7f)) / 10.0; // Temperature
if (dht_dat[3] & 0x80) { temperature = -temperature; } // Negative temperature
humidity = float(dht_dat[0]*10+dht_dat[1]) / 10.0; // Humidity
}
if (Par3 == 22)
{
@@ -145,13 +153,7 @@ boolean Plugin_005(byte function, struct EventStruct *event, String& string)
temperature = 0.1 * word(dht_dat[2], dht_dat[3]);
humidity = word(dht_dat[0], dht_dat[1]) * 0.1; // Humidity
}
if (temperature == 0 && humidity == 0)
{
String log = F("DHT : No reading!");
log += UserVar[event->BaseVarIndex];
addLog(LOG_LEVEL_INFO, log);
}
else
if (temperature != NAN || humidity != NAN) // According to negated original if, maybe use && instead?
{
UserVar[event->BaseVarIndex] = temperature;
UserVar[event->BaseVarIndex + 1] = humidity;
@@ -169,6 +171,8 @@ boolean Plugin_005(byte function, struct EventStruct *event, String& string)
} // !dht
if(!success)
{
String log = F("DHT : No reading!");
addLog(LOG_LEVEL_INFO, log);
UserVar[event->BaseVarIndex] = NAN;
UserVar[event->BaseVarIndex + 1] = NAN;
}
+113
View File
@@ -0,0 +1,113 @@
//#######################################################################################################
//######################## Plugin 034: Temperature and Humidity sensor DHT 12 (I2C) #####################
//#######################################################################################################
#define PLUGIN_034
#define PLUGIN_ID_034 34
#define PLUGIN_NAME_034 "Temperature & Humidity - DHT12 (I2C)"
#define PLUGIN_VALUENAME1_034 "Temperature"
#define PLUGIN_VALUENAME2_034 "Humidity"
boolean Plugin_034_init = false;
#define DHT12_I2C_ADDRESS 0x5C // I2C address for the sensor
boolean Plugin_034(byte function, struct EventStruct *event, String& string)
{
boolean success = false;
switch (function)
{
case PLUGIN_DEVICE_ADD:
{
Device[++deviceCount].Number = PLUGIN_ID_034;
Device[deviceCount].Type = DEVICE_TYPE_I2C;
Device[deviceCount].VType = SENSOR_TYPE_TEMP_HUM;
Device[deviceCount].Ports = 0;
Device[deviceCount].PullUpOption = false;
Device[deviceCount].InverseLogicOption = false;
Device[deviceCount].FormulaOption = true;
Device[deviceCount].ValueCount = 2;
Device[deviceCount].SendDataOption = true;
Device[deviceCount].TimerOption = true;
Device[deviceCount].GlobalSyncOption = true;
break;
}
case PLUGIN_GET_DEVICENAME:
{
string = F(PLUGIN_NAME_034);
break;
}
case PLUGIN_GET_DEVICEVALUENAMES:
{
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_034));
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[1], PSTR(PLUGIN_VALUENAME2_034));
break;
}
case PLUGIN_READ:
{
byte dht_dat[5];
byte dht_in;
byte i;
byte Retry = 0;
boolean error = false;
Wire.beginTransmission(DHT12_I2C_ADDRESS); // start transmission to device
Wire.write(0); // sends register address to read from
Wire.endTransmission(); // end transmission
Wire.beginTransmission(DHT12_I2C_ADDRESS); // start transmission to device
if (Wire.requestFrom(DHT12_I2C_ADDRESS, 5) == 5) { // send data n-bytes read
for (i = 0; i < 5; i++)
{
dht_dat[i] = Wire.read(); // receive DATA
}
} else {
error = true;
}
if (!error)
{
// Checksum calculation is a Rollover Checksum by design!
byte dht_check_sum = dht_dat[0] + dht_dat[1] + dht_dat[2] + dht_dat[3]; // check check_sum
if (dht_dat[4] == dht_check_sum)
{
float temperature = float(dht_dat[2]*10 + (dht_dat[3] & 0x7f)) / 10.0; // Temperature
if (dht_dat[3] & 0x80) { temperature = -temperature; }
float humidity = float(dht_dat[0]*10+dht_dat[1]) / 10.0; // Humidity
UserVar[event->BaseVarIndex] = temperature;
UserVar[event->BaseVarIndex + 1] = humidity;
String log = F("DHT12: Temperature: ");
log += UserVar[event->BaseVarIndex];
addLog(LOG_LEVEL_INFO, log);
log = F("DHT12: Humidity: ");
log += UserVar[event->BaseVarIndex + 1];
addLog(LOG_LEVEL_INFO, log);
/*
log = F("DHT12: Data: ");
for (int i=0; i < 5; i++)
{
log += dht_dat[i];
log += ", ";
}
addLog(LOG_LEVEL_INFO, log);
*/
success = true;
} // checksum
} // error
if(!success)
{
String log = F("DHT12: No reading!");
addLog(LOG_LEVEL_INFO, log);
UserVar[event->BaseVarIndex] = NAN;
UserVar[event->BaseVarIndex + 1] = NAN;
}
break;
}
}
return success;
}