diff --git a/Adafruit_DHT_Driver/Adafruit_DHT.c b/Adafruit_DHT_Driver/Adafruit_DHT.c index 9d1746be..b5235af3 100644 --- a/Adafruit_DHT_Driver/Adafruit_DHT.c +++ b/Adafruit_DHT_Driver/Adafruit_DHT.c @@ -33,7 +33,9 @@ #define DHT22 22 #define AM2302 22 -int readDHT(int type, int pin); +float *readDHT(int type, int pin, int print); +int parseType(char *input); +int init(void); int main(int argc, char **argv) { @@ -45,10 +47,9 @@ int main(int argc, char **argv) printf("example: %s 2302 4 - Read from an AM2302 connected to GPIO #4\n", argv[0]); return 2; } - int type = 0; - if (strcmp(argv[1], "11") == 0) type = DHT11; - if (strcmp(argv[1], "22") == 0) type = DHT22; - if (strcmp(argv[1], "2302") == 0) type = AM2302; + + int type = parseType(argv[1]); + if (type == 0) { printf("Select 11, 22, 2302 as type!\n"); return 3; @@ -63,20 +64,33 @@ int main(int argc, char **argv) printf("Using pin #%d\n", dhtpin); - readDHT(type, dhtpin); + + float *r = readDHT(type, dhtpin, 1); + if (r[0]==-1.){ + printf("Read failed\n"); + } else { + if (type == DHT11) printf("Temp = %.0f *C, Hum = %.0f \%\n", r[0], r[1]); + if (type == DHT22) printf("Temp = %.1f *C, Hum = %.1f \%\n", r[0], r[1]); + } + return 0; } // main -int bits[250], data[100]; -int bitidx = 0; -int readDHT(int type, int pin) { + +float *readDHT(int type, int pin, int print) { + int bits[250], data[100]; + int bitidx = 0; + int counter = 0; int laststate = HIGH; int j=0; + static float result[2]; + float t, h; + // Set GPIO pin to output bcm2835_gpio_fsel(pin, BCM2835_GPIO_FSEL_OUTP); @@ -124,25 +138,41 @@ int readDHT(int type, int pin) { } #endif - printf("Data (%d): 0x%x 0x%x 0x%x 0x%x 0x%x\n", j, data[0], data[1], data[2], data[3], data[4]); + if (print) printf("Data (%d): 0x%x 0x%x 0x%x 0x%x 0x%x\n", j, data[0], data[1], data[2], data[3], data[4]); - if ((j >= 39) && - (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) ) { + if ((j >= 39) && (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) ) { // yay! - if (type == DHT11) - printf("Temp = %d *C, Hum = %d \%\n", data[2], data[0]); + if (type == DHT11) { + t = (float)data[2]; + h = (float)data[0]; + } if (type == DHT22) { - float f, h; - h = data[0] * 256 + data[1]; - h /= 10; - - f = (data[2] & 0x7F)* 256 + data[3]; - f /= 10.0; - if (data[2] & 0x80) f *= -1; - printf("Temp = %.1f *C, Hum = %.1f \%\n", f, h); + //float f, h; + h = data[0] * 256 + data[1]; + h /= 10; + + t = (data[2] & 0x7F)* 256 + data[3]; + t /= 10.0; + if (data[2] & 0x80) t *= -1; } - return 1; + result[0] = t; + result[1] = h; + return result; } + result[0] = -1.; + result[1] = -1.; + return result; +} - return 0; +int parseType(char *input){ + int type = 0; + if (strcmp(input, "11") == 0) type = DHT11; + if (strcmp(input, "22") == 0) type = DHT22; + if (strcmp(input, "2302") == 0) type = AM2302; + return type; +} + +int init(void){ + if (bcm2835_init()) return 1; + return 0; } diff --git a/Adafruit_DHT_Driver/Adafruit_DHT_googledocs.ex.py b/Adafruit_DHT_Driver/Adafruit_DHT_googledocs.ex.py index c13d3033..caa6f87f 100755 --- a/Adafruit_DHT_Driver/Adafruit_DHT_googledocs.ex.py +++ b/Adafruit_DHT_Driver/Adafruit_DHT_googledocs.ex.py @@ -6,6 +6,7 @@ import time import datetime import gspread +from DHT_Wrapper import DHT # =========================================================================== # Google Account Details @@ -37,31 +38,25 @@ print "Unable to open the spreadsheet. Check your filename: %s" % spreadsheet sys.exit() +# Initialize sensor +d = DHT("2302", 4) + # Continuously append data while(True): # Run the DHT program to get the humidity and temperature readings! - - output = subprocess.check_output(["./Adafruit_DHT", "2302", "4"]); - print output - matches = re.search("Temp =\s+([0-9.]+)", output) - if (not matches): - time.sleep(3) - continue - temp = float(matches.group(1)) + r = d.read() - # search for humidity printout - matches = re.search("Hum =\s+([0-9.]+)", output) - if (not matches): - time.sleep(3) - continue - humidity = float(matches.group(1)) + if not r: + time.sleep(3) + continue + - print "Temperature: %.1f C" % temp - print "Humidity: %.1f %%" % humidity + print "Temperature: %.1f C" % r["temp"] + print "Humidity: %.1f %%" % r["hum"] # Append the data in the spreadsheet, including a timestamp try: - values = [datetime.datetime.now(), temp, humidity] + values = [datetime.datetime.now(), r["temp"], r["hum"]] worksheet.append_row(values) except: print "Unable to append data. Check your connection?" diff --git a/Adafruit_DHT_Driver/DHT_Wrapper.py b/Adafruit_DHT_Driver/DHT_Wrapper.py new file mode 100644 index 00000000..99842eaa --- /dev/null +++ b/Adafruit_DHT_Driver/DHT_Wrapper.py @@ -0,0 +1,29 @@ +import ctypes +from decimal import * + + +class DHT(object): + """ + Wrapper for Adafruit DHT11/DHT22/AM2302 sensor Raspberry Pi driver. + """ + def __init__(self, sensor, pin): + """ + Initialize with sensor type as string and pin Raspberry Pi pin number as integer + """ + self.DHTlib = ctypes.CDLL("./libAdafruit_DHT.so") + if not self.DHTlib.init(): + raise IOError("Could not initialise BCM2835 - may need to run as root") + self.sensor = self.DHTlib.parseType(ctypes.c_char_p(sensor)) + self.pin = ctypes.c_int(pin) + self.DHTlib.readDHT.restype = ctypes.POINTER(ctypes.c_float) + def read(self): + """ + Returns a dict of decimals representing measured temperature and humidity + """ + r = self.DHTlib.readDHT(self.sensor, self.pin, ctypes.c_int(0)) + if r[1] > 0: + q = Decimal("0.1") + return {"temp": Decimal(r[0]).quantize(q), "hum":Decimal(r[1]).quantize(q)} + else: + return False +