The discharge of the Raspberry Pi Pico W brings with it an fascinating alternative. Up to now if we wished to attach a Raspberry Pi to the world, we would wish one of many bigger fashions. The Raspberry Pi Zero 2 W, and Raspberry Pi 4 have been typically pressed into information assortment duties. The Raspberry Pi 4 is a little bit of an influence hog, the Zero 2 W is a bit higher however nonetheless overkill for a easy info mission.
With the arrival of the Raspberry Pi Pico W we’ve a low energy, microcontroller with a reliable Wi-Fi chip, within the Pico kind issue and solely $6!
So the place will we begin? How will we get our Raspberry Pi Pico W on-line, and the place can we discover fascinating information to gather? Allow us to information you thru taking advantage of your $6 Raspberry Pi Pico W.
Getting the Raspberry Pi Pico W On-line
The Raspberry Pi Pico W comes with an Infineon CYW43439 2.4 GHz Wi-Fi chip and onboard antenna. This implies we get good Wi-Fi reception with out the necessity for plenty of wires. We’re utilizing the most recent MicroPython launch for the Pico W because it affords the best means to get on-line and do enjoyable initiatives.
1. Setup your Raspberry Pi Pico W by following our getting began information. You will have to put in MicroPython in your Pico W earlier than you possibly can proceed additional.
2. Open the Thonny editor to a clean doc.
3. Create an object known as SSID and in it retailer the SSID of your Wi-Fi entry level.
SSID = "YOUR WIFI AP"
4. Create an object known as PASSWORD and retailer your Wi-Fi password.
PASSWORD = "TRUSTNO1"
5. Save the file to the Raspberry Pi Pico W as secrets and techniques.py By storing our delicate particulars in a secrets and techniques file, we will freely share the mission code with associates, or on-line. Simply keep in mind to not share the secrets and techniques file too.
6. Click on on New File to create a brand new clean doc.
7. Import three modules of code, community, secrets and techniques and time. These three modules allow our Pico to hook up with a Wi-Fi community, use the information saved in secrets and techniques.py and so as to add a pause to the code.
import community
import secrets and techniques
import time
8. Create an object, wlan, to create a connection from our code to the Pico W wi-fi chip. We use this connection to problem instructions that may join and verify our Wi-Fi connection.
wlan = community.WLAN(community.STA_IF)
9. Activate the Raspberry Pi Pico W’s Wi-Fi.
wlan.energetic(True)
10. Hook up with your router utilizing the SSID and PASSWORD saved within the secrets and techniques.py file.
wlan.join(secrets and techniques.SSID, secrets and techniques.PASSWORD)
11. Print the connection standing to the Python shell. This can print True if linked, and False if the connection failed.
12. Click on Save after which choose “Raspberry Pi Pico”. Save the file as Wi-Fi.py to the Raspberry Pi Pico W.
13. Click on on Run to start out the code.
14. Look within the Python Shell for True or False. True means we’re linked.
Full Code Itemizing
import community
import secrets and techniques
import time
wlan = community.WLAN(community.STA_IF)
wlan.energetic(True)
wlan.join(secrets and techniques.SSID, secrets and techniques.PASSWORD)
print(wlan.isconnected())
Utilizing the Raspberry Pi Pico W With Exterior Information
Now that we’ve an Web connection, we are going to use it with publicly obtainable datasets to tug information from exterior sources and show it on the Pico W. For this instance we’re going to use Open Notify’s “How many individuals are in area proper now” dataset. This has the quantity and names of all of the astronauts at the moment on the Worldwide Area Station.
We’re going to adapt our earlier instance code, Wi-Fi.py.
1. Add a line after “import time” and import the urequests module. This module permits us to work with community requests equivalent to HTTP and JSON.
import urequests
2. After print(wlan.isconnected()) add a brand new line which creates an object “astronauts” after which makes use of urequests to get the knowledge in a JSON format. JavaScript Object Notation is an open normal file format which bears a hanging resemblance to Python’s Dictionary which makes use of keys (names) to retrieve values from the article.
astronauts = urequests.get("http://api.open-notify.org/astros.json").json()
3. Create an object, quantity, which can open the astronauts object, and search for the important thing ‘quantity’. The worth linked to that secret is then saved within the quantity object.
quantity = astronauts['number']
4. Create a for loop that may iterate for the variety of individuals on the Worldwide Area Station. This worth might change as astronauts come and go, so moderately than onerous coding a worth we use the stay information.
for i in vary(quantity):
5. Print the identify of every astronaut on the Worldwide Area Station utilizing a collection of keys that focus on the precise information. Our dictionary ‘astronauts’ has many keys, however we have an interest within the ‘individuals’, the worth of “i” will increment every time the loop goes spherical, and it selects every particular person from an inventory embedded within the dataset. We then use one other key, ‘identify’ to get the identify of that astronaut.
print(astronauts['people'][i]['name'])
6. Save the code and when prepared click on on Run to start out the code.
7. The names of all of the astronauts on the Worldwide Area Station will seem within the Python Shell. Be aware that “True” nonetheless seems, confirming that our Web connection is established.
Full Code Itemizing
import community
import secrets and techniques
import time
import urequests
wlan = community.WLAN(community.STA_IF)
wlan.energetic(True)
wlan.join(secrets and techniques.SSID, secrets and techniques.PASSWORD)
print(wlan.isconnected())
astronauts = urequests.get("http://api.open-notify.org/astros.json").json()
quantity = astronauts['number']
for i in vary(quantity):
print(astronauts['people'][i]['name'])