Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1.  
  2. def on_message(client, userdata, msg):
  3. try:
  4. mqtt_message = str(msg.payload.decode('utf-8'))
  5.  
  6. # Inside the json message object should be a client id
  7. mqtt_object = json.loads(mqtt_message)
  8. mqtt_client_id = mqtt_object["client_id"]
  9.  
  10. if " " in mqtt_client_id or len(mqtt_client_id) < 3:
  11. print("Received an incorrect message, continuing")
  12.  
  13. print(f"New message from - {mqtt_client_id}, msg - {mqtt_message}.")
  14.  
  15. except KeyError:
  16. print("Received an incorrect message, continuing")
  17. pass
  18.  
  19. except Exception as error:
  20. print(error)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement