Learning Rust by Building a Bitcoin Price Tracker

Tristan Deane

I'm learning Rust and tinkering with Raspberry Pis. So I figured I'd merge the two and create a home server that runs the Bitcoin Tracker program indefinitely.

👋🏽 Howdy! Just want to some read code? Here's the GitHub Repo.

Overview

I built a simple Rust project that fetches the latest Bitcoin price (in USD) from the CoinMarket Cap API every 30 minutes and stores it in an SQLite database. If the price hits my buying criteria, the program sends me a notification via Discord.

Motivation

I'm learning Rust and thought it would be fun to build something useful while getting to know the language. I've also been tinkering with Raspberry Pis recently, so I figured I'd merge the two and create a home server that runs the Bitcoin Tracker program indefinitely.

Hurdles I Overcame

1. Async Rust: A Bit of a Pain

Working with async Rust was definitely a challenge. Compared to languages like Go, Python, or JavaScript, it feels a little more difficult to get the hang of. But it's a learning experience, and I'm getting better at it!

2. Cross-compiling on my M1 Mac

I ran into a few roadblocks when trying to cross-compile the Rust program on my M1 Mac and transfer the binary to my Raspberry Pi 4. Here's what happened:

Command that resolved openssl issue:

cross build --release --target armv7-unknown-linux-gnueabihf

3. Compiling Directly on the Pi

To make this work, I installed Rust directly on the Pi, cloned the project from my Git repo, and ran the build command:

cargo build --release

That worked fine, and I could finally compile the project directly on the Pi.

4. Keeping the Program Running

To ensure the program keeps running even after I close the SSH session, I used nohup to run it in the background. The command was:

nohup ./target/release/btc_tracker > output.log 2>&1 &

This starts the program, redirects output to a log file, and gives me the process ID to manage it later. To stop the program, I can run:

pgrep -f "btc_tracker"
kill -9 <process_id>

If I wanted to get fancy, I could set up a systemd service to run the program as a daemon, but this was just a side project, so I went with the quick and dirty solution.

Future Ideas

Wrapping up

This project has been a fun way to learn Rust while also integrating a useful API and deploying it on a Raspberry Pi. The process of troubleshooting compilation issues, working with async Rust, and getting the program running on a Pi has been challenging but rewarding. For the moment, I’m putting the project on ice and moving onto something else. However, I’m looking forward to continuing to expand on the current version as I get more comfortable with Rust!

Feel free to check out the code on GitHub.