Advertisement
AntonioVillanueva

Test Client MQTT with mosquitto broker

Aug 9th, 2022
763
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.21 KB | None | 0 0
  1. """ Antonio Villanueva Segura Test Mqtt with MOSQUITTO BROKER """
  2. import paho.mqtt.client as mqtt
  3. import time
  4.  
  5. TOPIC = "/home/rele/#" #Topic to suscribe ...on moquitto
  6.  
  7. address ="192.168.6.99" # Broker address UP with mosquitto MQTT server
  8. port =1883 # port
  9.  
  10. def on_message(client, userdata, message):
  11.     """ Called when a message has been received on a topic
  12.     that the client subscribes to and the message does not match
  13.     an existing topic filter callback.
  14.     Use message_callback_add() to define a callback that will be called
  15.     for specific topic filters. on_message will serve as fallback when none matched."""
  16.    
  17.     #print(message.topic+" "+str(message.payload))
  18.    
  19.     print("message received " ,str(message.payload.decode("utf-8")))
  20.     print("message topic=",message.topic)
  21.     print("message qos=",message.qos)
  22.     print("message retain flag=",message.retain,end="\n \n")
  23.  
  24. def on_connect(client,userdata,flags,rc):
  25.     """ When the client receives a CONNACK message from the broker
  26.     in response to the connect it generates an on_connect() callback. """
  27.  
  28.     print ("Connected to ",client._host,"port :",client._port)
  29.     print ("Flags ",flags , "Returned code ", str (rc))
  30.  
  31.     # Subscribing in on_connect() means that if we lose the connection and
  32.     # reconnect then subscriptions will be renewed.
  33.     client.subscribe(TOPIC,qos=0)
  34.  
  35. def on_subscribe(client, userdata, mid, granted_qos):
  36.     """ Called when the broker responds to a subscribe request. """
  37.     print ("on_subscribe userdata ",userdata)
  38.     print ("on_subscribe mid ",mid)
  39.     print ("on_subscribe granted_qos ",userdata)   
  40.  
  41. def on_disconnect(client, userdata, rc):
  42.     """ Called when the client disconnects from the broker. """
  43.     print ( "on_disconnect userdata",userdata)
  44.     print ( "on_disconnect rc",rc) 
  45. """ Client """ 
  46.  
  47. def on_publish(client, userdata, mid):
  48.     """
  49.     Called when a message that was to be sent using the publish()
  50.     call has completed transmission to the broker.
  51.     For messages with QoS levels 1 and 2,
  52.     this means that the appropriate handshakes have completed.
  53.     For QoS 0, this simply means that the message has left the client.
  54.     The mid variable matches the mid variable returned
  55.     from the corresponding publish() call, to allow outgoing messages
  56.     to be tracked.
  57.  
  58.     This callback is important because even if the publish()
  59.     call returns success,
  60.     it does not always mean that the message has been sent.
  61.     """
  62.     print ("on_publish ",userdata," , mid ",mid)
  63.  
  64. client =mqtt.Client ("Icarvs",
  65.                         clean_session=True,
  66.                         userdata=None,
  67.                         protocol=mqtt.MQTTv311,
  68.                         transport="tcp")
  69.  
  70. """ calls backs"""
  71. client.on_message=on_message #Attach function to callback
  72.  
  73. client.on_connect=on_connect #Attach function to callback
  74.  
  75. client.on_subscribe=on_subscribe  #Attach function to callback
  76.  
  77. client.on_publish=on_publish #Attach function to callback
  78.  
  79. client.on_disconnect=on_disconnect #Attach function to callback
  80.  
  81. """ login & pwd """
  82. #client.username_pw_set(None, password=None) #Login & Pwd
  83. client.username_pw_set("tony", password="icaro") #Login & Pwd
  84.  
  85. """ Connect """
  86. client.connect (address,port,keepalive=60) #Connet to broker (host, port,keepalive, bind_address="")
  87.  
  88. #time.sleep (5)
  89. #client.loop_start() #start the loop
  90.  
  91. client.loop_forever()
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement