Azure IoT Hub for Cloud to Device (C2D) and Device to Cloud (D2C) Messaging

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello world and welcome to this edition of tech on fire with blaze i'm blake stewart architect at winnelect and today we'll be looking at azure iot hub and we'll be talking about device simulators and messaging from the cloud 2 devices and devices to the cloud [Music] [Music] hi guys today i'm going to do a brief video on talking about azure iot hub i'm going to just show you how to set one of these up and how you can set up a device simulator so that you can get an idea of how a ic setup is going to look like so this is going to be basically an iot hub connected up to a service bus so that you can receive messages from the device simulator and then what we'll do is we'll take the device simulator wired up to the iot hub and then we'll be able to send messages from the device to the cloud and then see those in the service bus and then we'll be able to see the iot hub actually send messages down to the device simulator as well so this is going to be kind of looking at the two-way communication that an iot hub will get you of course there's a lot of the features in iot hub that we'll be covering in future videos but i wanted to do this one to kind of give you an introduction to the idea of what iot hub is really all about and then how you can use it to get started with some of your iot projects so setting up an azure iot hub is actually pretty straightforward in the azure portal i've already actually set one up that i've got i'm wired up for my demo but it's pretty straightforward to create one of these you can just go right through the wizard type in iot hub here and you will find the option and then you can create it now the the way you the thing that's real important about this is basically having the name figured out that you have a unique name because that's what's going to be used in the connection stream whenever you go to wire up whatever it is that you're using with this particular iot hub now the device name whatever one you call it i'm just going to call it iot demo one or something like that and it will let you know if it's unique so 123 is unique so you can pick a region and then give it a name and then of course the the subscription you want to use now networking you can have private endpoints for this if you want but you can have public endpoints as well so i'm going to use public because that's probably fine for what i'm doing here next to the tier now this is basically just the quota of messages that you can use inside of a given tier and you can use different options for different ones here and the one that you can use for development is this free tier which is fine for development purposes i already have one because i'm using it on a per subscription basis but if you're just doing development work this one's probably fine if not you know you can go with a basic one and that one will give you just the basic option and that one's you know going to be robust enough for building work as well but the one that you'll probably want to use for production is probably a standard s1 s2s and that's because it's got a little bit more robustness in terms of messaging and you know the thin defender right here that you can turn on and supports iot edge which we'll get to in a future video and other things like that so all in all it's probably the best one to use for that kind of purpose but free tier is probably going to be more than enough for my development purposes since i already have one and once you have that done you can then do a review and create and it will create it so once you have your iot hub created you can go into the portal or you can manage this through another tool i'm using visual studio code for my device simulator uh but you can also do a lot of the same things that we're gonna be talking about here inside of azure portal inside of visual studio code i'm just gonna be using this because it's more familiar so the azure portal probably to be the tool that most people are going to be using to do this kind of stuff with anyway now there's a longs about this i'm not going to look at this video today but i will be talking about a couple of things in future videos but i wanted to talk about devices and message routing today because those are two of the core features of azure iot hub and devices is obviously you're going to be managing with an iot hub so you expect that to be there and the device management is pretty straightforward just create devices here and they show up inside of this register and down here you have message routing which is how you get messages from devices into the cloud and we're going to get into that in just a minute but before we get too far at ourselves in that context let's go instead of a device now i already have one set up right here but it's really easy to do inside of the the portal and you can also do this through code so you can use the sdks to automatically register devices through uh onboarding processes and we'll get into device provisioning in other videos as well but in this case what we're looking at here is device id and this is basically just to uniquely identify the device on the azure it hub here i'm going to call it device tube now you can use different authentication symmetric key is basically a generated key from iot hub that you can use to register the device and then authenticate the device on iot hub you can also use certificate so you can use public key infrastructure to generate keys and then you can have the self-signed search or you can use ca sign search as well um i'm going to be using symmetric because that's that was probably the easiest one to do uh for you know dev test purposes um and and for some production services as well that's probably okay depending on what your context is but you know search are probably gonna be more secure but symmetric keys are fine for for a lot of applications as well so i'm going to save this right here and that's just going to create a device for me and it usually happens pretty quickly and you hit refresh here now you can see i have two devices here device one device two now once i have my devices set up what i need to do is send messages either to the device and that is called a cloud 2 device or a c2d message or i can send messages from a device and that is a device to cloud message or a d2c image so to do that i'm going to be using visual studio code before i go over to visual studio code i'm going to look at message routing first because message routing is how you get messages off of an iot hub into something that you can use now there's a lot of different uh services that you can route once it's just too but the one thing that you don't want to do is keep messages on iot hub and actually use it like a message bus it it's kind of got that concept where you can send messages to it but it's really not intended to be the end point that you consume messages from rather what you want to do is have iot hubs send those messages onto something else so the messages can be processed now typically that's either a service bus or an event hub now you can use there's a couple other things in here that you can look at but the the two that are primarily again are service bus and event hub so i've already set up a message route here using service bus service bus cues really in particular so to add one of these routes you just click add and you can give it a name so if i call it route 2 or route 1 whatever it might be you can choose storage uh so you could just into blob storage if you wanted to uh service bus topic or hue and event hubs so event hubs and service bus queues are typically the ones that are going to be used or topics because that is going to be a more real-time processing of messages and these can be easily triggered by a lot of different other actions on azure such as azure functions you can do stream analytics whatever it might be and i have a whole video that we did on talking about iot architecture you can look at that that will kind of discuss you know when that you might want to use one or the other and storage might be useful if you just want to do cool path uh type analysis on the messages that are coming off of your devices but in case adding an endpoint is pretty straightforward you basically you set up the end point you choose where you want to send it and once it's done you hit save and then you end up with an endpoint that looks like this one right here now this one i have wired up to a service bus as i said already and this one right here you have a couple different kinds of messages you can send i'm sending device telemetry messages to this so that's basically just you know the device to cloud messages and there's other things that you can have in here that are part of the sdk that we'll get into in other videos but the the twinning is important right here and digital twins and device twins are the kinds of things that you use to maintain uh state data about devices in the cloud so that you can have uh device twins and digital twins available so that you can query the state of a device without actually having to query the items directly and when something changes on the device it's nice to notify the cloud of those changes from the device so that you can update whatever you're managing in the cloud for twinning purposes so that's what some of these are are going to be right here but any case that's um what some of these other messages are for but this one is probably the main one that most folks are going to be interested in that is the device telemetry messages that is telemetry coming from the device to the cloud and that's going to be different kinds of measurements that sensors are going to be picking up on such as temperature pressure humidity maybe it's images i don't know there's a lot of different kinds of sensors out there that can read stuff and then you can send up that telemetry however you want to from the devices that you're going to be using once you have that all set up then you're good to go you hit save and now your messages will be sent over to a service bus or an event hub and depending on whatever your contacts may be so i used an event hub and sorry service bus and that's really what i have set up right here it's just a simple queue i called it uh d2c device to cloud and i have already listening to this queue right here uh a listener right here which is just basically listening for uh messages from that so i i guess i closed it let me just go ahead and create a cue listener here and this is basically just going to start listening for messages on that queue so what i would expect is when i run my device simulator is be able to send up messages from the cloud from the device to the cloud that is and they show up here inside of this listener right here and then i can reverse that process and then i can send messages from my cloud instance back down to my device by way of cloud 2 device messages so it's 2a communication through the event hub like i said i'm using visual studio code for my device simulator and for that i'm using the azure iot hub extension for visual studio code and this is available in the visual studio marketplace and you can simply just install this and once you install it restart visual studio code and you'll have an option to connect it up to your iot hub which i'm going to show you how to do in just a second i've opened up visual studio code now and this is what i'm going to use for my device simulator now if you have something open azure iot hub will typically be there in the lower left side if this is all collapsed up it'll be one of these options in explore so you can expand this and i already have mine wired up to a an iot here but if you don't you can click on this little ellipsis here and you can then find your iot hub you can select an iot hub and it's basically just gonna you know choose a subscription you're gonna if you haven't already log in to azure you can log into that process uh you can choose this one here and then you can choose your your iot hub which i've already wired i've selected mine right here so i have uh this one right here and you can see the devices i have you can see device one device two that have set up and you can you can kind of explore those by just expanding the the options here and uh you can expand the options on this one and there's there's a lot of stuff here that you know we're not going to get into but in any case uh we're looking at you know devices today and here you can see the the endpoints that you have i have service bus wired up here that's the one i wired up inside of the azure portal and that's probably the best place to do it you can you can uh mess around with that here as well uh but any case um what i'm mostly gonna interested in here is on the device itself so the device here allows me to do two things i can send a device to cloud message uh or set of messages from this uh and i can also um i can also send messages to this but i actually want to do that from the azure portal conceptually will be a little bit clearer here but to send messages to the device to cloud messages i'm going to send send dtc messages to iot hub now notice i can do cloud to device messages to this device as well but i can do that from azure portal as well so that's what i'm going to do from that side once i start doing it so let's send up some messages to azure by using device to cloud messages from down to iot hub so i'm gonna be doing it from device one and um let's go and minimize this right here so i can see it and this will you know give me some uh options here you can you can give it um you know some plain text that's fine hello from azure iot hub and then i can send 10 messages 100 messages and they can do it on one second interval so pretty straightforward there and once you have that you click send and the next thing that i'll expect to see is those showing up down here inside of my listener on my service bus because i've wired up that forwarder that route to forward the messages on to my service bus so each one of these messages here is going to be the hello from azure iot so they're all the same but uh i got 10 new messages now uh from my device simulator so if i send those again i will get even more messages inside of my listener here and i'm just getting new messages as come off of the azure iot hub into the service bus and this is a service bus listener using service bus explorer so you can see here the device simulator inside of my visual studio code is sending messages from my device really my laptop but the same principle would apply from you know an arduino a raspberry pi or some other device that you're building that is a custom pcb whatever that might be so now that we've seen the device to cloud messages to reverse that process and send cloud to device messages i'm going to use the portal to do this now you can do it right here using this option but to me it's a little bit more clear in my head if i'm using the browser because i'm working in the cloud to send messages down to the device simulator which is you know this particular instance of visual studio code right here so i can start receiving c2d messages by selecting this option here it's just going to print them here console so to do that it's pretty straightforward i'm going to come over here i've already got one wired up so i can just click send message and it's going to send messages now to get to this screen it's pretty straightforward you just come over here pick your device and then hit message to device and you can type in whatever you want test123 and send the message on and that will send the message down to your device simulator right here and so you can see here's the first one i sent and here's the second one i sent and it's just printing back the telemetry that i sent now you can use um key value pairs as well if you want to add some data to that or you can just do you know the raw data it could be you know json encoded xml whatever it might be in the message payload or you can use the the properties that are available to you as well but in any case you have that body that you can kind of shape to whatever your heart's content may be so this is how you do the two-way communication using devices on azure iot hub and we're gonna be exploring this more in depth as we look at other ways to broker messages particularly around iot edge which will give you a lot more options for what you can do with messaging and broker messaging and also filter things and other things like that but any case we'll get into more of that in future videos when we look at that but this is just kind of the basics of setting up devices and messages going to and from devices uh from the cloud and then seeing those being consumed on the cloud side by a service bus then ultimately something listening to that service bus which we saw today if you like this content please consider checking us out online at www.wineluck.com where you can find several blog entries about topics related to microsoft azure and software development and you can also subscribe to this channel by clicking the subscribe button below and then clicking on the bell icon to receive notifications when new content becomes available until next time thanks you
Info
Channel: Atmosera
Views: 9,023
Rating: undefined out of 5
Keywords:
Id: TrihMsudNBw
Channel Id: undefined
Length: 17min 20sec (1040 seconds)
Published: Thu Dec 30 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.