Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 0:dbffab632178, committed 2017-04-05
- Comitter:
- ledonger
- Date:
- Wed Apr 05 14:04:25 2017 +0000
- Commit message:
- First commit and wokring version. main.c example and DHT11.h classes
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/DHT11.h Wed Apr 05 14:04:25 2017 +0000
@@ -0,0 +1,85 @@
+#include "mbed.h"
+
+//Class to manage DHT11 sensor
+class DHT11{
+
+ public:
+ /* Takes the data pin of the sensor in parameter */
+ DHT11(PinName pin):dataPin(pin)
+ {
+ iBit = 0;
+ data = 0;
+ }
+
+ /* Request and receive new data from sensor
+ Returns :
+ 1 : OK
+ -1 : Checksum Error
+ */
+ int readData(void){
+ this->data = 0;
+ this->dataPin.output();
+ // Request a measurement (low during t > 18ms)
+ this->dataPin = 0;
+ wait_ms(20);
+ this->dataPin = 1;
+
+ //Wait for the sensor to take control and set low level /!\ Important
+ wait_us(20);
+
+ this->dataPin.input();
+
+ //TODO Check if timing is correct (low : 80µs ; high 80µs)
+ // Wait until end of 80µs low
+ while(!this->dataPin.read());
+ // Wait until end of 80 µs high
+ while(this->dataPin.read());
+
+ // Sensor reply 40bits
+ for(iBit=0; iBit<40; iBit++) {
+ this->data = this->data << 1; // Shift for new number
+ this->timer.stop();
+ this->timer.reset();
+
+ // Wait for low level to end
+ while(!this->dataPin.read());
+ this->timer.start();
+ // Count time while high level
+ while(this->dataPin.read());
+
+ if(this->timer.read_us() > 50)
+ {
+ //This bit is '1'
+ this->data++;
+ }
+ }
+
+ wait_ms(250);
+
+ //Checking checksum
+ if((this->data & 0x00000000000000ff) != ((this->data & 0x000000ff00000000) >> 32) +
+ ((this->data & 0x00000000ff000000) >> 24) +
+ ((this->data & 0x0000000000ff0000) >> 16) +
+ ((this->data & 0x000000000000ff00) >> 8))
+ {
+ return -1;
+ }
+
+ return 1;
+ }
+
+ int getTemperature(void){
+ return (int)((this->data & 0x0000000000ff0000) >> 16);
+ }
+
+ int getHumidity(void){
+ return (int)((this->data & 0x000000ff00000000) >> 32);
+ }
+
+
+ private:
+ DigitalInOut dataPin;//Communication with the sensor
+ Timer timer; //initialize timer
+ uint64_t data; // 64 bit variable for temporary data
+ int iBit;
+};
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Wed Apr 05 14:04:25 2017 +0000
@@ -0,0 +1,26 @@
+#include "mbed.h"
+#include "DHT11.h"
+
+//Tested with success on STM32 Nucleo L476
+
+DigitalIn mybutton(USER_BUTTON);
+Serial pc(SERIAL_TX, SERIAL_RX);
+
+DHT11 dht(A1);
+
+
+int main() {
+ pc.printf("DHT11 Reader example\n");
+ while(1) {
+ if(mybutton == 0){
+ if(dht.readData() < 0){
+ pc.printf("Error while reading\n\r");
+ }
+ else{
+ pc.printf("Temperature:%d\n\r",dht.getTemperature());
+ pc.printf("Humidity:%d\n\r",dht.getHumidity());
+ }
+
+ }
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Wed Apr 05 14:04:25 2017 +0000 @@ -0,0 +1,1 @@ +https://mbed.org/users/mbed_official/code/mbed/builds/856d2700e60b \ No newline at end of file