MQTT with Raspberry Pi

Viewing a single blog post

MQTT with Raspberry Pi

Prerequisite:

1) MQTT Client Library for Raspberry Pi

> sudo pip install paho-mqtt

Broker Host: mqtt.iocare.in

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.
client.subscribe("iocare/iot")

# 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.tls_set()
client.username_pw_set(user,passw)
client.on_connect = on_connect
client.on_message = on_message

client.connect("mqtt.iocare.in", 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.
client.loop_forever()

 

Basic MQTT

Download MQTT GPIO Control code

 

 

 

Posted in Internet, Raspberry Pi on Jun 20, 2018


Comments

Please sign in to comment!