kopia lustrzana https://github.com/pimoroni/pimoroni-pico
linting
rodzic
dd6d0a23ea
commit
9653dcaabd
|
@ -1,9 +1,8 @@
|
||||||
import WIFI_CONFIG
|
import WIFI_CONFIG
|
||||||
from network_manager import NetworkManager
|
from network_manager import NetworkManager
|
||||||
import uasyncio
|
import uasyncio
|
||||||
from urequests import get
|
import urequests
|
||||||
import time
|
import time
|
||||||
import ujson
|
|
||||||
import plasma
|
import plasma
|
||||||
from plasma import plasma2040
|
from plasma import plasma2040
|
||||||
|
|
||||||
|
@ -59,11 +58,14 @@ uasyncio.get_event_loop().run_until_complete(network_manager.client(WIFI_CONFIG.
|
||||||
while True:
|
while True:
|
||||||
# open the json file
|
# open the json file
|
||||||
print(f"Requesting URL: {URL}")
|
print(f"Requesting URL: {URL}")
|
||||||
data = get(URL).json()
|
r = urequests.get(URL)
|
||||||
|
# open the json data
|
||||||
|
j = r.json()
|
||||||
print("Data obtained!")
|
print("Data obtained!")
|
||||||
|
r.close()
|
||||||
|
|
||||||
# extract hex colour from the data
|
# extract hex colour from the data
|
||||||
hex = data['field2']
|
hex = j["field2"]
|
||||||
|
|
||||||
# and convert it to RGB
|
# and convert it to RGB
|
||||||
r, g, b = hex_to_rgb(hex)
|
r, g, b = hex_to_rgb(hex)
|
||||||
|
|
|
@ -78,4 +78,3 @@ while True:
|
||||||
count += STEPS_PER_REV
|
count += STEPS_PER_REV
|
||||||
|
|
||||||
count_changed(count)
|
count_changed(count)
|
||||||
|
|
|
@ -1,47 +0,0 @@
|
||||||
import plasma
|
|
||||||
from plasma import plasma2040
|
|
||||||
from pimoroni import RGBLED
|
|
||||||
from pimoroni_i2c import PimoroniI2C
|
|
||||||
import machine
|
|
||||||
import time
|
|
||||||
|
|
||||||
"""
|
|
||||||
Reads the internal temperature sensor on the Pico and changes the LED strip an appropriate colour.
|
|
||||||
"""
|
|
||||||
|
|
||||||
# Set how many LEDs you have
|
|
||||||
NUM_LEDS = 50
|
|
||||||
|
|
||||||
BRIGHTNESS = 1.0
|
|
||||||
|
|
||||||
MIN = 15
|
|
||||||
MAX = 30
|
|
||||||
|
|
||||||
# What you want your MIN colour to be - a hue between 0 and 360 degrees.
|
|
||||||
# Green is 120!
|
|
||||||
START_HUE = 120
|
|
||||||
|
|
||||||
# WS2812 / NeoPixel™ LEDs
|
|
||||||
led_strip = plasma.WS2812(NUM_LEDS, 0, 0, plasma2040.DAT, rgbw=False)
|
|
||||||
|
|
||||||
# Start updating the LED strip
|
|
||||||
led_strip.start()
|
|
||||||
|
|
||||||
sensor_temp = machine.ADC(4)
|
|
||||||
conversion_factor = 3.3 / (65535) # used for calculating a temperature from the raw sensor reading
|
|
||||||
|
|
||||||
while True:
|
|
||||||
|
|
||||||
# the following two lines do some maths to convert the number from the temp sensor into celsius
|
|
||||||
reading = sensor_temp.read_u16() * conversion_factor
|
|
||||||
temperature = 27 - (reading - 0.706) / 0.001721
|
|
||||||
print(f"""
|
|
||||||
Temperature: {temperature:0.2f} * C
|
|
||||||
""")
|
|
||||||
|
|
||||||
# calculates a colour
|
|
||||||
hue = max(0, START_HUE / 360 * (1 - (temperature - MIN) / MAX))
|
|
||||||
for i in range(NUM_LEDS):
|
|
||||||
led_strip.set_hsv(i, hue, 1.0, BRIGHTNESS)
|
|
||||||
|
|
||||||
time.sleep(0.5)
|
|
|
@ -1,9 +1,8 @@
|
||||||
import WIFI_CONFIG
|
import WIFI_CONFIG
|
||||||
from network_manager import NetworkManager
|
from network_manager import NetworkManager
|
||||||
import uasyncio
|
import uasyncio
|
||||||
from urequests import get
|
import urequests
|
||||||
import time
|
import time
|
||||||
import ujson
|
|
||||||
import plasma
|
import plasma
|
||||||
from plasma import plasma2040
|
from plasma import plasma2040
|
||||||
# Random functions! randrange is for picking integers from a range, and uniform is for floats.
|
# Random functions! randrange is for picking integers from a range, and uniform is for floats.
|
||||||
|
@ -28,7 +27,7 @@ LNG = -1.4239983439328177
|
||||||
TIMEZONE = "auto" # determines time zone from lat/long
|
TIMEZONE = "auto" # determines time zone from lat/long
|
||||||
|
|
||||||
URL = "https://api.open-meteo.com/v1/forecast?latitude=" + str(LAT) + "&longitude=" + str(LNG) + "¤t_weather=true&timezone=" + TIMEZONE
|
URL = "https://api.open-meteo.com/v1/forecast?latitude=" + str(LAT) + "&longitude=" + str(LNG) + "¤t_weather=true&timezone=" + TIMEZONE
|
||||||
UPDATE_INTERVAL = 500 # refresh interval in secs. Be nice to free APIs!
|
UPDATE_INTERVAL = 300 # refresh interval in secs. Be nice to free APIs!
|
||||||
|
|
||||||
# Weather codes from https://open-meteo.com/en/docs#:~:text=WMO%20Weather%20interpretation%20codes%20(WW)
|
# Weather codes from https://open-meteo.com/en/docs#:~:text=WMO%20Weather%20interpretation%20codes%20(WW)
|
||||||
WEATHERCODES = {
|
WEATHERCODES = {
|
||||||
|
@ -82,15 +81,19 @@ def status_handler(mode, status, ip):
|
||||||
for i in range(NUM_LEDS):
|
for i in range(NUM_LEDS):
|
||||||
led_strip.set_rgb(i, 255, 0, 0)
|
led_strip.set_rgb(i, 255, 0, 0)
|
||||||
|
|
||||||
|
|
||||||
def get_data():
|
def get_data():
|
||||||
global weathercode
|
global weathercode
|
||||||
# open the json file
|
|
||||||
print(f"Requesting URL: {URL}")
|
print(f"Requesting URL: {URL}")
|
||||||
j = get(URL).json()
|
r = urequests.get(URL)
|
||||||
|
# open the json data
|
||||||
|
j = r.json()
|
||||||
print("Data obtained!")
|
print("Data obtained!")
|
||||||
|
r.close()
|
||||||
|
gc.collect() # protecc the RAM
|
||||||
|
|
||||||
# parse relevant data from JSON
|
# parse relevant data from JSON
|
||||||
current= j["current_weather"]
|
current = j["current_weather"]
|
||||||
temperature = current["temperature"]
|
temperature = current["temperature"]
|
||||||
weathercode = current["weathercode"]
|
weathercode = current["weathercode"]
|
||||||
datetime_arr = current["time"].split("T")
|
datetime_arr = current["time"].split("T")
|
||||||
|
@ -101,7 +104,6 @@ def get_data():
|
||||||
Last Open-Meteo update: {datetime_arr[0]}, {datetime_arr[1]}
|
Last Open-Meteo update: {datetime_arr[0]}, {datetime_arr[1]}
|
||||||
""")
|
""")
|
||||||
|
|
||||||
gc.collect()
|
|
||||||
|
|
||||||
# the rest of our functions are for animations!
|
# the rest of our functions are for animations!
|
||||||
def display_current():
|
def display_current():
|
||||||
|
@ -109,6 +111,7 @@ def display_current():
|
||||||
for i in range(NUM_LEDS):
|
for i in range(NUM_LEDS):
|
||||||
led_strip.set_rgb(i, current_leds[i][0], current_leds[i][1], current_leds[i][2])
|
led_strip.set_rgb(i, current_leds[i][0], current_leds[i][1], current_leds[i][2])
|
||||||
|
|
||||||
|
|
||||||
def move_to_target():
|
def move_to_target():
|
||||||
# nudge our current colours closer to the target colours
|
# nudge our current colours closer to the target colours
|
||||||
for i in range(NUM_LEDS):
|
for i in range(NUM_LEDS):
|
||||||
|
@ -118,10 +121,12 @@ def move_to_target():
|
||||||
elif current_leds[i][c] > target_leds[i][c]:
|
elif current_leds[i][c] > target_leds[i][c]:
|
||||||
current_leds[i][c] = max(current_leds[i][c] - ANIMATION_SPEED, target_leds[i][c]) # reduce current, down to a minimum of target
|
current_leds[i][c] = max(current_leds[i][c] - ANIMATION_SPEED, target_leds[i][c]) # reduce current, down to a minimum of target
|
||||||
|
|
||||||
|
|
||||||
def clear():
|
def clear():
|
||||||
# nice sunny yellow
|
# nice sunny yellow
|
||||||
for i in range(NUM_LEDS):
|
for i in range(NUM_LEDS):
|
||||||
target_leds[i] = [242, 237,80]
|
target_leds[i] = [242, 237, 80]
|
||||||
|
|
||||||
|
|
||||||
def clouds():
|
def clouds():
|
||||||
# base colours:
|
# base colours:
|
||||||
|
@ -141,6 +146,7 @@ def clouds():
|
||||||
elif uniform(0, 1) < 0.005: # normal
|
elif uniform(0, 1) < 0.005: # normal
|
||||||
target_leds[i] = cloud_colour
|
target_leds[i] = cloud_colour
|
||||||
|
|
||||||
|
|
||||||
def storm():
|
def storm():
|
||||||
# heavy rain, with lightning!
|
# heavy rain, with lightning!
|
||||||
raindrop_chance = 0.01
|
raindrop_chance = 0.01
|
||||||
|
@ -158,24 +164,27 @@ def storm():
|
||||||
for i in range(NUM_LEDS):
|
for i in range(NUM_LEDS):
|
||||||
current_leds[i] = [255, 255, 255]
|
current_leds[i] = [255, 255, 255]
|
||||||
|
|
||||||
|
|
||||||
def rain():
|
def rain():
|
||||||
# splodgy blues
|
# splodgy blues
|
||||||
# first, work out how many raindrops:
|
# first, work out how many raindrops:
|
||||||
if weathercode in (51, 56, 61, 66, 80): # light rain
|
if weathercode in (51, 56, 61, 66, 80): # light rain
|
||||||
raindrop_chance = 0.001
|
raindrop_chance = 0.001
|
||||||
elif weathercode in (53, 63, 81): #moderate rain
|
elif weathercode in (53, 63, 81): # moderate rain
|
||||||
raindrop_chance = 0.005
|
raindrop_chance = 0.005
|
||||||
else: #heavy rain
|
else:
|
||||||
|
# heavy rain
|
||||||
raindrop_chance = 0.01
|
raindrop_chance = 0.01
|
||||||
|
|
||||||
for i in range(NUM_LEDS):
|
for i in range(NUM_LEDS):
|
||||||
if raindrop_chance > uniform(0,1):
|
if raindrop_chance > uniform(0, 1):
|
||||||
# paint a raindrop (use current rather than target, for an abrupt change to the drop colour)
|
# paint a raindrop (use current rather than target, for an abrupt change to the drop colour)
|
||||||
current_leds[i] = [randrange(0, 50), randrange(20, 100), randrange(50, 255)]
|
current_leds[i] = [randrange(0, 50), randrange(20, 100), randrange(50, 255)]
|
||||||
else:
|
else:
|
||||||
# paint backdrop
|
# paint backdrop
|
||||||
target_leds[i] = [0, 15, 60]
|
target_leds[i] = [0, 15, 60]
|
||||||
|
|
||||||
|
|
||||||
def snow():
|
def snow():
|
||||||
# splodgy whites
|
# splodgy whites
|
||||||
# first, work out how many snowflakes:
|
# first, work out how many snowflakes:
|
||||||
|
@ -183,7 +192,8 @@ def snow():
|
||||||
snowflake_chance = 0.001
|
snowflake_chance = 0.001
|
||||||
elif weathercode in (73, 77): # moderate snow
|
elif weathercode in (73, 77): # moderate snow
|
||||||
snowflake_chance = 0.005
|
snowflake_chance = 0.005
|
||||||
else: #heavy snow
|
else:
|
||||||
|
# heavy snow
|
||||||
snowflake_chance = 0.01
|
snowflake_chance = 0.01
|
||||||
|
|
||||||
for i in range(NUM_LEDS):
|
for i in range(NUM_LEDS):
|
||||||
|
@ -194,11 +204,14 @@ def snow():
|
||||||
# paint backdrop
|
# paint backdrop
|
||||||
target_leds[i] = [54, 54, 54]
|
target_leds[i] = [54, 54, 54]
|
||||||
|
|
||||||
|
|
||||||
# some variables we'll use for animations
|
# some variables we'll use for animations
|
||||||
ANIMATION_SPEED = 2 # higher number gets from current to target colour faster
|
ANIMATION_SPEED = 2 # higher number gets from current to target colour faster
|
||||||
|
|
||||||
current_leds = [ [0] * 3 for i in range(NUM_LEDS)] # Create an list of [r, g, b] values that will hold current LED colours, for display
|
# Create an list of [r, g, b] values that will hold current LED colours, for display
|
||||||
target_leds = [ [0] * 3 for i in range(NUM_LEDS)] # Create an list of [r, g, b] values that will hold target LED colours, to move towards
|
current_leds = [[0] * 3 for i in range(NUM_LEDS)]
|
||||||
|
# Create an list of [r, g, b] values that will hold target LED colours, to move towards
|
||||||
|
target_leds = [[0] * 3 for i in range(NUM_LEDS)]
|
||||||
|
|
||||||
# set up the WS2812 / NeoPixel™ LEDs
|
# set up the WS2812 / NeoPixel™ LEDs
|
||||||
led_strip = plasma.WS2812(NUM_LEDS, 0, 0, plasma2040.DAT)
|
led_strip = plasma.WS2812(NUM_LEDS, 0, 0, plasma2040.DAT)
|
||||||
|
@ -215,7 +228,7 @@ get_data()
|
||||||
|
|
||||||
# start timer (the timer will update our data every UPDATE_INTERVAL)
|
# start timer (the timer will update our data every UPDATE_INTERVAL)
|
||||||
timer = Timer(-1)
|
timer = Timer(-1)
|
||||||
timer.init(period=UPDATE_INTERVAL*1000, mode=Timer.PERIODIC, callback=lambda t:get_data())
|
timer.init(period=UPDATE_INTERVAL*1000, mode=Timer.PERIODIC, callback=lambda t: get_data())
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
# do some fancy stuff with the LEDs based on the weather code
|
# do some fancy stuff with the LEDs based on the weather code
|
||||||
|
@ -235,3 +248,4 @@ while True:
|
||||||
move_to_target() # nudge our current colours closer to the target colours
|
move_to_target() # nudge our current colours closer to the target colours
|
||||||
display_current() # display current colours to strip
|
display_current() # display current colours to strip
|
||||||
|
|
||||||
|
gc.collect() # try and conserve RAM
|
||||||
|
|
Ładowanie…
Reference in New Issue