A13 logging temperature and humidity with DH11 sensor

Started by elendar, July 04, 2015, 02:29:28 PM

Previous topic - Next topic

elendar

Hi there,

I'm not sure whether this will be useful to someone, but just in case, I wanted to share the work I have done to grab the info from a DH11 sensor with an A13 board. Basically it is using python and the provided modules (see https://olimex.wordpress.com/2014/09/08/python-modules-for-access-to-gpio-i2c-and-spi-to-all-olinuxino-boards-and-soms-on-github/) to access GPIO pins then reading another page (http://www.uugear.com/portfolio/dht11-humidity-temperature-sensor-module/) I could make sense of the inputs I got from the sensor. A few tweaks are required since the first bit of data is sometimes truncated and the length of a 1-array encoding an 0-value bit is longer than in the initial code. Here is the code:


#!/usr/bin/python
# sources :
# http://www.uugear.com/portfolio/dht11-humidity-temperature-sensor-module/
import time
import os
import sys

if not os.getegid() == 0:
    sys.exit('Script must be run as root')

from pyA13.gpio import gpio
from pyA13.gpio import connector

#init
sensor = connector.gpio2p9
gpio.init()

#--------------------------------------
#trigger measure
gpio.setcfg(sensor, gpio.OUTPUT)
gpio.output(sensor,1)
time.sleep(0.5)
gpio.output(sensor,0)
time.sleep(0.02)

#----------------------------------------
#grab data
gpio.setcfg(sensor, gpio.INPUT)
#gpio.input(sensor)

data = []
bit_count = 0
tmp = 0
count = 0
HumidityBit = "0"
TemperatureBit = ""
crc = ""
nombreseuil = 10

for i in range(0,1500):
    data.append(gpio.input(sensor))
#print data

#--------------------------------------------
#treat data to extract the values
def bin2dec(string_num):
    return str(int(string_num, 2))

try:
   if data[count] == 0:
while data[count] == 0:
count = count + 1
   while data[count] == 1:
      tmp = 1
      count = count + 1
       

   for i in range(0, 31):#shifting the indexes since the first bit of data is sometimes truncated (which explain the first test right above) ; since humidity wont go over 100 and this first bit is the one converting into 128, it doesnt really matter
      bit_count = 0
     
      while data[count] == 0:
         tmp = 1
         count = count + 1

      while data[count] == 1:
         bit_count = bit_count + 1
         count = count + 1

      if bit_count > nombreseuil:
         if i>=0 and i<7:
            HumidityBit = HumidityBit + "1"
         if i>=15 and i<23:
            TemperatureBit = TemperatureBit + "1"
      else:
         if i>=0 and i<7:
            HumidityBit = HumidityBit + "0"
         if i>=15 and i<23:
            TemperatureBit = TemperatureBit + "0"
           
except:
   print "ERR_RANGE"
   exit(0)

   
try:
   for i in range(0, 8):
      bit_count = 0
     
      while data[count] == 0:
         tmp = 1
         count = count + 1

      while data[count] == 1:
         bit_count = bit_count + 1
         count = count + 1

      if bit_count > nombreseuil:
         crc = crc + "1"
      else:
         crc = crc + "0"
except:
   print "ERR_RANGE"
   exit(0)
     
     
Humidity = bin2dec(HumidityBit)
Temperature = bin2dec(TemperatureBit)

print HumidityBit
print TemperatureBit
if int(Humidity) + int(Temperature) - int(bin2dec(crc)) == 0:
   print Humidity
   print Temperature
else:
   print "ERR_CRC"


Since I use only Python and I rely on the provided modules to access GPIO, I guess the same code can be used almost straightforward for other boards such as A20, Lime or Lime 2.

Since I have also bought a gas sensor I will probably make a similar script (if the way the sensor works is similar) to grab data from it.

Hope it can help.

Cheers,

otti

Hi,

Thank you for this very helpful topic, I am trying to use the A13 board with a MQ2 gas sensor myself and I was wondering if you have already tried that and if it worked.

As for the DH11 sensor, was it a A13 wifi board or simple board you used with it, do you have any pictures for the wiring?

Thanks for your answer!