Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Matrix Weather display
- # For Metro M4 Airlift with RGB Matrix shield, 64 x 32 RGB LED Matrix display
- """
- This example queries the Open Weather Maps site API to find out the current
- weather for your location... and display it on a screen!
- if you can find something that spits out JSON data, we can display it
- """
- import time
- import board
- import microcontroller
- from digitalio import DigitalInOut, Direction, Pull
- from adafruit_matrixportal.network import Network
- from adafruit_matrixportal.matrix import Matrix
- import openweather_graphics # pylint: disable=wrong-import-position
- # pylint: disable=global-statement
- # Create I2C bus.
- TEMP_ARRAY = [0xFF0000, 0xFFA800, 0x0FFF00, 0x0000AA, 0x00D3FF]
- # Get wifi details and more from a secrets.py file
- from secrets import secrets
- UNITS = "imperial"
- print("Jumper set to imperial")
- # Use cityname, country code where countrycode is ISO3166 format.
- # E.g. "New York, US" or "London, GB"
- LOCATION = "Athens, US"
- print("Getting weather for {}".format(LOCATION))
- # Set up from where we'll be fetching data
- DATA_SOURCE = (
- "http://api.openweathermap.org/data/2.5/weather?q=" + LOCATION + "&units=" + UNITS
- )
- DATA_SOURCE += "&appid=" + secrets["openweather_token"]
- # You'll need to get a token from openweather.org, looks like 'b6907d289e10d714a6e88b30761fae22'
- # it goes in your secrets.py file on a line such as:
- # 'openweather_token' : 'your_big_humongous_gigantor_token',
- DATA_LOCATION = []
- SCROLL_HOLD_TIME = 0 # set this to hold each line before finishing scroll
- UNITS = "imperial"
- # --- Display setup ---
- matrix = Matrix()
- network = Network(status_neopixel=board.NEOPIXEL, debug=True)
- if UNITS == "imperial" or UNITS == "metric":
- gfx = openweather_graphics.OpenWeather_Graphics(
- matrix.display, am_pm=True, units=UNITS
- )
- print("gfx loaded")
- localtime_refresh = None
- weather_refresh = None
- while True:
- # only query the online time once per hour (and on first run)
- if (not localtime_refresh) or (time.monotonic() - localtime_refresh) > 3600:
- try:
- print("Getting time from internet!")
- network.get_local_time()
- localtime_refresh = time.monotonic()
- except RuntimeError as e:
- print("Some error occured, retrying! -", e)
- continue
- # only query the weather every 10 minutes (and on first run)
- if (not weather_refresh) or (time.monotonic() - weather_refresh) > 600:
- try:
- value = network.fetch_data(DATA_SOURCE, json_path=(DATA_LOCATION,))
- print("Response is", value)
- temperature = value["main"]["temp"]
- print("ll ",temperature)
- global LOCAL_oCOLOR
- # global TEMP_ARRAY
- LOCAL_oCOLOR = TEMP_ARRAY[int((95//temperature)/2)+2]
- gfx.display_weather(value)
- weather_refresh = time.monotonic()
- except RuntimeError as e:
- print("Some error occured, retrying! -", e)
- continue
- gfx.scroll_next_label()
- # Pause between labels
- time.sleep(SCROLL_HOLD_TIME)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement