From 790b82a60232c885130e73a2ce9dae66e663d51e Mon Sep 17 00:00:00 2001 From: Shaun Setlock Date: Sat, 26 Apr 2025 21:30:47 -0400 Subject: [PATCH] Moving to dotenv. --- pico_funcs.py | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/pico_funcs.py b/pico_funcs.py index 9211612..0e76cae 100644 --- a/pico_funcs.py +++ b/pico_funcs.py @@ -1,34 +1,39 @@ import machine import network -import wifi_config import time +import os +from dotenv import load_dotenv -''' +load_dotenv() + +""" pico_funcs: Helper functions for things the pico can do. -''' +""" + def read_cpu_temp(): """ - If you print the value of the temperature value you are going to get an integer number between 0 and 65535. - So, we have to convert this value either to the Celsius degree scales. + If you print the value of the temperature value you are going to get an integer number between 0 and 65535. + So, we have to convert this value either to the Celsius degree scales. - The temperature sensor works by delivering a voltage to the ADC4 pin that is proportional to the temperature. - From the datasheet, a temperature of 27 degrees Celsius delivers a voltage of 0.706 V. - With each additional degree the voltage reduces by 1.721 mV or 0.001721 V. - The first step in converting the 16-bit temperature is to convert it back to volts, which is done based on the 3.3 V maximum voltage used by the Pico board. - ref: https://how2electronics.com/read-temperature-sensor-value-from-raspberry-pi-pico/ + The temperature sensor works by delivering a voltage to the ADC4 pin that is proportional to the temperature. + From the datasheet, a temperature of 27 degrees Celsius delivers a voltage of 0.706 V. + With each additional degree the voltage reduces by 1.721 mV or 0.001721 V. + The first step in converting the 16-bit temperature is to convert it back to volts, which is done based on the 3.3 V maximum voltage used by the Pico board. + ref: https://how2electronics.com/read-temperature-sensor-value-from-raspberry-pi-pico/ """ cpu_temp_conversion_factor = 3.3 / 65535 cpu_temp_sensor = machine.ADC(4) reading = cpu_temp_sensor.read_u16() * cpu_temp_conversion_factor temperature_c = 27 - (reading - 0.706) / 0.001721 - temperature_f = temperature_c * 9/5. + 32.0 + temperature_f = temperature_c * 9 / 5.0 + 32.0 return temperature_f + def wlan_up(wlan): wlan.active(True) - wlan.connect(wifi_config.HOME_WIFI_SSID, wifi_config.HOME_WIFI_PWD) - + wlan.connect(os.getenv("HOME_WIFI_SSID"), os.getenv("HOME_WIFI_PWD")) + # Wait for connect or fail max_wait = 10 while max_wait > 0: @@ -39,13 +44,14 @@ def wlan_up(wlan): if max_wait == 0: return None - + ifconfig = wlan.ifconfig() return ifconfig + def led_error_code(led, error_code: int): """Blink LED for a given error code (int). error code == number of times to blink""" - + # Run a quick 'start error code sequence' # So we know when LED error sequence starts start_sequence_counter = 0 @@ -55,7 +61,7 @@ def led_error_code(led, error_code: int): led.value(False) time.sleep(0.1) start_sequence_counter += 1 - + # Run real error code sequence blink_counter = 0 while blink_counter < error_code: @@ -65,4 +71,5 @@ def led_error_code(led, error_code: int): led.value(False) blink_counter += 1 # Make sure to turn off LED when this subroutine finished - led.value(False) \ No newline at end of file + led.value(False) +