Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from machine import Pin, ADC
- from time import sleep
- ldr = ADC(0)
- def sub_cb(topic, msg):
- print((topic, msg))
- if topic == b'notification' and msg == b'received':
- print('ESP received hello message')
- def connect_and_subscribe():
- global client_id, mqtt_server, topic_sub
- client = MQTTClient(client_id, mqtt_server)
- client.set_callback(sub_cb)
- client.connect()
- client.subscribe(topic_sub)
- print('Connected to %s MQTT broker, subscribed to %s topic' % (mqtt_server, topic_sub))
- return client
- def restart_and_reconnect():
- print('Failed to connect to MQTT broker. Reconnecting...')
- time.sleep(10)
- machine.reset()
- try:
- client = connect_and_subscribe()
- except OSError as e:
- restart_and_reconnect()
- while True:
- try:
- ldrValue = ldr.read()
- print(ldrValue)
- sleep(0.1)
- if ldrValue >= 350:
- msg = b'laser0 #%d' % counter
- client.publish(topic_pub, msg)
- last_message = time.time()
- counter += 1
- except OSError as e:
- restart_and_reconnect()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement