Python Forum
Raspberry PI with DHT22 Sensors
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Raspberry PI with DHT22 Sensors
#1
guys please can you help the noob

i have connected 6 x DHT22 Temperature and Humidity Sensors on my Raspberry PI 4

I am getting a reading from the first DHT22 Sensor on GPIO 22 [ The Other DHT22 Sensors are Connected to GPIO 2, 3, 4, 17, 27 ]

I want to test the Other 5 x DHT22 Sensors

I am Using the Following code to test One DHT22 Sensor

I need to change the Same Code to Test All 6 x DHT22 Sensors

What do i need to do in the code to Test All 6 x DHT22 Sensors with One Python File?

CODE I am Using:

import adafruit_dht
import time


class DHT22Module:
    def __init__(self, pin):
        self.dht_device = adafruit_dht.DHT22(pin)

    def get_sensor_readings(self):
        while True:
            try:
                # Print the values to the serial port
                temperature_c = self.dht_device.temperature
                temperature_f = temperature_c * (9 / 5) + 32
                humidity = self.dht_device.humidity
                print(
                    "Temp: {:.1f} F / {:.1f} C    Humidity: {}% ".format(
                        temperature_f, temperature_c, humidity
                    )
                )
                return temperature_c, humidity

            except RuntimeError as error:
                # Errors happen fairly often, DHT's are hard to read
                print(error.args[0])
                time.sleep(2.0)
                continue
            except Exception as error:
                self.dht_device.exit()
                raise error
buran write May-11-2023, 10:45 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
Is there a reason why you wouldn't make five DHT22Module objects and call them in order?
modules = {pin: DHT22Module(pin) for pin in (2, 3, 4, 17, 27)}

for pin, module in modules.items():
    print(pin, module.get_sensor_readings()
I would remove the prints inside the DHT22Module class (lines 13-19)
Reply
#3
(May-11-2023, 07:02 PM)deanhystad Wrote: Is there a reason why you wouldn't make five DHT22Module objects and call them in order?
modules = {pin: DHT22Module(pin) for pin in (2, 3, 4, 17, 27)}

for pin, module in modules.items():
    print(pin, module.get_sensor_readings()
I would remove the prints inside the DHT22Module class (lines 13-19)

deanhystad thank you for your reply

The class DHT22Module is the main interface to the DHT22 sensor as we pass the GPIO pin that we will use to communicate with a sensor.

The function get_sensor_readings() is what is called to retrieve the sensor readings from the DHT22 sensor. I am using the adafruit_dht library to retrieve the sensor readings and it would return a Python tuple of both the temperature and humidity readings.

any ideas?
Reply
#4
I don't know what you mean by "noob". No programming experience? No Python experience? No experience with Raspberry Pi? No experience with this sensor? "noob" is meaningless.

Do you understand what the DHT22Module: class does? Do you understand how you would use it to read a sensor?
Reply
#5
right, looks like i am over my head here. just being honest

oh well, time to read some books

thank you for your help
Reply
#6
(May-11-2023, 08:08 PM)deanhystad Wrote: I don't know what you mean by "noob". No programming experience? No Python experience? No experience with Raspberry Pi? No experience with this sensor? "noob" is meaningless.

Do you understand what the DHT22Module: class does? Do you understand how you would use it to read a sensor?

No programming experience? Yes some

No Python experience? No

No experience with Raspberry Pi? Lots of experience with Raspberry PI

No experience with this sensor? Yes, i can read the Sensor and Any Sensor on Any Raspberry PI. My Problem is Reading Multiple Sensors using python
So python is my issue, hence me coming here to learn.
Reply
#7
What is it that you want to accomplish here? You say you want to read 5 sensors. What do you want to do with the data returned by the sensors? Do you just want to print it, or do you want to save it to a file, or do something else? How often do you want to read the sensors? Just once? Or do you want to read them over and over until you stop the program?

My little example should read data from the 5 sensors and print it.

This part makes a sensor dictionary where the key is the pin number, and the value is an instance of the DHT device.
modules = {pin: DHT22Module(pin) for pin in (2, 3, 4, 17, 27)}
Instead of a dictionary I could have made a list.
modules = [
    DHT22Modules(2),
    DHT22Modules(3),
    DHT22Modules(4),
    DHT22Modules(17),
    DHT22Modules(27)
]
This is better than making a bunch of variables like this:
sensor1 = DHT22Modules(2)
sensor2 = DHT22Modules(3)
sensor3 = DHT22Modules(4)
sensor4 = DHT22Modules(17)
sensor5 = DHT22Modules(27)
)
The reason putting the sensor objects in a dictionary or a list instead of assigning each to a different variable is that it allows you to use a loop perform the same operation on each sensor. This is how you can read all the sensors if they were in a list.
data = []
for module in modules:
    data.append(module.get_sensor_readings()
Or even in just one line using a list comprehension.
data = [module.get_sensor_readings() for module in modules]
Compare that to this:
data = [
    sensor1.get_sensor_readings(),
    sensor2.get_sensor_readings(),
    sensor3.get_sensor_readings(),
    sensor4.get_sensor_readings(),
    sensor5.get_sensor_readings()
]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  get data (temperature and humidity) from DHT22 into CSV and sending them over the net apollo 0 3,866 Apr-16-2021, 07:49 PM
Last Post: apollo
  Convert looping home security to a one time scan of switches and sensors duckredbeard 0 1,755 Dec-08-2020, 04:31 AM
Last Post: duckredbeard

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020