diff --git a/micropython/examples/stellar_unicorn/README.md b/micropython/examples/stellar_unicorn/README.md index ec977769..231a7481 100644 --- a/micropython/examples/stellar_unicorn/README.md +++ b/micropython/examples/stellar_unicorn/README.md @@ -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 diff --git a/micropython/examples/stellar_unicorn/weather/icons/cloud.jpg b/micropython/examples/stellar_unicorn/weather/icons/cloud.jpg new file mode 100644 index 00000000..ba4a6aa6 Binary files /dev/null and b/micropython/examples/stellar_unicorn/weather/icons/cloud.jpg differ diff --git a/micropython/examples/stellar_unicorn/weather/icons/cloud1.jpg b/micropython/examples/stellar_unicorn/weather/icons/cloud1.jpg deleted file mode 100644 index 7c400e81..00000000 Binary files a/micropython/examples/stellar_unicorn/weather/icons/cloud1.jpg and /dev/null differ diff --git a/micropython/examples/stellar_unicorn/weather/icons/cloud2.jpg b/micropython/examples/stellar_unicorn/weather/icons/cloud2.jpg deleted file mode 100644 index d5f7b54b..00000000 Binary files a/micropython/examples/stellar_unicorn/weather/icons/cloud2.jpg and /dev/null differ diff --git a/micropython/examples/stellar_unicorn/weather/icons/cloud3.jpg b/micropython/examples/stellar_unicorn/weather/icons/cloud3.jpg deleted file mode 100644 index 9059882f..00000000 Binary files a/micropython/examples/stellar_unicorn/weather/icons/cloud3.jpg and /dev/null differ diff --git a/micropython/examples/stellar_unicorn/weather/icons/cloud4.jpg b/micropython/examples/stellar_unicorn/weather/icons/cloud4.jpg deleted file mode 100644 index 9059882f..00000000 Binary files a/micropython/examples/stellar_unicorn/weather/icons/cloud4.jpg and /dev/null differ diff --git a/micropython/examples/stellar_unicorn/weather/icons/rain.jpg b/micropython/examples/stellar_unicorn/weather/icons/rain.jpg new file mode 100644 index 00000000..85ea8436 Binary files /dev/null and b/micropython/examples/stellar_unicorn/weather/icons/rain.jpg differ diff --git a/micropython/examples/stellar_unicorn/weather/icons/rain1.jpg b/micropython/examples/stellar_unicorn/weather/icons/rain1.jpg deleted file mode 100644 index 9e676daf..00000000 Binary files a/micropython/examples/stellar_unicorn/weather/icons/rain1.jpg and /dev/null differ diff --git a/micropython/examples/stellar_unicorn/weather/icons/rain2.jpg b/micropython/examples/stellar_unicorn/weather/icons/rain2.jpg deleted file mode 100644 index 5fe470c7..00000000 Binary files a/micropython/examples/stellar_unicorn/weather/icons/rain2.jpg and /dev/null differ diff --git a/micropython/examples/stellar_unicorn/weather/icons/rain3.jpg b/micropython/examples/stellar_unicorn/weather/icons/rain3.jpg deleted file mode 100644 index 91be10a9..00000000 Binary files a/micropython/examples/stellar_unicorn/weather/icons/rain3.jpg and /dev/null differ diff --git a/micropython/examples/stellar_unicorn/weather/icons/rain4.jpg b/micropython/examples/stellar_unicorn/weather/icons/rain4.jpg deleted file mode 100644 index 9e676daf..00000000 Binary files a/micropython/examples/stellar_unicorn/weather/icons/rain4.jpg and /dev/null differ diff --git a/micropython/examples/stellar_unicorn/weather/icons/snow.jpg b/micropython/examples/stellar_unicorn/weather/icons/snow.jpg new file mode 100644 index 00000000..58fc521d Binary files /dev/null and b/micropython/examples/stellar_unicorn/weather/icons/snow.jpg differ diff --git a/micropython/examples/stellar_unicorn/weather/icons/snow1.jpg b/micropython/examples/stellar_unicorn/weather/icons/snow1.jpg deleted file mode 100644 index de769f95..00000000 Binary files a/micropython/examples/stellar_unicorn/weather/icons/snow1.jpg and /dev/null differ diff --git a/micropython/examples/stellar_unicorn/weather/icons/snow2.jpg b/micropython/examples/stellar_unicorn/weather/icons/snow2.jpg deleted file mode 100644 index 4e1b0c03..00000000 Binary files a/micropython/examples/stellar_unicorn/weather/icons/snow2.jpg and /dev/null differ diff --git a/micropython/examples/stellar_unicorn/weather/icons/snow3.jpg b/micropython/examples/stellar_unicorn/weather/icons/snow3.jpg deleted file mode 100644 index b85dc3b2..00000000 Binary files a/micropython/examples/stellar_unicorn/weather/icons/snow3.jpg and /dev/null differ diff --git a/micropython/examples/stellar_unicorn/weather/icons/snow4.jpg b/micropython/examples/stellar_unicorn/weather/icons/snow4.jpg deleted file mode 100644 index 8ae9bd6a..00000000 Binary files a/micropython/examples/stellar_unicorn/weather/icons/snow4.jpg and /dev/null differ diff --git a/micropython/examples/stellar_unicorn/weather/icons/storm.jpg b/micropython/examples/stellar_unicorn/weather/icons/storm.jpg new file mode 100644 index 00000000..28110bba Binary files /dev/null and b/micropython/examples/stellar_unicorn/weather/icons/storm.jpg differ diff --git a/micropython/examples/stellar_unicorn/weather/icons/storm1.jpg b/micropython/examples/stellar_unicorn/weather/icons/storm1.jpg deleted file mode 100644 index d7ac6251..00000000 Binary files a/micropython/examples/stellar_unicorn/weather/icons/storm1.jpg and /dev/null differ diff --git a/micropython/examples/stellar_unicorn/weather/icons/storm2.jpg b/micropython/examples/stellar_unicorn/weather/icons/storm2.jpg deleted file mode 100644 index 7ba0aaba..00000000 Binary files a/micropython/examples/stellar_unicorn/weather/icons/storm2.jpg and /dev/null differ diff --git a/micropython/examples/stellar_unicorn/weather/icons/storm3.jpg b/micropython/examples/stellar_unicorn/weather/icons/storm3.jpg deleted file mode 100644 index 6081f046..00000000 Binary files a/micropython/examples/stellar_unicorn/weather/icons/storm3.jpg and /dev/null differ diff --git a/micropython/examples/stellar_unicorn/weather/icons/storm4.jpg b/micropython/examples/stellar_unicorn/weather/icons/storm4.jpg deleted file mode 100644 index 98cdc59c..00000000 Binary files a/micropython/examples/stellar_unicorn/weather/icons/storm4.jpg and /dev/null differ diff --git a/micropython/examples/stellar_unicorn/weather/icons/sun.jpg b/micropython/examples/stellar_unicorn/weather/icons/sun.jpg new file mode 100644 index 00000000..78bbdc8b Binary files /dev/null and b/micropython/examples/stellar_unicorn/weather/icons/sun.jpg differ diff --git a/micropython/examples/stellar_unicorn/weather/icons/sun1.jpg b/micropython/examples/stellar_unicorn/weather/icons/sun1.jpg deleted file mode 100644 index b989b432..00000000 Binary files a/micropython/examples/stellar_unicorn/weather/icons/sun1.jpg and /dev/null differ diff --git a/micropython/examples/stellar_unicorn/weather/icons/sun2.jpg b/micropython/examples/stellar_unicorn/weather/icons/sun2.jpg deleted file mode 100644 index f6c78277..00000000 Binary files a/micropython/examples/stellar_unicorn/weather/icons/sun2.jpg and /dev/null differ diff --git a/micropython/examples/stellar_unicorn/weather/icons/sun3.jpg b/micropython/examples/stellar_unicorn/weather/icons/sun3.jpg deleted file mode 100644 index de34c7e9..00000000 Binary files a/micropython/examples/stellar_unicorn/weather/icons/sun3.jpg and /dev/null differ diff --git a/micropython/examples/stellar_unicorn/weather/icons/sun4.jpg b/micropython/examples/stellar_unicorn/weather/icons/sun4.jpg deleted file mode 100644 index 3d5a18c4..00000000 Binary files a/micropython/examples/stellar_unicorn/weather/icons/sun4.jpg and /dev/null differ diff --git a/micropython/examples/stellar_unicorn/weather/icons_sourcefile(doesn't need copying).svg b/micropython/examples/stellar_unicorn/weather/icons_sourcefile(doesn't need copying).svg deleted file mode 100644 index 8eedbcb0..00000000 --- a/micropython/examples/stellar_unicorn/weather/icons_sourcefile(doesn't need copying).svg +++ /dev/null @@ -1,1577 +0,0 @@ - - - - diff --git a/micropython/examples/stellar_unicorn/weather/weather.py b/micropython/examples/stellar_unicorn/weather/weather.py index 7cc00674..c36a7131 100644 --- a/micropython/examples/stellar_unicorn/weather/weather.py +++ b/micropython/examples/stellar_unicorn/weather/weather.py @@ -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)