From ff9906ca71617ca610aaa02218883e86d48e2403 Mon Sep 17 00:00:00 2001 From: Tomas Krejci Date: Thu, 30 May 2024 10:49:52 +0200 Subject: [PATCH] up batt monitor --- main.py | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/main.py b/main.py index 1818b75..2f252c5 100644 --- a/main.py +++ b/main.py @@ -4,16 +4,18 @@ import machine, time, re from sys import platform +from primitives import set_global_exception +import uasyncio as asyncio ESP32 = platform == "esp32" RP2 = platform == "rp2" PYBOARD = platform == "pyboard" + +#### onboard LED #### if ESP32 or RP2: from machine import Pin else: from pyb import Pin, LED -import uasyncio as asyncio - #### Device ID ### device_id_str = "" @@ -28,9 +30,6 @@ from machine import ADC from primitives import AADC # Define ADC battery pin according to platform -ESP32 = platform == "esp32" -RP2 = platform == "rp2" -PYBOARD = platform == "pyboard" if ESP32: battery_ADC = ADC(28) elif RP2: @@ -50,15 +49,17 @@ async def battery_monitor(aadc): # value = await aadc() # Wait until out of range # logger.debug("Out of range:", value) - voltage = aadc.read_u16() * (3.3 / 65535) - - logger.debug( - f"Battery Voltage: {voltage}V, Capacity: {int(voltage_to_capacity(voltage / 2))}%" - ) - await asyncio.sleep_ms(5000) + voltage = aadc.read_u16() * ( + 3.3 / 65535 + ) # reference voltage (3.3V) divided by the maximum ADC value (65535) + voltage = round(voltage, 2) + capacity = voltage_to_capacity(voltage / 2) + logger.info(f"Battery Voltage: {voltage}V, Capacity: {capacity}%") + uart.write(f"BATTERY: Voltage: {voltage}V, Capacity: {capacity}%\n") + await asyncio.sleep_ms(10000) -# simple voltage to capacity, model for alkaline batteries +# simple voltage to capacity, model for standart one cell alkaline batteries def voltage_to_capacity(voltage): if voltage >= 1.6: return 100 @@ -91,7 +92,7 @@ def ble_rx_handler(): if message_int == 0x01: uart.write(f"BLE: uart recive {message_int:08x}\n") else: - logger.debug(f"BLE: message is {message_int}") + logger.info(f"BLE: message is {message_int}") except TypeError: logger.debug("BLE: TypeError") @@ -259,6 +260,7 @@ async def cb(byte1, byte2, byte3, packet): ### main ### async def main(proto): + set_global_exception() # enable a global exception handler to simplify debugging logger.debug("main start") print(f"Device ID: {device_id_str}")