2016년 7월 30일 토요일

RPI is not good handling DHT11

Hello?

This is failure story about converting c code to python.

There is GPIO controlling library on Python such as RPI, wiringPI.

But they are not enough to controlling high frequency data.

some people already investigate about it.
http://codeandlife.com/2012/07/03/benchmarking-raspberry-pi-gpio-speed/


Follow code return wrong values because frequency.

So I should hybrid c++ and python codes.

import RPi.GPIO as GPIO
from array import *
from time import sleep
usleep = lambda x: sleep(x/2000000.0)
def readDHT11():
MAXTIMINGS = 85
DHTPIN = 4
laststate = GPIO.HIGH
j = 0
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
dht11_dat = array('i', [0,0,0,0,0])
GPIO.setup(DHTPIN, GPIO.OUT)
GPIO.output(DHTPIN, GPIO.LOW)
sleep(0.025)
GPIO.output(DHTPIN, GPIO.HIGH)
GPIO.setup(DHTPIN, GPIO.IN)
for i in range(0, MAXTIMINGS):
counter = 0;
while GPIO.input(DHTPIN) == laststate:
counter = counter + 1
if counter == 255:
break
laststate = GPIO.input(DHTPIN)
if counter == 255:
break
if (i >= 4) and (i % 2 == 0):
index = (int)(j/8)
dht11_dat[index] = dht11_dat[index] << 1
if counter > 16:
dht11_dat[index] = dht11_dat[index] | 1
j = j + 1
print("j= {0}".format(j))
condition1 = (j >= 40)
condition2 = (dht11_dat[4] ==
((dht11_dat[0] + dht11_dat[1] + dht11_dat[2] + dht11_dat[3]) & 0xff))
condition3 = ((dht11_dat[0] != 0) and (dht11_dat[2] != 0))
if (condition1 and condition2 and condition3):
f = dht11_dat[2] * 9.0 / 5.0 + 32;
print("Humidity = {0}.{1} % Temperature = {2}.{3} *C ({4} *F)".format(dht11_dat[0], dht11_dat[1], dht11_dat[2], dht11_dat[3], f))
print("{0}, {1}, {2}, {3}, {4}".format(dht11_dat[0], dht11_dat[1], dht11_dat[2], dht11_dat[3], dht11_dat[4] ))
def motion_callback(channel):
print("motion detect = {0}".format(GPIO.input(23)))
def registerMotionCallback():
MOTION = 23
GPIO.setup(MOTION, GPIO.IN)
GPIO.add_event_detect(MOTION, GPIO.RISING)
GPIO.add_event_callback(MOTION, motion_callback)
def main():
GPIO.setmode(GPIO.BCM)
registerMotionCallback()
while True:
readDHT11()
sleep(1)
if __name__ == "__main__":
main(
view raw sensorTest.py hosted with ❤ by GitHub

2016년 7월 28일 목요일

Change whole things.

Hello?


I decide to renew whole devices.




Finally, there is a lego frame for raspberry pi.

This one has motion sensor, humid sensor, camera, IR receiver.

I will use Django, python for gathering daily logs.

Fun coding guys :)

RPI is not good handling DHT11

Hello? This is failure story about converting c code to python. There is GPIO controlling library on Python such as RPI, wiringPI. But...