Stellar: update weather example
|
@ -155,7 +155,7 @@ Requires `logging.mpy` and `tinyweb` from [micropython/examples/common](../../ex
|
||||||
|
|
||||||
[weather](weather)
|
[weather](weather)
|
||||||
|
|
||||||
Display current weather data from the [Open-Meteo](https://open-meteo.com/) weather API.
|
Display current weather data from the [Open-Meteo](https://open-meteo.com/) weather API. Make sure to copy across the `icons` folder to your Unicorn.
|
||||||
|
|
||||||
## NumPy Examples
|
## NumPy Examples
|
||||||
|
|
||||||
|
|
Po Szerokość: | Wysokość: | Rozmiar: 776 B |
Przed Szerokość: | Wysokość: | Rozmiar: 1.1 KiB |
Przed Szerokość: | Wysokość: | Rozmiar: 1.5 KiB |
Przed Szerokość: | Wysokość: | Rozmiar: 1.6 KiB |
Przed Szerokość: | Wysokość: | Rozmiar: 1.6 KiB |
Po Szerokość: | Wysokość: | Rozmiar: 895 B |
Przed Szerokość: | Wysokość: | Rozmiar: 2.0 KiB |
Przed Szerokość: | Wysokość: | Rozmiar: 2.1 KiB |
Przed Szerokość: | Wysokość: | Rozmiar: 2.0 KiB |
Przed Szerokość: | Wysokość: | Rozmiar: 2.0 KiB |
Po Szerokość: | Wysokość: | Rozmiar: 1.0 KiB |
Przed Szerokość: | Wysokość: | Rozmiar: 2.1 KiB |
Przed Szerokość: | Wysokość: | Rozmiar: 2.1 KiB |
Przed Szerokość: | Wysokość: | Rozmiar: 2.2 KiB |
Przed Szerokość: | Wysokość: | Rozmiar: 2.1 KiB |
Po Szerokość: | Wysokość: | Rozmiar: 909 B |
Przed Szerokość: | Wysokość: | Rozmiar: 1.3 KiB |
Przed Szerokość: | Wysokość: | Rozmiar: 1.7 KiB |
Przed Szerokość: | Wysokość: | Rozmiar: 1.8 KiB |
Przed Szerokość: | Wysokość: | Rozmiar: 1.7 KiB |
Po Szerokość: | Wysokość: | Rozmiar: 991 B |
Przed Szerokość: | Wysokość: | Rozmiar: 2.2 KiB |
Przed Szerokość: | Wysokość: | Rozmiar: 2.1 KiB |
Przed Szerokość: | Wysokość: | Rozmiar: 2.2 KiB |
Przed Szerokość: | Wysokość: | Rozmiar: 2.1 KiB |
Przed Szerokość: | Wysokość: | Rozmiar: 81 KiB |
|
@ -3,28 +3,35 @@ from stellar import StellarUnicorn
|
||||||
from picographics import PicoGraphics, DISPLAY_STELLAR_UNICORN as DISPLAY
|
from picographics import PicoGraphics, DISPLAY_STELLAR_UNICORN as DISPLAY
|
||||||
import WIFI_CONFIG
|
import WIFI_CONFIG
|
||||||
from network_manager import NetworkManager
|
from network_manager import NetworkManager
|
||||||
import uasyncio as asyncio
|
import uasyncio
|
||||||
import urequests
|
import urequests
|
||||||
import jpegdec
|
import jpegdec
|
||||||
|
|
||||||
|
"""
|
||||||
|
This example connects to Open Meteo to access the current weather conditions.
|
||||||
|
It then displays an appropriate weather icon on Stellar Unicorn.
|
||||||
|
|
||||||
|
Find out more about the Open Meteo API at https://open-meteo.com
|
||||||
|
"""
|
||||||
|
|
||||||
# Set your latitude/longitude here (find yours by right clicking in Google Maps!)
|
# Set your latitude/longitude here (find yours by right clicking in Google Maps!)
|
||||||
LAT = 53.38609085276884
|
LAT = 53.38609085276884
|
||||||
LNG = -1.4239983439328177
|
LNG = -1.4239983439328177
|
||||||
TIMEZONE = "auto" # determines time zone from lat/long
|
TIMEZONE = "auto" # determines time zone from lat/long
|
||||||
|
|
||||||
URL = "http://api.open-meteo.com/v1/forecast?latitude=" + str(LAT) + "&longitude=" + str(LNG) + "¤t_weather=true&timezone=" + TIMEZONE
|
URL = "http://api.open-meteo.com/v1/forecast?latitude=" + str(LAT) + "&longitude=" + str(LNG) + "¤t_weather=true&timezone=" + TIMEZONE
|
||||||
WEATHER_TEXT = ''
|
|
||||||
user_icon = None
|
# how often to poll the API, in minutes
|
||||||
|
UPDATE_INTERVAL = 5
|
||||||
|
|
||||||
|
|
||||||
def get_data():
|
def get_data():
|
||||||
global WEATHER_TEXT, temperature, weathercode
|
global temperature, weathercode
|
||||||
print(f"Requesting URL: {URL}")
|
print(f"Requesting URL: {URL}")
|
||||||
r = urequests.get(URL)
|
r = urequests.get(URL)
|
||||||
# open the json data
|
# open the json data
|
||||||
j = r.json()
|
j = r.json()
|
||||||
print("Data obtained!")
|
print("Data obtained!")
|
||||||
print(j)
|
|
||||||
|
|
||||||
# parse relevant data from JSON
|
# parse relevant data from JSON
|
||||||
current = j["current_weather"]
|
current = j["current_weather"]
|
||||||
|
@ -33,8 +40,7 @@ def get_data():
|
||||||
winddirection = calculate_bearing(current["winddirection"])
|
winddirection = calculate_bearing(current["winddirection"])
|
||||||
weathercode = current["weathercode"]
|
weathercode = current["weathercode"]
|
||||||
date, now = current["time"].split("T")
|
date, now = current["time"].split("T")
|
||||||
WEATHER_TEXT = f"Temp: {temperature}°C Wind Speed: {windspeed}kmph Wind Direction: {winddirection} As of: {date}, {now}"
|
print(f"Temp: {temperature}°C Wind Speed: {windspeed}kmph Wind Direction: {winddirection} As of: {date}, {now}")
|
||||||
print(WEATHER_TEXT)
|
|
||||||
r.close()
|
r.close()
|
||||||
|
|
||||||
|
|
||||||
|
@ -46,8 +52,14 @@ def calculate_bearing(d):
|
||||||
|
|
||||||
|
|
||||||
def status_handler(mode, status, ip):
|
def status_handler(mode, status, ip):
|
||||||
global MESSAGE
|
# reports wifi connection status
|
||||||
print("Network: {}".format(WIFI_CONFIG.SSID))
|
print(mode, status, ip)
|
||||||
|
print('Connecting to wifi...')
|
||||||
|
if status is not None:
|
||||||
|
if status:
|
||||||
|
print('Wifi connection successful!')
|
||||||
|
else:
|
||||||
|
print('Wifi connection failed!')
|
||||||
|
|
||||||
|
|
||||||
# create galactic object and graphics surface for drawing
|
# create galactic object and graphics surface for drawing
|
||||||
|
@ -57,97 +69,39 @@ display = PicoGraphics(DISPLAY)
|
||||||
WIDTH = StellarUnicorn.WIDTH
|
WIDTH = StellarUnicorn.WIDTH
|
||||||
HEIGHT = StellarUnicorn.HEIGHT
|
HEIGHT = StellarUnicorn.HEIGHT
|
||||||
|
|
||||||
|
RED = display.create_pen(255, 0, 0)
|
||||||
|
|
||||||
jpeg = jpegdec.JPEG(display)
|
jpeg = jpegdec.JPEG(display)
|
||||||
TEXT_COLOUR = display.create_pen(200, 0, 200)
|
|
||||||
BLACK = display.create_pen(0, 0, 0)
|
|
||||||
WHITE = display.create_pen(255, 255, 255)
|
|
||||||
|
|
||||||
|
su.set_brightness(1.0)
|
||||||
|
|
||||||
def run():
|
# set up wifi
|
||||||
# Setup wifi
|
try:
|
||||||
network_manager = NetworkManager(WIFI_CONFIG.COUNTRY, status_handler=status_handler)
|
network_manager = NetworkManager(WIFI_CONFIG.COUNTRY, status_handler=status_handler)
|
||||||
|
uasyncio.get_event_loop().run_until_complete(network_manager.client(WIFI_CONFIG.SSID, WIFI_CONFIG.PSK))
|
||||||
|
except Exception as e:
|
||||||
|
print(f'Wifi connection failed! {e}')
|
||||||
|
|
||||||
# Connect to Wifi network
|
while True:
|
||||||
asyncio.get_event_loop().run_until_complete(network_manager.client(WIFI_CONFIG.SSID, WIFI_CONFIG.PSK))
|
get_data()
|
||||||
while (not network_manager.isconnected()):
|
if weathercode is not None:
|
||||||
time.sleep(0.1)
|
|
||||||
|
|
||||||
|
|
||||||
su.set_brightness(1)
|
|
||||||
run() # Sets up Wifi connection
|
|
||||||
|
|
||||||
|
|
||||||
def outline_text(text, x, y):
|
|
||||||
display.set_pen(BLACK)
|
|
||||||
display.text(text, x - 1, y - 1, -1, 1)
|
|
||||||
display.text(text, x, y - 1, -1, 1)
|
|
||||||
display.text(text, x + 1, y - 1, -1, 1)
|
|
||||||
display.text(text, x - 1, y, -1, 1)
|
|
||||||
display.text(text, x + 1, y, -1, 1)
|
|
||||||
display.text(text, x - 1, y + 1, -1, 1)
|
|
||||||
display.text(text, x, y + 1, -1, 1)
|
|
||||||
display.text(text, x + 1, y + 1, -1, 1)
|
|
||||||
|
|
||||||
display.set_pen(WHITE)
|
|
||||||
display.text(text, x, y, -1, 1)
|
|
||||||
|
|
||||||
|
|
||||||
def draw_page(cycle):
|
|
||||||
global user_icon
|
|
||||||
text_cycle = cycle % 1000
|
|
||||||
cycle = cycle % 4
|
|
||||||
# Clear the display
|
|
||||||
display.set_pen(15)
|
|
||||||
display.clear()
|
|
||||||
|
|
||||||
# Draw the page header
|
|
||||||
display.set_font("bitmap6")
|
|
||||||
|
|
||||||
if temperature is not None:
|
|
||||||
# Choose an appropriate icon based on the weather code
|
# Choose an appropriate icon based on the weather code
|
||||||
# Weather codes from https://open-meteo.com/en/docs
|
# Weather codes from https://open-meteo.com/en/docs
|
||||||
if user_icon is not None:
|
if weathercode in [71, 73, 75, 77, 85, 86]: # codes for snow
|
||||||
icons = ["icons/snow{0}.jpg".format(cycle + 1), "icons/rain{0}.jpg".format(cycle + 1), "icons/cloud{0}.jpg".format(cycle + 1), "icons/sun{0}.jpg".format(cycle + 1), "icons/storm{0}.jpg".format(cycle + 1)]
|
jpeg.open_file("icons/snow.jpg")
|
||||||
jpeg.open_file(icons[user_icon])
|
elif weathercode in [51, 53, 55, 56, 57, 61, 63, 65, 66, 67, 80, 81, 82]: # codes for rain
|
||||||
else:
|
jpeg.open_file("icons/rain.jpg")
|
||||||
if weathercode in [71, 73, 75, 77, 85, 86]: # codes for snow
|
elif weathercode in [1, 2, 3, 45, 48]: # codes for cloud
|
||||||
jpeg.open_file("icons/snow{0}.jpg".format(cycle + 1))
|
jpeg.open_file("icons/cloud.jpg")
|
||||||
elif weathercode in [51, 53, 55, 56, 57, 61, 63, 65, 66, 67, 80, 81, 82]: # codes for rain
|
elif weathercode in [0]: # codes for sun
|
||||||
jpeg.open_file("icons/rain{0}.jpg".format(cycle + 1))
|
jpeg.open_file("icons/sun.jpg")
|
||||||
elif weathercode in [1, 2, 3, 45, 48]: # codes for cloud
|
elif weathercode in [95, 96, 99]: # codes for storm
|
||||||
jpeg.open_file("icons/cloud{0}.jpg".format(cycle + 1))
|
jpeg.open_file("icons/storm.jpg")
|
||||||
elif weathercode in [0]: # codes for sun
|
|
||||||
jpeg.open_file("icons/sun{0}.jpg".format(cycle + 1))
|
|
||||||
elif weathercode in [95, 96, 99]: # codes for storm
|
|
||||||
jpeg.open_file("icons/storm{0}.jpg".format(cycle + 1))
|
|
||||||
jpeg.decode(0, 0, jpegdec.JPEG_SCALE_FULL)
|
jpeg.decode(0, 0, jpegdec.JPEG_SCALE_FULL)
|
||||||
display.set_pen(TEXT_COLOUR)
|
|
||||||
outline_text(WEATHER_TEXT, 16 - text_cycle, 0)
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
display.set_pen(0)
|
display.set_pen(RED)
|
||||||
display.set_pen(15)
|
display.text("ERR", 0, 0, WIDTH, 1)
|
||||||
display.text("Unable to display weather! Check your network settings in WIFI_CONFIG.py", 5, 65, WIDTH, 1)
|
|
||||||
|
|
||||||
su.update(display)
|
su.update(display)
|
||||||
|
time.sleep(UPDATE_INTERVAL * 60)
|
||||||
|
|
||||||
while 1:
|
|
||||||
|
|
||||||
get_data()
|
|
||||||
for count in range(500):
|
|
||||||
|
|
||||||
if su.is_pressed(StellarUnicorn.SWITCH_A):
|
|
||||||
user_icon = 0
|
|
||||||
if su.is_pressed(StellarUnicorn.SWITCH_B):
|
|
||||||
user_icon = 1
|
|
||||||
if su.is_pressed(StellarUnicorn.SWITCH_C):
|
|
||||||
user_icon = 2
|
|
||||||
if su.is_pressed(StellarUnicorn.SWITCH_D):
|
|
||||||
user_icon = 3
|
|
||||||
if su.is_pressed(StellarUnicorn.SWITCH_BRIGHTNESS_UP):
|
|
||||||
user_icon = 4
|
|
||||||
if su.is_pressed(StellarUnicorn.SWITCH_BRIGHTNESS_DOWN):
|
|
||||||
user_icon = None
|
|
||||||
draw_page(count)
|
|
||||||
time.sleep(0.2)
|
|
||||||
|
|