Temperature and Humidity Monitoring over Cloud using Raspberry Pi and Cayenne

Temperature and Humidity Monitoring using Cayenne and Raspberry Pi

Monitoring data using IoT platforms makes our work easy. There are number of IoT platforms and Cayenne is one of them. And, to setup Cayenne is easy and faster than other IoT platform like Artik, Particle, Blynk and ThingSpeak. Below are some features of the cayenne platform:

  • In Cayenne, we can add sensors, motors, actuators, GPIO boards, and more to control them remotely.
  • Cayenne have customizable dashboards with drag-and-drop widgets for different connected devices.
  • We can create triggers and threshold alerts for devices, events, and actions.
  • Easy to automate the process for one or multiple devices

 

Earlier we have setup the Cayenne with Raspberry Pi and controlled a LED using Cayenne Dashboard. Here, in this project we are going to monitor the temperature and humidity data over the Cayenne dashboard using DHT11 with Raspberry Pi. To publish data to Cayenne dashboard python MQTT library will be used and to read data from DHT11 sensor Adafruit DHT sensor library will be used.

 

Components Required

  • Raspberry Pi
  • DHT11 Sensor
  • Jumper Wires

 

Circuit Diagram

Circuit Diagram for Temperature and Humidity Monitoring using Cayenne and Raspberry Pi

 

Vcc pin of DHT11 is connected with Pi’s 3.3v pin and GND pin is connected with Pi’s GND pin. While data pin is connected with GPIO17 pin.

 

Cayenne Setup with Raspberry Pi

 

Setting Raspberry Pi for Monitoring on Cayenne Dashboard

Start with installing libraries for python MQTT and Adafruit DHT sensor, by using the below commands:

sudo pip install paho-mqtt
sudo apt-get install build-essential python-dev python-openssl
git clone https://github.com/adafruit/Adafruit_Python_DHT.git
cd Adafruit_Python_DHT
sudo python setup.py install

 

After installing libraries, we will need to get the username, password and client id to send data to Cayenne dashboard.

 

For this go to Cayenne dashboard and click on ‘Add new’ and then click on ‘Device/Widget’.

Cayenne for Temperature and Humidity Monitoring

 

After this scroll down and click on ‘Bring Your Own Thing’.

Create Thing on Cayenne for Temperature and Humidity Monitoring

 

A window with your device details will appear. Copy these details, for further use.

Login Details of Cayenne for Temperature and Humidity Monitoring

 

Programming Explanation

First, import all the libraries that will be used in this code:

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

 

Now enter your device details like username, password and client id to connect DHT11 sensor to Cayenne Dashboard.

username = "MQTT USERNAME"
password = "MQTT PASSWORD"
client = "CLIENT ID"

 

It will check the username and password and will connect the device with Cayenne dashboard at 1883 port. This process runs in a loop.

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()

 

Now using the below code, it will create two channels for temperature and humidity.

topic_dht11_temp = "v1/" + username + "/things/" + clientid + "/data/1"
topic_dht11_humidity = "v1/" + username + "/things/" + clientid + "/data/2"

 

In this while loop, it will calculate humidity and temperature and publish it to Cayenne dashboard using mqttc.publish function.

while True:
    try:
        humidity11, temp11 = Adafruit_DHT.read_retry(11, 17)   // 11 is the sensor type, 17 is the GPIO pin number   

        if temp11 is not None:
            temp11 = "temp,c=" + str(temp11)
            mqttc.publish(topic_dht11_temp, payload=temp11, retain=True)
        if humidity11 is not None:
            humidity11 = "rel_hum,p=" + str(humidity11)
            mqttc.publish(topic_dht11_humidity, payload=humidity11, retain=True)

 

Python Code for Cayenne Data Monitoring

The complete python code for this project is given at the end. Now, create a new file using below command:

Nano dht111.py

 

Then Copy the paste the code given at the end and save into this file.

 

Now run the file using:

Python dht11.py

 

Navigate to your Cayenne Dashboard and it will look like this:

Cayenne Dashboard for Temperature and Humidity Monitoring

 

Hence, we have successfully setup the Cayenne dashboard with Pi to monitor temperature and humidity data using DHT11 sensor.

Code

import paho.mqtt.client as mqtt

import time

import sys

import Adafruit_DHT

 

time.sleep(30)

 

username = "MQTT USERNAME"

password = "MQTT PASSWORD"

clientid = "CLIENT ID"

 

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_dht11_temp = "v1/" + username + "/things/" + clientid + "/data/1"

topic_dht11_humidity = "v1/" + username + "/things/" + clientid + "/data/2"

 

 

while True:

    try:

        humidity11, temp11 = Adafruit_DHT.read_retry(11, 17)   // 11 is the sensor type, 17 is the GPIO pin number

      

       

        if temp11 is not None:

            temp11 = "temp,c=" + str(temp11)

            mqttc.publish(topic_dht11_temp, payload=temp11, retain=True)

        if humidity11 is not None:

            humidity11 = "rel_hum,p=" + str(humidity11)

            mqttc.publish(topic_dht11_humidity, payload=humidity11, retain=True)

      

        time.sleep(5)

    except (EOFError, SystemExit, KeyboardInterrupt):

        mqttc.disconnect()

        sys.exit()

1 Comments