DHT11/DHT22 with Raspberry Pi

Finished processing dependencies for cayenne-mqtt==1.1.0

pi@raspberrypi:~ $ python3 /home/pi/python/tempsensor.py
Traceback (most recent call last):
File “/home/pi/python/tempsensor.py”, line 4, in
import Adafruit_DHT
ModuleNotFoundError: No module named ‘Adafruit_DHT’

add sudo

Yes, i installed it with Sudo,

now trying to run it again.

pi@raspberrypi:~ $ python3 /home/pi/python/tempsensor.py
Traceback (most recent call last):
File “/home/pi/python/tempsensor.py”, line 4, in
import Adafruit_DHT
ModuleNotFoundError: No module named ‘Adafruit_DHT’

pi@raspberrypi:~ $ sudo python3 /home/pi/python/tempsensor.py
Traceback (most recent call last):
File “/home/pi/python/tempsensor.py”, line 4, in
import Adafruit_DHT
ModuleNotFoundError: No module named ‘Adafruit_DHT’

so you need to install that module for python3

how ? , it could affect anything else ?

python3 Luminosity/light_sensor_1.py

With Light sensor i can run it with Python3. and its working fine.

sudo pip3 install Adafruit_DHT run that and it will install the correct version for you. I haven’t tested this script with python 3 so it may not work. Let me know either way.

1 Like

the light sensor uses Adafruit_DHT ???

i have to have last version, i just installed it with :
git clone https://github.com/adafruit/Adafruit_Python_DHT.git
cd Adafruit_Python_DHT
sudo python setup.py install

Also, I am confused now. Are you running 2 separate python scripts, or two scripts comined? Post the code you are using please

I have 2 Scripts, for 2 sensors of type 11.

pi@raspberrypi:~ $ ls python/
tempsensor2.py tempsensor.py

pi@raspberrypi:~ $ cat python/tempsensor2.py
#!/usr/bin/env python
import cayenne.client as cayenne
import time
import Adafruit_DHT

time.sleep(60) #Sleep to allow wireless to connect before starting MQTT

MQTT_USERNAME = “REMOVED”
MQTT_PASSWORD = “REMOVED”
MQTT_CLIENT_ID = “REMOVED”

client = cayenne.CayenneMQTTClient()
client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID)

timestamp = 0

while True:
client.loop()
if (time.time() > timestamp + 5):
humidity11, temp11 = Adafruit_DHT.read_retry(11, 17) #11 is the sensor type, 17 is the GPIO pin number that DATA wire is connected to

    if temp11 is not None:
        client.virtualWrite(5, temp11, cayenne.TYPE_TEMPERATURE, cayenne.UNIT_CELSIUS)
    if humidity11 is not None:
       client.virtualWrite(6, humidity11, cayenne.TYPE_RELATIVE_HUMIDITY, cayenne.UNIT_PERCENT)
    timestamp = time.time()

pi@raspberrypi:~ $ cat python/tempsensor.py
#!/usr/bin/env python
import cayenne.client as cayenne
import time
import Adafruit_DHT

time.sleep(60) #Sleep to allow wireless to connect before starting MQTT

MQTT_USERNAME = “REMOVED”
MQTT_PASSWORD = “REMOVED”
MQTT_CLIENT_ID = “REMOVED”

client = cayenne.CayenneMQTTClient()
client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID)

timestamp = 0

while True:
client.loop()
if (time.time() > timestamp + 5):
humidity11, temp11 = Adafruit_DHT.read_retry(11, 4) #11 is the sensor type, 4 is the GPIO pin number that DATA wire is connected to

    if temp11 is not None:
        client.virtualWrite(3, temp11, cayenne.TYPE_TEMPERATURE, cayenne.UNIT_CELSIUS)
    if humidity11 is not None:
       client.virtualWrite(4, humidity11, cayenne.TYPE_RELATIVE_HUMIDITY, cayenne.UNIT_PERCENT)
    timestamp = time.time()

###########

My issue is that after running it

i recieve logs in the console:

In FIrst:

PUB v1/REMOVED/things/REMOVED/data/3
temp,c=25.0

PUB v1/REMOVED/things/REMOVED/data/4

In second:

PUB v1/REMOVED/things/REMOVED/data/5
temp,c=25.0

PUB v1/REMOVED/things/REMOVED/data/6

I just cp cayenne dir in my script location .
Now i run it with Sudo Python its working and no logs appears.

@adam this is keep on going to continue. this is something specific to the user end, so the only option is, install all the latest module and start over.

excellent work.

1 Like

hi @adam , i was looking at these lines of codes and i don’t understand it well. This (places) and (2) what are they? what are their uses in the codes?

#def ReadTemperature(places):
humidity,temperature = Adafruit_DHT.read_retry(sensor, pin)
temperature = round(temperature, places)
return temperature
#def ReadHumidity(places):
humidity,temperature = Adafruit_DHT.read_rertry(sensor, pin)
humidity = round(humidity, places)
return humidity

while True:
#Take Temperature Reading via DHT22
temperature_percentage = ReadTemperature(2)
#Take Humidity Reading via DHT22
humidity_percentage = ReadHumidity(2)

places is the number of digits after the decimals and here is 2 places.

https://www.w3schools.com/python/ref_func_round.asp

Where did you get that code? I don’t remember it and don’t see it in my post. Seems to be what Shramik said though, simply rounding to a decimal place.

hi, i have use DHT22 before in my project and able to use the code above and a widget is added in the dashboard, but recently i reinstall the pi and install cayenne, im unable to get the widget appear in the dashboard. im still using the same code, but no widget is added.

can you be specific as to which code you are using?