IoT HardwaresRaspberry PiSensorsTutorials/DIY

Cloud Based Temperature and Humidity Monitoring System

Today we are presenting a Cloud Based Temperature and Humidity Monitoring System. In this tutorial, we are going to use Raspberry Pi and DHT11 with Cayenne.

What is Cayenne ?

Cayenne is the world’s first drag and drop IoT project builder that empowers developers, designers and engineers to quickly prototype and share their connected device projects. Cayenne was designed to help users create Internet of Things prototypes and then bring them to production.

There are several major components in the platform:

  • Cayenne Mobile Apps – Remotely monitor and control your IoT projects from the Android or iOS Apps.
  • Cayenne Online Dashboard – Use customizable widgets to visualize data, set up rules, schedule events and more.

Requirements

  • Raspberry Pi
  • DHT11 Sensor
  • Jumper Wires

Circuit Diagram

rpi dht11

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.

Recommended: Control LED using Raspberry Pi with Telegram Bot


Temperature and Humidity Monitoring over Cloud using Raspberry Pi and Cayenne


Setup Cayenne for Raspberry Pi

First Install 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

Now Open Cayenne – my Device and sign up.

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’.

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

Copy these details, as shown below

cayenne

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

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

Nano rpidht11.py

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

Now run the file using:

Python rpidht11.py

Navigate to your Cayenne Dashboard and create widgets for Temp and humidity.

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

 


Thanks for reading. If you like this post probably you might like my next ones, so please support me by subscribing my blog.

Harshvardhan Mishra

Hi, I'm Harshvardhan Mishra. Tech enthusiast and IT professional with a B.Tech in IT, PG Diploma in IoT from CDAC, and 6 years of industry experience. Founder of HVM Smart Solutions, blending technology for real-world solutions. As a passionate technical author, I simplify complex concepts for diverse audiences. Let's connect and explore the tech world together! If you want to help support me on my journey, consider sharing my articles, or Buy me a Coffee! Thank you for reading my blog! Happy learning! Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *