/ Weather_Display_Matrix / code.py
code.py
1 # SPDX-FileCopyrightText: 2020 John Park for Adafruit Industries 2 # 3 # SPDX-License-Identifier: MIT 4 5 # Matrix Weather display 6 # For Metro M4 Airlift with RGB Matrix shield, 64 x 32 RGB LED Matrix display 7 8 """ 9 This example queries the Open Weather Maps site API to find out the current 10 weather for your location... and display it on a screen! 11 if you can find something that spits out JSON data, we can display it 12 """ 13 import time 14 import board 15 import microcontroller 16 from digitalio import DigitalInOut, Direction, Pull 17 from adafruit_matrixportal.network import Network 18 from adafruit_matrixportal.matrix import Matrix 19 import openweather_graphics # pylint: disable=wrong-import-position 20 21 # Get wifi details and more from a secrets.py file 22 try: 23 from secrets import secrets 24 except ImportError: 25 print("WiFi secrets are kept in secrets.py, please add them there!") 26 raise 27 28 if hasattr(board, "D12"): 29 jumper = DigitalInOut(board.D12) 30 jumper.direction = Direction.INPUT 31 jumper.pull = Pull.UP 32 is_metric = jumper.value 33 elif hasattr(board, "BUTTON_DOWN") and hasattr(board, "BUTTON_UP"): 34 button_down = DigitalInOut(board.BUTTON_DOWN) 35 button_down.switch_to_input(pull=Pull.UP) 36 37 button_up = DigitalInOut(board.BUTTON_UP) 38 button_up.switch_to_input(pull=Pull.UP) 39 if not button_down.value: 40 print("Down Button Pressed") 41 microcontroller.nvm[0] = 1 42 elif not button_up.value: 43 print("Up Button Pressed") 44 microcontroller.nvm[0] = 0 45 print(microcontroller.nvm[0]) 46 is_metric = microcontroller.nvm[0] 47 else: 48 is_metric = False 49 50 if is_metric: 51 UNITS = "metric" # can pick 'imperial' or 'metric' as part of URL query 52 print("Jumper set to metric") 53 else: 54 UNITS = "imperial" 55 print("Jumper set to imperial") 56 57 # Use cityname, country code where countrycode is ISO3166 format. 58 # E.g. "New York, US" or "London, GB" 59 LOCATION = "Los Angeles, US" 60 print("Getting weather for {}".format(LOCATION)) 61 # Set up from where we'll be fetching data 62 DATA_SOURCE = ( 63 "http://api.openweathermap.org/data/2.5/weather?q=" + LOCATION + "&units=" + UNITS 64 ) 65 DATA_SOURCE += "&appid=" + secrets["openweather_token"] 66 # You'll need to get a token from openweather.org, looks like 'b6907d289e10d714a6e88b30761fae22' 67 # it goes in your secrets.py file on a line such as: 68 # 'openweather_token' : 'your_big_humongous_gigantor_token', 69 DATA_LOCATION = [] 70 SCROLL_HOLD_TIME = 0 # set this to hold each line before finishing scroll 71 72 # --- Display setup --- 73 matrix = Matrix() 74 network = Network(status_neopixel=board.NEOPIXEL, debug=True) 75 if UNITS in ("imperial", "metric"): 76 gfx = openweather_graphics.OpenWeather_Graphics( 77 matrix.display, am_pm=True, units=UNITS 78 ) 79 80 print("gfx loaded") 81 localtime_refresh = None 82 weather_refresh = None 83 while True: 84 # only query the online time once per hour (and on first run) 85 if (not localtime_refresh) or (time.monotonic() - localtime_refresh) > 3600: 86 try: 87 print("Getting time from internet!") 88 network.get_local_time() 89 localtime_refresh = time.monotonic() 90 except RuntimeError as e: 91 print("Some error occured, retrying! -", e) 92 continue 93 94 # only query the weather every 10 minutes (and on first run) 95 if (not weather_refresh) or (time.monotonic() - weather_refresh) > 600: 96 try: 97 value = network.fetch_data(DATA_SOURCE, json_path=(DATA_LOCATION,)) 98 print("Response is", value) 99 gfx.display_weather(value) 100 weather_refresh = time.monotonic() 101 except RuntimeError as e: 102 print("Some error occured, retrying! -", e) 103 continue 104 105 gfx.scroll_next_label() 106 # Pause between labels 107 time.sleep(SCROLL_HOLD_TIME)