How To Hide Passwords in Your Code With Raspberry Pi Pico W
Getting your Raspberry Pi projects online is easy and cheap thanks to the $6 Raspberry Pi Pico W. With just 5 lines of code, you can connect your Raspberry Pi Pico W to the world, but sharing your code can expose you to some security. concern.
MicroPython code now includes Wi-Fi passwords, API keys, and bespoke URLs. So how do you reduce risk while maintaining data portability?
Creating MicroPython modules is the best way to keep secrets out of your project code. You can import the module like any other module and reference its contents in the same way.
In this how-to, you’ll create a Secrets module and use it with Open Weather to get current weather details for your home location. Project code can be easily shared with others without the fear of including personal information.
For this project you will need
Create secret file for credentials
The secrets module is actually a standard MicroPython file containing objects referencing Wi-Fi access points, Wi-Fi passwords, and Open Weather API keys.
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 equal sign assigns the value on the right to the object.
SSID = “YOUR WI-FI AP NAME”
Four. Create an object PASSWORD and assign the Wi-Fi access point password.
PASSWORD = “YOUR WI-FI PASSWORD”
Five. Create an object owm_api and assign your Open Weather API key. You can get a free API key at Sign up for 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) After importing the secrets file, print the SSID of your Wi-Fi access point. I basically created a Python module containing all the details I want to keep safe.
import secrets
print(secrets.SSID)
Using secrets in your project
The purpose of the secrets file is to keep the main project code free of files that may contain personal or secure information. By keeping your project code confidential, you can easily share it with others. In this part of the method, we will import the secret module and use it with the Open Weather API to get the weather for our current location.
1. Create a new empty file in Thonny.
2. Import three modules of code. Network allows Pico W to connect to Wi-Fi. Secrets are files full of secret information. urequests is a module that allows you to send requests to a remote device (in this case Open Weather’s API).I used the same module and got Data of astronauts on board the International Space Station.
import network
import secrets
import urequests
3. Create an object wlan and use it to create a connection from your code to the Pico W’s Wi-Fi chip and turn on the Wi-Fi chip.
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
Four. Use the secrets module to connect to your Wi-Fi access point using the SSID and PASSWORD objects.
wlan.connect(secrets.SSID,secrets.PASSWORD)
Five. Create an object weather to store the data returned from the Open Weather API. Use secrets.owm_api to inject your API key into the URL. This specially crafted URL will send a request to Open Weather for the location q=Blackpool,UK. You can change this location to your own location. You can also specify the units to use &units=metric (this 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. Save the current temperature in the object temperature. The data returned is in JSON format, which is similar to Python’s dictionary data type. Dictionaries store data using a key-value format.Key [“main”][‘temp’] Go to the main section of the data and the temperature value will be saved in the newly created temperature object.
temperature = weather["main"]['temp']
7. Save the current humidity to the corresponding object.
humidity = weather["main"]['humidity']
8. Save the current overall weather conditions in a weather object. This data is embedded deep inside a JSON object. The order is dictionary, list, dictionary object.list [0]indicates 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 Use a string format that drops the corresponding data into the statement.
print("The weather today is {} with a temperature of {} degrees Celsius and a humidity of {}%".format(weather, temperature, humidity))
Ten. Save the code on your Raspberry Pi Pico W as weather.py. [実行]Click to start the code. This code uses the Open Weather API to download the latest weather for your location and prints a sentence containing the information to your Python shell.
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))