OriginalMrP
Posts: 4
Joined: Mon Mar 05, 2018 8:35 pm

RPI Zero W and two DHT11 confusion

Thu Aug 15, 2019 7:01 pm

Hi,
First off apologies if this has been asked previously, but I couldn’t locate answer via the search function.

I need to take temp and humidity readings from an underfloor space in the 3 separate rooms.
I’m using 3 rpi zero with a DHT11 and Adafruit IO and so far, so good using the Adafruit guide.
A2883251-9923-4CC5-AB85-4D2F64CFE433.jpeg
A2883251-9923-4CC5-AB85-4D2F64CFE433.jpeg (229.64 KiB) Viewed 1436 times
However, I’m coming unstuck when trying to add a second DHT to a zero w, as I need to monitor two separate locations beneath the floor.

Is there a way of modifying the Adafruit script for the zero w recognise 2 distinct DHT11’s?

Cheers.

"""
'temp_humidity.py'
==================================
Example of sending analog sensor
values to an Adafruit IO feed.

Author(s): Brent Rubell

Tutorial Link: Tutorial Link: https://learn.adafruit.com/adafruit-io- ... d-humidity

Dependencies:
- Adafruit IO Python Client
(https://github.com/adafruit/io-client-python)
- Adafruit_Python_DHT
(https://github.com/adafruit/Adafruit_Python_DHT)
"""

Code: Select all

# import standard python modules.
import time
 
# import adafruit dht library.
import Adafruit_DHT
 
# import Adafruit IO REST client.
from Adafruit_IO import Client, Feed
 
# Delay in-between sensor readings, in seconds.
DHT_READ_TIMEOUT = 5
 
# Pin connected to DHT22 data pin
DHT_DATA_PIN = 26
 
# Set to your Adafruit IO key.
# Remember, your key is a secret,
# so make sure not to publish it when you publish this code!
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
 
# Set to your Adafruit IO username.
# (go to https://accounts.adafruit.com to find your username).
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
 
# Create an instance of the REST client.
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
 
# Set up Adafruit IO Feeds.
temperature_feed = aio.feeds('temperature')
humidity_feed = aio.feeds('humidity')
 
# Set up DHT22 Sensor.
dht22_sensor = Adafruit_DHT.DHT22
 
while True:
    humidity, temperature = Adafruit_DHT.read_retry(dht22_sensor, DHT_DATA_PIN)
    if humidity is not None and temperature is not None:
        print('Temp={0:0.1f}*C Humidity={1:0.1f}%'.format(temperature, humidity))
        # Send humidity and temperature feeds to Adafruit IO
        temperature = '%.2f'%(temperature)
        humidity = '%.2f'%(humidity)
        aio.send(temperature_feed.key, str(temperature))
        aio.send(humidity_feed.key, str(humidity))
    else:
        print('Failed to get DHT22 Reading, trying again in ', DHT_READ_TIMEOUT, 'seconds')
    # Timeout to avoid flooding Adafruit IO
    time.sleep(DHT_READ_TIMEOUT)

pcmanbob
Posts: 7122
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: RPI Zero W and two DHT11 confusion

Fri Aug 16, 2019 8:40 am

Hi.

So if the adafruit code can only access on sensor, have you tried using different gpio pins for each sensor data pin and then just pointing the code at each gpio in turn.

Have never tried this but it seems the simple answer.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

OriginalMrP
Posts: 4
Joined: Mon Mar 05, 2018 8:35 pm

Re: RPI Zero W and two DHT11 confusion

Fri Aug 16, 2019 10:57 am

Hi,
I've added the GPIO pins for each sensor (and relevant feed info) within the original script and only one set of sensor readings have been collected.
I have also duplicated the script, changing the pin reference and feed info and runnning them at the same time, same issue.


I'm an absolutle beginner when it comes to this.

pcmanbob wrote:
Fri Aug 16, 2019 8:40 am
Hi.

So if the adafruit code can only access on sensor, have you tried using different gpio pins for each sensor data pin and then just pointing the code at each gpio in turn.

Have never tried this but it seems the simple answer.

PhatFil
Posts: 1423
Joined: Thu Apr 13, 2017 3:55 pm
Location: Oxford UK

Re: RPI Zero W and two DHT11 confusion

Fri Aug 16, 2019 2:43 pm

everyone was a beginner, some of us remain in the state perpetually..

#1 are you 100% sure dht11 sensor #2 works ok? try replacing it with #1 sensor in your pi circuit and running the original working with 1 sensor script to validate it..


You can always pop over to the github page for the adafruit python lib, there you will find documentation and example scripts. https://github.com/adafruit/Adafruit_Python_DHT

Perhaps forget all the fancy cloud server logging stuff for now and write a new script that only reads 2 x dht11 sensors based on examples from the link above..

3 or 4 lines of code is easier to focus on and digest than 10+

OriginalMrP
Posts: 4
Joined: Mon Mar 05, 2018 8:35 pm

Re: RPI Zero W and two DHT11 confusion

Fri Aug 16, 2019 4:49 pm

Yep, checked all the DHT11's and they all report back.
I'll take a look at the link.
Cheers

OriginalMrP
Posts: 4
Joined: Mon Mar 05, 2018 8:35 pm

Re: RPI Zero W and two DHT11 confusion

Tue Aug 20, 2019 7:52 am

Just in case anyone is following, a chap on the Adafruit forum helped with the following, and both DHT11 sensors on the 3rd rpi are reporting.


Code: Select all

# import standard python modules.
import time

# import adafruit dht library.
import Adafruit_DHT

# import Adafruit IO REST client.
from Adafruit_IO import Client, Feed

# Delay in-between sensor readings, in seconds.
DHT_READ_TIMEOUT = 60



# Pin connected to DHT22 data pin
DHT_DATA_PIN = 4
# Pin for second DHT11 ( 5 for example -- use an availalbe GPIO Pin)
DHT_DATA_PIN2 = 5


# Set to your Adafruit IO key.
# Remember, your key is a secret,
# so make sure not to publish it when you publish this code!
ADAFRUIT_IO_KEY = '111-111'

# Set to your Adafruit IO username.
# (go to https://accounts.adafruit.com to find your username).
ADAFRUIT_IO_USERNAME = '111-111'

# Create an instance of the REST client.
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)

# Set up Adafruit IO Feeds.
temperature_feed = aio.feeds('pi3.temperature')
humidity_feed = aio.feeds('pi3.humidity')
# additionalal feeds for second DHT11
temperature_feed2 = aio.feeds('pi3.temperature2')
humidity_feed2 = aio.feeds('pi3.humidity2')


# Set up DHT22 Sensor.
dht11_sensor = Adafruit_DHT.DHT11
# setup second sensor
dht11_sensor2 = Adafruit_DHT.DHT11

while True:
    humidity, temperature = Adafruit_DHT.read_retry(dht11_sensor, DHT_DATA_PIN)
    if humidity is not None and temperature is not None:
        print('Temp={0:0.1f}*C Humidity={1:0.1f}%'.format(temperature, humidity))
        # Send humidity and temperature feeds to Adafruit IO
        temperature = '%.2f'%(temperature)
        humidity = '%.2f'%(humidity)

        aio.send(temperature_feed.key, str(temperature))
        aio.send(humidity_feed.key, str(humidity))
    else:
        print('Failed to get DHT11 Reading, trying again in ', DHT_READ_TIMEOUT, 'seconds')
    humidity2, temperature2 = Adafruit_DHT.read_retry(dht11_sensor2, DHT_DATA_PIN2)
    if humidity2 is not None and temperature2 is not None:
        print('Temp2={0:0.1f}*C Humidity2={1:0.1f}%'.format(temperature2, humidity2))
        # Send humidity and temperature feeds to Adafruit IO
        temperature2 = '%.2f'%(temperature2)
        humidity2 = '%.2f'%(humidity2)

        aio.send(temperature_feed2.key, str(temperature2))
        aio.send(humidity_feed2.key, str(humidity2))
    else:
        print('Failed to get second DHT11 Reading, trying again in ', DHT_READ_TIMEOUT, 'seconds')



    # Timeout to avoid flooding Adafruit IO
    time.sleep(DHT_READ_TIMEOUT)

Return to “Automation, sensing and robotics”