Selling Real-World Goods with a 21 Bitcoin Computer

With the launch of last week’s micropayments marketplace, you can now buy all sorts of digital goods from 21 Bitcoin Computers, provided you have a 21 Computer yourself.

But if digital goods were all we could buy, we would soon be dead, because one cannot live on emojis alone.

Let’s make our 21 Computer control a vending machine to dispense candy.

Controller Pins

First, notice that the computer has a set of twelve header pins near the USB port. The GPIO pins can be used for reading an input signal, or sending an output signal.

Pin numbers correspond to the map below.

The GPIO pin has an output of 3.3V for “True”, 0V for “False”. This output will be used to control a connected device.

Control an LED

The python-periphery library provides controllers for the GPIO pins.

$pip3 install python-periphery

The following code controls pin#1, which maps to GPIO6. But you can choose a different pin, or several at once, if you like.

#gpio_test.py
from periphery import GPIO
import time
# Open GPIO 6 with output direction
gpio_out = GPIO(6, “out”)
# Turn it on
gpio_out.write(True)
# sleep for 5 seconds
time.sleep(5)
# Turn it off
gpio_out.write(False)
# Clean up
gpio_out.close()

We can test that it is working properly by connecting an LED. The longer lead goes to the control signal, the shorter lead goes to ground (pin#11).

Run the test code and you should see the LED light up for 5 seconds.

Note: You need to run the script using sudo.

Turn it into a Bitcoin-Payable API

This is similar to any of the other API tutorials, with one exception. The pin controller commands need to be run using sudo, so wrap them in a subprocess module. The subprocess should run the above test code as a script, saved here as gpio_controller.py.

The following code turns on the light for 5 seconds after payment is received, but you can also make the duration dependent on the request, as shown in this example.

import os 
import json
import requests
from flask import g
# import from the 21 Developer Library
from two1.lib.wallet import Wallet
from two1.lib.bitserv.flask import Payment
# import flask web microframework
from flask import Flask
from flask import request
import subprocess
app = Flask(__name__)
wallet = Wallet()
payment = Payment(app, wallet)
# buy a blinky light for 10000 satoshis
@app.route(‘/buy’)
@payment.required(10000)
def purchase():
# drive GPIO6 (pin 1)
subprocess.call(“sudo python3 gpio_controller.py %d %d” % (6, 5), shell=True)
    return “Success! Look at your blinky light!”
if __name__ == ‘__main__’:
app.run(host=’0.0.0.0')

Amplify the Signal

A GPIO pin can only provide 16 mA of current. This isn’t enough to do much besides turn on a diode. We need to amplify the output signal.

Assume that we want to drive a DC motor with a 10-ohm load. Let’s say we want 3V across this load. That means we need to turn our 16 mA output signal into 300 mA through the motor. We can do this using a common-emitter amplifier:

Without getting into too much circuit theory, we are using an NPN transistor to amplify a current signal.

The GPIO pin has an output value of 3.3V when set to “True”. We drop it across a 1k transistor, as shown in the diagram. Remember that I=V/R, and this gives us about 3 mA going into the base of the transistor (actually less, because there is another 0.6V drop across the transistor junction).

This is the transistor:

The job of the transistor is to amplify the current. The 2N3904 has a gain of 100, but a maximum collector current of only 100 mA. If we try to amplify 3 mA times 100, we’ll max out at 100 mA.

In order to generate 300 mA, connect three of these amplifiers in parallel. Of course, you may also use a single higher-current transistor like a 2N2222. (I went with three 2N3904’s because it was all I had at home, and the nearest RadioShack was closed. Forever.)

The ground terminal in the circuit should go to the ground pin on the 21 computer (pin#11).

I soldered everything together on a piece of cardboard, but you may achieve better results and fewer house fires with a protoboard.

The one part still missing from the circuit diagram is the power supply (Vdd). You can draw power from pin#12 (Vcc) on the 21 computer, but it’s safer to use an external power source.

In fact, the 21 computer comes with a 5V power supply that you can plug straight into a DC barrel jack.

If you choose to appropriate the computer’s power supply for the amplifier circuit, you will need to power the 21 computer with a standard micro USB cable.

Don’t worry, the power supply is only necessary for running the mining chip, and if you paid $400 for a Bitcoin computer, you can afford to buy your own satoshis. Besides, the mining chip isn’t even running a full node and contributes nothing to the security of the network.

Drive a Motor

Now that we have a Bitcoin-payable API with enough output power to turn a motor, we can do anything: control a robot, turn on some lights, start a car.

Here we are going to control a candy dispenser. I got this one from Sharper Image. Turn it upside down, open it up, and gut it:

The only thing you need is the two leads from the motor. Connect one lead to the power supply, connect the other lead to the collector of the transistor amplifier.

The motor is taking the place of the “M” in the circuit diagram above. Don’t worry if you get the two motor leads mixed up; the motor will simply spin backwards.

Finally, connect the amplifier to the GPIO pin. Publish the API to the 21 marketplace, and sell some candy to strangers!

Full source code available here.

Notes for Abra

Connect to the computer. You must be on the local office network to connect. The local IP address changes sometimes, ask Marc if this one doesn’t work. The password is “1234”. Feel free to change the password.

$ ssh twenty@192.168.1.103

Once connected, this starts the merchant API for paying with Abra:

$ tmux attach
(if error, just use "tmux")
$ cd Jellybean
$ sudo nodejs app.js

To allow payments from the 21 market, do this:

$ cd 21GPIO
$ python3 server.py

Troubleshooting

Help, I’m not getting my candy!

Is the red LED turning on? If so, the candy is probably stuck in the machine. Tip the machine forward and backward and that should loosen things up. Once the machine is empty, you may want to cut a bigger hole for the dispensing slot (plastic cover at the bottom of the container). You can just cut it with scissors, I made it out of an empty lunch tray and the hole is currently very small.

Still not getting any candy, and the LED is not turning on.

Check all the leads, and make sure no bare wires are touching. When shorts occur, the machine reboots. And if the machine has rebooted, you need to start the merchant API again with the instructions above.

See instructions for how to automatically run the app on reboot.

Still not working :(

There are five pounds of m&m’s in the drawer across from the fridge. No bitcoin required!

Like what you read? Give Elaine Ou a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.