ghzgod

UMQTT MICROPYTHON

Jun 18th, 2019
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.35 KB | None | 0 0
  1. from umqtt.robust import MQTTClient
  2. import network
  3. import time
  4. import machine
  5. import json
  6.  
  7. wlan = network.WLAN(network.STA_IF)
  8. ID = 'ESP32'
  9. mqtt_broker = '192.168.1.X'
  10. my_user = 'username'
  11. my_pass = 'password'
  12. topic = (b'home/garden/#')
  13. c = MQTTClient(ID, mqtt_broker, user=my_user, password=my_pass)
  14. # c.DEBUG = True
  15.  
  16.  
  17. def subscribe():
  18.     try:
  19.         c.connect(clean_session=False)
  20.         c.set_callback(publish)
  21.         c.subscribe(topic)
  22.         print('Connected to MQTT broker, subscribed to', topic)
  23.     except:
  24.         print('Error, MQTT broker is not responding')
  25.  
  26.  
  27. def check_json(msg):
  28.     try:
  29.         msg = json.loads(msg)
  30.     except:
  31.         print('Message was not JSON')
  32.         return False
  33.     return True
  34.  
  35.  
  36. def check_int(time):
  37.     try:
  38.         time = int(time)
  39.     except:
  40.         print('Time field invalid')
  41.         return False
  42.     return True
  43.  
  44.  
  45. def publish(topic, msg):
  46.     # print((topic, msg))
  47.     if check_json(msg):
  48.         message = {}
  49.         message = json.loads(msg)
  50.         relay = message['relay']
  51.         state = message['state']
  52.         time = int(message['time'])
  53.         if check_int(time):
  54.             print("Last received:", message)
  55.             message = json.dumps(message)
  56.             c.publish(topic, message)
  57.             # response = json.dumps("Relay": relay, "State": state, "Time": time)
  58.         else:
  59.             # response = "json.dumps("Time Invalid": type(time))"
  60.             print("Time entered was invalid.  Type was", type(time))
  61.  
  62.  
  63. # Blocking wait for message (this is for single task code)
  64. # c.wait_msg()
  65. # Non-blocking wait for message (this is for multi tasking projects)
  66. # c.check_msg()
  67.  
  68.  
  69. def wlan_disconnect():
  70.     tick = 1
  71.     while not wlan.isconnected():
  72.         print('WLAN is reconnecting...', tick, '/5')
  73.         time.sleep(5)
  74.         tick += 1
  75.         if tick == 5:
  76.             print("Restarting...")
  77.             machine.reset()
  78.  
  79.  
  80. def main():
  81.     if wlan.isconnected():  # Check if WiFi is connected
  82.         if not c.connect():  # Check if MQTT broker is connected
  83.             subscribe()
  84.             print("Waiting for messages")
  85.     while True:
  86.         if wlan.isconnected():
  87.             c.wait_msg()
  88.         else:  # If wifi is not connected...run a tick routine to reboot ESP32, then resubscribe
  89.             wlan_disconnect()
  90.             subscribe()
Advertisement
Add Comment
Please, Sign In to add comment