Mosquitto (mosquitto.org) is an open source message broker that implements the MQ Telemetry Transport protocol. MQTT (mqtt.org) privides a lightweight method of carrying out messaging using a publish/subscribe model. More about the MQTT protocol you can read at http://mosquitto.org/man/mqtt-7.html
After installing mosquitto 1.0.5 on my rasberry pi I wrote the following two python scripts to experiment myself.
The code is shown below and it is self explanatory:
mosqsrv.py
#!/usr/bin/python
import mosquitto
#define what happens after connection
def on_connect (mosq, obj, rc):
if rc==0:
print "connected"
#On receipt of a message create a message
def on_message(mosq, obj, msg):
print msg.topic+', '+msg.payload
#create a roker
mqttc = mosquitto.Mosquitto("python_sub")
#define the callbacks
mqttc.on_connect = on_connect
mqttc.on_message = on_message
#connect
mqttc.connect("localhost", 1883, 60)
#subscribe to topic test
mqttc.subscribe("test", 0)
#keep connected to broker
while mqttc.loop()==0:
pass
mosqcl.py
#!/usr/bin/python
import mosquitto
#define what happens after connection
def on_connect (mosq, obj, rc):
print("connected")
#create a broker
mqttc = mosquitto.Mosquitto("python_test")
#connect
mqttc.connect("192.168.1.105",1883, 60)
#publish to broker
mqttc.publish("test", "hello world!", 1)
mqttc.disconnect()
After installing mosquitto 1.0.5 on my rasberry pi I wrote the following two python scripts to experiment myself.
The code is shown below and it is self explanatory:
mosqsrv.py
#!/usr/bin/python
import mosquitto
#define what happens after connection
def on_connect (mosq, obj, rc):
if rc==0:
print "connected"
#On receipt of a message create a message
def on_message(mosq, obj, msg):
print msg.topic+', '+msg.payload
#create a roker
mqttc = mosquitto.Mosquitto("python_sub")
#define the callbacks
mqttc.on_connect = on_connect
mqttc.on_message = on_message
#connect
mqttc.connect("localhost", 1883, 60)
mqttc.subscribe("test", 0)
#keep connected to broker
while mqttc.loop()==0:
pass
mosqcl.py
#!/usr/bin/python
import mosquitto
#define what happens after connection
def on_connect (mosq, obj, rc):
print("connected")
#create a broker
mqttc = mosquitto.Mosquitto("python_test")
#connect
mqttc.connect("192.168.1.105",1883, 60)
#publish to broker
mqttc.publish("test", "hello world!", 1)
mqttc.disconnect()
To test it I run mosquitto broker from the command line after editing the mosquitto.conf file to define the run user.
Δεν υπάρχουν σχόλια:
Δημοσίευση σχολίου