Archived
1
0
This repository has been archived on 2025-04-27. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
pico-hcsr04/pico_funcs.py
2022-12-30 23:03:38 -05:00

68 lines
2.3 KiB
Python

import machine
import network
import wifi_config
import time
'''
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.
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
return temperature_f
def wlan_up(wlan):
wlan.active(True)
wlan.connect(wifi_config.HOME_WIFI_SSID, wifi_config.HOME_WIFI_PWD)
# Wait for connect or fail
max_wait = 10
while max_wait > 0:
if wlan.status() < 0 or wlan.status() >= 3:
break
max_wait -= 1
time.sleep(1)
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
while start_sequence_counter < 3:
led.value(True)
time.sleep(0.1)
led.value(False)
time.sleep(0.1)
start_sequence_counter += 1
# Run real error code sequence
blink_counter = 0
while blink_counter < error_code:
time.sleep(1)
led.value(True)
time.sleep(1)
led.value(False)
blink_counter += 1
# Make sure to turn off LED when this subroutine finished
led.value(False)