Advertisement
Guest User

Untitled

a guest
Nov 16th, 2016
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. import paho.mqtt.client as mqtt
  2.  
  3. # El callback para cuando el cliente reciva un Connack de respuesta del servidor.
  4. def on_connect(client, userdata, flags, rc):
  5. print("Conectado con el siguiente codigo "+str(rc))
  6. #Suscribirse dentro de On_connect signidica que si perdemos la conexion
  7. #y nos reconectamos la suscripcion se renovara.
  8. client.subscribe(MQTT_topicSub)
  9.  
  10. # The callback for when a PUBLISH message is received from the server.
  11. def on_message(client, userdata, msg):
  12. print(msg.topic+" "+str(msg.payload))
  13.  
  14.  
  15. MQTT_broker = "m13.cloudmqtt.com"
  16. MQTT_username = "fatvtybu"
  17. MQTT_password = "KqZz_bJ2xfdL"
  18. MQTT_topicSub = "jsonTopic" #<---topico al que te vas a suscribir
  19. MQTT_topicAlive = "Conection"#topico al que le vas a decir que ya estas conectad
  20. MQTT_port = 12754
  21. MQTT_clientID = "carwash pepe"#
  22. client = mqtt.Client(client_id="", clean_session=True, userdata=None, protocol="MQTTv311", transport="tcp")
  23. client.on_connect = on_connect
  24. client.on_message = on_message
  25. client.username_pw_set(MQTT_username,MQTT_password )
  26. client.connect(MQTT_broker, MQTT_port, 60)
  27. client.publish(MQTT_topicAlive,MQTT_clientID, qos=0, retain=False)
  28. # Blocking call that processes network traffic, dispatches callbacks and
  29. # handles reconnecting.
  30. # Other loop*() functions are available that give a threaded interface and a
  31. # manual interface.
  32. client.loop_forever()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement