async def pulse( ): # This demo pulses blue LED each time a subscribed msg arrives. blue_led(True) await asyncio.sleep(0.5) blue_led(False) await asyncio.sleep(0.5) return True
async def main(client): blue_led(True) try: await client.connect() except OSError: print('Connection failed') return while True: data = [] for i in range(8): data.append(None) # read the PMSA003 pm2_5 = p3.read() if pm2_5 != None: print("PMSx003: D={:f}µg/m³ X={:d}".format(pm2_5[0], pm2_5[1]))
async def heartbeat(): s = True while True: await asyncio.sleep_ms(500) blue_led(s) s = not s
async def pulse(): blue_led(True) await asyncio.sleep_ms(100) blue_led(False)
#print("pub:", data) #await client.publish(TOPIC+"/sensors", json.dumps(data), qos = 1) #await asyncio.sleep(1) # Define configuration #config['subs_cb'] = sub_cb config['wifi_coro'] = wifi_cb config['connect_coro'] = conn_cb config['keepalive'] = 120 # power config #machine.freq(80000000, min_freq=10000000) #config['listen_interval']=5 # Set up client. Enable optional debug statements. #MQTTClient.DEBUG = True client = MQTTClient(config) #import uasyncio, logging #logging.basicConfig(level=logging.DEBUG) #uasyncio.set_debug(True) print("Starting loop...") try: loop.run_until_complete(main(client)) finally: # Prevent LmacRxBlk:1 errors. client.close() blue_led(True)
from mqtt_as import MQTTClient, config from config import wifi_led, blue_led import uasyncio as asyncio import ujson from state import latest, history wifi_led(False) blue_led(False) def sub_cb(topic, msg, retained): sensor = topic.decode() print((sensor, msg, retained)) if type(msg) == int or type(msg) == float: data = msg else: data = ujson.loads(msg) if sensor in latest: latest[sensor] = data if sensor in history: history[sensor].append(data) if len(history[sensor]) > 15: del history[sensor][0] # ###################################################################### #
def exit_to_repl(pin): client.close() blue_led(True) sys.exit('Exiting to REPL')