Build a Bitcoin traffic light with balenaCloud

Follow along and build your own as we hack a traffic light to make a cool Bitcoin value indicator for your desk or office using a Raspberry Pi and balenaCloud.

Follow along and build your own as we hack a traffic light to make a cool Bitcoin value indicator for your desk or office using a Raspberry Pi and balenaCloud.

Bitcoin (₿) was initially released at the beginning of January 2009 by an unknown person/group with the codename of “Satoshi Nakamoto“, and since the beginning of its existence, the price has been extremely volatile, going from just a few cents in the beginning to as high as $19,000 in late 2017. At the time of this post, the current BTC price is about a quarter of its all-time high, at about $4,875.

Because of this intense price fluctuation, we decided to build a fun Hack Friday project where we modified a traffic light to indicate if the current live Bitcoin price is higher or lower than the opening price for the day.

Follow along as we go through the entire project development process, from writing the code in Python to hacking the hardware to be controlled from a Raspberry Pi Zero W and managed by balenaCloud.

Prerequisites

This tutorial expects you to already have an account with balenaCloud and a Raspberry Pi provisioned. If you haven’t, we have an excellent set of getting started guides to get you up and running.

Software

Before touching the hardware, let’s focus on the software, understand what we want to achieve and the steps we need to take to get there.

Initially, the code can be separated into two parts, one that fetches the current Bitcoin price and runs the algorithm to decide if the price is higher or lower than the opening price, and another that controls the electronics in the traffic light via the Raspberry Pi GPIO pins.

To obtain the latest Bitcoin price we’ll use the API provided by CoinDesk. The data is provided in JSON format, which you can find more about here.

Docker Container

As balenaCloud operates using Docker containers, we must first create a Dockerfile with the container configuration. The container configuration allows us to specify exactly what software components we will need for our application.

Python Code

You can find the entire source code in Python on Github, but let’s dive in block-by-block to understand the code.

We are going to use a library called RPi.GPIO in order to be able to control the GPIO pins of the Raspberry Pi Zero from our code. You can see that library being imported here

The second part of the code above enables the GPIO pins to be controlled by referencing the numbers in the connector. We decided to use pins 22, 23 and 27 to set the colors of the Traffic Light, and so we need to set those pins as digital output pins.

Now we can write the part of the code that reads and parses the information provided by the Coindesk API. The function get_price(url, hdr) reads the price in USD and returns the value with two decimal places. The three set_color_ functions set the color of the traffic light by toggling the appropriate GPIO pins.

To make the terminal output a bit more fun, we also decided to change the color or the text displayed in the terminal to green or red.

Once we have the initial code ready, it’s time to think about the main logic of the program. The opening price is the Bitcoin price at midnight, or at the time the system goes live. After that we need to fetch the current price every minute and perform a comparison to check if it is higher or lower than the opening price. If higher, we print the text in green, otherwise it’s printed in red. The same logic goes into the output for the traffic light, if higher we turn the green light on, otherwise red. We’ve also included a 5 second transition period through the orange light to make it more obvious when the state changes.

If there’s anything in the code you don’t understand, or would like help modifying it to fit your own project, we’re always hanging out in our forums and would be happy to help out with any questions you have.


Hardware

With our Raspberry Pi Zero W up and running the container and software from the previous section, now it is time to get our hands dirty and work on the hardware.

Parts list:

The first thing we need to do is disassemble the lamp and remove the electronics. Looking at the back of the lamp, we can see that it uses 3x AA batteries in series, that means 4.5V.

WIth everything working disconnected from the main case, it is time for the most fun part, to reverse engineer the circuit and understand how it works. Surprisingly enough the circuit is very simple, giving us a lot of room to play with it.

The device name of the microcontroller on the left has faded-away making it hard to know exactly how it works, but by exploring the rest of the circuit, we realized it is a very simple circuit with one BJT NPN transistor to drive each led.

In the original circuit, everything runs on 4.5V, but with the RPi Zero we only have 5V and 3V3, so what we decided to do here is to control the base of the transistor with 3V3 (which is the voltage of the GPIO pins) and power the led from the 5V, making it slightly brighter than the original.

From here we connect the 5V and GND pins from the RPi to the circuit, desoldered the microcontroller from the PCB and solder three wires that will go to the pins 22, 23 and 27, as chosen at the beginning of the project.


All there is left to do now is to put the circuit back inside the traffic lamp (we used some hot-glue to make sure everything stays in place) and power up the board. Once it connects to balenaCloud and downloads the latest code it will turn on the LEDs.

And voila! Our Bitcoin traffic light is complete and can keep us up-to-date on the latest price fluctuations. Don’t forget to check Github for the full project source code at https://github.com/balena-io-playground/balena-btc-price-tracker.

If you have any questions or feedback, or have built your own version of this project, we’d love to see you over in our forums! Happy hacking!


Posted

in

Tags:

Notable Replies

  1. Hi. I know this project is old but it seems interesting. Would you have time to help me sort this out? I am trying to build > push and it fails. Here is the output:

    23:28:01 helium /Users/robertreid/balena-btc-price-tracker
    $ git remote add balena [[email protected]](mailto:[email protected]):gh_robertreid/balena-btc-price-tracker.git
    
    23:29:51 helium /Users/robertreid/balena-btc-price-tracker
    $ git push balena master
    The authenticity of host 'git.balena-cloud.com (3.224.47.37)' can't be established.
    ECDSA key fingerprint is SHA256:NfwmqnKId5cx1RWpebbEuuM87bCJbdyhzRnqFES9Nnw.
    Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
    Warning: Permanently added 'git.balena-cloud.com,3.224.47.37' (ECDSA) to the list of known hosts.
    Enumerating objects: 33, done.
    Counting objects: 100% (33/33), done.
    Delta compression using up to 4 threads.
    Compressing objects: 100% (32/32), done.
    Writing objects: 100% (33/33), 1.60 MiB 14.92 MiB/s, done.
    Total 33 (delta 6), reused 0 (delta 0)
    
    [Info] Starting build for balena-btc-price-tracker, user gh_robertreid
    [Info] Dashboard link: https://dashboard.balena-cloud.com/apps/1881134/devices
    [Info] Building on arm01
    [Info] Pulling previous images for caching purposes...
    [Success] Successfully pulled cache images
    [main] Step 1/6 : FROM resin/rpi-raspbian:latest
    [main] # Executing 1 build trigger
    [main] Using cache
    [main] ---> 5323cc9bb782
    [main] Step 2/6 : ENV INITSYSTEM on
    [main] Using cache
    [main] ---> e1373d87b128
    [main] Step 3/6 : RUN apt-get update && apt-get install -yq python3-dev python3-pip python3-rpi.gpio vim wget && apt-get clean && rm -rf /var/lib/apt/lists/*
    [main] ---> Running in a5f1e840dbac
    [main] Ign [http://archive.raspbian.org](http://archive.raspbian.org/) jessie InRelease
    [main] Get:1 [http://archive.raspberrypi.org](http://archive.raspberrypi.org/) jessie InRelease [22.9 kB]
    [main] Ign [http://archive.raspbian.org](http://archive.raspbian.org/) jessie Release.gpg
    [main] Ign [http://archive.raspbian.org](http://archive.raspbian.org/) jessie Release
    [main] Get:2 [http://archive.raspberrypi.org](http://archive.raspberrypi.org/) jessie/main armhf Packages [170 kB]
    [main] Err [http://archive.raspbian.org](http://archive.raspbian.org/) jessie/main armhf Packages
    [main] 404 Not Found
    [main] Err [http://archive.raspbian.org](http://archive.raspbian.org/) jessie/contrib armhf Packages
    [main] 404 Not Found
    [main] Err [http://archive.raspbian.org](http://archive.raspbian.org/) jessie/non-free armhf Packages
    [main] 404 Not Found
    [main] Err [http://archive.raspbian.org](http://archive.raspbian.org/) jessie/rpi armhf Packages
    [main] 404 Not Found
    [main] Err [http://archive.raspbian.org](http://archive.raspbian.org/) jessie/firmware armhf Packages
    [main] 404 Not Found
    [main] Fetched 193 kB in 5s (33.1 kB/s)
    [main] W: Failed to fetch http://archive.raspbian.org/raspbian/dists/jessie/main/binary-armhf/Packages 404 Not Found
    [main] W: Failed to fetch http://archive.raspbian.org/raspbian/dists/jessie/contrib/binary-armhf/Packages 404 Not Found
    [main] W: Failed to fetch http://archive.raspbian.org/raspbian/dists/jessie/non-free/binary-armhf/Packages 404 Not Found
    [main] W: Failed to fetch http://archive.raspbian.org/raspbian/dists/jessie/rpi/binary-armhf/Packages 404 Not Found
    [main] W: Failed to fetch http://archive.raspbian.org/raspbian/dists/jessie/firmware/binary-armhf/Packages 404 Not Found
    [main] E: Some index files failed to download. They have been ignored, or old ones used instead.
    [main]
    [main] Removing intermediate container a5f1e840dbac
    [main] The command '/bin/sh -c apt-get update && apt-get install -yq python3-dev python3-pip python3-rpi.gpio vim wget && apt-get clean && rm -rf /var/lib/apt/lists/ <em>' returned a non-zero code: 100
    [Info] Uploading images
    [Success] Successfully uploaded images
    [Error] Some services failed to build:
    [Error] Service: main
    [Error] Error: The command '/bin/sh -c apt-get update && apt-get install -yq python3-dev python3-pip python3-rpi.gpio vim wget && apt-get clean && rm -rf /var/lib/apt/lists/</em> ' returned a non-zero code: 100
    [Info] Built on arm01
    [Error] Not deploying release.
    
    remote: error: hook declined to update refs/heads/master
    To git.balena-cloud.com:gh_robertreid/balena-btc-price-tracker.git
    ! [remote rejected] master -> master (hook declined)
    error: failed to push some refs to '[[email protected]](mailto:[email protected]):gh_robertreid/balena-btc-price-tracker.git'
    
  2. Avatar for mpous mpous says:

    Hey @robertreid looks like some of these libraries on the project are deprecated (3 years old). Let me take a look and try to ressurect this during the week.

    I see your issue and feel free to PR the project as well :slight_smile:

Continue the discussion at forums.balena.io

Participants

Avatar for andrewnhem Avatar for mpous Avatar for robertreid