Talks

Epic Intro Music: BLE Beacons and Ruby

Epic Intro Music: BLE Beacons and Ruby

by Christopher Sexton

In the video titled 'Epic Intro Music: BLE Beacons and Ruby,' Christopher Sexton introduces an innovative way to integrate BLE (Bluetooth Low Energy) beacons with a mobile app and Raspberry Pi to create a fun music-playing experience that announces when people enter the office. The session, delivered at RubyConf 2014, focuses on the mechanics of beacons, the emotional backdrop of creating thematic entrance music, and practical implementations of the technology. The key points discussed are as follows:

  • Introduction to the Concept: Christopher discusses the idea of playing personalized music when different personnel enter the office. He humorously reflects on what theme song would suit him, settling on the well-known 'Rick Roll.'

  • Need for Components: The system requires three main components:

    • Speakers to play the music,
    • A Mobile App to detect the beacons,
    • BLE Beacons for proximity detection.
  • Understanding BLE Beacons: He explains that BLE beacons operate as simple broadcasting devices that continuously send out identifiers. Though typically identified with Apple’s iBeacon specification, he mentions AltBeacon as a more open alternative created by his company.

  • System Architecture: The talk walks through a whiteboard session illustrating the architecture, where beacons send identifiers to a mobile app, prompting a Raspberry Pi server to play music when someone enters.

  • Implementation in Practice: Christopher demonstrates building the entire system on a Raspberry Pi, detailing how it broadcasts identifiers and plays music using a Sinatra app.

  • Misconceptions about Beacons: Several misconceptions surrounding beacon technology are addressed:

    • Beacons do not deliver content; they broadcast identifiers.
    • They do not track users or know when they are detected.
    • Signal strength can only provide approximate distance measurements, which can fluctuate.
  • User Interaction & Opt-in Mechanism: He explains that users need to opt-in for notifications, thereby maintaining privacy regarding tracking.

In conclusion, the integration of Raspberry Pi and BLE beacons enables exciting applications, such as playing themed music in creative workplace environments. The talk combines technical knowledge with light-hearted anecdotes, ultimately revealing that while technology can be complex, it can also be fun and engaging.

00:00:24.880 Oh my God!
00:00:56.840 Okay.
00:01:00.160 Now, I’ve got to figure out why the keyboard wasn’t working. Hi, I’m Christopher.
00:01:08.560 Seriously, I have no keyboard focus. It's very annoying. I like to build things.
00:01:12.720 This particular thing I built mostly out of cardboard, but we also used an Arduino to make the light on the top of the TARDIS work. That was a lot of fun, and I work for a company called Radius Networks.
00:01:21.920 We build products around proximity and location. One of the things that we do a lot with is beacons. We actually build and sell the hardware devices, as well as a bunch of services that kind of go on top of it.
00:01:30.240 This is our office. It’s the Powerhouse in DC, one of two locations where you don’t have to give a street or number in DC if you want to send a letter to it. It’s really cool; it is a hundred-year-old renovated powerhouse turned into an office space. We all work on a mezzanine.
00:02:01.040 The doors to come in are low on the main floor, where everyone walks in. As they come in, they stomp up these loud metal stairs, but you don’t know who’s coming in, which made us wonder, 'Well, we’re a proximity company. We can figure this out.' Wouldn’t it be really cool if some music would just magically play when somebody walks into the office?
00:02:10.479 So, of course, we started talking about playing music when the CEO walks into the office and comes up the stairs. We had the same conversation about the Chief Financial Officer. What’s he going to do when he walks up carrying all his ledgers and things? That was pretty obvious.
00:03:03.760 Then, I was faced with a real dilemma: what am I going to play when I walk in? I thought about it for a long time, and then it seemed just so blatantly obvious.
00:03:25.280 I was very happy that I had the first slot talking because I thought I’d be the very first person at this RubyConf to Rick Rolley! Okay, so we have this thing—let’s build it. What do we need?
00:03:35.680 We’re going to need three different things. First, we need speakers—some way to actually play the music, connected to a small computer. We’re going to need a mobile app, so something on your phone. Luckily, you always carry your phone with you, so this is a good representation of where you are.
00:04:03.280 Then, we’re going to need a beacon. You probably already know about computers and mobile phones, but I don’t know if everyone’s familiar with what exactly a beacon is.
00:04:09.840 There are several types of beacons, but the ones I’m talking about today are Bluetooth Low Energy beacons—or BLE beacons—or Bluetooth Smart, or Bluetooth 4.0; they’re all the same thing.
00:04:11.439 They are just marketing terms that the Bluetooth SIG has come up with to talk about these things. I think most people tend to have settled on Bluetooth Low Energy at this point.
00:04:32.720 What exactly is one of these beacons? It's kind of like a PA system; it just makes announcements over and over and ignores everything else. This means that you don't connect to it. It doesn’t know if you’ve heard it; it doesn’t know pretty much anything. It's just sending out the same identifiers over and over again.
00:04:40.960 What it's sending out are identifiers. This is nothing complicated. It looks something like this, which is just a big UUID along with a couple of other numbers. That’s basically all that it has. You can use these identifiers as a foreign key, like you would in a database or something to look up what it means when you see that beacon.
00:05:15.919 There are a few different types of beacons and while having options is good, it’s never bad to have too many players or too many cooks in the kitchen. The obvious one is iBeacon, which is a spec by Apple.
00:05:26.400 Whenever I talk about beacons, I almost always mean this, and I use the word 'beacon' synonymously with iBeacon technology, just the way you’re supposed to say it. This came out with iOS 7; they introduced it, and it works really well on iOS. However, the iBeacon spec isn't completely free; it involves proprietary intellectual property.
00:05:45.600 There’s a spec called AltBeacon, and to be completely honest, this is a little bit of a shameless plug because my company authored that spec. It’s also a little different from some of the other specifications out there because it was written with the idea of interoperability. It’s designed to work one-to-one with iBeacons.
00:06:21.840 In fact, we have a number of hardware devices that do both, and you wouldn't notice the difference between the two. It’s just a mechanism that we’re using. Finally, there are other proprietary beacons that can do some really cool stuff, including other sensors.
00:06:31.760 You might notice on Kickstarter these kinds of devices that help you find your lost bike or have an accelerometer or temperature sensor, and can do other cool things, but these are outside the scope of what I’m talking about, which is the simple BLE beacons. So cool!
00:07:00.720 Now we kind of know the different parts we’re going to need. Let’s whiteboard and walk through exactly what we would do to make all of this work. First, we need a beacon, and we'll set that up. We’ll need a mobile device, some sort of Ruby backend—like a server—and then the thing that’s actually going to play the audio.
00:07:25.680 What happens first is the beacon will broadcast its identifiers and just shouts these out, not knowing anything around it and sending those. The phone is going to notice that. So, on the operating system on the phone, we’ll wake up the app and tell it that it has seen this beacon. That app can then post off to the server, which will inform whatever needs to play the music that it should go ahead and play the music. This is, of course, the point where the epic music actually plays!
00:08:07.039 So, what exactly are all these abstract, weird drawings? If we're going to build it, how does this all fit together? For the demo of this talk, I built it all together so everything in the box all runs on a Raspberry Pi, and it's all sitting there together. We have the phone as a separate system, and these are the two things that need to interact together.
00:08:32.319 This is the actual system that I built. I brought it along, so you can see. We have a Raspberry Pi with a little Bluetooth adapter plugged in, some speakers, and a USB hub that was only for power. You’ll notice there’s a network cable because it needed to be connected; that was the only way we could tell the Pi to play anything.
00:09:01.600 The beacon, which is sticking out here, would only broadcast out; it wasn't receiving anything back. Alright, so we have this epic music system running on a Raspberry Pi.
00:09:30.640 There are a few things that we needed to do for us. One is broadcasting as a beacon. If you’re just using a generic Bluetooth module, you can still have it broadcast as a beacon. Luckily, the command line that you need to run is pretty intuitive.
00:09:38.320 You can use the BlueZ stack, which is the drivers for Linux for Bluetooth. Using something called hci_tool, you can command your Bluetooth radio to broadcast something. The main UUID and the other identifiers are necessary for the configuration.
00:10:06.399 The easy approach is to just plug in a Bluetooth powered beacon that looks almost identical to a Bluetooth radio, but has our firmware running on it. You just configure it in advance; all it needs is power, and it’ll broadcast.
00:10:34.080 This is a cheap, easy way to get it to broadcast as a beacon. If I want to cover more doors—like when you walk into the office—we need beacons at every entrance so that when you walk past them, we’ll know that you’re there.
00:11:10.160 Now, we need a music player. Once the system broadcasts, we need something that's going to listen for events and play the music. I did it with a Sinatra app, which is quite simple.
00:11:22.000 When the POST request comes in, it simply converts the parameters and looks for a parameter called 'name'. Then, what we do is we call up a method at the top, which is 'play'. All I’m going to do is shout out to a little command line app called mpeg321 and then I resolve the path to that audio file.
00:11:51.440 If I want to add more songs for other people, I would just add 'chris.mp3' to the Raspberry Pi, and you can change out whatever song you want.
00:12:14.640 Now, since I want it all to run on one device, I don’t want a separate server and Raspberry Pi setup. I want to tunnel out to the internet to manage connections, running the server within the firewall at my office. I used a tool called ngrok.
00:12:47.840 This tool is amazing! I discovered it while working on this project, and it gives you introspected traffic, allowing you to see what's going over the wire for the stuff that you’ve tunneled out. If you’ve ever worked with webhooks and want to test them, just check this out.
00:13:19.360 I have an iPhone, so I decided I wanted to write an iOS app using RubyMotion because we were talking about it at RubyConf. This is how you tell the core location APIs to listen for a beacon.
00:13:46.480 There are a couple of things—I set it up to look for any beacons with a specific UUID, and then inform me when any are nearby. I register with the operating system: please notify me about these beacons.
00:14:20.320 I also need to implement some sort of callback method, which acts as a delegate to core location. This means when the core location system detects an event, it calls back to my app.
00:14:59.840 What’s cool about this, and what was novel when Apple introduced iBeacons, is that this happens even if my app is in the background or if you terminated the app while the phone is locked in your pocket.
00:15:22.560 We can post this to the server. Using RubyMotion, I employed a library called BubbleWrap. If you’ve done anything with RubyMotion, here's a tidbit—BubbleWrap is fantastic.
00:15:49.440 To perform the same HTTP POST work using native APIs, it would take about 30 lines of code. So now we have a working system. We have the two components: the Raspberry Pi listening to events and the phone that reacts to it.
00:16:19.200 The events that come out of the music player will allow the phone to react to them, and it posts it back to the Raspberry Pi system. Cool! We have a working prototype!
00:16:54.800 Now, I’d like to talk a little more about how iBeacons work, particularly about some of the misconceptions and nuances of working with them because there are definitely things about iBeacons that aren’t obvious.
00:17:32.160 A couple of major misconceptions that we see repeatedly are that beacons deliver content. They don’t; they only send out identifiers. I feel like I should just keep repeating this because it’s something people have a hard time grasping.
00:17:53.920 People tend to think they’re receiving content or things like that from these devices, but the way it works is that beacons send out identifiers, and then you have the opportunity to react to them. It's just providing you the event, and none of the actual data, messaging, or content.
00:18:20.560 Another misconception is that beacons know when they're detected. They don’t have a clue; they are just dumb broadcasters. They keep announcing their identifiers repeatedly.
00:18:57.920 One important thing we haven’t discussed is the distance estimate. In our demo, it was simply about taking action when you see the beacon. However, you can use the RSSI or the relative signal strength to approximately estimate how far away you are from the beacon.
00:19:26.960 This uses Bluetooth; it wasn’t intended for distance measurement. Other RF-based technologies exist that are more accurate, but Bluetooth is great because it’s built into my phone.
00:20:02.480 In practice, you can get signal strength data from your phone and estimate distance, but this is very environmental. For example, plugging a beacon in underneath an obstruction affects how signals are received.
00:20:34.440 Many people think, 'It would be great to use beacons at the cash register. We need to detect when people are in line,' but that’s not very workable.
00:21:08.320 Even standing still at one meter away, signal estimations can fluctuate during controlled situations. Furthermore, the accuracy lessens with distance.
00:21:35.679 If you want precision, you need to be as close to the beacon as possible, which is reliable. However, if you are several meters away, results become unpredictable.
00:21:47.840 One more thing to note is that not all platforms behave the same.
00:22:09.760 For example, you can’t set up an iPhone app to detect all nearby beacons. This is restricted by the core location APIs, even if it might be possible on Android, where you have a more open platform.
00:23:30.560 Another misconception is the idea that beacons can track you. There have been news stories about issues raised by beacon installations in public areas.
00:23:40.320 These devices aren’t tracking you; they give your phone something which can facilitate tracking. It’s not more invasive than other methods, and you can control whether you receive information.
00:24:05.520 On iOS, it’s all opt-in, which is a pretty good way to engage people. It really helps when I want to play music as I walk into the office. If I choose not to receive promotional offers or notifications, I have that option.
00:24:30.960 Thank you, everyone!