MQTT with Raspberry Pi

MQTT with Raspberry Pi


1) MQTT Client Library for Raspberry Pi

> sudo pip install paho-mqtt

Broker Host:

TCP Ports Used

1883 MQTT Port
8883 MQTT/SSL Port
8083 MQTT/WebSocket Port
8084 MQTT/WebSocket/SSL Port
8080 HTTP Management API Port


# -*- coding: utf-8 -*-
#Paho Mqtt python client by ioCare
#Author : Rajendra Khope

import paho.mqtt.client as mqtt

user = ""
passw =""
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.

# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print "Topic: ", msg.topic+"\nMessage: "+str(msg.payload)

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.connect("", 8083, 60)
client.publish("iocare/iot","Welcome to ioCare MQTT !", qos=0, retain=True)
# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.


Basic MQTT

Download MQTT GPIO Control code


MQTT Client



Posted in Internet, Raspberry Pi 02 Oct 2019