Home ยป Get the latest Bitcoin Price in Micropython

Get the latest Bitcoin Price in Micropython

by 2b4a3pico71

In this example we will get the bitcoin price using micropython

We will use coingecko in this example, there are other apis that you can use

To test this out you can insert the following in your browser

https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd

You should see something like this

{"bitcoin":{"usd":59240}}

Table of Contents

Complete example

I used thonny for development

import network
import requests
from time import sleep

# Wi-Fi credentials
ssid = 'ssid here'
password = 'password here'

# Request URL
url = 'https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd'

def init_wifi(ssid, password):# Init Wi-Fi Interface
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    # Connect to your network
    wlan.connect(ssid, password)
    # Wait for Wi-Fi connection
    connection_timeout = 10
    while connection_timeout > 0:
        if wlan.status() >= 3:
            break
        connection_timeout -= 1
        print('Waiting for Wi-Fi connection...')
        sleep(1)
    # Check if connection is successful
    if wlan.status() != 3:
        return False
    else:
        print('Connection successful!')
        network_info = wlan.ifconfig()
        print('IP address:', network_info[0])
        return True

if init_wifi(ssid, password):
    try:
        # Make the request
        response = requests.get(url)
        #Print the response code
        print('Response code: ', response.status_code)
        
        # Get response content
        bitcoin = response.json()
        # Close the request
        response.close()
        
        # Print bitcoin price
        bitcoin_price = bitcoin['bitcoin']['usd']
        print('Bitcoin price (USD): ', bitcoin_price)

    except Exception as e:
        # Handle any exceptions during the request
        print('Error during request:', e)

First of all you need to change the ssid and password in this

# Wi-Fi credentials 
ssid = 'ssid here' 
password = 'password here'

The rest of code is commented but one thing you might want to change is the cryptocurrency and the target currency.

If you open the REPL window in thonny when you run this example you should see something like this

>>> %Run -c $EDITOR_CONTENT
Connection successful!
IP address: 192.168.1.122
Response code: 200
Bitcoin price (USD): 59590

In this case we will look at the price of ethereum in UK pounds

So the first part that needs changed is this

# Request URL
url = 'https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=gbp'

Then we change the following

 # Get response content
ethereum = response.json()
# Close the request
response.close()

# Print bitcoin price
ethereum_price = ethereum['ethereum']['gbp']
print('Ethereum price (GBP): ', ethereum_price)

If you open the REPL window in thonny when you run t his example you should see something like this

>>> %Run -c $EDITOR_CONTENT
Connection successful!
IP address: 192.168.1.187
Response code: 200
Ethereum price (GBP): 2100.14

You can add an OLED or LCD to display the price as a future project idea

Links

You can visit https://www.coingecko.com/en/api to see everything that is available

You may also like

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.