Stellar: update weather example
|
@ -155,7 +155,7 @@ Requires `logging.mpy` and `tinyweb` from [micropython/examples/common](../../ex
|
|||
|
||||
[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
|
||||
|
||||
|
|
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
|
||||
import WIFI_CONFIG
|
||||
from network_manager import NetworkManager
|
||||
import uasyncio as asyncio
|
||||
import uasyncio
|
||||
import urequests
|
||||
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!)
|
||||
LAT = 53.38609085276884
|
||||
LNG = -1.4239983439328177
|
||||
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
|
||||
WEATHER_TEXT = ''
|
||||
user_icon = None
|
||||
|
||||
# how often to poll the API, in minutes
|
||||
UPDATE_INTERVAL = 5
|
||||
|
||||
|
||||
def get_data():
|
||||
global WEATHER_TEXT, temperature, weathercode
|
||||
global temperature, weathercode
|
||||
print(f"Requesting URL: {URL}")
|
||||
r = urequests.get(URL)
|
||||
# open the json data
|
||||
j = r.json()
|
||||
print("Data obtained!")
|
||||
print(j)
|
||||
|
||||
# parse relevant data from JSON
|
||||
current = j["current_weather"]
|
||||
|
@ -33,8 +40,7 @@ def get_data():
|
|||
winddirection = calculate_bearing(current["winddirection"])
|
||||
weathercode = current["weathercode"]
|
||||
date, now = current["time"].split("T")
|
||||
WEATHER_TEXT = f"Temp: {temperature}°C Wind Speed: {windspeed}kmph Wind Direction: {winddirection} As of: {date}, {now}"
|
||||
print(WEATHER_TEXT)
|
||||
print(f"Temp: {temperature}°C Wind Speed: {windspeed}kmph Wind Direction: {winddirection} As of: {date}, {now}")
|
||||
r.close()
|
||||
|
||||
|
||||
|
@ -46,8 +52,14 @@ def calculate_bearing(d):
|
|||
|
||||
|
||||
def status_handler(mode, status, ip):
|
||||
global MESSAGE
|
||||
print("Network: {}".format(WIFI_CONFIG.SSID))
|
||||
# reports wifi connection status
|
||||
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
|
||||
|
@ -57,97 +69,39 @@ display = PicoGraphics(DISPLAY)
|
|||
WIDTH = StellarUnicorn.WIDTH
|
||||
HEIGHT = StellarUnicorn.HEIGHT
|
||||
|
||||
RED = display.create_pen(255, 0, 0)
|
||||
|
||||
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():
|
||||
# Setup wifi
|
||||
# set up wifi
|
||||
try:
|
||||
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
|
||||
asyncio.get_event_loop().run_until_complete(network_manager.client(WIFI_CONFIG.SSID, WIFI_CONFIG.PSK))
|
||||
while (not network_manager.isconnected()):
|
||||
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:
|
||||
while True:
|
||||
get_data()
|
||||
if weathercode is not None:
|
||||
# Choose an appropriate icon based on the weather code
|
||||
# Weather codes from https://open-meteo.com/en/docs
|
||||
if user_icon is not None:
|
||||
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[user_icon])
|
||||
else:
|
||||
if weathercode in [71, 73, 75, 77, 85, 86]: # codes for snow
|
||||
jpeg.open_file("icons/snow{0}.jpg".format(cycle + 1))
|
||||
elif weathercode in [51, 53, 55, 56, 57, 61, 63, 65, 66, 67, 80, 81, 82]: # codes for rain
|
||||
jpeg.open_file("icons/rain{0}.jpg".format(cycle + 1))
|
||||
elif weathercode in [1, 2, 3, 45, 48]: # codes for cloud
|
||||
jpeg.open_file("icons/cloud{0}.jpg".format(cycle + 1))
|
||||
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))
|
||||
if weathercode in [71, 73, 75, 77, 85, 86]: # codes for snow
|
||||
jpeg.open_file("icons/snow.jpg")
|
||||
elif weathercode in [51, 53, 55, 56, 57, 61, 63, 65, 66, 67, 80, 81, 82]: # codes for rain
|
||||
jpeg.open_file("icons/rain.jpg")
|
||||
elif weathercode in [1, 2, 3, 45, 48]: # codes for cloud
|
||||
jpeg.open_file("icons/cloud.jpg")
|
||||
elif weathercode in [0]: # codes for sun
|
||||
jpeg.open_file("icons/sun.jpg")
|
||||
elif weathercode in [95, 96, 99]: # codes for storm
|
||||
jpeg.open_file("icons/storm.jpg")
|
||||
jpeg.decode(0, 0, jpegdec.JPEG_SCALE_FULL)
|
||||
display.set_pen(TEXT_COLOUR)
|
||||
outline_text(WEATHER_TEXT, 16 - text_cycle, 0)
|
||||
|
||||
else:
|
||||
display.set_pen(0)
|
||||
display.set_pen(15)
|
||||
display.text("Unable to display weather! Check your network settings in WIFI_CONFIG.py", 5, 65, WIDTH, 1)
|
||||
display.set_pen(RED)
|
||||
display.text("ERR", 0, 0, WIDTH, 1)
|
||||
|
||||
su.update(display)
|
||||
|
||||
|
||||
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)
|
||||
time.sleep(UPDATE_INTERVAL * 60)
|
||||
|
|