Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import paho.mqtt.client as mqtt
- # El callback para cuando el cliente reciva un Connack de respuesta del servidor.
- def on_connect(client, userdata, flags, rc):
- print("Conectado con el siguiente codigo "+str(rc))
- #Suscribirse dentro de On_connect signidica que si perdemos la conexion
- #y nos reconectamos la suscripcion se renovara.
- client.subscribe(MQTT_topicSub)
- # The callback for when a PUBLISH message is received from the server.
- def on_message(client, userdata, msg):
- print(msg.topic+" "+str(msg.payload))
- MQTT_broker = "m13.cloudmqtt.com"
- MQTT_username = "fatvtybu"
- MQTT_password = "KqZz_bJ2xfdL"
- MQTT_topicSub = "jsonTopic" #<---topico al que te vas a suscribir
- MQTT_topicAlive = "Conection"#topico al que le vas a decir que ya estas conectad
- MQTT_port = 12754
- MQTT_clientID = "carwash pepe"#
- client = mqtt.Client(client_id="", clean_session=True, userdata=None, protocol="MQTTv311", transport="tcp")
- client.on_connect = on_connect
- client.on_message = on_message
- client.username_pw_set(MQTT_username,MQTT_password )
- client.connect(MQTT_broker, MQTT_port, 60)
- client.publish(MQTT_topicAlive,MQTT_clientID, qos=0, retain=False)
- # 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()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement