Skip to content
This repository was archived by the owner on Sep 30, 2019. It is now read-only.
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: adafruit/Adafruit-Raspberry-Pi-Python-Code
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: nebulans/Adafruit-Raspberry-Pi-Python-Code
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Can’t automatically merge. Don’t worry, you can still create the pull request.
  • 4 commits
  • 3 files changed
  • 1 contributor

Commits on Aug 4, 2013

  1. Moves parsing of sensor type to function, adds dedicated bcm2835_init

    function, moves variable definitions into readDHT function to allow
    multiple reads.
    nebulans committed Aug 4, 2013

    Verified

    This commit was signed with the committer’s verified signature.
    benjamn Ben Newman
    Copy the full SHA
    a10b254 View commit details
  2. readDHT returns array of two floats, printing done in main. Adds flag

    on readDHT to suppress printing of raw data.
    nebulans committed Aug 4, 2013

    Verified

    This commit was signed with the committer’s verified signature.
    benjamn Ben Newman
    Copy the full SHA
    e20e41d View commit details
  3. Verified

    This commit was signed with the committer’s verified signature.
    benjamn Ben Newman
    Copy the full SHA
    b239f83 View commit details
  4. Verified

    This commit was signed with the committer’s verified signature.
    benjamn Ben Newman
    Copy the full SHA
    b92f0c4 View commit details
Showing with 95 additions and 41 deletions.
  1. +54 −24 Adafruit_DHT_Driver/Adafruit_DHT.c
  2. +12 −17 Adafruit_DHT_Driver/Adafruit_DHT_googledocs.ex.py
  3. +29 −0 Adafruit_DHT_Driver/DHT_Wrapper.py
78 changes: 54 additions & 24 deletions Adafruit_DHT_Driver/Adafruit_DHT.c
Original file line number Diff line number Diff line change
@@ -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;
}
29 changes: 12 additions & 17 deletions Adafruit_DHT_Driver/Adafruit_DHT_googledocs.ex.py
Original file line number Diff line number Diff line change
@@ -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?"
29 changes: 29 additions & 0 deletions Adafruit_DHT_Driver/DHT_Wrapper.py
Original file line number Diff line number Diff line change
@@ -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