import network import urequests from time import sleep wlan = network.WLAN(network.STA_IF) # If the network isn't already connected, try to connect if not wlan.isconnected(): wlan.active(True) # Try to connect to Tufts_Wireless: ssid = "Tufts_Wireless" print("Connecting to {}...".format(ssid)) wlan.connect(ssid) while not wlan.isconnected(): sleep(1) print('.') print("Connected!") print("IP address:", wlan.ifconfig()[0]) # Grab the current weather for Medford # You can do a lookup with lat/lon coordinates to figure out which grid cell we're in, # e.g., https://api.weather.gov/points/42.4066,-71.1193 # For Tufts, we're at grid coordinates 68,78 for the Boston (BOX) station # The NOAA API requires that we identify ourselves with a "User-Agent" string. # We can use anything, but it has to be specified or our request will be rejected. headers = {"User-Agent":"tuftsen1-test"} # Now make the request, passing along the headers with the User-Agent string. response = urequests.get("https://api.weather.gov/gridpoints/BOX/68,78/forecast", headers=headers) blob = response.json() # The response contains a deep nested data structure, so we'll need to use some indexing to # pull out the bits we want: forecast = blob["properties"]["periods"] # Now `forecast` contains a list of forecast periods we could examine # Let's print each one out in turn: for f in forecast: # This loop will run once for every item in the list `forecast`, and `f` will # be given the value of that item. print("{}:".format(f["name"])) print(" {}".format(f["detailedForecast"]))