Lab 2 and 3
Lab 2 and 3
Lab 2 and 3: Interfacing Sensors and Introduction to MQTT and Node Red
Objectives:
• Understanding the importance of protocols such as MQTT
• Ability to use MQTT protocol for sharing information across computers
• Interfacing simple sensors and actuators with the Raspberry Pi
• Introduction to node red and its functionalities
• Employing Node Red to create a simple dashboard
Equipment:
• Raspberry Pi
• USB power supply and cable
• Monitor
• HDMI to VGA converter
• Mouse and Keyboard
• Laptop Computer
• Breadboard
• Jumper Wires
• LED
• 330Ω Resistor
• Micro Switch
• Ultrasonic Sensor (HC-SR04 or HC-SR05)
1
Background:
Background
MQTT stands for Message Queuing Telemetry Transport. It is a publish/subscribe,
extremely simple and lightweight messaging protocol, designed for constrained devices and
low-bandwidth, high-latency or unreliable networks. The design principles are to minimize
network bandwidth and device resource requirements whilst also attempting to ensure
reliability and some degree of assurance of delivery. These principles also turn out to make
the protocol ideal of the emerging “machine-to-machine” (M2M) or “Internet of Things”
world of connected devices, and for mobile applications where bandwidth and battery
power are at a premium. MQTT was invented by Dr Andy Stanford-Clark of IBM, and Arlen
Nipper of Arcom (now Eurotech), in 1999. TCP/IP port 1883 is reserved with IANA for use
with MQTT. TCP/IP port 8883 is also registered, for using MQTT over SSL.
On Raspberry Pi
Step 1-Raspberry Pi- Open a terminal and install paho-mqtt library using pip
Step 2-copy the provided python script mqtt_client.py to the Desktop and run it.
2
cd Desktop
python mqtt_client.py
If successful, the terminal would display the message Connected with result code 0
On Laptop/Desktop computer
Step 3- Open the command line and install the paho-mqtt library using pip
Step4- Open your favorite python IDE and run the given mqtt_publisher.py script
3
For instance: http://192.168.1.7:1880
You will be taken to the node red site which is hosted on your Raspberry Pi
Subtask 2- Create a Hello world program using inject and debug modules
Subtask 3- Setup a configuration to turn on and off a LED through node red
Expand the previous setup to turn on a LED through switch connected to raspberry pi
4
Expand the previous setup to send an email every time the button is pressed