Skip to content

houbena/mqtt-raspberryPi-workshop

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MQTT and RaspberryPi

What is our workshop about

This workshop is about applying MQTT protocol to turn ON/OFF a light, and to get Temperature and Humidity data and view it on an online dashboard

What is RaspberryPi

RaspberryPi is an open-source, low cost computer on a chip, in other words it is a cheap and small computer. The affordable price, the small size and the powerful hardware make RaspberryPi a perfect core for lots of IoT Projects. You can find more about RaspberryPi here

What is MQTT

MQTT is a machine-to-machine (M2M)/"Internet of Things" connectivity protocol. Further reading can be found here

How we will do it

The idea is to use a Dashboard to control light and to monitor the data received form the Temperature/Humidity sensor. What really happens behind the scenes when we move the button on the dashboard, is that a function will be triggered to send an MQTT message with a specific topic, on the other hand the python code that runs on the RaspberryPi is connected to the same MQTT Broker and subscribed to the same topic, and we have already specified in out code that if we received an MQTT message with "ON" we turn on the light (we send a signal to the relay that is connected to the RaspberryPi) and vice versa.

In the same way, the python script on the RaspberryPi is sending Temperature/Humidity as MQTT messages and the dashboard s connected to the same MQTT Broker and subscribed to the same topic, and after receiving the messages, a function is responsible about converting these messages to a user-friendly gauge.

Further Reading and Resources

MQTT mqtt.org

SSL for secure communication SSL

Eclipse Mosquitto

Eclipse Paho Paho library

Cool IoT Blog IOT BYTES

Python for Beginners Learn Python

Setup

The setup has two parts:

  • On the RaspberryPi

  • On your laptop

On RaspberryPi

  • ssh into yor RaspberryPi either from CLI (on MacOS and Linux):
ssh pi@<IP_ADDRESS>
  • or using PUTTY on Windows

  • clone this repo to your RaspberryPi by running the following command

cd ~
git clone https://github.com/ahasna/mqtt-raspberryPi-workshop.git
  • go to the repo you've just cloned
cd ~/mqtt-raspberryPi-workshop
  • Run the following:
sudo apt-get update
pip install paho-mqtt
  • edit code:
cd ~/mqtt-raspberryPi-workshop/htsensor
sudo nano run.py
  • edit lines 15 - 19 adding values to the following variables:

mqtt_broker, mqtt_broker_port, temp_topic, humidity_topic and light_topic

Note: You'll have to change the topics to unique ones of your choice to avoid receiving messages from other publishers on the same broker

# VARS
mqtt_broker = "iot.eclipse.org"
mqtt_broker_port = "1883"
temp_topic = "some_topic/sub_topic" # example: asem/home/temp
humidity_topic = "some_topic/another_sub_topic" # example: asem/home/humidity
light_topic = "some_topic/also_another_sub_topic" # example: asem/home/light
# sensor/led
led_pin = 14
sensor_pin = 4
  • if necessary edit lines 21 and 22 (in case you chose to use different GPIO Pins to connect the Sensor and LED)

  • Save changes: Ctrl + X then Y then finally Enter

On your Laptop

  • clone this repo to your local machine (laptop) by running the following command from your CLI
git clone https://github.com/ahasna/mqtt-raspberryPi-workshop.git
  • or just download from Github as a ZIP file if you don't have git installed. From here

  • go to mqtt-raspberryPi-workshop/dasboard/js (the repo you've just downloaded or cloned)

  • edit lines 24 - 27 in app.js to add the MQTT_BROKER_ADDRESS and make sure that the MQTT topics match those in run.py (in RaspberryPi)

const mqtt_broker = "iot.eclipse.org";
const temp_topic = "some_topic/sub_topic"; // example: asem/home/temp
const humidity_topic = "some_topic/another_sub_topic"; // example: asem/home/humidity
const light_topic = "some_topic/also_another_sub_topic"; // example: asem/home/light

connect Circuits

Temp./Humidity sensor (DHT11)

follow the diagram below to connect the sensor to your RaspberryPi

DHT11

LED

follow the diagram below to connect the LED to your RaspberryPi

LED

for more details see the GPIO layout for RaspberryPi3 below

GPIO

Running the Code

RaspberryPi

  • ssh into yor RaspberryPi either from CLI by using:
ssh pi@<IP_ADDRESS>

or using PUTTY

  • go to the repo you've cloned
cd ~/mqtt-raspberryPi-workshop/htsensor
python run.py
  • Run the following:
sudo apt-get update
pip install paho-mqtt

Dashboard

  • go to mqtt-raspberryPi-workshop/dasboard/
  • open index.html in browser

Expected results

if everything runs as expected you should see the following:

RaspberryPi CLI

CLI

Browser

CLI

Is all of this too easy for you?

if what we have been doing so far is not challenging enough for you, try controlling the LED using the Temp./Humidity values and add an indication alert of that to the dashboard

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 80.6%
  • Python 16.1%
  • HTML 3.3%