A simple library for reading data from a DHT22 sensor

Fork of lib_dht22 by Simon Cooksey

Committer:
co657_jnp8
Date:
Wed Nov 02 16:29:53 2016 +0000
Revision:
2:02cbaab7c6cd
Parent:
1:10ec58346011
Initial commit;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
co657_sjc80 0:257ba13e416e 1 /*
co657_sjc80 0:257ba13e416e 2 * (C) The University of Kent and Simon Cooksey 2015.
co657_sjc80 0:257ba13e416e 3 */
co657_sjc80 0:257ba13e416e 4
co657_sjc80 0:257ba13e416e 5 #ifndef __DHT22_h_
co657_sjc80 0:257ba13e416e 6 #define __DHT22_h_
co657_sjc80 0:257ba13e416e 7
co657_sjc80 0:257ba13e416e 8 // We'll pick a point to decide if a signal is 1 or 0 from.
co657_sjc80 0:257ba13e416e 9 #define DHT22_SIGNAL_HIGH_LOW_BOUNDARY 50 // uS
co657_sjc80 0:257ba13e416e 10 #define DHT22_START_BIT_TIME 500 // uS
co657_sjc80 0:257ba13e416e 11 #define DHT22_START_BIT_RESPONSE 80 // uS
co657_sjc80 0:257ba13e416e 12
co657_sjc80 0:257ba13e416e 13 typedef enum {
co657_sjc80 0:257ba13e416e 14 EDGE_TYPE_FALLING,
co657_sjc80 0:257ba13e416e 15 EDGE_TYPE_RISING,
co657_sjc80 0:257ba13e416e 16 } edge_type_t;
co657_sjc80 0:257ba13e416e 17
co657_sjc80 0:257ba13e416e 18 typedef struct {
co657_sjc80 0:257ba13e416e 19 int temp;
co657_sjc80 0:257ba13e416e 20 int humidity;
co657_sjc80 0:257ba13e416e 21 uint8_t checksum;
co657_sjc80 0:257ba13e416e 22 } DHT22_data_t;
co657_sjc80 0:257ba13e416e 23
co657_sjc80 0:257ba13e416e 24 class DHT22 {
co657_sjc80 0:257ba13e416e 25 public:
co657_sjc80 0:257ba13e416e 26 DHT22(PinName pin) : dht22_s(pin) { }
co657_sjc80 0:257ba13e416e 27 void read(DHT22_data_t * data);
co657_sjc80 0:257ba13e416e 28 private:
co657_sjc80 0:257ba13e416e 29 DigitalInOut dht22_s;
co657_sjc80 0:257ba13e416e 30
co657_sjc80 0:257ba13e416e 31 int wait_for_edge(edge_type_t type);
co657_sjc80 0:257ba13e416e 32 void send_start();
co657_sjc80 0:257ba13e416e 33 void await_start_response();
co657_sjc80 0:257ba13e416e 34 int16_t read_word();
co657_sjc80 0:257ba13e416e 35 uint8_t read_checksum();
co657_sjc80 0:257ba13e416e 36 };
co657_sjc80 0:257ba13e416e 37
co657_sjc80 0:257ba13e416e 38 #endif // __DHT22_h_