Lightning Snacks

A Vending Machine using the Bitcoin Lightning Network on a Full Node Raspberry Pi

Problem Definition

Most vending snack machines are coin-operated. This leads to many drawbacks such as incorrect/no change, some coins are not recognized, management of coins by owner and no coins in your wallet.

Sometimes my colleagues ask me for some coins, and in return they give banknotes or pay me via Revolut. Why should we all go through this hassle if when can do online transactions? We can use Visa/Mastercard scanners attached with vending machines and people can buy snacks easily. Well, the problem is that for every authorized transaction, the bank takes a commission. Is it worth for the merchant to pay a commission for a cheap snack item? I don’t think so. Apart from that, this technique is not recommended because there might be hackers that can attach card sniffers to the vending machine intending to hijack your card. So, what’s the solution?

The Solution

Nowadays everyone owns a smartphone. With Bitcoin, you can have your own non-custodial wallet on your phone. You can do global transactions on decentralized platforms without making use of third-party APIs (banks) to verify transactions. Can we apply this technology to our vending machine? We can but it won’t be ideal. The problem lies in the transaction confirmation time and the transaction fees.

One confirmation (1 block) takes about 10 minutes and we usually assume that a transaction is fully confirmed when it is 6 blocks deep. This means that a full confirmation takes about 1 hour (10 minutes * 6 blocks). This is very fast when compared to traditional systems, but it won’t be ideal for our vending machine system. Imagine our client waiting 1 hour to buy some candy. Since snacks are not expensive, we can assume that one confirmation (1 block deep) is enough, but our client must still wait 10 minutes for the transaction to be processed.

The other problem relies in the transaction fees. Transaction fees are included with every Bitcoin transaction in order to have transactions processed by a miner and confirmed by the Bitcoin network. The space available for transactions in a block is currently limited to 1 MB. This means that to get your transaction processed quickly you must outbid other users. Therefore, it can get expensive to buy some snacks.

Bitcoin Cash (another cryptocurrency – fork of Bitcoin) simply solves these problems by increasing the block size and disabling the ‘replace by higher fee’ feature. Therefore, a transaction can be created with a lower transaction fee and once created, we can safely assume that the transaction is going to be placed in the next block. This might lead to other problems, but I don’t want to debate Bitcoin Cash vs Bitcoin in this article.

The Bitcoin Lightning Network (Layer 2 Protocol on Bitcoin) is the ideal solution for our vending snack machine. It is a decentralized network powered by smart contracts that enables instant payments with very low-cost transaction fees across participants. Lightning enables one to send very small funds in Bitcoin without custodial risk. Everyone can be part of the network by hosting and opening a channel with a node. When opening a channel, participants must commit an amount which is stored on the blockchain. Transactions then can be passed through multiple channels in a mesh network of payment channels. These transactions are not stored on the public blockchain and therefore the base layer fees are avoided. I must admit that it is a bit complicated to work with but once you understand what is happening, you will appreciate the principles behind this system.

1024px-17_node_mesh_network.svg_

Building the Prototype

For this project one used the Raspberry Pi 3 B+ with an external hard disk storing the entire Bitcoin testnet blockchain. The Bitcoin Lightning Network was installed using the Raspibolt tutorial and Python was used to develop the whole system. That is creating the GUI using a game engine called PyGame, interfacing with the LND network, connecting with the Coinbase API for price conversion, making invoices into QRCodes, recursively checking for payments and eventually turning the motors using GPIO commands for successful payments. The Eclair Mobile Testnet app was installed on my phone to test for the client perspective.

LightningSnacks.JPG

Description Link
3D printed some of the objects found in this project. https://www.thingiverse.com/thing:3116905

 

Official Raspberry Pi 7″ Touchscreen Display https://thepihut.com/products/official-raspberry-pi-7-touchscreen-display
Bitcoin Lighting Network on Raspberry Pi (RaspiBolt) https://stadicus.github.io/RaspiBolt/
Python gRPC client for the Lightning Network Daemon https://github.com/lightningnetwork/lnd/blob/master/docs/grpc/python.md
LND gRPC API Reference https://api.lightning.community/#simple-rpc
Conversion from  Euro to Bitcoin https://developers.coinbase.com/api/v2
Python Game Engine for User Interface – PyGame https://www.pygame.org/news
Raspberry pi motor library https://github.com/gavinlyonsrepo/RpiMotorLib
NEMA 17 Motor https://reprap.org/wiki/NEMA_17_Stepper_motor
Shaft Holder Extension Rod https://www.ebay.co.uk/itm/7Pcs-ER11-1-7mm-Spring-Collets-Set-ER11A-5mm-Motor-Shaft-Holder-Extension-Rod/163608271173?ssPageName=STRK%3AMEBIDX%3AIT&_trksid=p2057872.m2749.l2649
QR Code Generator https://pypi.org/project/qrcode/
Android Eclair Mobile Testnet https://play.google.com/store/apps/details?id=fr.acinq.eclair.wallet&hl=en_US

Demonstration

Conclusion

This prototype clearly demonstrates that the Lightning Network is a potential solution for the problems mentioned before. One managed to buy snacks instantly with very low transaction fees. On average, every transaction fee was about 4 satoshis which is equivalent to 0.0002557 EUR (at the time of writing 1 BTC is equivalent to 6385 Euros). One problem that I constantly encountered was the inbound capacity limit which is described clearly here: https://blog.muun.com/the-inbound-capacity-problem-in-the-lightning-network/. Developers are working on this using a solution called Lightning Loops: https://blog.lightning.engineering/posts/2019/03/20/loop.html. In the future I want to test this prototype on the Bitcoin Mainnet using a 4GB Raspberry Pi 4 and experiment with the looping solution.

Thanks for reading 🙂