In this example, you'll use the OpenWeatherMap API to receive and parse a JSON feed of your location's weather.
You'll need to register for an account with OpenWeatherMap and get your API key. Go to this link and register for a free account. Once registered, you'll get an email containing your API key. This key will be added to your settings.toml file as the 'openweather_token'.
Code the JSON Feed Test
Once you've finished setting up your Pico W with CircuitPython, you can access the code and necessary libraries by downloading the Project Bundle.
To do this, click on the Download Project Bundle button in the window below. It will download as a zipped folder.
# SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries # # SPDX-License-Identifier: MIT import time import os import ssl import wifi import socketpool import microcontroller import adafruit_requests wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD')) # Use cityname, country code where countrycode is ISO3166 format. # E.g. "New York, US" or "London, GB" location = "Manhattan, US" # openweathermap URL, brings in your location & your token url = "http://api.openweathermap.org/data/2.5/weather?q="+location url += "&appid="+os.getenv('openweather_token') pool = socketpool.SocketPool(wifi.radio) requests = adafruit_requests.Session(pool, ssl.create_default_context()) while True: try: # pings openweather response = requests.get(url) # packs the response into a JSON response_as_json = response.json() print() # prints the entire JSON print(response_as_json) # gets location name place = response_as_json['name'] # gets weather type (clouds, sun, etc) weather = response_as_json['weather'][0]['main'] # gets humidity % humidity = response_as_json['main']['humidity'] # gets air pressure in hPa pressure = response_as_json['main']['pressure'] # gets temp in kelvin temperature = response_as_json['main']['temp'] # converts temp from kelvin to F converted_temp = (temperature - 273.15) * 9/5 + 32 # converts temp from kelvin to C # converted_temp = temperature - 273.15 # prints out weather data formatted nicely as pulled from JSON print() print("The current weather in %s is:" % place) print(weather) print("%s°F" % converted_temp) print("%s%% Humidity" % humidity) print("%s hPa" % pressure) # delay for 5 minutes time.sleep(300) # pylint: disable=broad-except except Exception as e: print("Error:\n", str(e)) print("Resetting microcontroller in 10 seconds") time.sleep(10) microcontroller.reset()
Upload the Code and Libraries to the Pico W
After downloading the Project Bundle, plug your Pico W into the computer's USB port with a known good USB data+power cable. You should see a new flash drive appear in the computer's File Explorer or Finder (depending on your operating system) called CIRCUITPY. Unzip the folder and copy the following items to the Pico W's CIRCUITPY drive.
- lib folder
- code.py
Your Pico W CIRCUITPY drive should look like this after copying the lib folder and the code.py file.
Add Your settings.toml File
Remember to add your settings.toml file as described in the Create Your settings.toml File page earlier in the guide. You'll need to include your WIFI_SSID
, WIFI_PASSWORD
and openweather_token
in the file.
CIRCUITPY_WIFI_SSID = "your-ssid-here" CIRCUITPY_WIFI_PASSWORD = "your-ssid-password-here" openweather_token = "your-openweather-token-here"
Run code.py
Once everything is saved to the CIRCUITPY drive, connect to the serial console to see the data printed out!
The code makes a request
to the OpenWeatherMap API and receives a JSON feed of your location's weather. That JSON output is printed to the REPL. Then, the location, weather, humidity, pressure and temperature converted to Fahrenheit is printed to the REPL. This call is repeated every five minutes.
Text editor powered by tinymce.