-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Sam Johnston
committed
Oct 4, 2016
0 parents
commit 22d3424
Showing
3 changed files
with
262 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
/* DHT11 temperature sensor library | ||
Usage is straight forward: | ||
Ser DHT PIN using setDHTPin(pin) command | ||
then use any of the getter functions to get temp/humidity data | ||
Sam Johnston | ||
October 2016 | ||
This example code is in the Public Domain (or CC0 licensed, at your option.) | ||
Unless required by applicable law or agreed to in writing, this | ||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR | ||
CONDITIONS OF ANY KIND, either express or implied. | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include "freertos/FreeRTOS.h" | ||
#include "freertos/task.h" | ||
#include "esp_system.h" | ||
#include "rom/ets_sys.h" | ||
#include "nvs_flash.h" | ||
#include "driver/gpio.h" | ||
#include "sdkconfig.h" | ||
#include "DHT11.h" | ||
|
||
|
||
int humidity = 0; | ||
int temperature = 0; | ||
int Ftemperature = 0; | ||
int DHT_PIN = 4; | ||
|
||
void setDHTPin(int PIN) | ||
{ | ||
DHT_PIN = PIN; | ||
} | ||
void errorHandle(int response) | ||
{ | ||
switch(response) { | ||
|
||
case DHT_TIMEOUT_ERROR : | ||
printf("DHT Sensor Timeout!\n"); | ||
case DHT_CHECKSUM_ERROR: | ||
printf("CheckSum error!\n"); | ||
case DHT_OKAY: | ||
break; | ||
default : | ||
printf("Dont know how you got here!\n"); | ||
} | ||
temperature = 0; | ||
humidity = 0; | ||
|
||
} | ||
void sendStart() | ||
{ | ||
//Send start signal from ESP32 to DHT device | ||
gpio_set_direction(DHT_PIN, GPIO_MODE_OUTPUT); | ||
gpio_set_level(DHT_PIN,0); | ||
//vTaskDelay(36 / portTICK_RATE_MS); | ||
ets_delay_us(22000); | ||
gpio_set_level(DHT_PIN,1); | ||
ets_delay_us(43); | ||
gpio_set_direction(DHT_PIN, GPIO_MODE_INPUT); | ||
} | ||
|
||
int getData() | ||
{ | ||
//Variables used in this function | ||
int counter = 0; | ||
uint8_t bits[5]; | ||
uint8_t byteCounter = 0; | ||
uint8_t cnt = 7; | ||
|
||
for (int i = 0; i <5; i++) | ||
{ | ||
bits[i] = 0; | ||
} | ||
|
||
sendStart(); | ||
|
||
//Wait for a response from the DHT11 device | ||
//This requires waiting for 20-40 us | ||
counter = 0; | ||
|
||
while (gpio_get_level(DHT_PIN)==1) | ||
{ | ||
if(counter > 40) | ||
{ | ||
return DHT_TIMEOUT_ERROR; | ||
} | ||
counter = counter + 1; | ||
ets_delay_us(1); | ||
} | ||
//Now that the DHT has pulled the line low, | ||
//it will keep the line low for 80 us and then high for 80us | ||
//check to see if it keeps low | ||
counter = 0; | ||
while(gpio_get_level(DHT_PIN)==0) | ||
{ | ||
if(counter > 80) | ||
{ | ||
return DHT_TIMEOUT_ERROR; | ||
} | ||
counter = counter + 1; | ||
ets_delay_us(1); | ||
} | ||
counter = 0; | ||
while(gpio_get_level(DHT_PIN)==1) | ||
{ | ||
if(counter > 80) | ||
{ | ||
return DHT_TIMEOUT_ERROR; | ||
} | ||
counter = counter + 1; | ||
ets_delay_us(1); | ||
} | ||
// If no errors have occurred, it is time to read data | ||
//output data from the DHT11 is 40 bits. | ||
//Loop here until 40 bits have been read or a timeout occurs | ||
|
||
for(int i = 0; i < 40; i++) | ||
{ | ||
//int currentBit = 0; | ||
//starts new data transmission with 50us low signal | ||
counter = 0; | ||
while(gpio_get_level(DHT_PIN)==0) | ||
{ | ||
if (counter > 55) | ||
{ | ||
return DHT_TIMEOUT_ERROR; | ||
} | ||
counter = counter + 1; | ||
ets_delay_us(1); | ||
} | ||
|
||
//Now check to see if new data is a 0 or a 1 | ||
counter = 0; | ||
while(gpio_get_level(DHT_PIN)==1) | ||
{ | ||
if (counter > 75) | ||
{ | ||
return DHT_TIMEOUT_ERROR; | ||
} | ||
counter = counter + 1; | ||
ets_delay_us(1); | ||
} | ||
//add the current reading to the output data | ||
//since all bits where set to 0 at the start of the loop, only looking for 1s | ||
//look for when count is greater than 40 - this allows for some margin of error | ||
if (counter > 40) | ||
{ | ||
|
||
bits[byteCounter] |= (1 << cnt); | ||
|
||
} | ||
//here are conditionals that work with the bit counters | ||
if (cnt == 0) | ||
{ | ||
|
||
cnt = 7; | ||
byteCounter = byteCounter +1; | ||
}else{ | ||
|
||
cnt = cnt -1; | ||
} | ||
} | ||
humidity = bits[0]; | ||
temperature = bits[2]; | ||
Ftemperature = temperature * 1.8 + 32; | ||
|
||
uint8_t sum = bits[0] + bits[2]; | ||
|
||
if (bits[4] != sum) | ||
{ | ||
return DHT_CHECKSUM_ERROR; | ||
} | ||
return DHT_OKAY; | ||
} | ||
|
||
int getFtemp() | ||
{ | ||
|
||
getData(); | ||
return Ftemperature; | ||
} | ||
int getTemp() | ||
{ | ||
getData(); | ||
return temperature; | ||
} | ||
int getHumidity() | ||
{ | ||
getData(); | ||
return humidity; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* DHT11 temperature sensor library | ||
Usage is straight forward: | ||
Ser DHT PIN using setDHTPin(pin) command | ||
then use any of the getter functions to get temp/humidity data | ||
Sam Johnston | ||
October 2016 | ||
This example code is in the Public Domain (or CC0 licensed, at your option.) | ||
Unless required by applicable law or agreed to in writing, this | ||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR | ||
CONDITIONS OF ANY KIND, either express or implied. | ||
*/ | ||
#ifndef DHT11_H_ | ||
#define DHT11_H_ | ||
|
||
#define DHT_TIMEOUT_ERROR -2 | ||
#define DHT_CHECKSUM_ERROR -1 | ||
#define DHT_OKAY 0 | ||
|
||
// function prototypes | ||
|
||
//Start by using this function | ||
void setDHTPin(int PIN); | ||
//Do not need to touch these three | ||
void sendStart(); | ||
int getData(); | ||
void errorHandle(int response); | ||
//call each function for live temperature updates | ||
int getFtemp(); | ||
int getTemp(); | ||
int getHumidity(); | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//DHT11 sensor reading | ||
|
||
#include <stdio.h> | ||
#include "freertos/FreeRTOS.h" | ||
#include "freertos/task.h" | ||
#include "esp_system.h" | ||
#include "rom/ets_sys.h" | ||
#include "nvs_flash.h" | ||
#include "driver/gpio.h" | ||
#include "sdkconfig.h" | ||
#include "DHT11.h" | ||
|
||
void DHT_task(void *pvParameter) | ||
{ | ||
setDHTPin(4); | ||
printf("Starting DHT measurement!\n"); | ||
while(1) | ||
{ | ||
printf("Temperature reading %d\n",getTemp()); | ||
printf("F temperature is %d\n", getFtemp()); | ||
printf("Humidity reading %d\n",getHumidity()); | ||
vTaskDelay(3000 / portTICK_RATE_MS); | ||
//sendStart(4); | ||
} | ||
} | ||
|
||
void app_main() | ||
{ | ||
nvs_flash_init(); | ||
system_init(); | ||
vTaskDelay(1000 / portTICK_RATE_MS); | ||
xTaskCreate(&DHT_task, "DHT_task", 2048, NULL, 5, NULL); | ||
} | ||
|
22d3424
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi Sam
Had cloned your dht11 library to test on esp32 using esp-idf. But im getting errors while building your code.
CC build/main/DHT_Testing.o
C:/esp/dht11/main/DHT_Testing.c: In function 'app_main':
C:/esp/dht11/main/DHT_Testing.c:29:5: warning: 'system_init' is deprecated [-Wdeprecated-declarations]
system_init();
^
In file included from C:/esp/dht11/main/DHT_Testing.c:6:0:
C:/esp/esp-idf/components/esp32/include/esp_system.h:42:6: note: declared here
void system_init(void) attribute ((deprecated));
^
CC build/main/DHT11.o
C:/esp/dht11/main/DHT11.c:31:19: error: expected expression before '[' token
int DHT_DATA[3] = [0,0,0]
^
C:/esp/dht11/main/DHT11.c:31:21: error: left-hand operand of comma expression has no effect [-Werror=unused-value]
int DHT_DATA[3] = [0,0,0]
^
C:/esp/dht11/main/DHT11.c:31:23: error: left-hand operand of comma expression has no effect [-Werror=unused-value]
int DHT_DATA[3] = [0,0,0]
^
C:/esp/dht11/main/DHT11.c: In function 'setDHTPin':
C:/esp/dht11/main/DHT11.c:36:2: error: 'DHT_PIN' undeclared (first use in this function)
DHT_PIN = PIN;
^
C:/esp/dht11/main/DHT11.c:36:2: note: each undeclared identifier is reported only once for each function it appears in
C:/esp/dht11/main/DHT11.c: In function 'sendStart':
C:/esp/dht11/main/DHT11.c:58:22: error: 'DHT_PIN' undeclared (first use in this function)
gpio_set_direction(DHT_PIN, GPIO_MODE_OUTPUT);
^
C:/esp/dht11/main/DHT11.c: In function 'getData':
C:/esp/dht11/main/DHT11.c:86:25: error: 'DHT_PIN' undeclared (first use in this function)
while (gpio_get_level(DHT_PIN)==1)
^
C:/esp/dht11/main/DHT11.c:178:14: error: expected expression before '[' token
DHT_DATA = [temperature,Ftemperature, humidity];
^
C:/esp/dht11/main/DHT11.c:178:26: error: left-hand operand of comma expression has no effect [-Werror=unused-value]
DHT_DATA = [temperature,Ftemperature, humidity];
^
C:/esp/dht11/main/DHT11.c:178:39: error: left-hand operand of comma expression has no effect [-Werror=unused-value]
DHT_DATA = [temperature,Ftemperature, humidity];
^
C:/esp/dht11/main/DHT11.c:179:10: warning: return makes integer from pointer without a cast [-Wint-conversion]
return DHT_DATA;
^
C:/esp/dht11/main/DHT11.c: In function 'getFtemp':
C:/esp/dht11/main/DHT11.c:186:14: error: subscripted value is neither array nor pointer nor vector
ftemp = Data[1];
^
C:/esp/dht11/main/DHT11.c:185:6: warning: variable 'Data' set but not used [-Wunused-but-set-variable]
int Data = getData();
^
C:/esp/dht11/main/DHT11.c: In function 'getTemp':
C:/esp/dht11/main/DHT11.c:193:13: error: subscripted value is neither array nor pointer nor vector
temp = Data[0];
^
C:/esp/dht11/main/DHT11.c:192:6: warning: variable 'Data' set but not used [-Wunused-but-set-variable]
int Data = getData();
^
C:/esp/dht11/main/DHT11.c: In function 'getHumidity':
C:/esp/dht11/main/DHT11.c:200:14: error: subscripted value is neither array nor pointer nor vector
humid = Data[2];
^
C:/esp/dht11/main/DHT11.c:199:6: warning: variable 'Data' set but not used [-Wunused-but-set-variable]
int Data = getData();
^
cc1.exe: some warnings being treated as errors
make[1]: *** [/c/esp/esp-idf/make/component_wrapper.mk:229: DHT11.o] Error 1
make: *** [C:/esp/esp-idf/make/project.mk:391: component-main-build] Error 2
Pl suggest me what to do.
thanks
satish