How To Hide Passwords in Your Code With Raspberry Pi Pico W
Getting your Raspberry Pi project online is now cheaper and easier thanks to the $6 Raspberry Pi Pico W. It only takes five lines of code to get your Raspberry Pi Pico W connected to the world, but sharing your code can open you up to a few security concerns.
Your MicroPython code now includes your WiFi password, API key, and custom URLs. So how can we mitigate risk while keeping our data portable?
Creating a MicroPython module is the best way to keep your secrets out of your project code. We can import the module like any other module and refer to its contents in the same way.
In this guide, we’ll create a Secrets module and use it with Open Weather to get the current weather details for our home location. The project code can be easily shared with others without fear of providing personal information.
For this project you will need
Creating a secrets file for credentials
The Secrets module is actually a standard MicroPython file that contains objects that point to our Wi-Fi access point, Wi-Fi password, and our Open Weather API key.
1. Follow this guide to set up your Raspberry Pi Pico W. Follow the steps up to “How to blink an LED”.
2. Create a new empty file.
3. Create an object SSID and assign it the name of your Wi-Fi access point. The equals sign assigns the value on the right to the object.
SSID = “YOUR WI-FI AP NAME”
4. Create a PASSWORD object and assign it the password for your wireless access point.
PASSWORD = “YOUR WI-FI PASSWORD”
5. Create an object, owm_api, and assign it your Open Weather API key. You can get a free API key by Register with Open Weather.
6. Save the file as secrets.py on your Raspberry Pi Pico W.
7. In the Python shell (bottom of Thonny’s window) Import the secret file and then print out the SSID of your Wi-Fi access point. Essentially, we’ve just created a Python module that contains all the details we want to protect.
import secrets
print(secrets.SSID)
Use secrets in a project
The purpose of the secret file is to keep our main project code free from files that may contain personal/secure information. By keeping the project code free of sensitive information, we can easily share it with others. In this part of the tutorial, we’ll import the Secrets module and use it with the Open Weather API to get the weather for our location.
1. Create a new empty file in Thonny.
2. Import three code modules. Network allows our Pico W to connect to Wi-Fi, Secrets is our file full of secret information, urequests is a module that allows us to make requests to remote devices, in this case Open Weather’s API. We used the same module to get Data on the astronauts on board the International Space Station.
import network
import secrets
import urequests
3. Create an object, wlan, and use it to connect from our code to the Wi-Fi chip on the Pico W, then turn on the Wi-Fi chip.
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
4. Using the Secrets module, use the SSID and PASSWORD objects to connect to your Wi-Fi access point.
wlan.connect(secrets.SSID,secrets.PASSWORD)
5. Create an object Weather to store the returned data from the Open Weather API. Use secrets.owm_api to include your API key in the URL. This specially crafted URL will send a request to Open Weather for our location q=Blackpool, UK, which can be changed to your own location. We can also specify the units used &units=metric (which can be changed to imperial).
weather = urequests.get("http://api.openweathermap.org/data/2.5/weather?q=Blackpool,UK&units=metric&appid="+(secrets.owm_api)).json()
6. Store the current temperature in an object, temperature. The returned data is in a JSON format, which is almost identical to Python’s dictionary data type. Dictionaries use a key-value format to store data. The button’s [“main”][‘temp’] brings us to the main area of data, and then the temperature value is stored in our newly created temperature object.
temperature = weather["main"]['temp']
7. Save the current humidity in a corresponding object.
humidity = weather["main"]['humidity']
8th. Save the current overall weather situation in the weather object. This data is buried a little deeper in the JSON object. It’s a dictionary, then a list, then a dictionary object. The list [0]indicates that we’re using the first item in the list, since Python starts counting from zero.
weather = weather["weather"][0]["main"]
9. Print the returned data Use string formatting that puts the appropriate data in the set.
print("The weather today is {} with a temperature of {} degrees Celsius and a humidity of {}%".format(weather, temperature, humidity))
10 Save the code on the Raspberry Pi Pico W as weather.py. Click Run to start the code. The code uses the Open Weather API, downloads the latest weather for your location, and then outputs a sentence containing the information to the Python shell.
Complete code list
import network
import secrets
import urequests
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(secrets.SSID,secrets.PASSWORD)
weather = urequests.get("http://api.openweathermap.org/data/2.5/weather?q=Blackpool,UK&units=metric&appid="+(secrets.owm_api)).json()
temperature = weather["main"]['temp']
humidity = weather["main"]['humidity']
weather = weather["weather"][0]["main"]
print("The weather today is {} with a temperature of {} degrees Celsius and a humidity of {}%".format(weather, temperature, humidity))