DHT11/DHT22 with Raspberry Pi


#230

I have been using the same Pi for this project but I went ahead and removed the temp/humidity device on my dashboard. I added the new device ID to my code and its online consistently but no gauges on my dashboard :expressionless:


#231

You’ll have to put the new credentials in your script as well (client ID)


#232

I updated the Client ID in the tempsensor.py file is that what your referring to ?


#233

Yes. I believe the username/password stays the same, but double check those as well.


#234

I confirmed the Username and password are correct with the new Client ID.


#235

Can you post the code you are using with the credentials taken out?


#236
import paho.mqtt.client as mqtt
import time
import sys
import Adafruit_DHT

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

username = " "
password = " "
clientid = " "

mqttc = mqtt.Client(client_id=clientid)
mqttc.username_pw_set(username, password=password)
mqttc.connect("mqtt.mydevices.com", port=1883, keepalive=60)
mqttc.loop_start()


topic_dht22_temp = "v1/" + username + "/things/" + clientid + "/data/3"
topic_dht22_humidity = "v1/" + username + "/things/" + clientid + "/data/4"

while True:
    try:
        humidity22, temp22 = Adafruit_DHT.read_retry(22, 4) #22 is the sensor type, 4 is the GPIO pin number (not physical pin number)

        if temp22 is not None:
            temp22 = "temp,c=" + str(temp22)
            mqttc.publish(topic_dht22_temp, payload=temp22, retain=True)
        if humidity22 is not None:
            humidity22 = "rel_hum,p=" + str(humidity22)
            mqttc.publish(topic_dht22_humidity, payload=humidity22, retain=True)
        time.sleep(5)
    except (EOFError, SystemExit, KeyboardInterrupt):
        mqttc.disconnect()
        sys.exit()

#237

Thanks, just wanted to make sure there were’t any errors there and there are not (that I saw). If you run the script below in a terminal with python (filename).py are you getting any errors? What is the connection status code?

import paho.mqtt.client as mqtt
import time
import sys
import Adafruit_DHT

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

username = " "
password = " "
clientid = " "

def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))

mqttc = mqtt.Client(client_id=clientid)
mqttc.username_pw_set(username, password=password)
mqttc.connect("mqtt.mydevices.com", port=1883, keepalive=60)
mqttc.on_connect = on_connect
mqttc.loop_start()


topic_dht22_temp = "v1/" + username + "/things/" + clientid + "/data/3"
topic_dht22_humidity = "v1/" + username + "/things/" + clientid + "/data/4"

while True:
    try:
        humidity22, temp22 = Adafruit_DHT.read_retry(22, 4) #22 is the sensor type, 4 is the GPIO pin number (not physical pin number)

        if temp22 is not None:
            temp22 = "temp,c=" + str(temp22)
            mqttc.publish(topic_dht22_temp, payload=temp22, retain=True)
        if humidity22 is not None:
            humidity22 = "rel_hum,p=" + str(humidity22)
            mqttc.publish(topic_dht22_humidity, payload=humidity22, retain=True)
        time.sleep(5)
    except (EOFError, SystemExit, KeyboardInterrupt):
        mqttc.disconnect()

#238

When I initially got it working I used “python2 /home/pi/python/tempsensor.py” I am not sure how to check the connection status code?

When I try running with python 3 I get this:
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
ImportError: No module named ‘Adafruit_DHT’


#239

I added a print statement to the code, it will just print it out when it connects. Looks like you don’t have the proper libraries installed under python3 just just use python 2. Save this file to /home/pi/python/test.py and then run it with python2 /home/pi/python/test.py


#240

I copied the code from tempsensor.py to /home/pi/python/test.py and used “python2 /home/pi/python/test.py”

python


#241

No, I mean use the code I posted a few posts up DHT11/DHT22 with Raspberry Pi


#242

python


#243

Why am I getting the error message below?
pi @ raspberrypi: ~ $ sudo pip install paho-mqtt
sudo: pip: command not found


#245

Why am I getting the error message below?
pi @ raspberrypi: ~ $ sudo pip install paho-mqtt
sudo: pip: command not found


#246

Just means you don’t have pip installed. Install with sudo apt-get install pip


#247

Thanks but now he has another problem!
pi@raspberrypi:~ $ sudo apt-get install pip
Reading package lists… Done
Building dependency tree
Reading state information… Done
E: Unable to locate package pip


#248

Sorry, it’s python-pip. Try:

sudo apt-get install python-pip


#249

Thanks it works. How pleace i a page into cd /home/pi/python/ white the name tempsensor.py
it says
pi@raspberrypi:~ $ cd /home/pi/python/
-bash: cd: /home/pi/python/: No such file or directory


#250

Try using the command below, then cd to it.

mkdir -p /home/pi/python/