#!/usr/bin/env python

import RPi.GPIO as GPIO, time, sys

def sample(channel):
  GPIO.setmode(GPIO.BCM)
  GPIO.setup(channel, GPIO.OUT)
  GPIO.output(channel, GPIO.LOW)
  time.sleep(0.02)
  GPIO.output(channel, GPIO.HIGH)
  GPIO.setup(channel, GPIO.IN)

  while GPIO.input(channel) == GPIO.LOW:
    continue
  while GPIO.input(channel) == GPIO.HIGH:
    continue

  data = []
  for i in range(40):
    while GPIO.input(channel) == GPIO.LOW:
      continue
    for j in range(100):
      if GPIO.input(channel) != GPIO.HIGH:
          break
    if j < 8:
      data.append(0)
    else:
      data.append(1)

  return data

def fn(x):
  v = 0
  for i in range(8):
    v += x[i] << (7-i)
  return v

print "RaspberryPi DH11(temperature+humidity) application\n" \
    "Copyright (c) 2015 winlin(winlin@vip.126.com)"

try:
  print "sample at GPIO18 for PI2"
  data=sample(18) # GPIO18
finally:
  GPIO.cleanup()

humidity = fn(data[0:8])
humidity_point = fn(data[8:16])
temperature = fn(data[16:24])
temperature_point = fn(data[24:32])
check = fn(data[32:40])
expect = humidity + humidity_point + temperature + temperature_point

#print "got data: %s"%(data)
if check == expect:
  print "ok, temperature: %s *C, humidity: %s %%"%(temperature, humidity) 
  sys.exit(0)
else:
  print "fail, temperature: %s *C, humidity: %s %%, check: %s != %s"%(temperature, humidity, check, expect)
  sys.exit(-1)

@@ -16,16 +16,108 @@
 python%0A%0A
+# depends on rpi.gpio, which can be install by:%0A#      sudo apt-get install python-rpi.gpio%0A
 import R
@@ -131,17 +131,23 @@
  as GPIO
-,
+%0Aimport
  time, s
@@ -718,18 +718,25 @@
 ta%0A%0Adef 
-fn
+bits2Byte
 (x):%0A  v
@@ -1017,18 +1017,25 @@
 idity = 
-fn
+bits2Byte
 (data%5B0:
@@ -1051,26 +1051,33 @@
 ity_point = 
-fn
+bits2Byte
 (data%5B8:16%5D)
@@ -1091,18 +1091,25 @@
 ature = 
-fn
+bits2Byte
 (data%5B16
@@ -1134,18 +1134,25 @@
 point = 
-fn
+bits2Byte
 (data%5B24
@@ -1169,10 +1169,17 @@
 k = 
-fn
+bits2Byte
 (dat

refine function name