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.
Fork of DHT22 by
Revision 0:7fd3ff04ae95, committed 2011-10-27
- Comitter:
- Julepalme
- Date:
- Thu Oct 27 15:11:06 2011 +0000
- Commit message:
- First Draft
Changed in this revision
| DHT22.cpp | Show annotated file Show diff for this revision Revisions of this file |
| DHT22.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/DHT22.cpp Thu Oct 27 15:11:06 2011 +0000
@@ -0,0 +1,49 @@
+#include "DHT22.h"
+
+
+DHT22::DHT22(PinName pin) {
+ _data_pin = pin;
+}
+
+int DHT22::getTemperature() {
+ return _temperature;
+}
+
+int DHT22::getHumidity() {
+ return _humidity;
+}
+
+bool DHT22::sample() {
+ DigitalInOut DHT22(_data_pin);
+ int dht22_dat [5];
+ DHT22.output();
+ DHT22.write(0);
+ wait_ms(18);
+ DHT22.write(1);
+ DHT22.input();
+ wait_us(40);
+ wait_us(80);
+ int i,j,result=0;
+ for (i=0; i<5; i++) {
+ result=0;
+ for (j=0; j<8; j++) {
+ while (DHT22);
+ while (!DHT22);
+ wait_us(50);
+ int p;
+ p=DHT22;
+ p=p <<(7-j);
+ result=result|p;
+ }
+ dht22_dat[i] = result;
+ }
+ int dht22_check_sum;
+ dht22_check_sum=dht22_dat[0]+dht22_dat[1]+dht22_dat[2]+dht22_dat[3];
+ dht22_check_sum= dht22_check_sum%256;
+ if (dht22_check_sum==dht22_dat[4]) {
+ _humidity=dht22_dat[0]*256+dht22_dat[1];
+ _temperature=dht22_dat[2]*256+dht22_dat[3];
+ return true;
+ }
+ return false;
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/DHT22.h Thu Oct 27 15:11:06 2011 +0000
@@ -0,0 +1,17 @@
+#ifndef MBED_DHT22_H
+#define MBED_DHT22_H
+
+#include "mbed.h"
+
+class DHT22 {
+private:
+ int _temperature,_humidity;
+ PinName _data_pin;
+public:
+ DHT22(PinName);
+ bool sample();
+ int getTemperature();
+ int getHumidity();
+};
+
+#endif
\ No newline at end of file
