Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Day2-MQTT-Lab Maual PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8
At a glance
Powered by AI
The key takeaways are about using MQTT protocol for IoT applications including publish subscribe operations between devices like Raspberry Pi and Android.

The steps to install MQTT broker on Raspberry Pi are to open a terminal, use apt-get install mosquitto to install the broker, and check that the broker is running using systemctl status mosquitto.

An Android application can be used to publish and subscribe to MQTT topics by installing an MQTT client app from the Google Play store, configuring a connection to the MQTT broker, and setting up publishers and subscribers with topics.

INTERNET OF THINGS – TRAINING LAB MANUAL

DAY 2

IOT PROTOCOLS

This tutorial explains how to use MQTT (Message Queue and Telemetry Transportation) Protocol in
Raspberrypi device.

----------------------------------------------------------------------------------------------------------------------------------------

EXERCISE 1:

Using mosquito package, Implement following operations


1) The Publish – Subscribe operation in Raspberrypi Terminals
2) The Publish in Android Application and Subscribe operation in Raspberrypi terminal
INPUT: 1. Publishing text: “Helloworld raspberrypi mqtt”
2. Publishing topic name: “temperature_sensors_data”
OUTPUT: Subscription in Subscriber terminal of Raspberrypi device and the subscription in Android
Application
------------------------------------------------------------------------------------------------------------------------------------------

Step I :Installation of the MQTT Broker on Raspberry Pi


1. Open terminal-(here after referred as TERMINAL 1).
2. Use the following command to install the broker.
apt-get install mosquito
Check broker is running
systemctl status mosquitto
Step 2: Installation of the MQTT Client on Raspberry Pi
1. Command to install MQTT Client
apt-get install mosquitto-clients
Step 3: Testing the Working of MQTT
1. Open terminal-(here after referred as TERMINAL 2).
2. On TERMINAL 2, to register the subscriber use the following command. The subscriber
mqtt_topic_hello.
mosquitto_sub –d –t temp
3. On TERMINAL 1 -To publish data, MQTT publisher uses the same topic mqtt_topic_hello.
mosquitto_pub –d –t temp –m “Helloworld raspberrypi mqtt”
OUTPUT: Observe that the text published in TERMINAL 1 “Helloworld raspberrypi mqtt “will be
displayed in TERMINAL 2
Step4: Install Android Application to test MQTT
A) Setup a connection
1. Go to googleplay and install application MQTT Dashboard.
2. Connect your mobile to same WiFi network to which raspberrypi is connected. If no Wifi is
present, please use a Hotspot.
3. Open MQTT Dashboard Application. Click ‘+’ symbol to add a connection. Fill the textboxes.
4. Intialize a) ClientID(say mqtt_mobile_client)
b) Server IP is your Raspberrypi device IP
c) Port No: 1883
4. Press SAVE Button
B) Setup a subscriber
1. Click the Connection Name and TAB SUBSCRIBE.
2. Click the ‘+’ button at the top right corner. Fill the textboxes.
a) Friendly Name: mqtt_mobile_subscriber
b) Topic: temp
c) Unit : words
Press CREATE button
C) Setup a publisher
1. Click the Connection Name and TAB PUBLISH.
2. Click the ‘+’ button at the top right corner. Fill the textboxes.
a) Friendly Name: mqtt_mobile_publisher
b) Topic: temp
Press CREATE button
D) Testing Android application publish subscribe using MQTT
1. Click the Connection Name and TAB PUBLISH.
2. Type the text “Helloworld raspberrypi mqtt “ in the textbox.
3. Press PUBLISH Button.

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:

Using Paho package, write a python program for publish-subscribe

INPUT: Publisher Say ‘Helloworld!’

OUTPUT: Subscriber says ‘Yes’ on receiving the message from Publisher

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

def on_messagefun(client, userdata, msg):


if msg.payload.decode() == "Hello world!":
print("Yes!")
client.disconnect()
Step 4:
Initialize the function names to be called, when subscriber has a connection is established with
broker. Also initialize, the function names to be called, when a message is received by the
subscriber
client.on_connect = on_connectfun
client.on_message = on_messagefun
Step 5: Wait for publishers
client.loop_forever();

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.

INPUT: MQTT Dashboard mobile application publishing text/ BUTTON Controls

OUTPUT:

1. Rotation of Motor Connected to Raspberrypi subscriber device, when a command is sent by


mobile application.
2. ON and OFF LED connected to Raspberrypi subscriber, when a command is send by mobile
application
3. ON and OFF Buzzer connected to Raspberrypi subscriber, when a command is send by
mobile application.

Step 1: Connect Servo Motor to GPIO PIN 16(Use DC power source).


Connect LED to GPIO PIN 10 (Use resistor).
Connect Buzzer to GPIO PIN 8.

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

You might also like