Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-Enable the battery monitoring on Enviro! 🥳 #146

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
37 changes: 12 additions & 25 deletions enviro/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
# ===========================================================================
from enviro.constants import *
from machine import Pin

from enviro.util_functions import get_battery_voltage

hold_vsys_en_pin = Pin(HOLD_VSYS_EN_PIN, Pin.OUT, value=True)

# detect board model based on devices on the i2c bus and pin state
Expand Down Expand Up @@ -105,24 +108,6 @@ def stop_activity_led():
# read the state of vbus to know if we were woken up by USB
vbus_present = Pin("WL_GPIO2", Pin.IN).value()

#BUG Temporarily disabling battery reading, as it seems to cause issues when connected to Thonny
"""
# read battery voltage - we have to toggle the wifi chip select
# pin to take the reading - this is probably not ideal but doesn't
# seem to cause issues. there is no obvious way to shut down the
# wifi for a while properly to do this (wlan.disonnect() and
# wlan.active(False) both seem to mess things up big style..)
old_state = Pin(WIFI_CS_PIN).value()
Pin(WIFI_CS_PIN, Pin.OUT, value=True)
sample_count = 10
battery_voltage = 0
for i in range(0, sample_count):
battery_voltage += (ADC(29).read_u16() * 3.3 / 65535) * 3
battery_voltage /= sample_count
battery_voltage = round(battery_voltage, 3)
Pin(WIFI_CS_PIN).value(old_state)
"""

# set up the button, external trigger, and rtc alarm pins
rtc_alarm_pin = Pin(RTC_ALARM_PIN, Pin.IN, Pin.PULL_DOWN)
# BUG This should only be set up for Enviro Camera
Expand Down Expand Up @@ -176,7 +161,7 @@ def connect_to_wifi():
logging.info(" - ip address: ", ip)
"""
import rp2
rp2.country("GB")
rp2.country("GB")
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(wifi_ssid, wifi_password)
Expand Down Expand Up @@ -224,7 +209,7 @@ def low_disk_space():
return (os.statvfs(".")[3] / os.statvfs(".")[2]) < 0.1
return False

# returns True if the rtc clock has been set recently
# returns True if the rtc clock has been set recently
def is_clock_set():
# is the year on or before 2020?
if rtc.datetime()[0] <= 2020:
Expand Down Expand Up @@ -283,10 +268,10 @@ def sync_clock_from_ntp():
return False

logging.info(" - rtc synched")

# write out the sync time log
with open("sync_time.txt", "w") as syncfile:
syncfile.write("{0:04d}-{1:02d}-{2:02d}T{3:02d}:{4:02d}:{5:02d}Z".format(*timestamp))
syncfile.write("{0:04d}-{1:02d}-{2:02d}T{3:02d}:{4:02d}:{5:02d}Z".format(*timestamp))

return True

Expand Down Expand Up @@ -354,8 +339,10 @@ def get_sensor_readings():
logging.info(f" - seconds since last reading: {seconds_since_last}")


readings = get_board().get_sensor_readings(seconds_since_last, vbus_present)
# readings["voltage"] = 0.0 # battery_voltage #Temporarily removed until issue is fixed
readings = get_board().get_sensor_readings(seconds_since_last)
if hasattr(config, 'enable_battery_voltage') and config.enable_battery_voltage:
readings["voltage"] = get_battery_voltage()


# write out the last time log
with open("last_time.txt", "w") as timefile:
Expand Down Expand Up @@ -438,7 +425,7 @@ def upload_readings():
# remove the sync time file to trigger a resync on next boot
if helpers.file_exists("sync_time.txt"):
os.remove("sync_time.txt")

# write out that we want to attempt a reupload
with open("reattempt_upload.txt", "w") as attemptfile:
attemptfile.write("")
Expand Down
3 changes: 3 additions & 0 deletions enviro/config_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
# how often to upload data (number of cached readings)
upload_frequency = 5

# Feature toggles
enable_battery_voltage = False

# web hook settings
custom_http_url = None
custom_http_username = None
Expand Down
30 changes: 30 additions & 0 deletions enviro/util_functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import machine

ADC_VOLT_CONVERSATION = 3.3 / 65535
Copy link

@LionsPhil LionsPhil Oct 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ADC_VOLT_CONVERSATION = 3.3 / 65535
ADC_VOLT_CONVERSION = 3.3 / 65535

(And below, appears to be a sneaky typo. :) I hope this gets merged for v0.0.11!)



def set_pad(gpio, value):
machine.mem32[0x4001c000 | (4 + (4 * gpio))] = value


def get_pad(gpio):
return machine.mem32[0x4001c000 | (4 + (4 * gpio))]


def get_battery_voltage():
old_pad = get_pad(29)
set_pad(29, 128) # no pulls, no output, no input

sample_count = 10
battery_voltage = 0
for i in range(0, sample_count):
battery_voltage += _read_vsys_voltage()
battery_voltage /= sample_count
battery_voltage = round(battery_voltage, 3)
set_pad(29, old_pad)
return battery_voltage


def _read_vsys_voltage():
adc_Vsys = machine.ADC(3)
return adc_Vsys.read_u16() * 3.0 * ADC_VOLT_CONVERSATION