pull/536/head
helgibbons 2022-10-05 12:17:00 +01:00
rodzic dd6d0a23ea
commit 9653dcaabd
4 zmienionych plików z 66 dodań i 98 usunięć

Wyświetl plik

@ -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
@ -37,7 +36,7 @@ def status_handler(mode, status, ip):
# light up red if connection fails # light up red if connection fails
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 hex_to_rgb(hex): def hex_to_rgb(hex):
# converts a hex colour code into RGB # converts a hex colour code into RGB
@ -59,15 +58,18 @@ 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)
# light up the LEDs # light up the LEDs
for i in range(NUM_LEDS): for i in range(NUM_LEDS):
led_strip.set_rgb(i, r, g, b) led_strip.set_rgb(i, r, g, b)
@ -76,4 +78,4 @@ while True:
# sleep # sleep
print(f"""Sleeping for {UPDATE_INTERVAL} seconds. print(f"""Sleeping for {UPDATE_INTERVAL} seconds.
""") """)
time.sleep(UPDATE_INTERVAL) time.sleep(UPDATE_INTERVAL)

Wyświetl plik

@ -78,4 +78,3 @@ while True:
count += STEPS_PER_REV count += STEPS_PER_REV
count_changed(count) count_changed(count)

Wyświetl plik

@ -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)

Wyświetl plik

@ -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) + "&current_weather=true&timezone=" + TIMEZONE URL = "https://api.open-meteo.com/v1/forecast?latitude=" + str(LAT) + "&longitude=" + str(LNG) + "&current_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,54 +81,60 @@ 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")
print(f""" print(f"""
Temperature = {temperature}°C Temperature = {temperature}°C
Conditions = {WEATHERCODES[weathercode]} Conditions = {WEATHERCODES[weathercode]}
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():
# paint our current LED colours to the strip # paint our current LED colours to the strip
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):
for c in range(3): # 3 times, for R, G & B channels for c in range(3): # 3 times, for R, G & B channels
if current_leds[i][c] < target_leds[i][c]: if current_leds[i][c] < target_leds[i][c]:
current_leds[i][c] = min(current_leds[i][c] + ANIMATION_SPEED, target_leds[i][c]) # increase current, up to a maximum of target current_leds[i][c] = min(current_leds[i][c] + ANIMATION_SPEED, target_leds[i][c]) # increase current, up to a maximum of 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:
if weathercode == 2: if weathercode == 2:
cloud_colour = [165, 168, 138] # partly cloudy cloud_colour = [165, 168, 138] # partly cloudy
if weathercode == 3: if weathercode == 3:
cloud_colour = [93, 94, 83] # cloudy cloud_colour = [93, 94, 83] # cloudy
if weathercode in (45, 48): if weathercode in (45, 48):
cloud_colour = [186, 185, 182] # foggy cloud_colour = [186, 185, 182] # foggy
# add highlights and lowlights # add highlights and lowlights
@ -137,14 +142,15 @@ def clouds():
if uniform(0, 1) < 0.001: # highlight if uniform(0, 1) < 0.001: # highlight
target_leds[i] = [x+20 for x in cloud_colour] target_leds[i] = [x+20 for x in cloud_colour]
elif uniform(0, 1) < 0.001: # lowlight elif uniform(0, 1) < 0.001: # lowlight
target_leds[i] = [x-20 for x in cloud_colour] target_leds[i] = [x-20 for x in cloud_colour]
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
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)
@ -152,40 +158,44 @@ def storm():
else: else:
# paint backdrop # paint backdrop
target_leds[i] = [0, 15, 60] target_leds[i] = [0, 15, 60]
lightning_chance = 0.001 lightning_chance = 0.001
if lightning_chance > uniform(0, 1): if lightning_chance > uniform(0, 1):
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:
if weathercode in (71, 85): # light snow if weathercode in (71, 85): # light 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):
if snowflake_chance > uniform(0, 1): if snowflake_chance > uniform(0, 1):
# paint a snowflake (use current rather than target, for an abrupt change to the drop colour) # paint a snowflake (use current rather than target, for an abrupt change to the drop colour)
@ -193,12 +203,15 @@ def snow():
else: else:
# 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,15 +228,15 @@ 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
if 0 <= weathercode <= 1: if 0 <= weathercode <= 1:
clear() clear()
elif 2 <= weathercode <= 48: elif 2 <= weathercode <= 48:
clouds() clouds()
elif 51 <= weathercode <= 67 or 80 <= weathercode <= 82: elif 51 <= weathercode <= 67 or 80 <= weathercode <= 82:
rain() rain()
elif 71 <= weathercode <= 77 or 85 <= weathercode <= 86: elif 71 <= weathercode <= 77 or 85 <= weathercode <= 86:
snow() snow()
@ -231,7 +244,8 @@ while True:
storm() storm()
else: else:
print("Unknown weather code :(") print("Unknown weather code :(")
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