async def info_sender(mqclient, topic, interval): global _upticks, _lastticks log.info(topic) while True: try: gc.collect() f = gc.mem_free() mf = gc.mem_maxfree() t = time.ticks_ms() if _upticks is None: _upticks = t # we hope it hasn't rolled-over yet... else: _upticks += time.ticks_diff(t, _lastticks) _lastticks = t # compose json message with data msg = '{"up":%d,"free":%d,"cont_free":%d,"mqtt_conn":%d}' % ( _upticks // 1000, f, mf, _mqttconn, ) log.info(msg) await mqclient.publish(topic, msg, qos=0) # micropython.mem_info() except Exception as e: log.exc(e, "Exception") await asyncio.sleep(interval)
async def info_sender(mqclient, topic, interval): global _upticks, _lastticks log.info(topic) wlan_sta = network.WLAN(network.STA_IF) while True: try: gc.collect() f = gc.mem_free() mf = gc.mem_maxfree() t = time.ticks_ms() if _upticks is None: _upticks = t # we hope it hasn't rolled-over yet... else: _upticks += time.ticks_diff(t, _lastticks) _lastticks = t bv = get_battery_voltage() * 1000 try: rssi = wlan_sta.status("rssi") except ValueError: rssi = "null" # compose json message with data msg = '{"up":%d,"free":%d,"cont_free":%d,"mqtt_conn":%d,"rssi":%s,"batt":%d}' % ( _upticks // 1000, f, mf, _mqttconn, rssi, bv, ) log.info(msg) await mqclient.publish(topic, msg, qos=0) # micropython.mem_info() except Exception as e: log.exc(e, "Exception") await asyncio.sleep(interval)
async def info_sender(mqclient, topic, interval): global _upticks, _lastticks log.info(topic) wlan_sta = network.WLAN(network.STA_IF) while True: try: gc.collect() f = gc.mem_free() mf = gc.mem_maxfree() t = time.ticks_ms() if _upticks is None: _upticks = t # we hope it hasn't rolled-over yet... else: _upticks += time.ticks_diff(t, _lastticks) _lastticks = t bv = get_battery_voltage() * 1000 try: rssi = wlan_sta.status("rssi") except ValueError: rssi = "null" idf_f = 0 # esp-idf free bytes idf_mf = 0 # esp-idf max contig free block if idf_heap_info: for mi in idf_heap_info(HEAP_DATA): idf_f += mi[1] # sum free bytes if mi[2] > idf_mf: idf_mf = mi[2] # max of contig free # compose json message with data msg = ( '{"up":%d,"free":%d,"cont_free":%d,"mqtt_conn":%d,"rssi":%s,"batt":%d,' '"c_free":%d,"c_cont_free":%d}' % (_upticks // 1000, f, mf, _mqttconn, rssi, bv, idf_f, idf_mf)) log.info(msg) await mqclient.publish(topic, msg, qos=0) # micropython.mem_info() except Exception as e: log.exc(e, "Exception") await asyncio.sleep(interval)
async def query_sensors(client, topic, interval): global bme680, si7021, sht31, pmsx003, anemo, vane, rain global cwop, display global pm_cnt, pm_sum global mode t0 = time.ticks_ms() while True: data = {} gas, pm25 = None, None # convert the bme680 if bme680: await asyncio.sleep_ms(bme680.convert()) while not bme680.ready(): await asyncio.sleep_ms(10) (t, h, p, gas) = bme680.read_data() tF = t * 1.8 + 32 log.info("BME680 : T=%.1f°F H=%.0f%% P=%.3fmBar G=%.3fkΩ", tF, h, p, gas / 1000) (data["t_bme680"], data["h_bme680"]) = (t, h) (data["p_bme680"], data["g_bme680"]) = (p / 1000, gas) # read the si7021 if si7021: await asyncio.sleep_ms(si7021.convert() + 2) (t, h) = si7021.read_temp_humi() log.info("Si7021 : T=%.1f°F H=%.0f%%", t * 1.8 + 32, h) (data["t_si7021"], data["h_si7021"]) = (t, h) # read sht31 if sht31: await asyncio.sleep_ms(sht31.convert() + 2) (t, h) = sht31.read_temp_humi() log.info("SHT31 : T=%.1f°F H=%.0f%%", t * 1.8 + 32, h) (data["t_sht31"], data["h_sht31"]) = (t, h) # read wind if anemo: (w, g) = anemo.read() logstr = "Wind : %.0fmph gust:%.0fmph" logvars = [w, g] (data["wind"], data["gust"]) = (w*0.44704, g*0.44704) # in m/s if vane: d = vane.read() logstr += " dir=%0f°" logvars.append(d) data["wdir"] = d log.info(logstr, *logvars) # read rain gauge # TODO! # insert averaged dust data if pm_cnt > 0: d = [v / pm_cnt for v in pm_sum] pm25 = d[0] data["pm25"] = pm25 log.info("PMSx003: D=%.1fµg/m³ X=%.1f", d[0], d[1]) pm_sum = [0 for _ in pm_sum] pm_cnt = 0 # AQI conversions if gas is not None: data["aqi_tvoc"] = aqi.tvoc_bme680(gas) if pm25 is not None: data["aqi_pm25"] = aqi.pm25(pm25) if display: display.fill(0) if mode == 1: # Test mode for wind vane wdir = data.get("wdir", -1) display.text("Wind dir: %d" % wdir, 0, 0) wdir_str = "%3do" % wdir seg7.draw_number(display, wdir_str, 10, 14, 18, 48, 1, 3) elif mode == 2: # Test mode for wind speed wspd = data.get("wind", -1) * 2.237 display.text("Wind: %.1f mph" % wspd, 0, 0) wspd_str = "%4.1f" % (wspd/2.5) seg7.draw_number(display, wspd_str, 10, 14, 18, 48, 1, 3) # else mode == 3: # regular function is rapid update test mode, "falls thru" into else # else mode == 4: # regular function 1 quick update then switch to mode 0, "falls thru" else: # Regular operating mode, display lots of data and send it too # publish data if mode == 0 and any(d is not None for d in data): log.debug("pub: %s", data) await client.publish(topic, json.dumps(data), qos=1, sync=False) # display data display.text( "BME {:.1f}F {:.0f}%".format(data["t_bme680"] * 1.8 + 32, data["h_bme680"]), 0, 0, ) display.text( " {:.0f}mB {:.0f}kO".format( data["p_bme680"] * 1000, data["g_bme680"] / 1000 ), 0, 9, ) display.text( "SHT {:.1f}F {:.0f}%".format(data["t_sht31"] * 1.8 + 32, data["h_sht31"]), 0, 18, ) display.text( "Si {:.1f}F {:.0f}%".format(data["t_si7021"] * 1.8 + 32, data["h_si7021"]), 0, 27, ) display.text("PM {:.1f} Rn {:.2f}".format(data.get("pm25", -1), 0), 0, 36) display.text( "Wnd {:.0f} {:3d}*".format(data.get("wind", -1), data.get("wdir", -1)), 0, 45 ) display.text("Free {:d} {:d}".format(gc.mem_free(), gc.mem_maxfree()), 0, 54) if mode_led: await mode_blink() display.show() if mode == 0 and cwop: asyncio.get_event_loop().create_task( cwop( temp=data.get("t_bme680", None), hum=data.get("h_bme680", None), baro=data.get("p_bme680", None), winddir=data.get("wdir", None), windspeed=data.get("wind", None), windgust=data.get("gust", None), ) ) # sleep iv = interval while True: t1 = time.ticks_ms() dt = time.ticks_diff(t1, t0) if dt >= iv or mode and dt > mode_period[mode]: break await asyncio.sleep_ms(min(iv - dt, 500)) if dt >= iv and dt < iv * 3 / 2: t0 = time.ticks_add(t0, iv) else: t0 = time.ticks_ms() if mode > mode_max: # hack to get mode 0 to display immediately when switching to it mode = 0
def lm(): log.info("MEM free=%d contig=%d", gc.mem_free(), gc.mem_maxfree())
def lm(): log.info("MEM free=%d contig=%d", gc.mem_free(), gc.mem_maxfree()) log.info("IDF %s", [h[2] for h in idf_heap_info(HEAP_DATA) if h[2] > 0])