Posts: 22
Joined: Sat May 23, 2015 11:50 am

Adafruit DHT22 pass sensor value dynamically

Thu Jul 19, 2018 6:53 am

I am using below python code to get the temp and humidity reading.

Code: Select all

sensor = Adafruit_DHT.DHT22
pin = 23
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
everything works fine but since i am using multiple sensors(DHT11, DHT22, AM2302) I am planing to make it dynamic and pass sensor and pin values from database. Sample code as below.

Code: Select all

for s in sensorDict:      
            humidity, temperature = Adafruit_DHT.read_retry(s['sensortype'], s['pin'])
here s['sensortype'] = Adafruit_DHT.DHT22

received error "ValueError: Expected DHT11, DHT22, or AM2302 sensor value"

how can i pass sensor value dynamically??

User avatar
Posts: 33342
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Adafruit DHT22 pass sensor value dynamically

Thu Jul 19, 2018 9:57 pm

Sensortype needs to be a simple string: SENSORS = [DHT11, DHT22, AM2302]

You can read the code at: ... /
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

Return to “Python”

Who is online

Users browsing this forum: raspi_fun and 14 guests