Utiliser le broker MQTT de l'AMIPO

Staging : amipo.mby.ovh

Publication de données

Pour envoyer des données avec mosquitto : mosquitto_pub -h amipo.mby.ovh -t “TOPIC” -m DATA

Avec :

  • TOPIC = amipo/le_nom_de_ma_data
  • DATA = la valeur numérique de ma data

Exemple :

mosquitto_pub -h amipo.mby.ovh -t "amipo/mon_test" -m 42

Visualisation

Pour visualiser les données publiées en live : lien vers le grafana de l'Amipo

Envoyer des données issues d'un port série via mqtt

Un script gore en python :

#!/usr/bin/python
#
#simple app to read string from serial port
#and publish via MQTT
#
#uses the Python MQTT client from the Mosquitto project
#http://mosquitto.org
#
#Andy Piper http://andypiper.co.uk
#2011/09/15
 
import serial
import paho.mqtt.client as mqtt #import the client1
import os
import time
 
serialdev = '/dev/ttyACM0'
broker_address="amipo.mby.ovh"
port = 1883
 
 
#MQTT callbacks
 
def on_connect(rc):
    if rc == 0:
    #rc 0 successful connect
        print "Connected"
    else:
        raise Exception
 
 
def on_publish(val):
    print "Published ", val
 
############
def on_message(client, userdata, message):
    print("message received " ,str(message.payload.decode("utf-8")))
    print("message topic=",message.topic)
    print("message qos=",message.qos)
    print("message retain flag=",message.retain)
########################################
 
 
#called on exit
#close serial, disconnect MQTT
 
def cleanup():
    print "Ending and cleaning up"
    ser.close()
    client.disconnect()
	#mqttc.disconnect()
 
 
try:
    print "Connecting... ", serialdev
    #connect to serial port
    ser = serial.Serial(serialdev, 115200, 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)
	#print("creating new instance")
    client = mqtt.Client(client_uniq) #create new instance
    client.on_message=on_message #attach function to callback
    print("connecting to broker")
    client.connect(broker_address) #connect to broker
    client.loop_start() #start the loop
    print("Subscribing to topic","amipo/radioactiviy1")
    client.subscribe("amipo/radioactiviy1")
    #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("Publishing message to topic","amipo/radioactiviy1", temp)
        client.publish("amipo/radioactiviy1",temp)
        #time.sleep(4) # wait
    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 "Interrupt received"
    cleanup()
except (RuntimeError):
    print "uh-oh! time to die"
    cleanup()
  • amipo_mqtt.txt
  • Dernière modification: 2019/11/12 00:19
  • par Benjamin Labomedia