Added Pico W lab code
This commit is contained in:
parent
4dbe7119d2
commit
a23bf2ca82
29 changed files with 974 additions and 7 deletions
21
picow-lab-code/Lab12Code/blink.py
Executable file
21
picow-lab-code/Lab12Code/blink.py
Executable 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)
|
||||
|
||||
|
46
picow-lab-code/Lab12Code/dhtcode.py
Executable file
46
picow-lab-code/Lab12Code/dhtcode.py
Executable 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)
|
||||
|
40
picow-lab-code/Lab12Code/readAnalog01.py
Executable file
40
picow-lab-code/Lab12Code/readAnalog01.py
Executable file
|
@ -0,0 +1,40 @@
|
|||
# SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries
|
||||
#
|
||||
# SPDX-License-Identifier: MIT
|
||||
|
||||
"""CircuitPython Essentials Analog In example"""
|
||||
import time
|
||||
import board
|
||||
from analogio import AnalogIn
|
||||
|
||||
analog_in0 = AnalogIn(board.A0)
|
||||
analog_in1 = AnalogIn(board.A1)
|
||||
|
||||
minLightValue = .1
|
||||
maxLightValue = 2.8
|
||||
|
||||
minLightValue = .01
|
||||
maxPotValue = 3.28
|
||||
|
||||
def getAnalogValue(pin):
|
||||
return [pin.value,(pin.value * 3.3) / 65536]
|
||||
|
||||
def get_voltage(pin):
|
||||
return (pin.value * 3.3) / 65536
|
||||
|
||||
|
||||
|
||||
while True:
|
||||
# print((get_voltage(analog_in0),))
|
||||
# print(get_voltage(analog_in0)," ",get_voltage(analog_in1))
|
||||
pinValue0 = getAnalogValue(analog_in0)
|
||||
pinValue1 = getAnalogValue(analog_in1)
|
||||
|
||||
scaledLightValue = pinValue0[1]/maxLightValue * 100
|
||||
scaledPotValue = pinValue1[1]/maxPotValue * 100
|
||||
|
||||
print(getAnalogValue(analog_in0),"\t",getAnalogValue(analog_in1))
|
||||
print("\tScaled Light Value = ",scaledLightValue,"\tScaled Pot Value = ",scaledPotValue)
|
||||
print()
|
||||
|
||||
time.sleep(1.0)
|
104
picow-lab-code/Lab12Code/sensors2cloud.py
Normal file
104
picow-lab-code/Lab12Code/sensors2cloud.py
Normal file
|
@ -0,0 +1,104 @@
|
|||
# SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries
|
||||
# SPDX-License-Identifier: MIT
|
||||
|
||||
import os
|
||||
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:
|
||||
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 >= 10:
|
||||
# read sensors
|
||||
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(f"{data[0]:.1f}F {data[1]:.1f}% Humidity")
|
||||
|
||||
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))
|
||||
time.sleep(3)
|
||||
# microcontroller.reset()
|
||||
# delay
|
||||
time.sleep(1)
|
||||
print(clock)
|
143
picow-lab-code/Lab12Code/sensors2cloud2.py
Normal file
143
picow-lab-code/Lab12Code/sensors2cloud2.py
Normal file
|
@ -0,0 +1,143 @@
|
|||
# SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries
|
||||
# SPDX-License-Identifier: MIT
|
||||
|
||||
import os
|
||||
import time
|
||||
import ssl
|
||||
import sys
|
||||
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
|
||||
from analogio import AnalogIn
|
||||
|
||||
analog_in0 = AnalogIn(board.A0) # Pin 31 (Right side, 10 down from top)
|
||||
analog_in1 = AnalogIn(board.A1) # Pin 32 (Right side, 9 down from top)
|
||||
|
||||
|
||||
minLightValue = .1
|
||||
maxLightValue = 2.8
|
||||
|
||||
minPotValue = .1
|
||||
maxPotValue = 3.3
|
||||
|
||||
|
||||
def getAnalogValue(pin):
|
||||
return [pin.value,(pin.value * 3.3) / 65536]
|
||||
|
||||
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")
|
||||
picowLight_feed = io.get_feed("mylightlevel")
|
||||
picowPot_feed = io.get_feed("mypotlevel")
|
||||
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,picowLight_feed,picowPot_feed]
|
||||
print("feeds created")
|
||||
|
||||
clock = 300
|
||||
|
||||
|
||||
while True:
|
||||
try:
|
||||
# when the clock runs out..
|
||||
if clock > 10:
|
||||
# read sensors
|
||||
lightLevel = getAnalogValue(analog_in0)
|
||||
scaledLightValue = lightLevel[1]/maxLightValue * 100
|
||||
potLevel = getAnalogValue(analog_in1)
|
||||
scaledPotValue = potLevel[1]/maxPotValue * 100
|
||||
print("Scaled Light Value = ",scaledLightValue)
|
||||
print("Scaled Pot Value = ",scaledPotValue)
|
||||
|
||||
# print("light level: ",lightLevel)
|
||||
# lightPerCent = lightLevel[1] /3
|
||||
|
||||
dhtData = readDHTSensor()
|
||||
|
||||
data = [dhtData[0], dhtData[1], scaledLightValue,scaledPotValue]
|
||||
# send sensor data to respective feeds
|
||||
for z in range(4):
|
||||
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(type(data[0]), type(data[1]), type(data[2]))
|
||||
print(f"{data[0]:.1f}F {data[1]:.1f}% Humidity {data[2]:.1f}% Light Level {data[3]:.1f}% Pot Level")
|
||||
# print("\nTemperature: %0.1f C" % data[0])
|
||||
# print("Humidity: %0.1f %%" % data[1])
|
||||
# print("\nLight percent: %0.1f " % data[2])
|
||||
# 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)
|
8
picow-lab-code/Lab12Code/settings.toml
Normal file
8
picow-lab-code/Lab12Code/settings.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
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"
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue