DHT11/DHT22 with Raspberry Pi


#64

Woohoo! Thanks for confirming the code @ognqn.chikov and also thanks to @jburhenn for providing the edits. Way to go!

-B


#65

Here is the code also for DHT22. Sorry that I have remove it, but I split the code for every sensor in separate files.

import cayenne.client as cayenne
import time
import Adafruit_DHT


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

MQTT_USERNAME  = "YOUR_USERNAME"
MQTT_PASSWORD  = "YOUR_PASSWORD"
MQTT_CLIENT_ID = "YOUR_CLIENDID"

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, 18) #11 is the sensor type, 18 is the GPIO pin number that DATA wire is connected to
        humidity22, temp22 = Adafruit_DHT.read_retry(22, 19) #22 is the sensor type, 19 is the GPIO pin number that DATA wire is connected to

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

#66

Thank you, very much.


#67

In Monday I will test for sure that is working with DHT22! Stay tuned :slight_smile:


#68

I just tried running the script…with my DHT22…i got the following error message:


#69

Yes I have a mistake! Sorry! I put one more space than needed! Sorry.
Can you try with this code:

import cayenne.client as cayenne
import time
import Adafruit_DHT


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

MQTT_USERNAME  = "YOUR_USERNAME"
MQTT_PASSWORD  = "YOUR_PASSWORD"
MQTT_CLIENT_ID = "YOUR_CLIENDID"

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, 18) #11 is the sensor type, 18 is the GPIO pin number that DATA wire is connected to
        humidity22, temp22 = Adafruit_DHT.read_retry(22, 19) #22 is the sensor type, 19 is the GPIO pin number that DATA wire is connected to

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

Also when you copy it, sometimes some more spaces are added. I use pycharm just for indentation verification.


#70

No need to be sorry, you`re doing a great service to the community…

with the new script i get this error message:


#71

I start the command with sudo python3


#72

I was able to get rid of the error message using

sudo pip install cayenne-mqtt

The DHT22 seam to be Online but there are no widgets in the menu


#73

I just tried using the sudo command…

shortly after that i noticed some Messages popping up:


#74

Can you try sudo pip3 install cayenne-mqtt
and then run the file with sudo python3 filename.py


#75

Yes, I need to research about this message. It is ok to receive messages but with measurements of the sensor, not this one


#76

sudo pip3 install cayenne-mqtt

gives me the following error message:

As it turns out, i’ve got some spare time at this moment…if you need someone for troubleshooting, keep the sugestions coming :slight_smile:


#77

You properly connected the DHT22 sensor and edit the code for the GPIO wire?


#78

I will investigate this error and try to give you suggestions. Les’t get back to the empty dashboard to try send data using python2 (sudo python) command. Because I run my code using python3


#79

This is the Script I`m using…

import cayenne.client as cayenne
import time
import Adafruit_DHT


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

MQTT_USERNAME  = "e4c4f330-c845-11e6-bd19-2daeb3a13a46"
MQTT_PASSWORD  = "4471f03b7f4cd13453a6d95724dbbe92ee409efc"
MQTT_CLIENT_ID = "4c88c740-0706-11e7-a905-f3d1e6fb8dd5"

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, 18) #11 is the sensor type, 18 is the GPIO pin number that DATA wire is connected to
        humidity22, temp22 = Adafruit_DHT.read_retry(22, 26) #22 is the sensor type, 19 is the GPIO pin number that DATA wire is connected to

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

The DHT is connected to 3,3V, GPIO 26 & GND…
I going to take a picture… ~5min


#80

When I try to run my code with sudo python myfile.py I get the same error as you:

Traceback (most recent call last):
  File "dht11.py", line 1, in <module>
    import cayenne.client as cayenne
ImportError: No module named cayenne.client

I think that it is because I install cayenne-mqtt using python3.


#81

Can you run sudo apt-get install python3-pip


#82

We have Liftoff!:slight_smile:

sudo python2 /home/pi/python/tempsensor.py
Seams to work, but unfortunatly i get some Reading-error.

but this might be because of the 3-4Meter Cable I`m using.


#83

sudo apt-get install python3-pip

is already installed