Linux Format

Convert currency

Work with remote data using an API and the Pi Pico W to make a live currency converter.

-

In this project, we’re creating a simple app to get the latest exchange rates for the euro and US dollar against the UK pound. The Raspberry Pi Pico W comes with MicroPytho­n, a version of Python 3 for microcontr­ollers. It is regularly updated and new versions can be found at https://bit. ly/lxf298upyt­hon. It’s a good idea to update MicroPytho­n and ensure you get the version for the Pico W, as it has extra modules for Wi-Fi connectivi­ty.

Download and install Thonny for your OS and connect your Pico to the computer. Open Thonny, go to Tools > Options and click on the Interprete­r tab. Set the interprete­r to be MicroPytho­n (Raspberry Pi Pico) and then set the serial/COM port to auto-detect the Pico W. Thonny should now detect the Pico W and the Python shell should confirm this.

Start the project code in a blank document. The first task is to import libraries of prewritten code – modules, in Python parlance. These modules enable our Pico W to connect to a network and make requests to remote machines over the network:

import network import urequests

Next create an object, wlan. This object represents a connection between the code and the Pico W’s Wi-Fi chip. You need to set the connection to active to enable the Wi-Fi chip:

wlan = network.WLAN(network.STA_IF) wlan.active(True)

Connect the Pico W to your Wi-Fi access point. Change YOUR SSID and YOUR PASSWORD to match those of your router and keep the quotation marks. Print the connection status to the Python shell. True means we are connected, False we are not:

wlan.connect(“YOUR SSID”, “YOUR PASSWORD”) print(wlan.isconnecte­d())

The exchange rate data is stored in an online resource that uses JSON, an open standard file format. By creating an object, gbp, we can make a network request for the data in JSON format by getting the data from the URL:

gbp = urequests.get(“https://api.exchangera­te-api.com/v4/latest/GBP”).json()

Data is stored inside the JSON object as a series of keys and values. By giving the name of a key, JSON gives us the value. So, using USD as a key, we return the value of the pound to the US dollar and store it in a variable, dollar. The same applies to the value of the pound to the euro. dollar = gbp[“rates”][“USD”] euro = gbp[“rates”][“EUR”]

Print the exchange rate data and mix it into a sentence so it has context. Here we explain the current value of the pound to the US dollar and the euro. The variables (dollar and euro) are appended to the end of the sentences. The comma is used to add a space: print(“The current value of the UK Pound to one US

Dollar is £”,dollar) print(“The current value of the UK Pound to one Euro is £”,euro)

Click File > Save and save the code as Pico_API.py to the Pi Pico W. Click on the run button (green play button) to start the code. In the Python shell we see either True or False appear. True means we are connected to Wi-Fi. The next two lines we see are the current exchange rates for the dollar and euro. We could mix the code from the Raspberry Pi Pico GPIO tutorial (opposite) into this project to create a tool that fetches the latest data at a button press. That is the beauty and power of MicroPytho­n and the Pi Pico W.

 ?? ?? The only outward difference between the Pico and Pico W is the large silver square in which the Wi-Fi chip resides.
The only outward difference between the Pico and Pico W is the large silver square in which the Wi-Fi chip resides.
 ?? ??

Newspapers in English

Newspapers from Australia