Monero Monitor

Blog: Beam Bitcoin with a Raspberry Pi Physical Web Beacon

18 Sep 2017 by bigreddmachine


Header

I love fiddling with Raspberry Pis. As I explained in my bio, the Raspberry Pi community is how I first heard about Bitcoin. Over the past 4+ years I’ve been making motion-sensor alarm clocks, web-streaming wildlife cameras, ridiculous home Christmas lights displays, voice activated personal assistants, low-powered Bitcoin and Monero nodes, and more.

When I first saw the Raspberry Pi Zero W, and then finally got my hands on one a few weeks ago, I knew exactly what I wanted to do with it. I wanted to make a cheap, DIY, reprogrammable Physical Web Beacon. And specifically I wanted to make a beacon that would broadcast out bitcoin.org or getmonero.org, creating an alert on any nearby person’s phone about Bitcoin, all while remaining safely tucked away in my bag or pocket.

Why, you ask? Well, because guerrilla marketing is fun. Because imagining random people stumbling upon Bitcoin or Monero via a simple phone alert is kind of exciting. Because you could broadcast a link to your service at your next Bitcoin meetup. And because why not?

In this blog post, I’ll tell you how you can build your own beacon and broadcast out a link to bitcoin.org or anywhere else you’d like :)

In this post:


What is a Physical Web Beacon?

I’m not going to go into a ton of detail here. If you want to learn all about the technology behind web beacons, Google has created a great page with lots of content. Apple probably has a similar resource. But stealing from Google’s page, “The Physical Web is an open approach to enable quick and seamless interactions with physical objects and locations.” Physical web beacons are bluetooth devices that facilitate those interactions, usually using the Eddystone protocol specification to broadcast out web URLs to any nearby bluetooth-enabled devices.

Both Android and iOS phones support the protocol, though sometimes not as seamlessly as we might like. From the perspective of a normal user, these beacons send out a message to nearby phones, and those phones (if configured to do so) will show a “Nearby” or “Physical Web” alert in their notification drawers. It’s an intriguing technology that can facilitate interactions between devices in proximity with one another, such as parking meter payments, museum information portals, and so on. Beacons also create a funky way to share a simple website with random strangers, like bitcoin.org for example.


Making the Beacon

Turning a Pi into a Physical Web Beacon is super simple, and requires basically no parts. If you’re comfortable with the command line, you can probably be up and running in under a half hour.

The easiest way to do it is with either a model 3 B or a Zero W, both of which come with bluetooth on board. I like the Zero W because it’s smaller, costs a lot less, and can be used as a USB Gadget (more on that in a bit). But you can use any Pi or similar device and a bluetooth dongle; that just adds more parts, more cost, and more bulk. For an easy, compact solution, I went with the Zero W.

Now, a few of you are probably thinking “why don’t you just buy a specifically made beacon instead of going through the trouble of turning a Zero W into one?” And to you I say simply, because I wanted to :)

So what parts do you need? Here’s what I used:

Parts Parts
All beacon parts / fully active beacon.

Preparing the Raspberry Pi

Note: This section gives a brief overview on installing Raspbian Lite and logging into your Pi. Specifically, it says how to configure the Pi as a USB Gadget so that you can plug it directly into a computer and not need anything else. If you already know how to log in and get started with a Pi (using whatever method you prefer), you can skip to the Configuring your Beacon section!

Once you have your Pi and SD card, you’ll need to download an OS for the Pi. To keep things lightweight and simple, I recommend using Raspbian Lite - a great lightweight OS designed for headless Pi projects like this. Head over to the official Raspberry Pi site and download the latest version of Raspbian Lite. Then follow their installation guide and burn a copy onto your SD card.

Using your Pi as a USB Gadget

Once you’ve installed Raspbian, you could stick your SD card into your Pi and plug it in, and your Pi would start up. But to do this, at a minimum you’d need a keyboard and monitor plugged into your Pi, which in the case of a Zero W would also mean having adapters for the micro USB and mini HDMI interfaces.

I wanted to keep things simple and make it easy to reconfigure the beacon on the road, and so instead I took advantage of a unique feature of the Zero and Zero W in the Raspberry Pi ecosystem: the ability to turn your Pi into a USB Gadget. Without going into a lot of detail, using your Zero as a USB Gadget allows you to plug your Zero directly into a laptop and log into it remotely, all while requiring only a single micro USB to USB connector.

Adafruit has an excellent writeup on doing this, so I won’t reproduce the steps here. The writeup starts with installing Raspbian (which you might have already done) and takes care of everything you need to have a working Gadget. Be sure to follow all the configuration steps, including the bit about SSH.

Gadget
Logged into my Zero W ethernet gadget from my laptop.

Log in and finish setup

You should now be able to plug your Zero W into your computer, wait for it to boot up, and log in using the simple command ssh [email protected] from a command line. The default user is pi and password is raspberry. I recommend changing your password before doing anything else. Open the Pi configuration menu using the command sudo raspi-config and change your password.

Warning: If you’re new to Linux, the sudo command is very powerful, giving superuser privileges. You should always double check these commands before submitting.

It’s also a good idea to make sure your OS is up-to-date. This might require telling your computer to share internet access with your Pi, which you can test by typing the command ping google.com. If you get some type of error, do a quick search on how to share your computer’s internet access with a USB device (this is different for Linux, Windows, and macOS and I’m not covering it here). Once you confirm that your Pi can access the outside world, do a quick update with:

sudo apt-get update && sudo apt-get upgrade

Type in your password if prompted and answer y to any Yes/No prompts.

Configuring your Beacon

At this point you should have a fully functioning Linux install on your Zero W that you can somehow access (via keyboard and monitor, as a USB Gadget, or some other way). All that’s left is to configure your Pi’s bluetooth device to broadcast bitcoin.org, getmonero.org, or whatever other website you desire.

The following instructions are taken from an excellent hackaday post on configuring a Pi as a beacon. I’ve reproduced them here for conciseness. In that post, the author uses a Raspberry Pi 3 B, but the instructions are exactly the same for a Zero W.

Assuming you are logged into your Pi, enable the Bluetooth device:

sudo hciconfig hci0 up

Next, set the device to “advertise and not-connectable”:

sudo hciconfig hci0 leadv 3

Finally, tell the device to broadcast your site. For https://bitcoin.org:

sudo hcitool -i hci0 cmd 0x08 0x0008 16 02 01 06 03 03 aa fe 0e 16 aa fe 10 00 03 62 69 74 63 6f 69 6e 08 00 00 00 00 00 00 00 00 00

Or for https://getmonero.org:

sudo hcitool -i hci0 cmd 0x08 0x0008 18 02 01 06 03 03 aa fe 10 16 aa fe 10 00 03 67 65 74 6d 6f 6e 65 72 6f 08 00 00 00 00 00 00 00

For another site, use this great open source tool to determine the correct command. Note that you must use https to be compatible with the latest versions of Chrome and Android.

Note: If you’d like to understand the mechanics of how the beacon code is defined, give the blog post mentioned above a read. It talks about each bit in the hcitool code and how you could tweak the command.

Your beacon should now be broadcasting! If you have an Android phone, you can discover your beacon broadcast with either the Physical Web app or simple Google Notifications. iOS can similarly find beacon signals using the Physical Web app or Chrome, from what I understand. Your phone may or may not detect it automatically… more info on that at support.google.com.

Notification
Example notifications: “Nearby” or “Physical Web” depending on if app is installed.

Broadcast on Startup:

You’ve successfully gotten your Pi to broadcast as a web beacon. Congrats! But unless you want to have to manually type those commands each time you want to broadcast, this still isn’t all that useful yet. I wanted to be able to plug my Zero W into any power source, have it boot up, and immediately and automatically start broadcasting. Accomplishing that was super simple.

First, in your main directory on the Pi, create a new shell script

nano ~/start_beacon.sh

nano is a lightweight text editor automatically included in Raspbian. You should now see a basic text editor screen. Input the following:

#!/bin/sh

sudo hciconfig hci0 up
sudo hciconfig hci0 leadv 3

# For bitcoin.org (change for another URL):
sudo hcitool -i hci0 cmd 0x08 0x0008 16 02 01 06 03 03 aa fe 0e 16 aa fe 10 00 03 62 69 74 63 6f 69 6e 08 00 00 00 00 00 00 00 00 00

Save and exit nano with the following commands: cntl + X, Y, enter.

Next, make your script executable:

chmod +x ~/start_beacon.sh

At this point, you can type ~/start_beacon.sh in the terminal and your beacon will start. There’s one final step, however, to get it to start up automatically on boot. We are going to add that command to your rc.local file:

sudo nano /etc/rc.local

Then add the following at the end of the file, above the exit command:

sudo /home/pi/start_beacon.sh

Save and exit (same as before) and you should be all set up! Restart your Pi and it should start broadcasting as soon as it boots up.


Next Steps

You now have a simple beacon broadcasting your site. This can be a great way to share your site at a meetup or just have someone stumble upon it around town, and could make for interesting advertising. But this just scrapes the surface of what you can do with these beacons. The real magic lies in creating interactive ones.

Here are some ideas for how you can take your beacon to the next level:

If you build one of these ideas or come up with your own, please tweet me (@MoneroMonitor).

Most of all, get creative and have fun!

- Mike

Did you enjoy this post? Please show your support by donating to the author:

4AL2cmmrVC41if2TzxfYUKFoVF8xnkzszFX7KVS21zh1HgMWWTfa8wh23xBWLG2htS42LaqfQGgz69vVWA5uZGhaNLc72jB

BTC: 1PNz3gnrzzgNjUCu4mJNQWw3s8cBwBLd97