amipo_mqtt
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
amipo_mqtt [2019/11/04 17:12] – [Visualisation] bigMax | amipo_mqtt [2020/07/25 09:13] (Version actuelle) – bigMax | ||
---|---|---|---|
Ligne 17: | Ligne 17: | ||
===== Visualisation ===== | ===== Visualisation ===== | ||
Pour visualiser les données publiées en live : [[ https:// | Pour visualiser les données publiées en live : [[ https:// | ||
+ | |||
+ | ===== Envoyer des données issues d'un port série via mqtt ===== | ||
+ | Un script gore en python : | ||
+ | <code lang=" | ||
+ | # | ||
+ | # | ||
+ | #simple app to read string from serial port | ||
+ | #and publish via MQTT | ||
+ | # | ||
+ | #uses the Python MQTT client from the Mosquitto project | ||
+ | # | ||
+ | # | ||
+ | #Andy Piper http:// | ||
+ | #2011/09/15 | ||
+ | |||
+ | import serial | ||
+ | import paho.mqtt.client as mqtt #import the client1 | ||
+ | import os | ||
+ | import time | ||
+ | |||
+ | serialdev = '/ | ||
+ | broker_address=" | ||
+ | port = 1883 | ||
+ | |||
+ | |||
+ | #MQTT callbacks | ||
+ | |||
+ | def on_connect(rc): | ||
+ | if rc == 0: | ||
+ | #rc 0 successful connect | ||
+ | print " | ||
+ | else: | ||
+ | raise Exception | ||
+ | |||
+ | |||
+ | def on_publish(val): | ||
+ | print " | ||
+ | |||
+ | ############ | ||
+ | def on_message(client, | ||
+ | print(" | ||
+ | print(" | ||
+ | print(" | ||
+ | print(" | ||
+ | ######################################## | ||
+ | |||
+ | |||
+ | #called on exit | ||
+ | #close serial, disconnect MQTT | ||
+ | |||
+ | def cleanup(): | ||
+ | print " | ||
+ | ser.close() | ||
+ | client.disconnect() | ||
+ | # | ||
+ | |||
+ | |||
+ | try: | ||
+ | print " | ||
+ | #connect to serial port | ||
+ | ser = serial.Serial(serialdev, | ||
+ | except: | ||
+ | print " | ||
+ | #unable to continue with no serial input | ||
+ | raise SystemExit | ||
+ | |||
+ | |||
+ | try: | ||
+ | # | ||
+ | #create an mqtt client | ||
+ | mypid = os.getpid() | ||
+ | client_uniq = " | ||
+ | # | ||
+ | client = mqtt.Client(client_uniq) #create new instance | ||
+ | client.on_message=on_message #attach function to callback | ||
+ | print(" | ||
+ | client.connect(broker_address) #connect to broker | ||
+ | client.loop_start() #start the loop | ||
+ | print(" | ||
+ | client.subscribe(" | ||
+ | #remain connected to broker | ||
+ | while True: | ||
+ | #read data from serial and publish | ||
+ | line = ser.readline() | ||
+ | print(line) | ||
+ | #split line as it contains V,temp | ||
+ | list = line.split(" | ||
+ | #second list element is temp | ||
+ | temp = list[1].rstrip() | ||
+ | print(" | ||
+ | client.publish(" | ||
+ | # | ||
+ | client.loop_stop() #stop the loop | ||
+ | |||
+ | |||
+ | # 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 " | ||
+ | cleanup() | ||
+ | except (RuntimeError): | ||
+ | print " | ||
+ | cleanup() | ||
+ | </ | ||
+ | {{tag> |
amipo_mqtt.1572887522.txt.gz · Dernière modification : 2019/11/04 17:12 de bigMax