What I'm trying to do is to read temperature from TMP36 sensor (over MCP3008 ADC), light from LDR, and humidity from DHT22 sensor. Firstly, I had one script for temperature and light readings, and it worked ok. And also I had another script for humidity readings, that I downloaded from github, by joan2937 - pigpio site, for DHT22 sensor, and its also working ok. So the circuit is correctly connected.
Now I have everything in one script, here is the full code:
#!/usr/bin/python
print "Content-type: python\n"
import spidev
import os
import time
import math
import sqlite3
import pigpio
import cgitb
import sys
import DHT22
import atexit
cgitb.enable()
pi=pigpio.pi()
#Open SPI bus
#spi = spidev.SpiDev()
#spi.open(0,0)
h = pi.spi_open(0,1000000)
conn = sqlite3.connect('sensordb2.db')
curs = conn.cursor()
#Read SPI data from ADC
#Channel is integer 0-7
def ReadChannel(channel):
#adc = spi.xfer2([1,(8+channel)<<4,0])
count,adc = pi.spi_xfer(h,[1,(8+channel)<<4,0])
data = ((adc[1]&3)<<8) + adc[2]
return data
#Convert data to voltage level
#rounded to specified number of decimal places
def ConvertVolts(data,places):
volts = (data*3.3)/float(1023)
#volts = (data*5)/float(1023)
volts = round(volts,places)
return volts
#Calculate temperature from thermistor
#rounded to specified number of decimal places
def ConvertTemp(data,places):
temp = ((data*330)/float(1023))-50
#temp = ((data*500)/float(1023))-50
#temp = ((data*1000)-500)/10
temp = round(temp,places)
return temp
#Define sensor channels
light_channel = 0
temp_channel = 7
#Define delay between readings
delay = 5
class sensor:
"""
A class to read relative humidity and temperature from the
DHT22 sensor. The sensor is also known as the AM2302.
The sensor can be powered from the Pi 3V3 or the Pi 5V rail.
Powering from the 3V3 rail is simpler and safer. You may need
to power from 5V if the sensor is connected via a long cable.
For 3V3 operation connect pin 1 to 3V3 and pin 4 to ground.
Connect pin 2 to a gpio.
For 5V operation connect pin 1 to 5V and pin 4 to ground.
The following pin 2 connection works for me. Use at YOUR OWN RISK.
5V--5K_resistor--+--10K_resistor--Ground
|
DHT22 pin 2 -----+
|
gpio ------------+
"""
def __init__(self, pi, gpio, LED=None, power=None):
"""
Instantiate with the Pi and gpio to which the DHT22 output
pin is connected.
Optionally a LED may be specified. This will be blinked for
each successful reading.
Optionally a gpio used to power the sensor may be specified.
This gpio will be set high to power the sensor. If the sensor
locks it will be power cycled to restart the readings.
Taking readings more often than about once every two seconds will
eventually cause the DHT22 to hang. A 3 second interval seems OK.
"""
self.pi = pi
self.gpio = gpio
self.LED = LED
self.power = power
if power is not None:
pi.write(power, 1) # Switch sensor on.
time.sleep(2)
self.powered = True
self.cb = None
atexit.register(self.cancel)
self.bad_CS = 0 # Bad checksum count.
self.bad_SM = 0 # Short message count.
self.bad_MM = 0 # Missing message count.
self.bad_SR = 0 # Sensor reset count.
# Power cycle if timeout > MAX_TIMEOUTS.
self.no_response = 0
self.MAX_NO_RESPONSE = 2
self.rhum = -999
self.temp = -999
self.tov = None
self.high_tick = 0
self.bit = 40
pi.set_pull_up_down(gpio, pigpio.PUD_OFF)
pi.set_watchdog(gpio, 0) # Kill any watchdogs.
self.cb = pi.callback(gpio, pigpio.EITHER_EDGE, self._cb)
def _cb(self, gpio, level, tick):
"""
Accumulate the 40 data bits. Format into 5 bytes, humidity high,
humidity low, temperature high, temperature low, checksum.
"""
diff = pigpio.tickDiff(self.high_tick, tick)
if level == 0:
# Edge length determines if bit is 1 or 0.
if diff >= 50:
val = 1
if diff >= 200: # Bad bit?
self.CS = 256 # Force bad checksum.
else:
val = 0
if self.bit >= 40: # Message complete.
self.bit = 40
elif self.bit >= 32: # In checksum byte.
self.CS = (self.CS<<1) + val
if self.bit == 39:
# 40th bit received.
self.pi.set_watchdog(self.gpio, 0)
self.no_response = 0
total = self.hH + self.hL + self.tH + self.tL
if (total & 255) == self.CS: # Is checksum ok?
self.rhum = ((self.hH<<8) + self.hL) * 0.1
if self.tH & 128: # Negative temperature.
mult = -0.1
self.tH = self.tH & 127
else:
mult = 0.1
self.temp = ((self.tH<<8) + self.tL) * mult
self.tov = time.time()
if self.LED is not None:
self.pi.write(self.LED, 0)
else:
self.bad_CS += 1
elif self.bit >=24: # in temp low byte
self.tL = (self.tL<<1) + val
elif self.bit >=16: # in temp high byte
self.tH = (self.tH<<1) + val
elif self.bit >= 8: # in humidity low byte
self.hL = (self.hL<<1) + val
elif self.bit >= 0: # in humidity high byte
self.hH = (self.hH<<1) + val
else: # header bits
pass
self.bit += 1
elif level == 1:
self.high_tick = tick
if diff > 250000:
self.bit = -2
self.hH = 0
self.hL = 0
self.tH = 0
self.tL = 0
self.CS = 0
else: # level == pigpio.TIMEOUT:
self.pi.set_watchdog(self.gpio, 0)
if self.bit < 8: # Too few data bits received.
self.bad_MM += 1 # Bump missing message count.
self.no_response += 1
if self.no_response > self.MAX_NO_RESPONSE:
self.no_response = 0
self.bad_SR += 1 # Bump sensor reset count.
if self.power is not None:
self.powered = False
self.pi.write(self.power, 0)
time.sleep(2)
self.pi.write(self.power, 1)
time.sleep(2)
self.powered = True
elif self.bit < 39: # Short message receieved.
self.bad_SM += 1 # Bump short message count.
self.no_response = 0
else: # Full message received.
self.no_response = 0
def temperature(self):
"""Return current temperature."""
return self.temp
def humidity(self):
"""Return current relative humidity."""
return self.rhum
def staleness(self):
"""Return time since measurement made."""
if self.tov is not None:
return time.time() - self.tov
else:
return -999
def bad_checksum(self):
"""Return count of messages received with bad checksums."""
return self.bad_CS
def short_message(self):
"""Return count of short messages."""
return self.bad_SM
def missing_message(self):
"""Return count of missing messages."""
return self.bad_MM
def sensor_resets(self):
"""Return count of power cycles because of sensor hangs."""
return self.bad_SR
def trigger(self):
"""Trigger a new relative humidity and temperature reading."""
if self.powered:
if self.LED is not None:
self.pi.write(self.LED, 1)
self.pi.write(self.gpio, pigpio.LOW)
time.sleep(0.017) # 17 ms
self.pi.set_mode(self.gpio, pigpio.INPUT)
self.pi.set_watchdog(self.gpio, 200)
def cancel(self):
"""Cancel the DHT22 sensor."""
self.pi.set_watchdog(self.gpio, 0)
if self.cb != None:
self.cb.cancel()
self.cb = None
while True:
#Read the light sensor data
light_level = ReadChannel(light_channel)
light_volts = ConvertVolts(light_level,2)
#Read the temp sensor data
temp_level = ReadChannel(temp_channel)
temp_volts = ConvertVolts(temp_level,2)
tempc = ConvertTemp(temp_level,2)
if tempc>24:
flag = 1
else:
flag = 0
# Humidity
s = DHT22.sensor(pi, 25, LED=21, power=8)
s.trigger()
time.sleep(0.2)
#Print out results
print "-------------------------------"
print("Humidity (DHT22): {} %".format(s.humidity()))
print("Light (LDR): {} ({}V)".format(light_level,light_volts))
print("Temperature (TMP36): {} ({}V) {} deg C flag {}".format(temp_level,temp_volts,tempc,flag))
curs.execute("INSERT INTO analogPins VALUES(date('now','localtime'),time('now','localtime'),'%d','%f','%d','%f','%f','%d')" %(light_level,light_volts,temp_level,tempc,s.humidity(),flag))
conn.commit()
print "Saved to DB..."
#Wait before repeating loop
time.sleep(delay)
conn.close()
NOTE: All code from class sensor (DHT22.py code) is the same, I didn't changed anything.
I noticed that after execution of DHT22 code part (class sensor), I can't get any readings from another sensors, so can anyone tell me why is this happening and how can I get this work? Thanks in advance.
Here is the output screenshot:
