Advertisement
Astranome

Launcher

Apr 1st, 2021
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.95 KB | None | 0 0
  1. import gc
  2. gc.mem_free()
  3. gc.collect()
  4. gc.mem_free()
  5. import network
  6. sta_if = network.WLAN(network.STA_IF); sta_if.active(True)
  7. ifc=str(sta_if.ifconfig())
  8. isc=str(sta_if.isconnected())
  9.  
  10. print(ifc)
  11. sta_if.isconnected()        
  12. #from  imp  import  reload
  13. from umqtt.robust import MQTTClient
  14. import ubinascii
  15. import machine
  16. import time
  17. egg='on'
  18. # Modify below section as required
  19. CONFIG = {
  20.      # Configuration details of the MQTT broker
  21.      "MQTT_BROKER": "192.168.1.39",
  22.      "USER": "",
  23.      "PASSWORD": "",
  24.      "PORT": 1883,
  25.      "TOPIC": b"test",
  26.      # unique identifier of the chip
  27.      "CLIENT_ID": b"esp32_" + ubinascii.hexlify(machine.unique_id())
  28. }
  29.  
  30. # Method to act based on message received  
  31. def onMessage(topic, msg):
  32.     print("Topic: %s, Message: %s" % (topic, msg))
  33.  
  34.     if msg == b"on":
  35.        
  36.         print('onnnn---------')
  37.         import gcollect
  38.         import ESP32_TpShortBTC
  39.     elif msg == b"onl":
  40.         print('Long')
  41.         import gcollect
  42.         import ESP32_TpLongBTC        
  43.  
  44.     elif msg == b'ona':
  45.         global egg
  46.         egg='stop'
  47. def listen():
  48.     #Create an instance of MQTTClient
  49.     client = MQTTClient(CONFIG['CLIENT_ID'], CONFIG['MQTT_BROKER'], user=CONFIG['USER'], password=CONFIG['PASSWORD'], port=CONFIG['PORT'])
  50.     # Attach call back handler to be called on receiving messages
  51.     client.set_callback(onMessage)
  52.     client.connect()
  53.     client.publish("test", "ESP32 is Connected")
  54.     client.subscribe(CONFIG['TOPIC'])
  55.     print("ESP32 is Connected to %s and subscribed to %s topic" % (CONFIG['MQTT_BROKER'], CONFIG['TOPIC']))
  56.     ham=1
  57.     try:
  58.         while True:
  59.             #msg = client.wait_msg()
  60.             msg = client.check_msg()
  61.             ham = ham+1
  62.             client.publish("alive", str(ham))
  63.             print(ham)
  64.             time.sleep(0.3)
  65.             if egg == 'stop':
  66.                 break
  67.     finally:
  68.         client.disconnect()  
  69.  
  70. listen()        
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement