Arduino Makers! Now you are able to build machines that accept payments in cryptocurrencies. At Elkrem, we made a simple software Arduino library called Koyn, to easily build crypto-enabled machines starting with Bitcoin. The usage of cryptocurrencies are much easier and faster than adding any other payment gateway to a machine.
Koyn Arduino Library
Instead of just relying on web API’s like most of the current Arduino + Bitcoin hardware projects do, Koyn is a fully decentralized and trustless Bitcoin light client (aka SPV) implementation for the Arduino platform based on Electrum protocol .
With Koyn, your hardware is running a Bitcoin light client, and in this tutorial I will explain how to use the library, and what are the hardware and software requirements to start building your first decentralized hardware project. But before we get into building projects, I will explain a little about light Bitcoin clients.
Light Bitcoin Clients
As stated on Koyn’s GitHub repo
“Light Bitcoin clients (nodes) don’t store the full transaction history (blockchain) of the network, unlike full clients. They only download the headers (80 bytes every ~10 minutes) of the created blocks to be able to verify that a particular transaction was indeed included and accepted by the network. The concept is often referred to by the term SPV (Simplified Payment Verification).
There are two popular protocols for SPV clients. Bitcoin Core and Electrum, they pretty much have the same concept. The core protocol works with raw bytes and Electrum is JSON based. This library supports only Electrum protocol, for now, thus it only works with Electrum servers.”
By this let’s start building…
In the meantime, Koyn library supports ESP8266 boards along with SD card shield, Plus a microSD card as a single partition (FAT formatted, preferably class 10 and at least 1 GB of empty space) and of course a micro USB cable to program your board from your PC.
I have got the main three hardware parts for a reasonable price (nearly 30$) on Amazon:
- Wemos D1 mini
- Wemos D1 mini SD card shield
- SanDisk micro SD card class 4
- Some LEDs
- A breadboard
- Some 330 Ohm resistors
You can also check other boards supported by the library.
- Download ESP8266 toolchain to your Arduino IDE to be able to compile and upload your sketches to the ESP8266 board or download it automatically from the boards’ manager inside Arduino IDE.
- Download Koyn library either manually from GitHub, or from Arduino Library Manager inside the IDE just by writing in the search bar “Koyn” and you will be able to install the library.
Downloading Bitcoin Testnet Headers
Stated on Koyn’s GitHub readme, it will take the board a huge time downloading and verifying the testnet headers, that’s why the recommended solution is to use the koyn-sync command line tool provided by Elkrem to sync the headers over your host device, saving time and effort. Once you have downloaded koyn-sync executable you can run it according to your host device OS:
- OSX & Linux users, open the terminal over the same directory and write down “java -jar koyn-sync.jar”.
- Windows users, double-click the executable file and it will run the command automatically opening a window and showing the progress.
The tool by default will download the testnet headers by creating a folder called “koyn/test/” containing the blkhdrs file.
Setup SD Card
- Now copy the final blkhdrs file generated by the tool to your SD card under a directory called “koyn”. So you will have a folder called koyn containing a single file called blkhdrs (koyn/blkhdrs).
- Place the SD card to the SD card shield and attach the SD card shield to your ESP8266 board.
Place the 330 Ohm resistor on the breadboard, one end to the ground and the other to the short end of the LED, with the jumper connect the other lead of the LED to pin D0 on Wemos board.
Now your hardware is ready, let’s go coding..!
Let’s create a simple program that will turn on an LED once a new transaction is sent to your provided address and tracked by the library.
Compile & Upload
From Arduino IDE and under tools, make sure first to choose wemos D1 board. Then upload the code to your hardware using the micro USB cable. It will take like seconds for the hardware (Wemos+SD card shield) to be synced with the network (since we already got the latest header using the koyn-sync tool).
- Currently the supported network is the Bitcoin test network, so make sure not to use the library with the main network preventing losing your funds, as the library is still under development.
- There’s a Config.h file in the project where the user can configure his own preferences according to the interfaced hardware, check the documentation section about these preferences and also the public methods and functions.
Please check the repo’s README.md for more information about the library.