add battery voltage read

This commit is contained in:
Tomas Krejci 2024-05-29 15:32:33 +02:00
parent 47ccba5c3e
commit d91153dbc5

36
main.py
View File

@ -14,6 +14,7 @@ else:
from pyb import Pin, LED
import uasyncio as asyncio
#### Device ID ###
device_id_str = ""
for b in machine.unique_id():
@ -22,6 +23,37 @@ for b in machine.unique_id():
device_id_str = device_id_str[-4:] # Keep last 8 char
device_id = int(device_id_str, 16) # Convert to int from hex in string
#### Voltage monitor ###
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(26)
elif RP2:
battery_ADC = ADC(26)
else:
battery_ADC = ADC("X2")
aadc = AADC(battery_ADC)
async def battery_monitor(aadc):
while True:
aadc.sense(normal=False)
# value = await aadc(25_000, 39_000) # Wait until in range
# logger.debug("In range:", value)
# aadc.sense(normal=True)
# value = await aadc() # Wait until out of range
# logger.debug("Out of range:", value)
read = aadc.read_u16()
logger.debug(f"Battery Voltage: {read}")
await asyncio.sleep_ms(5000)
#### BLE UART ###
import ble_uart
import bluetooth
@ -191,6 +223,10 @@ async def main(proto):
### alive check ###
asyncio.create_task(alive_check())
### battery monitor ###
# aadc = AADC()
asyncio.create_task(battery_monitor(aadc))
### RGB LED ###
logger.debug("RGB init")
global rgb1