Getting your Raspberry Pi mission on-line is now cheaper and simpler due to the $6 Raspberry Pi Pico W. It solely takes 5 traces of code to attach your Raspberry Pi Pico W to the world, however sharing your code can depart you open to some safety issues.
Your MicroPython code now accommodates your Wi-Fi password, API keys and bespoke URLs. So how will we mitigate the chance whereas retaining our information transportable?
Making a MicroPython module is the easiest way to maintain your secrets and techniques out of your mission code. We are able to import the module similar to some other module, and reference its contents in the identical method.
On this how-to, we are going to create a secrets and techniques module and use it, together with Open Climate to get the present climate particulars for our house location. The mission code may be simply shared with others, with out concern of together with any private info.
For this mission you will want
Making a Secrets and techniques File for Credentials
The secrets and techniques module is admittedly an ordinary MicroPython file that accommodates objects that reference our Wi-Fi entry level, Wi-Fi password and our Open Climate API key.
1. Observe this information to setup your Raspberry Pi Pico W. Observe the steps till “Find out how to blink an LED”.
2. Create a brand new clean file.
3. Create an object SSID and assign it the identify of your Wi-Fi entry level. The equals signal will assign the worth to the precise, into the thing.
SSID = “YOUR WI-FI AP NAME”
4. Create an object PASSWORD and assign it the password in your Wi-Fi entry level.
PASSWORD = “YOUR WI-FI PASSWORD”
5. Create an object, owm_api and assign it your Open Climate API key. You may get a free API key by signing as much as Open Climate.
6. Save the file to your Raspberry Pi Pico W as secrets and techniques.py.
7. Within the Python shell (backside of Thonny’s window) import the secrets and techniques file after which print the SSID of your Wi-Fi entry level. Primarily now we have simply made a Python module that accommodates all the particulars that we want to preserve secure.
import secrets and techniques
print(secrets and techniques.SSID)
Utilizing Secrets and techniques in a Undertaking
The aim of the secrets and techniques file is to maintain our essential mission code freed from any recordsdata which will comprise private / safe info. By retaining the mission code freed from delicate info, we are able to simply share it with others.On this a part of the how one can we will import the secrets and techniques module and use it with the Open Climate API to get the climate for our location.
1. In Thonny create a brand new clean file.
2. Import three modules of code. Community permits our Pico W to connect with Wi-Fi, Secrets and techniques is our file filled with secret info, urequests is a module that allows us to make requests to distant units, on this case Open Climate’s API. We used the identical module to get information on the astronauts onboard the Worldwide House Station.
import community
import secrets and techniques
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 flip the Wi-Fi chip on.
wlan = community.WLAN(community.STA_IF)
wlan.lively(True)
4. Utilizing the secrets and techniques module, use the SSID and PASSWORD objects to connect with your Wi-Fi entry level.
wlan.join(secrets and techniques.SSID,secrets and techniques.PASSWORD)
5. Create an object, climate, to retailer the returned information from the Open Climate API. Use secrets and techniques.owm_api to insert your API key into the URL. This specifically created URL sends a request to Open Climate for our location q=Blackpool,UK, which may be modified to your personal location. We are able to additionally specify the models used &models=metric (which may be modified to imperial).
climate = urequests.get("http://api.openweathermap.org/information/2.5/climate?q=Blackpool,UK&models=metric&appid="+(secrets and techniques.owm_api)).json()
6. Retailer the present temperature in an object, temperature. The returned information is in a JSON format, which is nearly an identical to Python’s dictionary datatype. Dictionaries use a key, worth format to retailer information. The keys [“main”][‘temp’] will take us to the principle part of information, after which the temperature worth is saved to our newly created temperature object.
temperature = climate["main"]['temp']
7. Retailer the present humidity in a corresponding object.
humidity = climate["main"]['humidity']
8. Retailer the present total climate situation within the climate object. This information is buried just a little deeper within the JSON object. It’s a dictionary, then listing, then dictionary object. The listing [0], identifies that we’re utilizing the primary merchandise within the listing, as Python begins counting from zero.
climate = climate["weather"][0]["main"]
9. Print the returned information utilizing string formatting that may drop the corresponding information into the sentence.
print("The climate at the moment is {} with a temperature of {} levels Celsius and a humidity of {}%".format(climate, temperature, humidity))
10. Save the code to the Raspberry Pi Pico W as climate.py. Click on run to begin the code. The code will use the Open Climate API, obtain the newest climate in your location, after which print a sentence containing the data to the Python Shell.
Full Code Itemizing
import community
import secrets and techniques
import urequests
wlan = community.WLAN(community.STA_IF)
wlan.lively(True)
wlan.join(secrets and techniques.SSID,secrets and techniques.PASSWORD)
climate = urequests.get("http://api.openweathermap.org/information/2.5/climate?q=Blackpool,UK&models=metric&appid="+(secrets and techniques.owm_api)).json()
temperature = climate["main"]['temp']
humidity = climate["main"]['humidity']
climate = climate["weather"][0]["main"]
print("The climate at the moment is {} with a temperature of {} levels Celsius and a humidity of {}%".format(climate, temperature, humidity))