Advertisement
aalh

Code.py Weatherportal

Oct 25th, 2021
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.07 KB | None | 0 0
  1. # Matrix Weather display
  2. # For Metro M4 Airlift with RGB Matrix shield, 64 x 32 RGB LED Matrix display
  3.  
  4. """
  5. This example queries the Open Weather Maps site API to find out the current
  6. weather for your location... and display it on a screen!
  7. if you can find something that spits out JSON data, we can display it
  8. """
  9. import time
  10. import board
  11. import microcontroller
  12. from digitalio import DigitalInOut, Direction, Pull
  13. from adafruit_matrixportal.network import Network
  14. from adafruit_matrixportal.matrix import Matrix
  15. import openweather_graphics  # pylint: disable=wrong-import-position
  16.  
  17. # pylint: disable=global-statement
  18. # Create I2C bus.
  19. TEMP_ARRAY = [0xFF0000, 0xFFA800, 0x0FFF00, 0x0000AA, 0x00D3FF]
  20.  
  21. # Get wifi details and more from a secrets.py file
  22. from secrets import secrets
  23.  
  24.  
  25.  
  26. UNITS = "imperial"
  27. print("Jumper set to imperial")
  28.  
  29. # Use cityname, country code where countrycode is ISO3166 format.
  30. # E.g. "New York, US" or "London, GB"
  31. LOCATION = "Athens, US"
  32. print("Getting weather for {}".format(LOCATION))
  33. # Set up from where we'll be fetching data
  34. DATA_SOURCE = (
  35.     "http://api.openweathermap.org/data/2.5/weather?q=" + LOCATION + "&units=" + UNITS
  36. )
  37. DATA_SOURCE += "&appid=" + secrets["openweather_token"]
  38. # You'll need to get a token from openweather.org, looks like 'b6907d289e10d714a6e88b30761fae22'
  39. # it goes in your secrets.py file on a line such as:
  40. # 'openweather_token' : 'your_big_humongous_gigantor_token',
  41. DATA_LOCATION = []
  42. SCROLL_HOLD_TIME = 0  # set this to hold each line before finishing scroll
  43. UNITS = "imperial"
  44. # --- Display setup ---
  45. matrix = Matrix()
  46. network = Network(status_neopixel=board.NEOPIXEL, debug=True)
  47. if UNITS == "imperial" or UNITS == "metric":
  48.     gfx = openweather_graphics.OpenWeather_Graphics(
  49.         matrix.display, am_pm=True, units=UNITS
  50.     )
  51.  
  52. print("gfx loaded")
  53. localtime_refresh = None
  54. weather_refresh = None
  55. while True:
  56.     # only query the online time once per hour (and on first run)
  57.     if (not localtime_refresh) or (time.monotonic() - localtime_refresh) > 3600:
  58.         try:
  59.             print("Getting time from internet!")
  60.             network.get_local_time()
  61.             localtime_refresh = time.monotonic()
  62.         except RuntimeError as e:
  63.             print("Some error occured, retrying! -", e)
  64.             continue
  65.  
  66.     # only query the weather every 10 minutes (and on first run)
  67.     if (not weather_refresh) or (time.monotonic() - weather_refresh) > 600:
  68.         try:
  69.             value = network.fetch_data(DATA_SOURCE, json_path=(DATA_LOCATION,))
  70.             print("Response is", value)
  71.             temperature = value["main"]["temp"]
  72.             print("ll ",temperature)
  73.  
  74.  
  75.             global LOCAL_oCOLOR
  76.            # global TEMP_ARRAY
  77.             LOCAL_oCOLOR = TEMP_ARRAY[int((95//temperature)/2)+2]
  78.  
  79.             gfx.display_weather(value)
  80.             weather_refresh = time.monotonic()
  81.         except RuntimeError as e:
  82.             print("Some error occured, retrying! -", e)
  83.             continue
  84.  
  85.     gfx.scroll_next_label()
  86.     # Pause between labels
  87.     time.sleep(SCROLL_HOLD_TIME)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement