How To Hide Passwords in Your Code With Raspberry Pi Pico W

Hide Passwords With Raspberry Pi Pico W
(Image credit: Tom's Hardware)

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 connect your Raspberry Pi Pico W to the world, but sharing your code can leave you open to a few security concerns.

Your MicroPython code now contains your Wi-Fi password, API keys and bespoke URLs. So how do we mitigate the 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 just like any other module, and reference its contents in the same manner. 

In this how-to, we will create a secrets module and use it, along 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 including any personal information.

For this project you will need

Creating a Secrets File for Credentials

The secrets module is really a standard MicroPython file that contains objects that reference our Wi-Fi access point, Wi-Fi password and our Open Weather API key.

1. Follow this guide to setup your Raspberry Pi Pico W. Follow the steps until “How to blink an LED”.

2. Create a new blank file.

3. Create an object SSID and assign it the name of your Wi-Fi access point. The equals sign will assign the value to the right, into the object.

SSID = “YOUR WI-FI AP NAME”

4. Create an object PASSWORD and assign it the password for your Wi-Fi 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 signing up to Open Weather.

(Image credit: Tom's Hardware)

6. Save the file to your Raspberry Pi Pico W as secrets.py.

(Image credit: Tom's Hardware)

7. In the Python shell (bottom of Thonny’s window) import the secrets file and then print the SSID of your Wi-Fi access point. Essentially we have just made a Python module that contains all of the details that we wish to keep safe.

import secrets
print(secrets.SSID)

(Image credit: Tom's Hardware)

Using Secrets in a Project

The purpose of the secrets file is to keep our main project code free of any 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 how to we shall import the secrets module and use it with the Open Weather API to get the weather for our location.

1. In Thonny create a new blank file.

2. Import three modules of code. Network enables our Pico W to connect to Wi-Fi, Secrets is our file full of secret information, urequests is a module that enables 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 onboard the International Space Station.

import network
import secrets
import urequests

3. Create an object, wlan, and use it to create a connection from our code to the Wi-Fi chip on the Pico W, then turn the Wi-Fi chip on.

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 insert your API key into the URL. This specially created URL sends 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 datatype. Dictionaries use a key, value format to store data. The keys ["main"]['temp'] will take us to the main section of data, and then the temperature value is saved to our newly created temperature object.

temperature = weather["main"]['temp']

Using a tool such as https://jsonformatter.curiousconcept.com/ we can visualize the weather data structure. (Image credit: Tom's Hardware)

7. Store the current humidity in a corresponding object.

humidity = weather["main"]['humidity']

8. Store the current overall weather condition in the weather object. This data is buried a little deeper in the JSON object. It is a dictionary, then list, then dictionary object. The list [0], identifies that we are using the first item in the list, as Python starts counting from zero.

weather = weather["weather"][0]["main"]

9. Print the returned data using string formatting that will drop the corresponding data into the sentence.

print("The weather today is {} with a temperature of {} degrees Celsius and a humidity of {}%".format(weather, temperature, humidity))

10. Save the code to the Raspberry Pi Pico W as weather.py. Click run to start the code. The code will use the Open Weather API, download the latest weather for your location, and then print a sentence containing the information to the Python Shell.

(Image credit: Tom's Hardware)

Complete Code Listing

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))
Les Pounder

Les Pounder is an associate editor at Tom's Hardware. He is a creative technologist and for seven years has created projects to educate and inspire minds both young and old. He has worked with the Raspberry Pi Foundation to write and deliver their teacher training program "Picademy".