kopia lustrzana https://github.com/pimoroni/pimoroni-pico
linting
rodzic
dd6d0a23ea
commit
9653dcaabd
|
@ -1,9 +1,8 @@
|
|||
import WIFI_CONFIG
|
||||
from network_manager import NetworkManager
|
||||
import uasyncio
|
||||
from urequests import get
|
||||
import urequests
|
||||
import time
|
||||
import ujson
|
||||
import plasma
|
||||
from plasma import plasma2040
|
||||
|
||||
|
@ -59,11 +58,14 @@ uasyncio.get_event_loop().run_until_complete(network_manager.client(WIFI_CONFIG.
|
|||
while True:
|
||||
# open the json file
|
||||
print(f"Requesting URL: {URL}")
|
||||
data = get(URL).json()
|
||||
r = urequests.get(URL)
|
||||
# open the json data
|
||||
j = r.json()
|
||||
print("Data obtained!")
|
||||
r.close()
|
||||
|
||||
# extract hex colour from the data
|
||||
hex = data['field2']
|
||||
hex = j["field2"]
|
||||
|
||||
# and convert it to RGB
|
||||
r, g, b = hex_to_rgb(hex)
|
||||
|
|
|
@ -78,4 +78,3 @@ while True:
|
|||
count += STEPS_PER_REV
|
||||
|
||||
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
|
||||
from network_manager import NetworkManager
|
||||
import uasyncio
|
||||
from urequests import get
|
||||
import urequests
|
||||
import time
|
||||
import ujson
|
||||
import plasma
|
||||
from plasma import plasma2040
|
||||
# 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
|
||||
|
||||
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)
|
||||
WEATHERCODES = {
|
||||
|
@ -82,12 +81,16 @@ def status_handler(mode, status, ip):
|
|||
for i in range(NUM_LEDS):
|
||||
led_strip.set_rgb(i, 255, 0, 0)
|
||||
|
||||
|
||||
def get_data():
|
||||
global weathercode
|
||||
# open the json file
|
||||
print(f"Requesting URL: {URL}")
|
||||
j = get(URL).json()
|
||||
r = urequests.get(URL)
|
||||
# open the json data
|
||||
j = r.json()
|
||||
print("Data obtained!")
|
||||
r.close()
|
||||
gc.collect() # protecc the RAM
|
||||
|
||||
# parse relevant data from JSON
|
||||
current = j["current_weather"]
|
||||
|
@ -101,7 +104,6 @@ def get_data():
|
|||
Last Open-Meteo update: {datetime_arr[0]}, {datetime_arr[1]}
|
||||
""")
|
||||
|
||||
gc.collect()
|
||||
|
||||
# the rest of our functions are for animations!
|
||||
def display_current():
|
||||
|
@ -109,6 +111,7 @@ def display_current():
|
|||
for i in range(NUM_LEDS):
|
||||
led_strip.set_rgb(i, current_leds[i][0], current_leds[i][1], current_leds[i][2])
|
||||
|
||||
|
||||
def move_to_target():
|
||||
# nudge our current colours closer to the target colours
|
||||
for i in range(NUM_LEDS):
|
||||
|
@ -118,11 +121,13 @@ def move_to_target():
|
|||
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
|
||||
|
||||
|
||||
def clear():
|
||||
# nice sunny yellow
|
||||
for i in range(NUM_LEDS):
|
||||
target_leds[i] = [242, 237, 80]
|
||||
|
||||
|
||||
def clouds():
|
||||
# base colours:
|
||||
if weathercode == 2:
|
||||
|
@ -141,6 +146,7 @@ def clouds():
|
|||
elif uniform(0, 1) < 0.005: # normal
|
||||
target_leds[i] = cloud_colour
|
||||
|
||||
|
||||
def storm():
|
||||
# heavy rain, with lightning!
|
||||
raindrop_chance = 0.01
|
||||
|
@ -158,6 +164,7 @@ def storm():
|
|||
for i in range(NUM_LEDS):
|
||||
current_leds[i] = [255, 255, 255]
|
||||
|
||||
|
||||
def rain():
|
||||
# splodgy blues
|
||||
# first, work out how many raindrops:
|
||||
|
@ -165,7 +172,8 @@ def rain():
|
|||
raindrop_chance = 0.001
|
||||
elif weathercode in (53, 63, 81): # moderate rain
|
||||
raindrop_chance = 0.005
|
||||
else: #heavy rain
|
||||
else:
|
||||
# heavy rain
|
||||
raindrop_chance = 0.01
|
||||
|
||||
for i in range(NUM_LEDS):
|
||||
|
@ -176,6 +184,7 @@ def rain():
|
|||
# paint backdrop
|
||||
target_leds[i] = [0, 15, 60]
|
||||
|
||||
|
||||
def snow():
|
||||
# splodgy whites
|
||||
# first, work out how many snowflakes:
|
||||
|
@ -183,7 +192,8 @@ def snow():
|
|||
snowflake_chance = 0.001
|
||||
elif weathercode in (73, 77): # moderate snow
|
||||
snowflake_chance = 0.005
|
||||
else: #heavy snow
|
||||
else:
|
||||
# heavy snow
|
||||
snowflake_chance = 0.01
|
||||
|
||||
for i in range(NUM_LEDS):
|
||||
|
@ -194,11 +204,14 @@ def snow():
|
|||
# paint backdrop
|
||||
target_leds[i] = [54, 54, 54]
|
||||
|
||||
|
||||
# some variables we'll use for animations
|
||||
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
|
||||
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
|
||||
# Create an list of [r, g, b] values that will hold current LED colours, for display
|
||||
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
|
||||
led_strip = plasma.WS2812(NUM_LEDS, 0, 0, plasma2040.DAT)
|
||||
|
@ -235,3 +248,4 @@ while True:
|
|||
move_to_target() # nudge our current colours closer to the target colours
|
||||
display_current() # display current colours to strip
|
||||
|
||||
gc.collect() # try and conserve RAM
|
||||
|
|
Ładowanie…
Reference in New Issue