Skip to content

Instantly share code, notes, and snippets.

@yeffrimic
Created November 16, 2016 23:13
Show Gist options
  • Save yeffrimic/9257d1a55d27e6cc59eb458ea31c9998 to your computer and use it in GitHub Desktop.
Save yeffrimic/9257d1a55d27e6cc59eb458ea31c9998 to your computer and use it in GitHub Desktop.
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment