We're sorry but this page doesn't work properly without JavaScript enabled. Please enable it to continue.
Feedback

Bluetooth && Azure IoT hub == IoT

00:00

Formal Metadata

Title
Bluetooth && Azure IoT hub == IoT
Alternative Title
Bluetooth && Azure IoT hub == IoT - Or how to rule bilions of devices
Title of Series
Number of Parts
96
Author
License
CC Attribution - NonCommercial - ShareAlike 3.0 Unported:
You are free to use, adapt and copy, distribute and transmit the work or content in adapted or unchanged form for any legal and non-commercial purpose as long as the work is attributed to the author in the manner specified by the author or licensor and the work or content is shared also in adapted form only under the conditions of this
Identifiers
Publisher
Release Date
Language

Content Metadata

Subject Area
Genre
Abstract
The market totally explodes with new gadgets that can connect to tablets, pc's and phones. In 2015 alone there were 3 billion Bluetooth devices manufactured. In this session I will show how you can take advantage of that and how to communicate with devices that are Bluetooth enabled. I will also show how to figure out undocumented devices and how to use Azure IoT hub to turn them into IoT devices.
Internet der DingeLetterpress printing19 (number)Level (video gaming)Software developerMobile appRoutingVideo gameSource codeMoment (mathematics)CalculationLetterpress printingNeuroinformatikCodeComputer animation
Food energyCommunications protocolPlastikkarteFood energyCodeSoftware developerCommunications protocolPlastikkarteGroup actionCore dumpService (economics)Game controllerMeasurementNumberMobile appApplication service providerMultiplication signNeuroinformatikWindowMereologyStandard deviationLevel (video gaming)Perfect groupSocial classMoment (mathematics)Internet der DingeCodeComputer animation
Service (economics)Service (economics)Characteristic polynomialSelf-organizationElectronic program guideEvent horizonSega Enterprises Ltd.Group action
WritingRational numberLevel (video gaming)Service (economics)Level (video gaming)Service (economics)Characteristic polynomialGroup actionMereologyRight angleMobile appComputer animation
CodeInformationString (computer science)Physical systemSynchronizationOvalWindowForm (programming)Service (economics)CodeSynchronizationSet (mathematics)Category of beingSinc functionSlide ruleEvent horizonScaling (geometry)CASE <Informatik>Dot productAddress spaceComputer animationMeeting/Interview
Scale (map)Scaling (geometry)Generic programmingWeightInformationService (economics)DatabaseCartesian coordinate systemAddress spaceReal numberAttribute grammarSpecial unitary groupRevision controlCharacteristic polynomial
Scale (map)Cartesian coordinate systemService (economics)Physical systemCASE <Informatik>Characteristic polynomial
Scale (map)Graphical user interfaceMereologyWeightCodeSign (mathematics)Food energyMathematicsScaling (geometry)Well-formed formulaNegative numberMultiplication signDemo (music)Line (geometry)Right angle1 (number)Computer animation
Physical systemTask (computing)Run time (program lifecycle phase)Characteristic polynomialFluid staticsSynchronizationGeneric programmingForceLengthCodierung <Programmierung>Programmable read-only memoryOvalWeightCore dumpSign (mathematics)Service (economics)Characteristic polynomialClient (computing)Goodness of fitReading (process)Bit rateCASE <Informatik>Dependent and independent variablesRight angleEvent horizonDirected graphConfiguration spaceLoop (music)Cartesian coordinate systemCategory of beingNumberWeb pageGeneric programmingWeightSign (mathematics)SynchronizationWritingMultiplication signComputer animationSource code
Sign (mathematics)WeightLine (geometry)MathematicsPresentation of a groupFood energyCodeScaling (geometry)
Internet der DingeOpen sourceRoboticsRemote administrationLink (knot theory)Point cloudShared memoryBitInternet der DingeComputer animation
Data storage deviceService (economics)Point cloudSoftware developerDatabaseLocal GroupGroup actionFreewareMessage passingScaling (geometry)BitInternet der DingeTouchscreenUniform resource locatorMultitier architectureSlide ruleComputer animation
Connected spaceUniform resource locatorString (computer science)Installation artSource codeComputer animation
Key (cryptography)Shared memoryCountingGamma functionString (computer science)Connected spaceKey (cryptography)Data managementElectronic mailing listSinc functionCartesian coordinate systemCodeComputer animation
Software development kitInternet der DingeMobile appUniform resource locatorComputing platformUniverse (mathematics)Client (computing)WindowComputer animation
Software developerSuite (music)Convex hullLevel (video gaming)Green's functionProduct (business)WhiteboardIntelArrow of timeComputing platformInclusion mapComputerClient (computing)String (computer science)Formal languageInstance (computer science)Matrix (mathematics)CodeEvent horizonInternet der DingeElectronic mailing listWindowCodeComputer animation
Connected spaceString (computer science)InternetworkingCycle (graph theory)RoboticsAreaRight angleInternet der DingeComputer animation
Execution unitSoftware bugRoboticsParallel portMultiplication signSet (mathematics)ExplosionTelecommunicationCodeMobile appMessage passingDemo (music)Computer animationSource code
WeightFluid staticsSign (mathematics)RoboticsCartesian coordinate systemGame controllerElectronic mailing listCodeInternet der DingeComputer animation
FingerprintSynchronizationOvalString (computer science)Instance (computer science)Client (computing)Inclusion mapMessage passingFluid staticsTask (computing)Connected spaceConvex hullString (computer science)Event horizonConnected spaceSocial classMessage passingClient (computing)RoboticsInternet der DingeSource codeComputer animation
Message passingSynchronizationFluid staticsTask (computing)String (computer science)Event horizonClient (computing)ASCIICodierung <Programmierung>Exception handlingCharacteristic polynomialAliasingInformationData conversionLengthService (economics)Social classCASE <Informatik>Enumerated typeRoboticsCharacteristic polynomialHexagonCartesian coordinate systemMessage passingString (computer science)Mobile appLinear programmingSource codeComputer animation
WindowScheduling (computing)Software developerPresentation of a groupSampling (statistics)Linear programmingComputer animation
Transcript: English(auto-generated)
Good morning. I'm so happy to see so many of you here this early in the morning. Thanks for joining me. So as developers, we tend to go through different stages.
My first stage was when I was about seven or eight years old. I got my first computer, and I remember I sat down, I wrote 10 print Jimmy, 20 go to 10. So that was my code. I got my computer to do stuff for me.
This is actually my first app, and if anyone wants the source code, I will be happy to share it. This is actually the moment when I realized that I wanted to be a developer. This is what I wanted to do with my life. The next stage was when I got a calculator and a mobile phone.
There's a very special feeling when you're writing something on your computer, and then you deploy it to something smaller that you can bring with you in your pocket. I could actually sit down and write code on my calculator during class.
Mind-blowing for me. The third stage was when I was introduced to circuit boards. Raspberry Pi, Netduino, Arduino, and stuff like that. I remember I went into the living room, and I yelled to my wife, Jessica, Jessica, you've got to come and see this. The LED is blinking. Look, the LED, it's blinking.
On, off, on, see, it's blinking. And she just went into the room, yeah, what about it? No, no, you don't understand. The LED here on the circuit board is blinking. I made that happen. I think it was a bigger moment for me than for her,
but I was so happy. The LED was blinking. Then the next stage was when I realized that I can control other devices, devices that I actually don't have any code control over. I can get measurements from sensors. I can turn off and on lights.
And this is what we're going to talk about today. Bluetooth, how we can control devices through Bluetooth. But why should we care about this? Well, first of all, it's really, really fun. And the second reason is this number.
Three billion Bluetooth devices were manufactured last year. Of course we want to be here as app developers, make apps for this customer base.
So what we're going to talk about today is how does Bluetooth low energy work? How can we figure out a Bluetooth protocol? And then we're going to go from Bluetooth low energy all the way to IoT. And see if that works.
So my name is Jim Engstrom and I work as an ASP.NET developer. I spend my spare time doing Windows and Windows phone development and some HoloLens. And together with my wife, I run a user group called Coding After Work and a podcast for the same name. I'm also a Windows development MVP.
But enough about me. Let's dig deeper into this fun stuff. So Bluetooth low energy has many names. It's called Bluetooth Smart. It's called, let's see, there we go. It's called Bluetooth LE, Bluetooth Low Energy, BLE.
And sometimes, wrongly I should say, Bluetooth 4.0. It's part of the Bluetooth 4.0 standards. But it's not the whole thing. Bluetooth low energy was actually introduced back in 2006 by Nokia
under the name Vibri or Vibri, however you want to pronounce it. And then 2010, it became part of the Bluetooth Core 4.0 standard. The nice thing about Bluetooth LE is that you can actually ask the device, what can you do?
What services are you implementing? That's perfect for developers. It's easy to reverse engineer, to figure out the protocols. So a BLE device can tell you what services, what service it has.
A BLE device always has one service or many. Must have one. The service is identified by a GUID. So there's an organization called Bluetooth SIG,
Special Interest Group, that has named a couple of these services. They have documented them and they have said that to be able to use this GUID, this service needs to look a particular way. There are also, of course, services that are not defined by Bluetooth SIG,
and we'll go into those later on. One service has one characteristic or more. Must have one. You can think of them like methods or events. Because every characteristic has a way to access them.
You can read, you can write, you can indicate, and you can notify. And the last two of them are, you can see them as events. So those are, your device is going to send those values back.
So this is the documentation from the Bluetooth SIG, the Special Interest Group, for the battery service. So their documentation says that to implement a battery service,
you need to have a characteristic called battery level. It must have read access. It wouldn't be much of a use if you couldn't read the battery level.
And then it may have notify. It doesn't have to, but it could. So let's look at this service in another way. So we have the BLE device. It has a battery service.
It has a battery level characteristic. And it must have read, and it may have notify. The really cool part of this is that you can actually build generic apps.
You can say that my app wants to connect to any device that implements the battery service. So you don't have to write specific app for a specific device. So now when we know how to communicate or how they work, how do we talk to the device?
Well, the first step is to pair the device. You can do that from within Windows, and you can do it programmatically. They're actually working on, now I'm talking Windows 10 specifically, they're actually working on pairless Bluetooth as well. But they're not there yet.
So you just search for Bluetooth settings. You click your device, and you click pair. Sometimes you get this window. Usually it's 00001234 or something like that. Or you can probably find it in the documentation for the device.
Or you can do as I did. I just tried 000000010002 until you got it. Luckily it's what 0007, so you didn't have to do that for long. So if you want to do this programmatically, you set up a device watcher.
You can request properties that you want to have access to. So in my case, I want a device address, and I want to know if is it connected. Then you create a watcher, and you supply it with a filter.
Those three dots, we will look at the filter on the next slide. And you supply it with the request properties. What properties do I want from it?
And what kind of endpoint am I looking for? Then you just add the added and updated event handlers. And you just start a watcher. And the filter looks something like this. You can find this at MSDN or something like that.
So you don't have to write it down or anything. Then we have the added event, or the event handler. In my case, I'm checking the name. I don't have to check the name, since I'm checking for services.
But in some cases, they reuse their services. They reuse the grid. So it could be a good thing to actually check the name. If it's just a particular set of device you want to use.
And then if it is the right one, you just pair it. You can also use the pairing.custom.pair sync if you want to do some special pairing code.
So I found this scale on netonnet. This is an Anderson scale. But it's actually Senson who makes them. So I've written a small application that lists all the services, all their characteristics, all the ways that they can be accessed.
Because the device knows these things. And are happy to share them with you. I also created a collection or a database that converts these services to their Bluetooth SIG real names, if you wish.
So if you look at the Senson scale, it contains generic access. This service will contain things like name and device address.
Then we have the generic attribute. This is the service that actually makes it possible to ask the device, what can you do? Then we have device information. It contains manufacturer, versions, name of the device name and stuff like that. Then we have another one. Not defined by the Bluetooth SIG.
Interesting. So the one, I know that generic access, generic attribute and device information won't give me a weight value. Because these are defined by the Bluetooth SIG. They don't have a get weight.
So the one named 000FFB, I'm sorry, I'm not going to let you suffer through that. So the last one is the one I want. So looking at our mystery service, it has two characteristics.
They both have a way to access them. In this case, I can write to it and I can get a notification from both of them. So I wrote a very simple application that listened to those notifications.
And from one of the services, I actually got a reply. So probably with that one. So now the fun part starts. The detective work. So I get 10 bytes back.
It looks like this. I started off with a baseline. Zero grams. Not putting anything on the scale. So I'm guessing that I'm actually looking for bytes that are zero. Those ones.
Then I tried to put an energy drink on my scale. Just happened to be next to me. It weighed 235 grams. Now, these values changed. And I actually have 235 on byte 5.
So I'm guessing that's the weight value. So now we reset the scale. Remove the energy drink. And I got minus 235. Now byte 7 and 9 changed. But byte 5 is still 235.
So I'm guessing 7 or 9 is a sign byte. Then I tried with 120 grams. Suddenly byte 4 changed. So after testing different weights, I managed to figure out that the bytes
I wanted to talk to or read from was 4, 5, and 7. And the formula ended up being like this. So if I take the byte 4 times 256 plus byte 5, that's going to give me the weight. And then I can use byte 7 to figure out what sign it is.
So 0 for positive, minus 1 for negative. So my first demo is going to be a legend. Wait for it. Dairy.
I'm sorry, I really like puns. So let's take a look at the code. So let's start with, I've actually written a couple of helper methods.
So the first one is set up notify a sync. To get notifications, I have to tell my Bluetooth characteristic that, hey, I want notifications from you. So what I do is, the first thing is that I get the service with this particular grid.
Then I get the characteristic with a particular grid. And then I write, write client characteristic configuration, descriptor a sync, the longest method name ever.
And I say that I'm interested in notifications. And this method is going to return the characteristics to me when it's done. Then I also have a read value. The same thing here.
I get the service, I get the characteristic. I read the value, and I say I want it uncached. Because sometimes the device will cache it for me. So that's good if you want to have low battery consumption. But in this case, I want the latest value.
Then as everything goes well, I get the data. I feel I get an array, byte rate. And in my case, I have made this method generic. So I can actually say that I want to string back.
But the Bluetooth device is always going to return bytes to me. In this case, I just convert it. I'm guessing it's UTF-8. That's not a given. And then I just return the bytes.
And then we have write value. Same thing here. Get the service, get the characteristic. And here I'm actually asking, okay, what kind of way can I write to you?
Can I write with the response? Sorry, without response? Or can I write with the response? And depending on what it can do, I just implement that. So I just write value async.
Again, bytes. Always bytes. So in our application, I define the service ID. This is our mystery service. Then we have the characteristic.
Characteristics GWT. Then I create a device selector from GWT. So this is going to give me all the devices that implement this service. I get a collection. And I just loop through them.
I get the Bluetooth LE device. And well, here I check, does the name contain Samsung? Again, I don't have to do this if I know that all the devices that implement this
particular service are going to use the same characteristics. But just to be safe. Then if I get a device, I run the my method setup notify async. So I say to the service, I want notifications.
I get the characteristics back. And then I just listen to the value changed event. In the value changed event, I get an array back.
An array of bytes. So I send it into my convert to weight method. And just set the property I have on my page to the new weight. And the weight convert to weight method is doing well. It's going to check whether or not byte 7 is set or not.
And it's going to take the sign times bytes, byte number 4, byte shifted, and then byte 5. So let's see if this works.
I have an energy drink. Put it on the scale. And the value changes.
Pretty simple, huh? I mean, this is not a lot of lines of code to do this. So let's go back to the presentation.
So Huawei is a company who makes robots. Toy robots. And they released a whole bunch of robots. This is the one to your, let's see, your left is the MIP.
It's a Bluetooth robot. And that one is the MIPOSAUR. I actually written an SDK to talk to these two. Actually, the one for MIP is out already as open source. I'll share the link later on.
And the MIPOSAUR is coming. They also took a lot of old toys. These were, these robots were controlled by remote control. But they upgraded them. So now they can be controlled by Bluetooth.
So let's talk a little bit about the Azure IoT hub. Sorry, Microsoft has released the Azure IoT hub. A way to communicate with IoT devices.
So what I could have done here is that I could have shown how to upload sensor data to the cloud and do stuff with it. That's no fun. I wanted to do something more fun. So we're going to try to control one of these robots
through the Azure IoT hub. So the first thing we need to do is actually set up the IoT hub. I've done that in advance. So it's a little bit quicker to do this with slides. So the first step is to click new.
So this is the Azure portal. The next step is to click Internet of Things and then Azure IoT hub. So let's name this NDC. I have chosen to use the free price and scale tier.
And I've chosen location Northern Europe. Just create and then you wait a couple of minutes. Then you will see this screen. And your IoT hub is ready for use. The free tier actually gives you 8,000 messages
that you can send through the IoT hub for free. That's awesome. The next step is to install the device explorer. It can be found on this URL.
You can download the source for it, or you can download a full installable. It looks something like this. So to be able to use the device explorer, you need a connection string. So let's go back to the Azure portal.
Click the key. Then IoT hub owner. This is created for you. So just click away. And then you have the connection string just there. Copy it and paste it into your device explorer.
And you're all done. So now we can go to the management tab. And click add. So we could have done this from code. But since we're just only going to use one device,
this will work perfectly. So I'll name this Robosapien. And I just press create. And I get it nicely in my list. Then to create your application, I have chosen to create a universal Windows platform app.
This will, of course, work with any app. So the first thing you need to do is to install the Microsoft Azure device client NuGet package. Once that's done, you can go to this URL.
Microsoft actually helps us even more with this. So this is not going to be an Azure IoT hub deep dive. I want to open your eyes to see how cool and how easy it is to communicate through the IoT hub with Bluetooth devices.
So the first thing, you go to this portal. You click connect your device. You will see a list of some of the devices you can connect to the IoT hub. I have chosen use another device and Windows. And Microsoft is actually going to provide you with all the code you need.
How nice is this? Just copy and paste it into your application, and you're done. You also need a connection string to your device. So that's the same connection string you can find in the Azure portal,
but you need to add a device ID. But the device explorer actually gives you the correct connection strings. You can just copy it from there. So let's see. Bluetooth Internet of Things robot.
That's a really boring name. It should be called Bluetooth Robiot, right? Of course. So there we go. Much better. Right. We're about to demo something. Now, actors know that they should never work with children or animals.
As a speaker, you know that you should never work with Bluetooth. That can be disturbed by a thousand people in the same area or venue. We also know that we should never rely on internet. So that's exactly what I've done. I'm relying on internet and Bluetooth.
So let's just hope this works. There we go. So this robot actually has a bug in it.
So I have to pair it every time I use it. This is made for perilous communication, but it will work hopefully. I'm actually talking to Huawei trying to solve this.
So here I went into the Bluetooth settings and I paired the device. Let's go into messages. I'm going to show you the code in a while.
So let's try. Let's do a explosive demo. C4. Sorry, I need to start the app as well.
There we go. Try that again. So now I'm actually controlling my robot
through the Device Explorer, into IoT Hub, then back to my application, and then over to the robot. So let's try something else. Let's see. I actually have a list of things it can do. So let's try a seven.
So all the commands that is available through the infrared controller is also available through Bluetooth. So let's take a look at the code.
So what I've done here is that I create a device client. I create it from my connection string. So this is the connection string to my device.
Then I have a robot sapient class. This is the class that talks to the robot sapient. And we will look at that in just a second. And then I say robot sapient connect. This is going to try to find the device and connect to it.
And then I also have a send event. So the send event is going to send robot sapient connected message to my IoT Hub. And if you look at the Device Explorer, let's see, if I monitor it, and I start it.
So if I start the app, it will connect to the robot sapient and I will get the message robot sapient connected if I monitor the hub.
And then I have receive commands. So this is going to check, do I have any messages? On my hub. If I have a message, I will convert it. I will get bytes. I will convert it to a message.
And then I will call the send command on my robot sapient and send in the message. So in this case, I'm going to get the bytes for C4, for example. Then I'm going to convert it to text and send it in to my robot sapient.
So let's take a look at the robot sapient class. So here I'm actually not using my helper methods, just to show how easy it is. So first thing, I get the service grid. Get the characteristics grid.
Then I connect. I try to find the device selector. So I try to get all the devices that implement this service. And 1Wii is actually one of the examples that reuse their service grids. So I have another robot that works completely differently,
has different characteristics, but the same service ID. I actually think it has the same characteristics ID as well. So I get the enumerator and I get the device.
Here I check, does it contain robot sapient? The other robot is called MIP. I get the service and I get the characteristic. Then we have the send command. So here, just take the characteristic
and write it, write the data straight to the characteristic. So I also have a method here that converts my C4 string, hex, to a byte array.
That's all it takes. So I can send commands up to the IT hub. Then it will return to my application, or actually every application that listens to that particular device. And then it will just send the bytes out to the robot.
So here are some resources. Microsoft actually has a lot of great samples on their GitHub account.
We have the device explorer. And then we have the URL to my MIP WinRT SDK.
Do we have any questions? I'm zoomed down. Thank you. Do we have any questions? No questions. So then let me round up just to say that,
as I mentioned before, there are 3 billion devices out there that were manufactured last year alone. It is so much fun to do this. And if you have any questions about this presentation or Windows development in general,
or if you're ever in Sweden, please come by coding off to work. So I'm kind of ahead of schedule, so I'm going to let you go. So please contact me if you want to. Thank you.