 Hi, and welcome back to another episode of Visual Studio Toolbox. My name is Dimitri Lailand. I'm the host and I'm here again with Zeus. Hi, Zeus, welcome back to Toolbox. Thank you. It's really good to be here. Yeah, it's awesome. So we're back here to talk about another one of our episodes based on the Windows IoT series that we're creating. This is episode number five, I believe. Yes, it is. Episode five. So today, we're going to cover some interesting stuff. So tell us a little bit about what you're going to show. Yeah. So in the last episode, we were showing how to take some telemetry from some of our sensors and send them to the Cloud. But this time, we wanted to talk about scenarios where maybe we want the Cloud to talk back down to our smart device. Cool. So we're going to look at that. I thought about the scenario of we already have a camera, and we were using that to recognize and greet somebody. But I thought, what if we could use it as a spy cam when you're not in your home and you want to know something like are the cats on the kitchen counter, are they misbehaving or you just want to check up on the house. I thought it would be cool to be able to control the camera remotely and to tell the Raspberry Pi running Windows IoT Core to actually take a photo and upload it somewhere so that we can actually see what's going on. Cool. So I guess another way that I like to think about this, this scenario is contrived. This is just the thing we're showing. But in general, any device sitting somewhere out in the world, you at some point probably want to communicate to it, get telemetry from it, send commands to it, get data from it. That's the flow we're trying to show between these couple of episodes here. Today's thing happens to be a video camera thing, but it could be any sensor, it could send data back or you can real-time request some updates or trigger some mechanical arm to slap Dimitri in the side of the head or whatever it is. It would work. This is the scenario we're showing. So let's talk about the products involved that we're going to demonstrate. So from the hardware, I mean this is just still the same Raspberry Pi we had before. Yes. We have a camera here. This is a USB camera standard. There's a bunch that Windows IT supports, right? Yes. So this is just your regular live cam from Microsoft, and that one is guaranteed to work with this. Great. So we just plug it in, there was no drivers, nothing else. No, just USB connection, so ready to go? Yes. I think if people saw all the stuff you have in front of you and they never watch another episode, they'd be like, well, it's a lot of stuff. I don't know how to set that up. But really, for this particular demo, it's just ethernet and the camera and the Raspberry Pi. That's right. It's not actually like technically, we're still sending telemetry with these sensors, but we're not really talking about them as much today. So at the minimum, like you said, you would just need a camera in order to start remotely taking pictures, which is pretty cool. Awesome. So from that perspective, we've got all the code in Visual Studio, right? We're going to show that still the C sharp project that's up on GitHub that people can take a look at, same code. And what makes then the magic work of talking through the Cloud, which product enables that? Yeah. So we used IoT Hub earlier in other episodes, and it's the same thing again. So we're going to be using our existing Azure IoT Hub instance, and it has this really cool built-in messaging feature, which allows you to connect like some kind of Azure Storage container to it. So in this case, we're going to be using Blob Storage. And it's really, really clever because once you configure it, you can just use the regular Azure IoT Hub SDK that we've used in our code in former episodes, in order to then upload a file. And in this case, our file is going to be the photo that the camera took. So we're going to be using both Azure Storage and Azure IoT Hub together to do that. And Azure IoT Hub is also going to take care of the remote call down to the device as well. So it's only a couple of products in order to get some kind of powerful scenario like this going. Cool. So Azure IoT Hub is like our gateway. It's the thing that we can send it a message and all the devices registered with it will then receive the message downstream, the ones that we intend in this case, when we have one-to-one kind of scenario. And then from the perspective of then the device sending data back, it just gets put into a blob because that's our contrived picture scenario. Cool. So is that what we're looking at here, the IoT Hub? That's right. So after you've clicked on your IoT Hub incidents in the portal, you can scroll down to the messaging section and you'll see an option called File Upload. So I just click directly on that. And if you've already set up an Azure storage account in your portal, which I would recommend you do first, you'll see it come up in the list. And so you're able to select that. So that's your account and then if you create a storage container within that account for blobs, that's going to come up automatically as well. And so from there, I just have a couple of different options. I've left them all in the defaults for now. And that's really all it takes in order to link storage and IoT Hub together, which is really cool. And then the code that you write doesn't really have to care about those finer details. It's just using SDK from there. Cool. So one specific question, like how do we tell the IoT Hub that this is the Raspberry Pi that we want to talk to? Like how do we connect the two together? Oh, are you talking about the IoT devices here? Yes. Yes. Because to me, like that wouldn't be clear. If I was watching this the first time, I might not realize that you have to go in here first, try to make sure your device is listed. Yes. And then that's how IoT Hub knows that this is one of the devices. And here's a bunch of configuration that makes it possible to write the keys. Yes. So I'm using my connection string here, and I'm using that within my C-Shop UWP app that I made with Windows IoT Core. Cool. So when we start the code, I guess the code tells IoT, hey, I'm a device now that's live and listening. And based on those connection strings, it knows, okay, we have one device listening, and then we start executing code against it. Exactly. Yep. That's really how it works. So every time the Pi comes on, it has to sort of like phone home to IoT Hub and say, hey, these are my credentials, am I allowed to like have that bi-directional communication? Okay. Cool. Cool. All right. So maybe we can take a look at the code to see like how that's configured like, all right, you have your C-Shop project, what did you have to add some new get package to work with IoT Hub for example, I'm assuming. So in the last episode, we added the IoT Hub SDK, and it's just the regular C-Shop SDK. So there's no difference, whether you're using it on like a Pi or a different machine. And what's nice is that we have this feature that essentially can listen for device method calls. So we can create a name of a method. It can be for example, in this case we have one called upload photo. And then we can have once it connects to the IoT Hub, it can say, okay, I'm now going to listen for these very special messages coming from the cloud, from the IoT Hub. And if that message happens to be a request to call a certain device method called upload photo, then I'm going to then take an action from there. So this is a very sort of standard setting a handler in order to listen for that. And that's through our Azure IoT Hub clients, which is pretty cool. Cool. So this maps just back to a method right there so you have something called an upload photo. That's correct, yep. So this is my method name for Azure IoT Hub to send. And this is the actual method that it's going to call locally. Awesome. So I have one here, it is on upload photo as you mentioned. And then from there, you'll see that a lot of this code is pretty standard. Again, if you're just using a webcam class, for example, you can just take a photo and then we can create that in a storage file instance. And then from there, the Azure IoT Hub SDK has this really cool and very convenient method called upload to blob. And that's where we give it, maybe we just need to give it the name of it. So whatever we actually called it in the first place, that should be something like I think photo.jpeg. And then we give it the photo stream and it's able to then just take care of it from there, which is actually really convenient. Like the SDK just does it in one line for you. Yeah, this is cool because you don't have to learn a separate SDK to upload to blob storage and understand all those connection strings, all of that separate stuff you normally would do with Azure storage. Here, it's part of the IoT Hub SDK is one of the scenarios they support out of the box, which is really cool to kind of point out. And so now we have this program, it's actually running. So it's actually running on your pie right there. It's showing us the temperature and stuff we showed in the previous episode. And it's waiting for a potential call. So I think one of the things we wanted to demonstrate in the episode was this notion that in the real world scenario, we would have yet another app we would have built that would talk up to the cloud then the cloud would talk down to the device. But as you're developing this part of the code, you might not have that app ready yet, right? So you might need some way to use IoT Hub in a debug scenario. So show us how that works. Yeah, I'm really glad you asked about that because it seems like you have to create all of these pieces all at the same time and it can be really overwhelming just to get the device call going. So we like to use a really cool tool called the Device Explorer. It is offered by Microsoft. And so if you navigate to the SDK for C Sharp, which is our offering for Azure IoT, you'll actually be able to download the Device Explorer from this repository here. Yeah, we'll put a link and we'll, you know, to make sure folks know. Yeah, totally. So in the releases, we do have one that was released recently and it works really well. So I wanted to sort of show you some of the really powerful features of this. I use it literally every time I'm creating devices like this so that I don't have to have like some kind of dashboard going on just to even see if my messages are coming into IoT Hub. And so the data tab as part of this program is able to monitor all of the different devices that are under your Hub. In this case, I only have my smart IoT device which is literally live sending telemetry on the desk right now. And so you can see that coming in with the timestamps and the different temperature settings. And then if I was to like try and breathe on this, then it might get a little warmer but I'm not going to do that. Yeah, we believe you. But this is actually live data coming in which is really cool. We have another tab that we can use and that is call method on device. And this is truly a lifesaver when you're debugging this stuff because you just want to, you know, test out this scenario really quickly. And so again, you choose your IoT Hub on the configuration tab that I won't show. It does actually ask for your connection string. So that's how it kind of does it. And then you can pick your device ID from there. Again, we've only got one device. And then I typed in the method name ahead of time which is upload photo. And I just wanted to link that really, really quickly back here. So this is where I'm actually setting up the keyword for that method name. And then I have to put the exact same one in here. So it's not actually my C sharp method, it's the actual string that we're setting there. It's a string that's mapped to the call back this way for that. That's cool. And then you can also supply a method payload. So you can have something like, oh, maybe I want to take three photos. And so maybe you'll put some JSON in here to say count equals three or something like that. Or you could just have a certain folder name that maybe you want to upload it to in blob storage and things like that. So I really like method payloads just for being able to call one method but almost like sending arguments in a way for it to react based on them. But I tried to simplify it just for this specific example and the payload is unnecessary in this case. So what I'm going to do is I'm going to click call method. And it's actually going to tell me whether or not the device responded. And you can kind of see in the background it's doing a couple of things. And usually we should get a response telling us whether or not it was successful or not. And so this is the actual code that ran. And if we navigate to our blob storage, we should actually see whether or not it took a photo for us of our lovely duck. Yeah, our stung duck. Our stung duck, our debugging duck, I like it. Yeah, so I'm going to click on device uploads, which you might remember is the container that I used for the file upload. So you see here, device uploads. So I'm going to go over here. I'm going to click and I'm going to click on this folder, which is where I was uploading things to you. And you'll have to believe us because you don't have a timestamp. But this is the exact photo that was just taken. And I'm going to click on photo. And what's neat about this is I don't have to have, again, another app with showing photos that got taken. The portal actually allows you to download that photo immediately so you can kind of see whether or not it worked. And yeah, so you see part of our studio in the background with the camera facing us, but you also see our duck friend here who was blown out a little bit by the bright studio lights. Yeah, we have really bright lights above us. Sorry about the duck. But there is that. We'll get little lights above you so that the duck can get its respect. Yes, I think he needs to go into makeup as well. Yeah, that's all right. So that was not a lot of code to do that. And you can set up multiple methods. And again, you can kind of filter on some of those payload properties as well, depending on what you want to do, which is pretty cool. Yeah, this is awesome. I mean, this enables a ton of scenarios. And I love the fact that we have like all these tools to send, you know, like any method in your code could be sent using that tool to say, hey, run this thing with this payload. And then we've got all the tools to visualize or download the files that are being uploaded to Blob Storage. In general, I think lots of us have built the custom tools just to get for our date of debugging before we build the real tool that calls it a real app. This enables us to just move forward. So that's really cool. Cool. Awesome. All right, well, this is another piece of the puzzle. And, you know, we'll show folks more stuff in future episodes, but is that all we wanted to cover today? Yeah, that was pretty much it. Just being able to sort of use some of the more advanced features of IoT Hub once you're actually comfortable with sending the telemetry up. All right, well, thank you folks for watching. We hope you check out the other episodes of the series. We have four other episodes before this one and we're gonna record one more that we for sure know. We might do more than that, but one more is coming. So thank you for watching and please send us your comments. Thank you for watching Visual Studio Toolbox. See you next time. Bye.