Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. import paho.mqtt.client as mqtt
  2. import json, time, jwt, getpass, socket, random
  3.  
  4. # mqtt auth stuff
  5. MQTT_CLIENT_HOST = ""
  6. MQTT_CLIENT_PORT = 1883
  7.  
  8. # device config
  9. dev_name = getpass.getuser()
  10. dev_id = socket.gethostname()
  11.  
  12. class MSG_Constructor:
  13.  
  14. def __init__(self):
  15. pass
  16.  
  17. # create a simple json object
  18. def create_object(self):
  19. self.object = {
  20. "device": dev_name,
  21. "device_id": dev_id,
  22. "message": random.choice("ON", "OFF", "IDLING")
  23. }
  24.  
  25. return self.object
  26.  
  27. # publish message
  28. def publish(self, topic, message):
  29. try:
  30. self.topic = topic
  31. self.message = message or self.create_object
  32.  
  33. # Try to publish the message to mqtt broker
  34. client.publish(self.topic, self.message)
  35. return True
  36.  
  37. except KeyError:
  38. print('keyerror occured, request failed...')
  39. return False
  40.  
  41. msg_object = MSG_Constructor()
  42.  
  43. def on_connect(client, userdata, flags, rc):
  44. full_hostname = MQTT_CLIENT_HOST + ":" + str(MQTT_CLIENT_PORT)
  45. print(f"successfully connected to - {full_hostname}, as {dev_name}, code - {str(rc)}")
  46. client.subscribe(topic)
  47.  
  48. def on_message(client, userdata, msg):
  49. try:
  50. mqtt_message = str(msg.payload.decode('utf-8'))
  51.  
  52. # Inside the json message object should be a client id
  53. mqtt_object = json.loads(mqtt_message)
  54. mqtt_client_id = mqtt_object["client_id"]
  55.  
  56. if " " in mqtt_client_id or len(mqtt_client_id) < 3:
  57. print("Received an incorrect message, continuing")
  58.  
  59. print(f"New message from - {mqtt_client_id}, msg - {mqtt_message}.")
  60.  
  61. except KeyError:
  62. print("Received an incorrect message, continuing")
  63. pass
  64.  
  65. except Exception as error:
  66. print(error)
  67.  
  68.  
  69. def on_disconnect(self, client, userdata, rc):
  70. if rc != 0:
  71. full_hostname = MQTT_CLIENT_HOST + ":" + str(MQTT_CLIENT_PORT)
  72. print("Disconnected from -", full_hostname)
  73.  
  74.  
  75. def on_publish(client, userdata, mid):
  76. print("The publish request was successfully sent to mqtt.")
  77.  
  78.  
  79. def on_subscribe(client, userdata, mid, granted_qos):
  80. print("Successfully subscribing to -", topic)
  81.  
  82.  
  83. client = mqtt.Client()
  84. client.on_connect = on_connect
  85. client.on_subscribe = on_subscribe
  86. client.on_message = on_message
  87. client.on_publish = on_publish
  88. client.on_disconnect = on_disconnect
  89. # client authentication
  90. client.username_pw_set("user", "user")
  91. topic = "/dev2" # we connect to the other dev which is on dev2
  92.  
  93. # connect to mqtt broker
  94. client.connect(MQTT_CLIENT_HOST, MQTT_CLIENT_PORT, 600)
  95. client.loop_forever()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement