Day2-MQTT-Lab Maual PDF
Day2-MQTT-Lab Maual PDF
Day2-MQTT-Lab Maual PDF
DAY 2
IOT PROTOCOLS
This tutorial explains how to use MQTT (Message Queue and Telemetry Transportation) Protocol in
Raspberrypi device.
----------------------------------------------------------------------------------------------------------------------------------------
EXERCISE 1:
OUTPUT: a) Observe that the text published by Android Application TAB “Helloworld
raspberrypi mqtt “will be displayed in TERMINAL 2
b) Press the SUBSCRIBE Tab in Android Application MQTT Dashboard. The text published by
Android Application TAB will be displayed in TERMINAL 2 of Raspberrypi and will also be
displayed in SUBSCRIBE TAB of Android Application.
----------------------------------------------------------------------------------------------------------------------------------------
EXERCISE 2:
Install Paho
Sudo pip install paho-mqtt
Subscriber Program
Step 1: Import mqtt client library.
import paho.mqtt.client as mqtt
Step 2: Intialize mqtt client variable and connect client
client = mqtt.Client()
client.connect("THE_IP_ADDRESS_OF_OUR_BROKER",1883,60)
Step 3: Following is the function body, that will be executed on connection establishment and
on message publishing by publisher respectively
def on_connectfun(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe("topic/test")
Publisher Program
Step 1: Import mqtt client library.
import paho.mqtt.client as mqtt
Step 2: Intialize mqtt client Instance
client = mqtt.Client()
Step 3: Connect to the client device
SYNTAX:connect (host, port=1883, keepalive=60, bind_address=""). Bind address optional
client.connect("localhost",1883,60)
Step 4: client.publish("topic/test", "Hello world!");
Step 5: Disconnect Client
client.disconnect();
-------------------------------------------------------------------------------------------------------------------------------------
EXERCISE 3:
Using Paho package, write a python program to activate actuation (running the motor)/LED on –
off/BUZZER on-off based on the commands from mobile application.
OUTPUT:
Subscriber Program
import paho.mqtt.client as mqtt
import RPi.GPIO as GPIO
from time import sleep
#This is the GPIO PIN used for data Output
Motor1A = 16
LED1 = 10
Buzzer = 8
#Set GPIO Mode
GPIO.setmode(GPIO.BOARD)
flag=0
#GPIO set as output for Motor, LED and Buzzer
GPIO.setup(Motor1A,GPIO.OUT)
GPIO.setup(LED1,GPIO.OUT)
GPIO.setup(Buzzer,GPIO.OUT)
#Generate PWM signal at 16 to control motor
p=GPIO.PWM(16,50)
def on_connectfun(client, userdata, flags, rc):
print("Connected with result code "+ str(rc));
client.subscribe("topic/test");
# Function called on subscription
def on_messagefun(client, userdata, msg):
if msg.payload.decode()=="R":
print "Turning motor to 180 degree"
p.start(12.5)
sleep(2)
elif msg.payload.decode()=="Z":
print "Turning motor to 0 degree"
p.ChangeDutyCycle(2.5)
sleep(2)
elif msg.payload.decode()=="LON":
print "LED ON"
GPIO.output(LED1,GPIO.HIGH)
sleep(1)
elif msg.payload.decode()=="LOFF":
print "LED OFF"
# flag=1;
GPIO.output(LED1,GPIO.LOW)
sleep(2)
elif msg.payload.decode()=="BON":
print "Buzzer ON"
GPIO.output(Buzzer,GPIO.HIGH)
sleep(2)
elif msg.payload.decode()=="BOFF":
print "Buzzer OFF"
GPIO.output(Buzzer,GPIO.LOW)
sleep(2)
elif msg.payload.decode()=="MOFF":
p.stop()
else:
p.stop() #Stop Motor
GPIO.cleanup()
client.disconnect();
client= mqtt.Client();
client.connect("10.184.6.211",1883,60);
client.on_connect = on_connectfun
client.on_message = on_messagefun
client.loop_forever();
Publisher
Configure your MQTT Dashboard mobile application buttons. Use the necessary Publish Text and topic
name for configuring Control Buttons
Step 1: Configure MQTT Dashboard Connection to Raspberrypi broker by clicking RED + button at the
bottom right corner and inserting necessary textboxes. After configuration, click ‘connection name’.
Step2: Configure Subscriber by clicking + button at top right corner and inserting necessary textboxes
Step 3: Configure Publisher by clicking + button and selecting Button Component Type. Insert the text
fields with necessary values. Repeat the same to create control buttons for all devices.
Step 4: Click the Control button to control the devices-Motor, LED and Buzzer
OUTPUT: The Buzzer, LED and MOTOR can be controlled by Button press from mobile application.
_____________________________________________________________________________________
REFERENCES
Ref: http://blog.thelifeofkenneth.com/2016/07/driving-leds-on-raspberry-pi-via-mqtt.html
https://pypi.python.org/pypi/paho-mqtt/1.2
https://stackoverflow.com/questions/37006863/python-mqtt-script-on-raspberry-pi-to-send-
and-receive-messages
38.186, indigo
http://www.ev3dev.org/docs/tutorials/sending-and-receiving-messages-with-mqtt/
https://pypi.python.org/pypi/paho-mqtt/1.2