Guest User

Untitled

a guest
Sep 2nd, 2019
8
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from machine import Pin, ADC
  2. from time import sleep
  3.  
  4. ldr = ADC(0)
  5.  
  6. def sub_cb(topic, msg):
  7.   print((topic, msg))
  8.   if topic == b'notification' and msg == b'received':
  9.     print('ESP received hello message')
  10.  
  11. def connect_and_subscribe():
  12.   global client_id, mqtt_server, topic_sub
  13.   client = MQTTClient(client_id, mqtt_server)
  14.   client.set_callback(sub_cb)
  15.   client.connect()
  16.   client.subscribe(topic_sub)
  17.   print('Connected to %s MQTT broker, subscribed to %s topic' % (mqtt_server, topic_sub))
  18.   return client
  19.  
  20.  
  21. def restart_and_reconnect():
  22.   print('Failed to connect to MQTT broker. Reconnecting...')
  23.   time.sleep(10)
  24.   machine.reset()
  25.  
  26. try:
  27.   client = connect_and_subscribe()
  28. except OSError as e:
  29.   restart_and_reconnect()
  30.  
  31. while True:
  32.   try:
  33.     ldrValue = ldr.read()
  34.     print(ldrValue)
  35.     sleep(0.1)
  36.     if ldrValue >= 350:
  37.       msg = b'laser0 #%d' % counter
  38.       client.publish(topic_pub, msg)
  39.       last_message = time.time()
  40.       counter += 1
  41.   except OSError as e:
  42.     restart_and_reconnect()
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×