Difference between revisions of "Mosquitto"
Jump to navigation
Jump to search
Line 14: | Line 14: | ||
sudo python get-pip.py |
sudo python get-pip.py |
||
+ | </pre> |
||
+ | ====sur MacOS X==== |
||
+ | <pre> |
||
+ | brew install mosquitto |
||
+ | </pre> |
||
+ | ====sur Ubuntu==== |
||
+ | <pre> |
||
+ | brew install mosquitto |
||
</pre> |
</pre> |
||
− | === |
+ | ===Lancement du broker=== |
+ | Lancez le broker Mosquitto sur l'hôte (10.0.1.3) |
||
<pre> |
<pre> |
||
− | + | mosquitto |
|
+ | </pre> |
||
+ | |||
+ | ===Command lines=== |
||
+ | Souscription depuis l'hôte http://mosquitto.org/man/mosquitto_sub-1.html |
||
+ | <pre> |
||
+ | BROKER=10.0.1.3 |
||
+ | mosquitto_sub -h $BROKER -d -t arduino/temp |
||
+ | </pre> |
||
+ | |||
+ | Publication depuis le Raspberry Pi http://mosquitto.org/man/mosquitto_pub-1.html |
||
+ | <pre> |
||
+ | BROKER=10.0.1.3 |
||
+ | mosquitto_pub -h $BROKER -d -t arduino/temp -m "100" |
||
+ | </pre> |
||
+ | |||
+ | |||
+ | ===Python=== |
||
+ | Publication depuis le Raspberry Pi http://mosquitto.org/documentation/python/ |
||
+ | <pre> |
||
+ | BROKER=10.0.1.3 |
||
+ | python publisharduino.py $BROKER |
||
+ | </pre> |
||
+ | |||
+ | <pre> |
||
+ | #!/usr/bin/python |
||
+ | # |
||
+ | # simple app to read string from serial port (arduino board) |
||
+ | # and publish via MQTT |
||
+ | # |
||
+ | # uses the Python MQTT client from the Mosquitto project |
||
+ | # http://mosquitto.org |
||
+ | # |
||
+ | # initially Andy Piper http://andypiper.co.uk |
||
+ | # 2011/09/15 |
||
+ | |||
+ | import serial |
||
+ | import mosquitto |
||
+ | import os |
||
+ | import sys |
||
+ | |||
+ | broker=sys.argv[1] |
||
+ | #broker = "10.0.1.3" |
||
+ | port = 1883 |
||
+ | |||
+ | serialdev = '/dev/ttyACM0' |
||
+ | |||
+ | |||
+ | |||
+ | #MQTT callbacks |
||
+ | |||
+ | def on_connect(mosq, obj, rc): |
||
+ | if rc == 0: |
||
+ | print("Connected successfully.") |
||
+ | else: |
||
+ | raise Exception |
||
+ | |||
+ | |||
+ | def on_disconnect(mosq, obj, rc): |
||
+ | print("Disconnected successfully.") |
||
+ | |||
+ | |||
+ | def on_publish(mosq, obj, mid): |
||
+ | print("Message "+str(mid)+" published.") |
||
+ | |||
+ | |||
+ | def on_subscribe(mosq, obj, mid, qos_list): |
||
+ | print("Subscribe with mid "+str(mid)+" received.") |
||
+ | |||
+ | |||
+ | def on_unsubscribe(mosq, obj, mid): |
||
+ | print("Unsubscribe with mid "+str(mid)+" received.") |
||
+ | |||
+ | |||
+ | def on_message(mosq, obj, msg): |
||
+ | print("Message received on topic "+msg.topic+" with QoS "+str(msg.qos)+" and payload "+msg.payload) |
||
+ | |||
+ | |||
+ | #called on exit |
||
+ | #close serial, disconnect MQTT |
||
+ | def cleanup(): |
||
+ | print "Ending and cleaning up" |
||
+ | ser.close() |
||
+ | mqttc.disconnect() |
||
+ | |||
+ | try: |
||
+ | print "Connecting... ", serialdev |
||
+ | #connect to serial port |
||
+ | ser = serial.Serial(serialdev, 9600, timeout=20) |
||
+ | except: |
||
+ | print "Failed to connect serial" |
||
+ | #unable to continue with no serial input |
||
+ | raise SystemExit |
||
+ | |||
+ | |||
+ | try: |
||
+ | ser.flushInput() |
||
+ | #create an mqtt client |
||
+ | mypid = os.getpid() |
||
+ | client_uniq = "arduino_pub_"+str(mypid) |
||
+ | mqttc = mosquitto.Mosquitto(client_uniq) |
||
+ | |||
+ | #attach MQTT callbacks |
||
+ | mqttc.on_connect = on_connect |
||
+ | mqttc.on_disconnect = on_disconnect |
||
+ | mqttc.on_publish = on_publish |
||
+ | mqttc.on_subscribe = on_subscribe |
||
+ | mqttc.on_unsubscribe = on_unsubscribe |
||
+ | #mqttc.on_message = on_message |
||
+ | |||
+ | #connect to broker |
||
+ | mqttc.connect(broker, port, 60) |
||
+ | |||
+ | #remain connected to broker |
||
+ | #read data from serial and publish |
||
+ | while mqttc.loop() == 0: |
||
+ | line = ser.readline() |
||
+ | #split line as it contains V,temp |
||
+ | list = line.split(",") |
||
+ | #second list element is temp |
||
+ | temp = list[0].rstrip() |
||
+ | print("Temp is "+temp) |
||
+ | mqttc.publish("arduino/temp", temp) |
||
+ | pass |
||
+ | |||
+ | |||
+ | # handle list index error (i.e. assume no data received) |
||
+ | except (IndexError): |
||
+ | print "No data received within serial timeout period" |
||
+ | cleanup() |
||
+ | # handle app closure |
||
+ | except (KeyboardInterrupt): |
||
+ | print "Interrupt received" |
||
+ | cleanup() |
||
+ | except (RuntimeError): |
||
+ | print "uh-oh! time to die" |
||
+ | cleanup() |
||
+ | |||
+ | |||
+ | </pre> |
||
+ | |||
+ | |||
+ | Souscription depuis l'hôte http://mosquitto.org/documentation/python/ |
||
+ | <pre> |
||
+ | BROKER=10.0.1.3 |
||
+ | python subscribearduino.py $BROKER |
||
+ | </pre> |
||
+ | |||
+ | <pre> |
||
+ | #!/usr/bin/python |
||
+ | # |
||
+ | # simple app to subscribe MQTT topic |
||
+ | # |
||
+ | # uses the Python MQTT client from the Mosquitto project |
||
+ | # http://mosquitto.org |
||
+ | # |
||
+ | # initially Andy Piper http://andypiper.co.uk |
||
+ | # 2011/09/15 |
||
+ | |||
+ | import os |
||
+ | import mosquitto |
||
+ | import sys |
||
+ | |||
+ | broker=sys.argv[1] |
||
+ | #broker = "10.0.1.3" |
||
+ | port = 1883 |
||
+ | |||
+ | serialdev = '/dev/ttyACM0' |
||
+ | |||
+ | #MQTT callbacks |
||
+ | |||
+ | def on_connect(mosq, obj, rc): |
||
+ | if rc == 0: |
||
+ | print("Connected successfully.") |
||
+ | else: |
||
+ | raise Exception |
||
+ | |||
+ | def on_disconnect(mosq, obj, rc): |
||
+ | print("Disconnected successfully.") |
||
+ | |||
+ | |||
+ | def on_publish(mosq, obj, mid): |
||
+ | print("Message "+str(mid)+" published.") |
||
+ | |||
+ | |||
+ | def on_subscribe(mosq, obj, mid, qos_list): |
||
+ | print("Subscribe with mid "+str(mid)+" received.") |
||
+ | |||
+ | |||
+ | def on_unsubscribe(mosq, obj, mid): |
||
+ | print("Unsubscribe with mid "+str(mid)+" received.") |
||
+ | |||
+ | |||
+ | def on_message(mosq, obj, msg): |
||
+ | print("Message received on topic "+msg.topic+" with QoS "+str(msg.qos)+" and payload "+msg.payload) |
||
+ | |||
+ | |||
+ | #called on exit |
||
+ | # disconnect MQTT |
||
+ | def cleanup(): |
||
+ | print "Ending and cleaning up" |
||
+ | mqttc.disconnect() |
||
+ | |||
+ | try: |
||
+ | #create an mqtt client |
||
+ | mypid = os.getpid() |
||
+ | client_uniq = "arduino_pub_"+str(mypid) |
||
+ | mqttc = mosquitto.Mosquitto(client_uniq) |
||
+ | |||
+ | #attach MQTT callbacks |
||
+ | mqttc.on_connect = on_connect |
||
+ | mqttc.on_disconnect = on_disconnect |
||
+ | mqttc.on_publish = on_publish |
||
+ | mqttc.on_subscribe = on_subscribe |
||
+ | mqttc.on_unsubscribe = on_unsubscribe |
||
+ | mqttc.on_message = on_message |
||
+ | |||
+ | #connect to broker |
||
+ | mqttc.connect(broker, port, 60) |
||
+ | |||
+ | mqttc.subscribe("arduino/temp") |
||
+ | |||
+ | #remain connected to broker |
||
+ | while mqttc.loop() == 0: |
||
+ | pass |
||
+ | |||
+ | |||
+ | # handle app closure |
||
+ | except (KeyboardInterrupt): |
||
+ | print "Interrupt received" |
||
+ | cleanup() |
||
+ | except (RuntimeError): |
||
+ | print "uh-oh! time to die" |
||
+ | cleanup() |
||
+ | |||
</pre> |
</pre> |
Revision as of 11:28, 17 August 2013
Serveur MQTT écrit en C et C++
Installation rapide et simple
Premiers pas
Installation
sur Raspberry Pi
wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py -O - | python sudo python get-pip.py
sur MacOS X
brew install mosquitto
sur Ubuntu
brew install mosquitto
Lancement du broker
Lancez le broker Mosquitto sur l'hôte (10.0.1.3)
mosquitto
Command lines
Souscription depuis l'hôte http://mosquitto.org/man/mosquitto_sub-1.html
BROKER=10.0.1.3 mosquitto_sub -h $BROKER -d -t arduino/temp
Publication depuis le Raspberry Pi http://mosquitto.org/man/mosquitto_pub-1.html
BROKER=10.0.1.3 mosquitto_pub -h $BROKER -d -t arduino/temp -m "100"
Python
Publication depuis le Raspberry Pi http://mosquitto.org/documentation/python/
BROKER=10.0.1.3 python publisharduino.py $BROKER
#!/usr/bin/python # # simple app to read string from serial port (arduino board) # and publish via MQTT # # uses the Python MQTT client from the Mosquitto project # http://mosquitto.org # # initially Andy Piper http://andypiper.co.uk # 2011/09/15 import serial import mosquitto import os import sys broker=sys.argv[1] #broker = "10.0.1.3" port = 1883 serialdev = '/dev/ttyACM0' #MQTT callbacks def on_connect(mosq, obj, rc): if rc == 0: print("Connected successfully.") else: raise Exception def on_disconnect(mosq, obj, rc): print("Disconnected successfully.") def on_publish(mosq, obj, mid): print("Message "+str(mid)+" published.") def on_subscribe(mosq, obj, mid, qos_list): print("Subscribe with mid "+str(mid)+" received.") def on_unsubscribe(mosq, obj, mid): print("Unsubscribe with mid "+str(mid)+" received.") def on_message(mosq, obj, msg): print("Message received on topic "+msg.topic+" with QoS "+str(msg.qos)+" and payload "+msg.payload) #called on exit #close serial, disconnect MQTT def cleanup(): print "Ending and cleaning up" ser.close() mqttc.disconnect() try: print "Connecting... ", serialdev #connect to serial port ser = serial.Serial(serialdev, 9600, timeout=20) except: print "Failed to connect serial" #unable to continue with no serial input raise SystemExit try: ser.flushInput() #create an mqtt client mypid = os.getpid() client_uniq = "arduino_pub_"+str(mypid) mqttc = mosquitto.Mosquitto(client_uniq) #attach MQTT callbacks mqttc.on_connect = on_connect mqttc.on_disconnect = on_disconnect mqttc.on_publish = on_publish mqttc.on_subscribe = on_subscribe mqttc.on_unsubscribe = on_unsubscribe #mqttc.on_message = on_message #connect to broker mqttc.connect(broker, port, 60) #remain connected to broker #read data from serial and publish while mqttc.loop() == 0: line = ser.readline() #split line as it contains V,temp list = line.split(",") #second list element is temp temp = list[0].rstrip() print("Temp is "+temp) mqttc.publish("arduino/temp", temp) pass # handle list index error (i.e. assume no data received) except (IndexError): print "No data received within serial timeout period" cleanup() # handle app closure except (KeyboardInterrupt): print "Interrupt received" cleanup() except (RuntimeError): print "uh-oh! time to die" cleanup()
Souscription depuis l'hôte http://mosquitto.org/documentation/python/
BROKER=10.0.1.3 python subscribearduino.py $BROKER
#!/usr/bin/python # # simple app to subscribe MQTT topic # # uses the Python MQTT client from the Mosquitto project # http://mosquitto.org # # initially Andy Piper http://andypiper.co.uk # 2011/09/15 import os import mosquitto import sys broker=sys.argv[1] #broker = "10.0.1.3" port = 1883 serialdev = '/dev/ttyACM0' #MQTT callbacks def on_connect(mosq, obj, rc): if rc == 0: print("Connected successfully.") else: raise Exception def on_disconnect(mosq, obj, rc): print("Disconnected successfully.") def on_publish(mosq, obj, mid): print("Message "+str(mid)+" published.") def on_subscribe(mosq, obj, mid, qos_list): print("Subscribe with mid "+str(mid)+" received.") def on_unsubscribe(mosq, obj, mid): print("Unsubscribe with mid "+str(mid)+" received.") def on_message(mosq, obj, msg): print("Message received on topic "+msg.topic+" with QoS "+str(msg.qos)+" and payload "+msg.payload) #called on exit # disconnect MQTT def cleanup(): print "Ending and cleaning up" mqttc.disconnect() try: #create an mqtt client mypid = os.getpid() client_uniq = "arduino_pub_"+str(mypid) mqttc = mosquitto.Mosquitto(client_uniq) #attach MQTT callbacks mqttc.on_connect = on_connect mqttc.on_disconnect = on_disconnect mqttc.on_publish = on_publish mqttc.on_subscribe = on_subscribe mqttc.on_unsubscribe = on_unsubscribe mqttc.on_message = on_message #connect to broker mqttc.connect(broker, port, 60) mqttc.subscribe("arduino/temp") #remain connected to broker while mqttc.loop() == 0: pass # handle app closure except (KeyboardInterrupt): print "Interrupt received" cleanup() except (RuntimeError): print "uh-oh! time to die" cleanup()