Get the price of Bitcoin using Micropython using a RP2040w

PICO-W

In this article we will display cryptocurrency prices using a RP2040W, we will use Micropython and the Thonny editor. The prices are retrieved from coinbase using their api

You can visit coinbase api and take a look at the various requests and responses

We are using this request

Get Spot Price

Get the current market price for bitcoin. This is usually somewhere in between the buy and sell price.

Note that exchange rates fluctuates so the price is only correct for seconds at the time.

You can also get historic prices with date parameter.

This endpoint doesn’t require authentication.

HTTP Request

GET https://api.coinbase.com/v2/prices/:currency_pair/spot

In our example we will get the price of bitcoin in UK punds

Code example

You need to fill in the ssid and password with your wifi name and password

import utime
import network
import urequests
from time import sleep

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

station.disconnect()
station.connect("ssid", "password")

#prints true if connected
station.isconnected()

#returns the IP address, subnet mask, gateway and DNS as output parameters
station.ifconfig()

while True:

    #get spot price of BTC from coinbase
    response = urequests.get('https://api.coinbase.com/v2/prices/BTC-GBP/spot')

    #print the response
    print(response.text)

    #parse the response
    parsed = response.json()

    crypto_px = float(parsed["data"]["amount"])

    #print the price
    print(parsed["data"]["amount"])
    response.close()
    utime.sleep(5)

 

Results

In the REPl window you will see something like this

>>> %Run -c $EDITOR_CONTENT
{"data":{"base":"BTC","currency":"GBP","amount":"17000.26"}}
17000.26
{"data":{"base":"BTC","currency":"GBP","amount":"17000.26"}}
17000.26

Related posts

A Raspberry Pi Pico and KY-006 Passive Buzzer

A Raspberry Pi Pico and KY-012 Active Buzzer

A Raspberry Pi Pico and KY-032 Obstacle Detector

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Read More