Added Pico W lab code

This commit is contained in:
Edward Bigos 2024-04-19 10:47:24 -04:00
parent 4dbe7119d2
commit a23bf2ca82
29 changed files with 974 additions and 7 deletions

View file

@ -0,0 +1,21 @@
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""Example for Pico. Turns the built-in LED on and off with no delay."""
import board
import digitalio
import time
led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT
while True:
led.value = True
print("LED On")
time.sleep(1)
led.value = False
print("LED Off")
time.sleep(1)

View file

@ -0,0 +1,46 @@
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
import time
import board
import adafruit_dht
def readDHTSensor():
runFlag = True
while runFlag:
try:
# Print the values to the serial port
temperature_c = dhtDevice.temperature
temperature_f = temperature_c * (9 / 5) + 32
humidity = dhtDevice.humidity
print(
"Temp: {:.1f} F / {:.1f} C Humidity: {}% ".format(
temperature_f, temperature_c, humidity
)
)
runFlag = False
except RuntimeError as error:
# Errors happen fairly often, DHT's are hard to read, just keep going
print(error.args[0])
time.sleep(2.0)
continue
except Exception as error:
dhtDevice.exit()
raise error
return [temperature_f, temperature_c, humidity]
# Initial the dht device, with data pin connected to:
dhtDevice = adafruit_dht.DHT11(board.GP4)
# you can pass DHT22 use_pulseio=False if you wouldn't like to use pulseio.
# This may be necessary on a Linux single board computer like the Raspberry Pi,
# but it will not work in CircuitPython.
# dhtDevice = adafruit_dht.DHT22(board.D18, use_pulseio=False)
while True:
sensorReadings = readDHTSensor()
print(sensorReadings)
time.sleep(2.0)

View file

@ -0,0 +1,112 @@
# SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries
# SPDX-License-Identifier: MIT
import os
import sys
import time
import ssl
import wifi
import socketpool
import microcontroller
import board
import busio
import adafruit_requests
import adafruit_ahtx0
import adafruit_dht
from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError
from microcontroller import cpu
def readDHTSensor():
runFlag = True
while runFlag:
try:
# Print the values to the serial port
temperature_c = dhtDevice.temperature
temperature_f = temperature_c * (9 / 5) + 32
humidity = dhtDevice.humidity
print(
"Temp: {:.1f} F / {:.1f} C Humidity: {}% ".format(
temperature_f, temperature_c, humidity
)
)
runFlag = False
except RuntimeError as error:
# Errors happen fairly often, DHT's are hard to read, just keep going
print(error.args[0])
time.sleep(2.0)
continue
except Exception as error:
dhtDevice.exit()
raise error
return [temperature_f, humidity, temperature_c]
# Initial the dht device, with data pin connected to:
dhtDevice = adafruit_dht.DHT11(board.GP4)
wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD'))
aio_username = os.getenv('aio_username')
aio_key = os.getenv('aio_key')
pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())
# Initialize an Adafruit IO HTTP API object
io = IO_HTTP(aio_username, aio_key, requests)
print("connected to io")
# use Pico W's GP0 for SDA and GP1 for SCL
#i2c = busio.I2C(board.GP1, board.GP0)
#aht20 = adafruit_ahtx0.AHTx0(i2c)
try:
# get feed
picowTemp_feed = io.get_feed("mytemperature")
picowHumid_feed = io.get_feed("myhumidity")
except AdafruitIO_RequestError:
# if no feed exists, create one
print("Feed error")
sys.exit(-1)
# pack feed names into an array for the loop
feed_names = [picowTemp_feed, picowHumid_feed]
print("feeds created")
clock = 300
while True:
try:
# when the clock runs out..
if clock > 50:
# read sensor
dhtData = readDHTSensor()
data = [dhtData[0], dhtData[1]]
# send sensor data to respective feeds
for z in range(2):
io.send_data(feed_names[z]["key"], data[z])
print("sent %0.1f" % data[z])
time.sleep(1)
# print sensor data to the REPL
print("\nTemperature: %0.1f C" % data[0])
print("Humidity: %0.1f %%" % data[1])
print()
time.sleep(1)
# reset clock
clock = 0
else:
clock += 1
# pylint: disable=broad-except
# any errors, reset Pico W
except Exception as e:
print("Error:\n", str(e))
# print("Resetting microcontroller in 10 seconds")
time.sleep(3)
# microcontroller.reset()
# delay
time.sleep(1)
print(clock)

View file

@ -0,0 +1,6 @@
CIRCUITPY_WIFI_SSID = "cset@stcc"
CIRCUITPY_WIFI_PASSWORD = "c1s2e3t4"
# Replace the next two lines with your Adafruit username and key.
aio_username = "csetuser"
aio_key = "aio_MoIy372savdsgdasgasgasgd"