Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import gc
- gc.mem_free()
- gc.collect()
- gc.mem_free()
- import network
- sta_if = network.WLAN(network.STA_IF); sta_if.active(True)
- ifc=str(sta_if.ifconfig())
- isc=str(sta_if.isconnected())
- print(ifc)
- sta_if.isconnected()
- #from imp import reload
- from umqtt.robust import MQTTClient
- import ubinascii
- import machine
- import time
- egg='on'
- # Modify below section as required
- CONFIG = {
- # Configuration details of the MQTT broker
- "MQTT_BROKER": "192.168.1.39",
- "USER": "",
- "PASSWORD": "",
- "PORT": 1883,
- "TOPIC": b"test",
- # unique identifier of the chip
- "CLIENT_ID": b"esp32_" + ubinascii.hexlify(machine.unique_id())
- }
- # Method to act based on message received
- def onMessage(topic, msg):
- print("Topic: %s, Message: %s" % (topic, msg))
- if msg == b"on":
- print('onnnn---------')
- import gcollect
- import ESP32_TpShortBTC
- elif msg == b"onl":
- print('Long')
- import gcollect
- import ESP32_TpLongBTC
- elif msg == b'ona':
- global egg
- egg='stop'
- def listen():
- #Create an instance of MQTTClient
- client = MQTTClient(CONFIG['CLIENT_ID'], CONFIG['MQTT_BROKER'], user=CONFIG['USER'], password=CONFIG['PASSWORD'], port=CONFIG['PORT'])
- # Attach call back handler to be called on receiving messages
- client.set_callback(onMessage)
- client.connect()
- client.publish("test", "ESP32 is Connected")
- client.subscribe(CONFIG['TOPIC'])
- print("ESP32 is Connected to %s and subscribed to %s topic" % (CONFIG['MQTT_BROKER'], CONFIG['TOPIC']))
- ham=1
- try:
- while True:
- #msg = client.wait_msg()
- msg = client.check_msg()
- ham = ham+1
- client.publish("alive", str(ham))
- print(ham)
- time.sleep(0.3)
- if egg == 'stop':
- break
- finally:
- client.disconnect()
- listen()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement