Let's Learn .NET: IoT

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] [Music] [Music] [Music] [Music] [Music] [Music] [Music] hello everyone good morning good evening good afternoon wherever you are joining us from in the world we really appreciate you taking your time to spend it with us here today my name is jamie singleton senior program manager on the.net community team at microsoft and i'm joined here with my good friend and colleague can sopher hello everybody so today we have a lot of really exciting stuff to show you i cannot wait we've been planning for this all month um we are going to be talking about iot or the internet of things for net libraries and uh we have a lot of stuff to cover so i'm going to let cam jump right into it and uh yeah let's see where we go well thank you jamie um yeah as everybody mentioned uh today is iot day on let's learn live and what do we mean when we talk about iot we're talking about internet of things which is really kind of a broad catch-all term for um running code on little computers that can be put kind of everywhere um and the the really cool thing about this is we can put like sensors in different places we put relays in different places so what this means is we can uh through code monitor and control the world around us so this is really big in like industrial situations or home automation situations um various scenarios like that so to give just a little bit of a primer on the different kinds of iot devices i'm going to start off with just a little bit of show and tell um so if we could throw my desk up on the monitor yeah there we go this is here we change the orientation this is a red this is a raspberry pi 4. this raspberry pi this is like the latest uh generation of raspberry pi devices and everybody knows what a raspberry pi device i won't say no they've heard raspberry pi um raspberry pi is a general purpose computer right it's basically just a very small pc with a low power processor and a limited amount of memory um but they can run lots of general purposes i'm sorry lots of uh general purpose code on there with um all kinds of things they can connect to we're gonna come back to this row of pins later on um if you have a raspberry pi 4 great you can do what we're going to show you today if you have an older raspberry pi like a raspberry pi 3 this is also great you'll notice the main difference between the three and the four is the three has a full hdmi port and the four has two mini hdmi ports beyond that they're basically the same form factor and all the code we're going to show you today will run on either one we can also run on raspberry pi 2. now raspberry pi is just one kind of um general i'm sorry it's just one kind of single board computer for iot scenarios there's lots of others there's beagleboard there's hummingboard um orange pie is out there what else have i seen um that's that's probably enough examples of that the other type of iot that we're not really going to go into today but um is something that i would encourage everyone to explore later on is when we talk about microprocessor uh devices microprocessor devices like this one this is an azure mx chip i forget the number that goes with it but mx chip iot kit something um this is a super low power processor and very limited amount of memory device um that is really i think better for a lot of scenarios where you're putting sensors in weird places and you don't want to you don't want to um have to provide a large amount of power to them if you want to operate off like batteries or whatever um this to develop for this device you would follow the same kind of development processes that you would follow for like arduino devices and then we have a video later on to talk about this device this is another microprocessor device this is a wilderness labs meadow now the cool thing about wilderness labs meadow is whereas we are going to for the raspberry pi compiler.net code and run as a.net executable within the linux operating system on raspberry pi meadow works a little bit differently meadow natively understands.net so you compile your.net code into some sort of byte language and i don't know the the you know like the metal level details but long story short it gets deployed to this little thing and it's super low power um super inexpensive uh and we'll have more about that later on um so to get started i'm gonna go to my uh to my browser here and i'm gonna go to aka.ms.learn.net iot this is going yeah for those of us following along feel free to head on over to that site as well and we can start doing the workshop together now this is a microsoft learn module if you're not familiar with microsoft learn it's a um it's an interactive learning environment that keeps track of where you are in um in the content you can come back and continue it different bite-sized chunks of it at different times um some of the content that we have out there also enables a sandbox where you get um to play around with with azure services free of charge we're gonna hopefully get to that later on uh in the presentation so this learn module um let's look at the prerequisites real quick we're expecting you to have an intermediate knowledge of.net um the reason for that is we we do a lot of you know we're working with visual studio code we're working with that sdk directly you know command line operations if this is your first exposure to net and c sharp you're probably not going to have a good time and i i would strongly encourage you to go check out some of the beginner c-sharp content that we have uh before uh doing this module um also just having a basic understanding of raspberry pi and have been having to being able to install the os and being comfortable configuring the os uh that's also a prerequisite that is uh covered in great detail by the raspberry pi docs and then finally we're going to be using net sdk fiber later now as far as hardware components you need a raspberry pi device two or greater i've showed you a three and a four i need you to have it pre-configured with raspberry pi os so if you're watching live right now and you haven't done that i'm going to recommend you just kind of follow along and come back and watch the recording later on you're going to need a bme 280 humidity barometric pressure and temperature sensor breakout i'm gonna go back to my desk and i'm gonna show you what one of these looks like that's that's the that's the breakout we're talking about this one is from adafruit um i had okay there i also have some other ones here that are just some generic sensors same here let me get the focus focus please camera yeah that's good enough anyway it's um these are generic uh bme 280 sensors that i got off of amazon they're um they work basically the same you'll see in the module there's some notes that say if you're using something other than adafruit it might use a different address and we'll get to that when we get to it so we'll need these sensors we'll need an led so just a light emitting diode a five millimeter light emitting diode like this we're going to need a resistor now i'm going to use a 1k ohm resistor for this module um you can use a 220 you can use a 330. uh i just said 1.0 k ohm because it's it's a nice round number and that's what i've been using for for testing you're going to need some jumper wires so jumper wires are wires that have little pins on the end you can plug into a breadboard like what we have here this is the breadboard we'll talk more about the breadboard here in a minute and then you technically don't need it but i strongly recommend it this is what we call a raspberry pi gpio breakout board and we'll go more into what it does here just in just a moment [Applause] so with that let's step out of the introduction and oh i don't have my navigation buttons out of the the the title screen go into the introduction here we go um so the introduction talks about how net runs on all these different platforms and iot devices uh like we have earlier we go over the prerequisites again and this part is really important i want you to have raspberry pi os installed on your device you can use the light image if you want because we're not going to use the desktop and then after you have raspberry pi os i need you to follow the instructions at the raspberry pi documentation and make sure you've enabled ssh and i2c okay we're going to use ssh to connect remotely and i'm sorry i said i2c it's actually referred to as i squared c i keep mispronouncing it uh i squared c is um is a uh uh type of serial bus that we're going to use to communicate with our humidity sensor and then the last thing before we dive into actually working with some of this hardware is to talk a little bit about the scenario that we're the the the hypothetical scenario that we're embracing uh as part of this module um suppose you work for a company that makes cheese and of course cheese aging is a process that requires specific um temperature and humidity parameters right they and this company that you're working for they use a cave to age their cheese they've built some racks somewhere in a cave somewhere and they age their cheese in this cave and and they have some some uh some standards that they've set right they want to keep it around 50 degrees fahrenheit with a little bit of variance and a humidity level between 75 and 95 um and they have tasked you with building a sensor to monitor this and to also activate a fan to control the humidity so that's what we're doing today we're building a device with a raspberry pi to monitor the temperature and humidity and to activate a fan as we need to now we don't actually have a fan here instead of a fan i'm using the led to stand in and represent a fan uh if we actually had a fan it would make that a prerequisite i i i just know that i would probably regret it so um the prerequisite we went with was an led and the led is just kind of filling in for us so i'm going to walk through the content on this next unit uh again uh it talks about the gpio header and the modules and everything let's go back to my desk so let's grab this raspberry pi 3 here this array of pins is called the gpio header gpio stands for general purpose input and output and this gpio header supports a number of different protocols for communication it also supports being able to just turn uh a pin on and off um for uh to output a a low amount of voltage that gpio header has i think these 20 pins and they're all they all have different purposes they're all um they they're all documented there's images that you can get at the raspberry pi documentation site or on the dot net iot documentation site that lists all these out but personally i don't like interacting with the pens directly on the raspberry pi it's just such a pain to count them all and know which ones which so i prefer to use my gpio breakout now the idea behind a gpio breakout is it has a little 20-pin ribbon cable and you connect the ribbon cable to your device on one end and then on the other end the breakout you can plug right into a breadboard so what does this do for us well the breadboard is a device that's made for prototyping electronic components all of these pins in these side strips down the length of the board are connected there's two strips here negative and positive the idea is uh that you provide power for all your components on these strips there's another one on the other side another set of them rather on the other side negative and positive and then in the center all of these strips of pins are connected laterally so for example looking at my gpio breakout i'll wait for the camera to focus looking at my gpio breakout if i wanted to say interact with the ground this ground pin on the raspberry pi i could plug i could plug my jumper wires into the holes right next to the uh the ground pin on the breakout and it would be exactly the same as plugging it in directly to the raspberry pi we've already discussed the breakouts we're going to use i'm going to use this this adafruit bmi 280 breakout to start um if we have time i'll switch out with my generic bme 280 but it's it's literally one line of code that i would change just to change the address and it's other than that exactly the same and then the last thing that i want to show you is a relay module now we're not using a relay module today um but we're hypothetically simulating a relay module with uh with an led now this relay module is a double relay module it actually provides two relays and the idea is that on you provide power to it with the voltage and ground pins here and the the two uh input pins just listen for a low amount of dc voltage and when they get a low amount of dc voltage they trigger the corresponding relay which will activate a device um so this is the type of thing that you would use if you were going to control a fan like in in a cave a fan is going to be um probably uh in the u.s anyway it's going to run on 110 ac voltage which is going to be you know it could it's it's more than what you're going to be dealing with with these gpio pins so the idea is that you can control the larger amount of voltage with a smaller amount of voltage now again for purposes of uh for our discussion today we're not actually going to trigger the relay we're going to just trigger we're going to trigger this led it's it's exactly the same code um well i say exactly there's there's one little thing that might be different and we'll get to it when we get when we get to that and that that covers all the components so let's put it together we're going to build our um our device using this breadboard with the diagram that we have on screen and i'm actually going to do this live i'm going to do it upside down so the orientation on my desk matches the orientation on the chart so i've already got my my breakout positioned i'm going to come along here and we're going to put the bmi 280 right next to it and then i'm going to uh put a little bit later on i'm gonna put my [Applause] i gotta look at my my diagram here my led is going to be um the anode the longer positive lead is what goes to pin 21. so we'll put that here like that and then between the negative lead and ground we'll put a resistor now you wouldn't actually need to use a resistor with a relay the resistor we're just adding so that our uh 3.3 volts of dc current coming from the raspberry pi doesn't burn out the led because that's really too high for an led so the resistor is just that is there to reduce the voltage going to the led and not burn it out in a real world scenario with a relay you would just wire it to the relay and now i'm going to connect all this stuff with jumper wires i've even color coded my jumper wires to match uh the the uh the diagram i'm gonna bring my diagram back up here so just briefly try to do this quick this is actually the fun part i don't know if you ever played with like those snap circuit kits when you were a kid um i did i never knew what i was making i i always had to follow the book um i wasn't until i actually got into this iot stuff that i actually learned any idea of what i was actually doing so let's wire up the 3.3 volts to the voltage in on the bme there we go we're gonna do the gotta look at my chart again oh yeah i got a wired ground uh ground ground ground which ground did i use i'm using middle ground on the chart let's do ground from the bme to ground looks right and then we need to wire up the actual data connection from the bme on the on the bme um the where we need to find a pin labeled sdi if you're using a generic one it'll be labeled sda but anyway we're gonna find that sdi pin which is this one here i'm using the one wire which is this one here and we're going to connect it to the sda pin on the breakout there we go and we need s the sck pin on the bme and that needs to go to the scl pin on the breakout and then i believe i've got everything wired so just to review that's that's 3.3 volt on the breakout to here let me add some light 3.3 volt on the breakout to voltage in on the bme ground i put that to the the ground um strip here so that i can connect ground to the ground strip and then also this resistor i've got connected to ground um we have s sda on the breakout connected to that's the blue one so that's sdi on the bme we have scl and it's connected to sck on the bme and then lastly we have pin 21 connected to the um positive lead the anode on the led which then goes the the the cathode the negative lead goes to the resistor so that's our device that's all the wiring we have to do for our device if i was going to use my generic bme instead there we go i was going to use my generic bme instead you notice that the pins are labeled a little bit differently and those are those are accounted for in the notes all right so that's it that's that's that's the hardware that's we should have a device uh it's not tested yet and i just built it live so it's probably not going to work uh maybe it will but we'll find out um so now now that we've built this hardware i'm just going to go ahead i'm going to connect it i've got a raspberry pi 3 here in nifty little azure case that i 3d printed and see that little azure logo isn't that fun um i'm going to go ahead and connect it now my my device is powered up and i've done this many times you might want to do this when your device is not powered it's probably safer you probably shouldn't do it when it's powered up like i just did but i just did okay oh and you can see we do have power my bme has a little green light now so it's at least getting voltage from the raspberry pi so we at least know that parts working all right so now it's time to do our software hey cam do you have a couple times for a couple quick questions before we move on just since that was like the initial like hardware installation absolutely let's do it okay great i'll make them quick um so we have a couple great questions out there um so uh ray from learntv says does the azure sphere count as an iot device i kind of think of it as a carrier board of sensors you know that's that's a good question um unfortunately i'm not real well versed on azure sphere um that's kind of its own little thing that i haven't seen a whole lot of crossover with that in the dot-net area yet maybe um maybe i know we have a expert moderator um laurent who is helping um answer questions in the chat maybe he's seen something there but i really haven't had much exposure to azure sphere sorry no worries i just wanted to ask that really quick um yes we do have a moderator in the chat so feel free to ask your questions there we will get to as many as possible yeah and i guess yeah is there um is windows iot going to get an update for raspberry pi 4 that's an interesting question i don't have any idea so so people um just to give a little bit of background i the question there is does windows iot going to get a update for raspberry pi 4. i honestly didn't even know that it didn't have the the capability for raspberry pi 4 because i never use windows iot windows iot is a version of the windows operating system that is it's a very scaled back kind of minimal version of windows designed specifically to run on single board devices like raspberry pi i've done personally i've done very little with it raspberry pi i think the most compelling scenario with raspberry pi is raspberry pi os the linux os because it just opens up your world to all kinds of different things inside and outside of the.net ecosystem so um that would be my recommendation honestly is if you're doing raspberry pi do raspberry pi os i'm not much of a linux i i'm sorry windows iot expert okay alrighty we'll keep those questions coming we will get to them we will also save some more time for for more direct q a and i'll let you keep going with the workshop sure thing so this is the part where i need to introduce everyone to the.net iot libraries so the dotnet lt libraries is a collection of two open source new get packages system.device.gov and iot.device.bindings system.device.gpio really provides kind of the low level hardware interface it's good for flipping bits on and off uh and uh it implements several protocols like the i squared c the spi uh so that's serial peripheral interface uh pulse width modulation uh and others now when you're working with individual devices like the bme 280 you may not want to like actually deal with a low level i squared c communication you may just want to like interact with your device as some kind of abstraction to say hey give me a temperature reading give me a humidity reading and that's where iot.device.bindings comes in iot.device.bindings is a community supported open source package that contains dozens of bindings for all kinds of different commonly available iot devices um for example the bme280 chips if you check out the iot.net iot documentation there's a quick start on there that takes advantage of a binding for this device um this device is called a raspberry pi sense hat it's really kind of a cool thing it's got like six seven sensors on there i forget the count and mine looks a little bit different because i've got some black film over the leds because they were way too bright to photograph so i got some some semi-transparent film to put on there to um to reduce the the lumens generated by the leds but the idea behind the sense that oh dear i don't have the uh i don't have my yeah i don't have my my little adapter on the bottom the idea behind the sense hat is that you it just like a hat just sits on top of the raspberry pi um and provides all these like i said six or seven different sensors there's like a an accelerometer and a gyroscope and a humidity sensor uh temperature i think two temperature sensors if that's if that's right um and of course this whole little led matrix little joystick here the net quick start for iot on the iot libraries documentation site which we will share as a resource later on includes a quick start for that this this sense hat where literally all you have to do is run one line of script on the raspberry pi and it will download a code sample compile it and run it and make this thing um working uh for you in under five minutes so um and it takes advantage the the reason i brought that up is it takes advantage of one of those iot uh device.bindings classes so kind of kind of spinning off of that question that we had earlier about windows iot core um where do where can we run this stuff where can we one.net with the.net iot libraries well the answer is anywhere.net is supported so that includes windows 10 iot core includes raspberry pi os includes ubuntu it includes mac os it includes a whole lot of places um so the hardware platform specifically targeted by these iot libraries are these single board platforms not so much on the micro uh the the microcontroller platforms although there is some some in there the the real target for for these is the single board computers um just a note i'm gonna call out this note here too uh we mentioned earlier raspberry pi uh ii being supported anything prior to raspberry pi 2 is not going to be supported so raspberry pi like the like the initial raspberry pi 1 devices those aren't supported and raspberry pi 0 isn't supported reason being that these work on an arm v6 processor and arm v6 is not supported by the dotnet iot libraries actually not supported by dotnet at all so before we move to the next unit we're going to check our knowledge uh the net iot library support which operating systems well we just covered that they are supported on yes many of them and jamie do you maybe you can help me out here which of these is not a true statement system.device.gpio enables communication using several iot protocols iot.device.bindings wraps that that package to streamline communication with iot devices all versions of raspberrypi support.net or the.net iot libraries are composed of two open source nuget packages oh okay so i'm thinking uh i'm thinking b or c does it really the correct answer is c yep the the the the raspberry pi devices that use the arm v6 processor are not supported so this one is not a true statement congratulations we passed our first portion of the workshop i mean i should hope i wrote it [Laughter] all right so finally we get to the code i'm gonna pop my little um my little learn module over to one side i'm gonna open visual studio code on the other i'm going to open up a little terminal here everybody has in visual studio code everybody has their favorite way to make new projects i i just do it in the terminal and i know i'm going to get questions on this i'm going to i'm just going to respond to it before um before they get asked why does my terminal prompt look so weird it's because i use a powershell add-in called oh my posh oh my posh.dev right and it's a nippy little add-in for uh powershell and other shells that will give you like a customizable prompt so i i use that um just to head off any questions all right going back over to visual studio code i'm going to make my new um i'm going to make my new project i keep all of my code on my d drive in a directory called source and i am going to just following along with the instructions here i'm going to make a new console app all right i'm sorry i i got ahead of myself i was not here i'll just copy and paste the code there we go i forgot my dash o command switch that dash o command switch what that does is it says create a new directory for this project named this and i i forgot it so whoops all right so cheesecake.net i'm in that directory of the terminal but visual studio code doesn't see i haven't opened any folder yet um i have a shortcut in the terminal to reopen visual studio code pointed at the current folder we're gonna type we're going to type code dot that's current folder uh dash r to reuse the current window so i've got the project loaded um if we look at the project the uh the project file we haven't got any references in so let's go ahead and add our references go back again to my terminal and i'm going to again just paste in these um net add package lines so i'm adding the latest versions of system.device.gpio and iot.device.bindings so there we go we've got our project references and then i'm going to go into program.cs i don't have a lot of room here i'm going to program.cs i'm going to get rid of all that i'm going to replace it with the code from the module let's look at this code a little bit so what are we doing so we have we have all of our are our using statements here at the top that that bring in all of our name spaces um we set a variable called pin uh and we set that to 21 and we also set a couple of booleans just to track program flow and then i initialize the gpio controller so gpi controller this is a this is an abstraction that represents uh just the the pins and the other functions of that that gpio pin header on the raspberry pi and then my code is going to open that pin 21 and write pin value dot low so what does pin value.low mean well with these gpa gpio pins there's actually several different states they can have they can have no current they can have the lowest amount of current available or they can have the highest amount of current available and we're saying okay we're going to turn open we're going to turn on that pin and we're going to set it to the lowest amount of voltage available um logically this is off this is a little bit different than what we're actually going to do with relays though in a relay like the little relay modules i use pin value dot hi is off and pin value.low is on so that's the only difference between what we're doing with the led and what we're doing with the relay is with a relay you're going to set pin value to high and with the led we're going to set it to low to start for off right the next thing our program does is it creates a variable to hold some some i squared c connection settings and we pass in the the id of the i squared c bus on the raspberry pi that id is one and we also pass in the address of the device that we're going to be using for bme 280s bme the there's a there's a enum in the um somewhere in the the name space that defines i'm not getting my intellisense oh well it defines um uh two addresses default i2 i squared c address and secondary i squared c address this is important if you're using one of the generic bme 280s because some of them use the secondary i squared c address so you would want to come in here and change that but if you're using one of the adafruit ones the default is going to be fine then we create a reference i'm using what's called a using declaration here to scope this reference until it it automatically finalizes it when it falls out of scope to to uh create an abstraction called i2c device created from those settings so we're creating a device pointed to this address on this bus and then the final thing we do is we initialize a bme280 object passing in that that i squared c device it's just another layer of abstraction but now we have our abstraction our bme2e 280 abstraction so on the console we can just write out the current status let's look at what that write status does so it says all right my device is is my fan on or off well it basically just displays the the content of that boolean that uses a ternary expression display on if fan if the fan on boolean value is true and off if it's false then it displays the temperature and the humidity that it got from the bme280 so we start this we start this method by reading the bme280 and getting the read result as an output object and that output object exposes temperature humidity and uh barometric pressure and the really cool thing that the um developers of this uh of this class have done is they use uh an open source uh package units.net unitsnet i'm not sure what it's called it's the it's the units i i anyway they use this open source package to um provide all these automatic conversions right so you have temperature dot value but value has and again i apologize my intellisense isn't working right now um but it has degrees celsius degrees fahrenheit i think it has degrees kelvin so you just say okay give me the give me the the readout i don't have to do any conversion so write status gets that and writes all that out to the to the command line and then the um our control loop just sits there and uh waits for text commands and our text commands are if i type fan it toggles that fan variable right when we toggle the fan variable sorry there's the toggling but when we do it we set we then we have that pin 21 that we were interacting with earlier we set the value to the opposite of whatever it is if it's low we set it high if it's high we set it low because we're turning the fan on and off again this is actually reversed from what you're actually going to see with an actual relay but the led is just standing in for a relay in this case we have status command to just write the status again and we have an exit command that sets exit to true which is again it's just a control loop variable to get us to get us out of the loop here and then the last thing we do before we end the program is we close the pin so there's our code and we've gone over the code that's great does it build let's see if it builds dotnet build looks like it builds all right so how do we get that over to the raspberry pi well i'm glad you asked because that's the next step so there are two types of deployment in.net and by the way another important call out on this is if you're if you're following along at home and you're watching this video or whatever um at this point you need to make sure your raspberry pi is turned on and double check that you've turned on the ssh service because we're going to be connecting to it over ssh and the i squared c service is the one that i always forget to turn on and i always get an exception that is um um i've got it listed somewhere in the notes i always get that exception and i go oh that's because i forgot to turn on the i squared c service use the razpi config command and make sure you turn on the i squared c service that is probably the number one thing you're going to forget while you're doing this module all right so net has two types of publishing modes framework dependent and self-contained framework dependent is when you have the dotnet runtime installed in the operating system and all you have to deploy are the the assemblies that come out of your project right and whatever other dependencies they might have a self-contained deployment contains all that plus all the runtime so you don't have to have the runtime installed in the operating system i like to use self-contained deployments for my iot devices just so i don't have to worry about uh what version of of the dotnet runtimes on there doesn't matter it what what version the net runtime is on there is the one that i need because it goes out with my app the downside to this is that it hey it's it's much bigger it's the deployment footprint is uh goes from being you know uh maybe 10 or 15 megabytes up to around 80 megabytes so um that's that's a concern so what are we going to do uh to get it out there so the first thing we're going to do i'm going to clear my terminal there i'm going to actually open a windows terminal here we go i'm going to zoom that guy in clear it again so it refreshes my nifty little prompt and i'm going to ssh out to my raspberry pi device now if you have changed the hostname of your raspberry pi then you're not going to ssh to pi at raspberry pi you're going to ssh 2 pi at my host name or whatever you named it right or or maybe your ad the ip address will also work if you know the ip address okay but raspberry pi so here we go and what's his password now if you are doing a lot of this uh deploying things to a pie and testing over and over again i strongly recommend you look into passwordless ssh a passwordless ssh is a method by which you can generate a certificate on the host device that you can put on your client device that serves as automatic authentication so you don't have to type that password every time you ssh in now i'm on my raspberry pi device this is the one on my desk um you can see there's nothing in my home directory so i'm going to make a directory i call it my cheesecake.net directory nothing in there all right so that's the first part now back on my device here i'll do this in my visual studio code terminal window i'm going to publish my app.net publish dash r is what says do a self-contained deployment targeted for a specific run time and which run time am i targeting i am targeting the linux arm runtime but it's going to go build and it's going to go do all that and whoops wrong button and this is the location where we have all of our files um and mine says net60 yours might say net5o i'm using a preview build of of uh the.net 6 sdk it's this is all the exact same code it works in in either um let's real quickly see what's in that location so there we go and we've got there's a cheesecake.net shim that is basically the command that we'll use to launch our application uh in linux uh we've got you know the binary the the assembly that goes behind that we've got there's iot.device.bindings.dll we've got a whole bunch of linux runtime requirements there that i don't even know what they all do keep going keep going keep going somewhere in there we have system.device.gpio yeah i don't know where that uh would help if i remember my alphabet system.device.gpio um ah unitsnet that's the assembly i was talking about that does the the conversion so there's all of our all of our different um files that go along with this deployment we're going to copy them over there i'm going to go back over to my terminal i'm going to clear it again so it resets my my prompt because it's driving me nuts um we're going to scp so that's secure copy that's built into openssh which is what gives us the ssh command in windows and linux copy all the files from that location make sure make sure you include the star right the asterisk for all we copy them to i at raspberry pi so that's username at hostname right colon and then you specify the location on that device which is going to be my home folder slash cheesecafe.net and again it props before my password and again you can avoid this if you set up ssh for passwordless access and i mistyped my password there we go it's copying all those files it's going very quick for me because i've got a really fast wi-fi network here in my office it might go slower for you depending on your network so there we go we're we're done we're copied so there's one other step that we have to do so let's verify all those files yep they're all here there's one other step that we have to do that cheesecake.net file the executable the shim that i said that's going to you know trigger our assembly and run the code um it's not executable it we're in linux and binary files aren't trusted they aren't allowed to execute unless they are explicitly given the execute bit so we are going to set that execute bit on our file and now we can run it fingers crossed ah you know what i did this is important i didn't save my code compiled that original program.cs whoops i could fix that i must type my password again too because i have one of those really clicky mechanical keyboards if you hesitate at all it registers as a double keystroke all right so coming back over here um let's run that code again i think it worked right so that looks like 73 degrees and 48 humidity in my office right now um can we can we call up my my desk so we can see the device yeah so let's uh okay let's do um we probably can't do a view that shows both my screen share wait yeah we can can we do the the one that's like the um it's like the third from the right the one that does shows my screen screen share is can we can we put my my desk there instead yeah yeah that's what i wanted to do all right so i'm going to zoom this way in zoom it way in so everybody can see all right so enter command so we have my status command that just writes it out again let's see if the fan command works i'm going to turn the fan on fan we have a fan i mean it's an led but it's a fan all right let's turn it off [Music] let's turn it on again let's turn it off again you guys have no idea if you've never done this this is super addicting right being able to manipulate real world stuff like little leds and relays and stuff in code it's just it's the most powerful feeling in the world and i love it so that my friends i believe let me check let me check the content let me check our content for the day i think that's the first part of our workshop we tested the app we just did this part we can skip that yep yep yep what to do if something doesn't work this is important so let's review this real quick before we call it a day here on this part of the workshop if something doesn't work first double check all the connections with the diagrams all right make sure you've got the i squared c service enabled okay make sure you're using the right address for your bme 280. if you're using the adafruit one or some generic ones it'll be the default i squared c address if it's not if a lot of the generic ones however use the secondary i squared c address if you need to debug the code remotely you can you can use visual studio or visual studio code to connect to the raspberry pi remotely that's beyond the scope of this discussion today but we do have it documented with an easy walkthrough and then lastly if you're just not sure that your bme 280 is working or your i squared c bus is working which i have had um i did buy a pack of those generic uh breakouts and one of them did not work and you want to test it there is an i squared c detect tool that you can install on your raspberry pi that will help you troubleshoot that so that that's that that's that is all there is to using the.net iot libraries to construct internet of things devices with net well that was amazing you didn't see me i also got very excited when the light turned on um it did feel like a little bit of magic there um so that was awesome i hope all of you following along uh we're also able to get the light to turn on um or are able to do it in the future when you uh watch the recording um we have ton of great questions here we're really good on time uh what do you say you should we bring a few questions on in let's bring in some questions excellent okay um and we also have um laurent in the chat as well um answering questions um so uh you mentioned that.net uh runs everywhere and that this works with it but does this work for f-sharp or for visual basic vb um the short answer is yes you can consume um you can consume the net iot libraries with vb and f-sharp in fact i was recently contacted by a community member who looked at all of the samples can can we throw my screen up there again real quick um so if we in the resource links we have a link to the dotnet iot libraries documentation and um i was recently contacted by a community member who who has went through these four tutorials over here and he he loved them and he said you know i've been going through these with my daughter but we we do vb instead would you like for us to contribute vb samples and i said sure so that's that's something that we've actually got in in motion right now as a community member is is converting the samples that i made for these tutorials to vb um if somebody out there is an f sharp person i'm not but i have friends who are if you're an f sharp person and you would like to do the same please by all means um go right ahead excellent yes and i wanted to bring laura on uh as uh during the q and a uh look at another expert yay party is all here um so um yes as you heard we are always accepting folks who are working on different uh technologies and we do um as an open source project always looking forward to those um so uh we have a couple questions from youtube um can you talk a little bit about the industrial protocols like modbus tcp and rtu and ethernet ip all that stuff um so some of those uh terms that i just heard i don't actually know the definitions of laurent how about you yes i know some of them but but really by hand the the key point is yes there is a support limited support for some of them you know.net iot now for anything which is ethernet anything which is networking that's part of the os that support is with net whatever platform you are using we're using the us for that so that's really transparent i see another question about mqtt so of course you can use mqtt you can connect to azure you can do all of that all the networking part is supported now for things which are um more specific there is part of the support for those ones and we are welcome as you say um jane it's open source so can you contribute contributions are welcome we do a couple of people who have already been routine in some of those their industrial protocols excellent um yes is it possible to integrate more sensors using a serial protocol yes it is you can use it you can use the normal cyan support system.io.port is supported for all platforms some of the bindings are using that some of the nfc readers for example and some others so yes of course you can use it ah excellent um and i know you answered this um in the chat around but uh is the iot library available for c plus well any any language that support.net can consume it okay so come already answer about vd so if you are doing um uh development we see plus plus then yes if it's not 50 plus wizard.net that's going to be more complicated so no in this case okay okay um let's see uh can you talk about other frameworks out there besides iot libraries um there's a lot of chat about windows iot uh but cam mentioned a few other ones so i'll start this one excuse me coffee went down my windpipe and that's not helping so um i'm just getting into the the the meadow the the wilderness labs meadow um i had a great conversation with one of the developer advocates the other day they actually have their own library um this this device doesn't use the iot the net iot libraries they they have their own um that they're actually going to open up they said to additional boards um so that's one example i can think of as far as other libraries uh what about you laura oh well i can think about the netana framework running on devices like this one esp32 stm32 so it's very complementary and the interesting part in there is that all what you've what you've shown can you can do it with an esp32 for example with the dot net on the framework it will be almost exactly the same code almost exactly the same thing the reason is because the ui the api are aligned so um you can use and with importing as well a lot of those bindings to uh net nano framework so you have the bmp it's using as well unit snatch so it's very very easy and very very simple and the experience you have is the same you can debug with visual studio um 2019 and um that's another one but that's for mcu's right and as you explained came at the beginning that's for very little um a processors when you have a little little bit of of space but the advantages as well that runs on the battery for example or on the solar panel and what you can you cannot really do that uh seriously with the raspberry pi now i do need to i think that question i think was asked in such a way that i need to clarify something a little bit i need to differentiate between windows iot core which is an operating system and the windows iot libraries which um those were created to run on windows iot core and do very a lot of the stuff that the net iot libraries do today um the the net iot libraries are based on the windows iot libraries except they run in.net.net core and when run basically anywhere um and including windows iot core right so basically what we're saying is the windows iot libraries and i wouldn't so much bother with those i would go here first even if you're even if you're developing for windows use these libraries instead even if you're developing for mac you can run.net iot on the mac and use a simple extension for example you can have a gpio an i2c spi you can have all this fan as well do not really care about the os behind just have to be a net lover and that's it great uh well you both actually mentioned um a few really amazing technologies uh for iot uh wilder meadow uh wilderness labs and also nanotech and we actually um spoke with uh both of those teams and uh we have a couple demos for you um to kind of elaborate a little bit more um for nanotech and wilderness lab so if uh now is a good time i say we queue up that video let's do it for the demo all right let's see hello i'm lauren erbach i'm principal uh software engineering manager at microsoft and today i'm with jose to speak about the net nano framework jose can you introduce yourself and explain us what's.net micro framework is sure laura hi uh i'm jose mourish i'm the ceo of ecosolutions one of the co-founders of net neural firmwork and recent microsoft mvp on developer technologies um about nano framework dot net network framework it's um it's a framework too that allows you to write c sharp code uh for uh small constraining video systems uh like a sd microcontrollers esp um and all those um to get you started with it the the best place to go is to our github organization where you can find several repos that can help you with with that for example there is the the samples repo where you can find pretty much code samples for every api that we have available from the famous hello world with the blinking and led to complex projects connecting to azure iot there is also the the iot device bindings which we share with without net iot core that help you get started with pretty much every device that found out there like sensors stepper motors others um wherever you are up to you'll find pretty almost certainly the and you get there for it um also there is the the firmware for the the various devices um and the one yeah on the firmware side jose i see esp32 stm32 ti and xp are those the only boards supported or i mean because i see like different numbers so you can support many others like we have support for other bots these are the reference targets the the main ones and we also have the the community boards which are provided by community members uh those kind of expand them the the reference ones um you also they they also target the the same platforms uh of the the reference ones and what i can propose you can do a demo maybe so the the blinking one is usually the best one right so here i have uh i have uh some code it's very simple uh the it's a device a gpio i create a pin i open the pin the number two is the embedded led and what you will do is just like change the state of the pins and i put a breakpoint you see it's already deployed it's already on a breakpoint and i toggle the value which means like it was off as you've seen now it's on i have the capacity to see the variables i can add them to the watch for example and i can see them here i can continue that will pause again i can change the value here as you would do in a in a normal net application and if i continue i should have of course here now one more which is 11 because i change my value i can remove the breakpoint i can continue like that and do i mean pretty much what i'm used to do when i'm developing in in a c-sharp do you have any questions for me on that on this demo um yes um why do you have all those three negative references there oh the nuggets it's because in nano framework we do have a lot of nuggets like you are used to for developers they are just smaller than the normal ones we are trying to break things down to keep the size very very small and yeah you can see like you really have hundreds of of them and you already mentioned that we have uh things from uh iot device and a lot of device that already set up so you don't have to write low level stuff with spi to see etc that's fully transparent for you if you just say this device give the piece and all the magic happens cool so you just grab exactly what you need it you don't have to bring everything all the time to make your firmware image huge absolutely and we have a beautiful getting started guide which will guide you on how you can make your first application in minutes it's about installing the uh the the the the visual studio extension it's about tools to be able to flash the device and it's about increasing your first blinking application of course because we are in embedded world all right um also was mentioning that if you need help the the best way is to go to our discord server uh on there you can find a friendly and helpful community where you can ask everything about your development discuss ideas uh suggest the new stuff that you want to to work with and interact with with community members thank you jose and looking forward to see all of you in the netherlands framework thank you have fun with dot net nano framework well thank you lauren for pre-recording that demo for us that was amazing um so uh i think so yeah let's actually we do have one more demo um for you all talking about meadow from wilderness labs uh adrian was kind enough to give us a demo on that as well and so let's go ahead and run that hi everyone my name is adrian stevens and i'm a program manager for microsoft's developer relations and i'm also an advisor for a company called wilderness labs and wilderness labs makes a platform called meadow meadow's really pretty amazing it melto enables you to build embedded apps running on microcontrollers using full.net this is the same modern.net you'd use for other platforms today it's using.net standard 2.1 and net 6 is coming soon so this is not a slimmed down iot specific development package that's full.net and being a modern.net platform it has strong security posture so it's built for enterprise it includes something called meadowcloud which allows customers to monitor update and manage hardware over the air out in the field so why does meadow exist well traditionally embedded development on real microcontrollers is well it's hard and it's often done with languages like c and it's written by people with deep expertise and a lot of experience on specific hardware platforms and so meadow is here to try and drastically reduce that time to market for hardware and really make embedded development much much easier and we're in the midst of a bit of a quiet revolution so the last decade we all heard about mobile mobile mobile mobile but something has happened the world is rapidly deploying smart technologies and embedded computing or iot and it really is the new epoch like in my lifetime we've gone from this vision back in the 80s the bill gates quote of you know a computer on every desk well we've now gone to a place where we can literally put a computer in everything everywhere and this really is a consequence of that mobile revolution that brought us this amazing hardware and commodity cheap sensors and microcontrollers and parts and what's happening now is this iot market is now more than three times the size of the mobile market and in just a few years there's going to be 10 times more non-mobile connected things on the planet as people 75 billion hit to things by 2025. this is bringing a real shift in the culture of development so nearly everyone now is tasked with building iot companies that previously would never even consider building hardware have now hired teams dedicated to iot to microcontroller development and a lot of these teams well honestly they're failing they're failing early which is good but they're still filling and that's bad we're in a market where there's a real lack of skilled iot developers we have a lot of non-embedded developers that need to build hardware and so wilderness labs has set out to make hardware development as fast and as easy as doing web or mobile apps so for meadow there's no real tool chain setup you open visual studio you hit file new and so or it's as easy as opening up the terminal and typing dot net new so not fight with make files you're not set up compilers you're doing dot net development like you would for any other net platform and part of the magic here is the iot stack so it does all that cool complex real-time os stuff on the bottom but meadow puts these layers on top of it so you don't have to learn real-time os programming use your net skills using the meadow stack to build pretty fantastic hardware applications so build hardware like software so if we look on the left imagine we want to control a servo sounds like a complex thing well with meadow we would instantiate a servo object new servo you tell it where it's plugged in physically onto the meadow board and possibly half in some specific configurations in this case the type of servo it is and that's it and now we can interact with the hardware in software it's just regular.net coding and this really opens up a world of opportunities wilderness labs has invested heavily in creating an open source driver library to control all of these peripherals all of this hardware so of course you can do things like make games you can build rovers and robots you can build intelligent devices that can measure distance or sound or take pictures and of course it's a dot-net platform you can integrate with azure you can integrate with the xamarin mobile application commonly over wi-fi or bluetooth and this is a really fantastic platform to build enterprise hardware and enterprise iot but also a really fantastic platform for learning.net development thank you very much i love the dot-net community i love seeing all the innovations happening here if you want to learn more about meadow and wilderness labs head on over to www.wildernesslabs excellent well thank you all of our good friends at the wilderness labs and showing us meadow and also thank you everyone from nanotech laura and your team um we have a few more questions that i want to get to before we get on to our next portion of um the second workshop and i'll just go ahead and read those right along now sure um great so uh what about deploying.net 5 or 6 to raspberry pi and run the development entirely on the raspberry pi you can do that there's a caveat however um the caveat is that for debugging and intellisense um in like visual studio code or uh well visual studio code is all you're going to be able to to run on raspberry pi that has uh you know it has c-sharp functionality built in to there is a dependency on a project called omnisharp omnisharp is installed as an extension in visual studio code um it's the c-sharp extension i forget what it's called exactly omnisharp does not run on arm devices so you can develop you can you can write your code you can compile your code it's kind of slow like the the sdk is really not optimized for that environment um you can do all that but you're not going to be able to debug it and you're not going to be able to get intellisense for that i would recommend to use a normal windows mix on my device and use an extender that will provide you eye to cspi um and gpio like the ft 422 the ft edge or even just a simple arduino that you can use as a as a extender funny you should mention that lauren i just got in the mail my ft 232 i'll show it up there my f uh come on focus uh anyway this is one of the boards that laurent is talking about here i'll throw it over on my desk maybe you just you just plug it and you install the driver and then you know you get uh you get really the the all the beauty uh on on your on your main machine pc mac linux it doesn't doesn't matter for development purpose that's really the best and you can then enjoy the best environment either visual studio if you love it on the pc is there i don't know ds code if you'd like on mac it doesn't matter then it will be much more efficient yeah so here on the one end we have just a little usbc connector and yeah you can plug it right into your windows machine and it gives you these pins are basically the equivalent of the all these different holes on the board that i haven't i haven't soldered a pin header into yet these are essentially the same as uh i won't say the same but very similar to the gpio header on a raspberry pi and it's it's basically giving you a gpio header on your windows device excellent good timing on the delivery there right yeah um okay so um laurent you had mentioned this in the chat um but i'm just gonna address it here for everybody um what other boards are supported is the esp32 supportedby.net as well [Music] so that's for the net nano framework that none of them works worse works on mcu's so those little things so all the esp32 family that supported stm32 as well and then we have like some nxp nti uh ti board supported as well we have like some flagship board if you want those where we put a bit more support and then we have a lot of community boards as well and to create board with any processor and any stm321 for example it's a matter of for day and then you have your own custom one where you can then have the fun of a running.net on an mcu gotcha excellent um so okay let's move on uh what kind of architecture should i use to read stats and control iot apps from a web api for example a blazer wasa map or azure iot hub oh that's a very open-ended question um there's lots of different ways that you can do it funny you should mention it though the second half of our workshop today uh is about azure iot hub which actually provides that kind of functionality that you're looking for it allows you to um make it easy to get information from and send commands to an iot device over the cloud which then you could just abstract away behind like your your your wasm app or you know whatever other ui you want to build yeah so good it's a good timing on the question because we will actually cover that in the second portion of this event um and it looks like uh our friends got to speak at some of the wilderness with some of the wilderness labs folks we do know that they are sold out they sold out in one day which is amazing um congrats for them uh it says that they're allowing pre-orders so uh keep on the lookout for that um so okay well should we get started on the next portion yeah okay let's let's um let's bring my desktop up and um and we'll start on the not my desk my my my screen sorry my mistake okay we have a lot of we have a lot of uh cameras going here yeah um all right thank you so much for joining us for the q a um we'll you will still be hanging around in the chat if you can yeah i'll be on the chat see you later my friend i love hanging out with that guy all right so um yeah what are we going to do we're going to talk about azure iot hub now i'll be honest i just kind of discovered azure iot hub this month uh working you know prep preparing for this um this discussion we're having today um we were looking for some kind of content that would that would would take you know what we did with the with the net iot uh workshop that we did a minute ago and extend that and i found this great learn module um and we've we've uh we've made a short url to get there akka.ms slash learn dash azure dash iot dash hub and that takes us to this learn module remotely monitor and control devices with azure iot hub let's just hop right into the introduction now if you read this introduction it's going to look pretty familiar when you get down to the bottom because i when we wrote that.net module and we wanted them to go together we we said you know we're going to go with the same scenario and the kind of the same you know we want to monitor temperature and humidity in this cave so that's it's these two modules were really kind of designed the.net module was really kind of designed to go with this azure module that that that preceded it um this module will also support node.js if if you're more comfortable in javascript than c sharp but um we're going to be doing all the c sharp today so we'll hop right in so the very first step that we're going to do if you're doing this module and you come to this module uh you're going to see a big button right here that says activate your sandbox remember i mentioned earlier about microsoft learn we have this ability to do sandboxes that give you um free microsoft azure for a limited amount of time to learn with i went ahead and activated mine so we wouldn't have to wait on it uh you're gonna have an activate button uh and you will it'll take like two or three minutes to activate and then you'll be able to launch the azure portal right so i'm going to just middle click on that guy and create to create a new tab wants me to sign into the azure portal okay and once you're here you're going to go to create a resource you're going to search for iot hub and you're going to click create it's going to want to know what resource group you want to uh put your resource in and there's only one resource group available to me in my sandbox and it's been created for me and that's this learn dash followed by a grid so we'll pick that guy we're gonna name our hub we're gonna name it cheese cave cam soaper i'm actually gonna name it three because this is my third one today because i've been preparing for this part and pick a region close to you geographically close we hop over to the networking tab there's nothing we need to change there we hop over to the management tab there's nothing we need to change there either but i am going to go ahead and draw your attention actually you know what instead of zoom in let's let's zoom in on the browser there we go um i'm going to draw your attention to uh the pricing and scale tier the standard tier is selected by default standard tier um they even give you pricing down here uh that you're you're uh is it showing me my pricing no it's not showing me my pricing if you go look at the pricing calculator it's 25 a month for up to 400 thousand messages a day i did the math um actually let's let's do the math again um where's my where's my numerator keypad [Music] so let's say we send one update message from our device every second so that's 60 seconds in an hour um 60 seconds in a minute rather 60 minutes in an hour times 24 hours in a day that would be 86 400 update messages per day if you updated if you sent a message from your device every second so that s1 if you're just doing one device that s1 is plenty um there is a free tier as well right so we could we could select that and that's limited to 8 000 messages per day that'll work for a lot of devices too obviously you just wouldn't want to you know send update messages quite as frequently we're going to go ahead and leave it on standard because we're not playing with real money here we're playing with a sandbox so there's no reason not to there's some other settings down here earlier it had when we came in here and it was it was it was set to s1 standard tier it was also set to four device to cloud partitions that's just how they divide up the uh uh the cues for incoming messages from your devices four is plenty for most applications in fact for what we're doing today we could turn it all the way down if we wanted um i'm just not going to bother i'm just going to leave it alone and we can go next next review and create and gives us all of this um uh validates our our choices shows uh here we go here's the pricing 25 dollars per month um and then we could click create now i'm going to go ahead and click create and it's going to take me to this deployment in progress screen there we go and it's going to sit here for an indeterminate number of minutes um because that number is indeterminate and because i was planning ahead i've already provisioned one of these things um i provisioned it while we were watching the videos a minute ago so um this is cam sober too cam sober one actually glitched and is still deploying so uh we're gonna use number two here uh there was a moment of panic in there when i wasn't sure it was gonna get done and i just anyway um so we've we've created our iot hub there's a couple just all defaults there's a couple of things we need to do to configure it the first thing we're going to do is we're going to go down under explorers in the menu here and we're going to select iot devices you see we have no devices i'm going to add a device and i'm going to name this you can name it whatever you want the workshop uses cheese cave id i would have said like cheesecake device but i'm just going to go with what the module lists and there's nothing else we need to change we're just going to create it okay it says there are no iot devices to display i've had this happen to me a few times just refresh and it'll it'll list all right so there it is and we're going to go into the device and it lists let's look at what we have here we have the ability to send a message to that device we have the ability to invoke what's called a direct method we'll get to that in a little bit we have the ability to look at something called a device twin we'll get to that in a bit too but for now what we're going to do is we're going to take this device we're going to come down here to where it says primary connection string i'm going to copy that and i'm going to pop up an instance of notepad and i'm going to paste it in there um since this is a this device is transient this thing is going to go away after this workshop so i don't care if you guys see the key right now um so there it is that's the connection string so the connection string contains the host name which you see i have a a the host name is actually cheesecake camsober2.azuredevices.net contains the device id and it contains the access key to get into it all right i'm gonna get that off screen before somebody starts messing with me live um now i hope i didn't give anybody ideas now i'm going to come over here to the shared access policies that's one connection string that i need that's the device connection string i'm going to label that in my notepad by the way device connection string you can't see that but we'll come back to it and then over here under shared access policies they've created some shared access policies for us for different identities to interact with this iot hub now we're we're going to grab the the um shared access policy that we want to use for our back end service now in general this this policy right here service might be the one you would use for that if we go into service we can see service has permissions for service connect and that's it well we're going to be doing stuff later on with device twins and other properties that require registry read and registry right i could add those to this identity but i'm not going to in fact the the the module says to use the iot hub owner policy i don't know that that's the best idea because we come in you see he actually has everything but for purposes of this discussion we'll go ahead and use it so i'm going to copy that connection string bring up my notepad again service connection string and you can see that connection string looks very similar same host name except instead of defining a device we're defining the shared access key name and then we actually provide the shared access key and then i need one other thing i'm going to come down here to where it says built-in endpoints and that built-in endpoints that's where they provide other endpoints other ways to interact with the iot hub that uses that use um other protocols for example the event the azure event hub that this thing is built on on the back end we're going to grab that event hub compatible endpoint and we're going to make sure that it's for this shared access policy our choices are iot hub owner or service we're going to make sure it's for iot hub owner we're going to grab that endpoint that event hub compatible endpoint whoops there we go and you'll notice that's a that's a completely different connection string format i mean it has some similarities but it's it's different all right so let's grab this tab here i'm going to dock it here close that tab go back to our exercise what's next make sure i got everything make sure i got everything we covered all this we did all that all right check our knowledge which iot hub left hand menu entry do you select to locate the service connection string do you remember jamie i'm gonna say shared access policies okay and which iot hub device and service connect i'm sorry i all iot hub device and service connection strings begin with what text do you remember so i'm going to say host name because i saw it in your string you and you are right in both cases yay congratulations everybody who did it online yay all right so now we're going to write code to send and receive telemetry right so the first thing we're going to do and this all talks about how we're you know building this basically a virtual device this is all going to be in software this isn't going to take advantage of the hardware device we built in the previous workshop although i might have a surprise pertaining to that later on um what we're going to do right now is we're going to build a virtual device just in code when you come to this exercise make sure you've selected c sharp um i'm going to do it with visual studio code i did run through it with the visual studio directions just to make sure they were up to date and and and uh you know applicable to what we're doing uh so if you want to use visual studio that should probably be okay but i'm going to do everything in code so um from code actually let me make a let me open a new code window because i took my other one to a different monitor we're going to just like i again like the way i like to make new projects in code is through terminal and go to my d directory source dot net new console uh oh and i'm gonna call this one cheese cave device all right we're going to go into the cheesecake device directory and there were a couple of a couple of packages we needed to add actually three packages so i'll just copy those clear my console here so it's more legible i'm just going to paste in and i'll hit enter for the last one so we've added the packages we need and now i'm going to open it up in code so again just a regular old console app here's our project file that shows our our um our references that we here let's pop that up shows our references that we added um if we go to program.cs you know there's the standard hello world you can guess what we're going to do here then they give us some code and this code is actually a lot longer and a little bit more convoluted than would be my preference but we're going to run with it really quickly let's just step through the flow of the code so when we launch the app we're setting some constants here and we're creating some static variables uh for like this device client thing and oh wait your device connection string we're gonna paste that into the code now i hope this goes without saying but i'm going to say it anyway don't ever do this right this is the connection strings on encode are always a bad idea unless you're doing exactly what we're doing today which is just like trying to learn then it's okay but don't ever do it in production code please please please please don't ever do this um all right so you use something like azure key vault or or whatever all right so uh let's get down to the main method the program entry uh don't have my outline because for whatever reason i don't have my c-sharp extension but that's fine we'll just roll with it all right here's main so main instantiates that device client static variable creates creates it from the connection string and then goes to the send device to cloud messages async method which if we go to connect device to cloud messages async it's doing some things here to simulate telemetry it starts with an initial temperature and initial humidity and then it does some stuff with random numbers basically to just generate a new reading and occasionally generate a a a fake fan failure and just generate telemetry to send up to iot hub that's all it's doing it's just all this code is just doing math to make some random numbers to make it look like um to make it look like a humidity and temperature setting and then the magic that we want to get to is down here where we are creating a message and that message contains all the different properties that we've calculated and and you know mocked up you know sensor id and the fan alert if it's if there's an if there's a fake alert that we generated and if there's a temperature alert because our our current temperatures outside of our desired range and things like that we're going to build all that into a message and then we're going to send that message on the device client okay that's all this code does generates telemetry sends it up to iot hub let's real quickly pop open my terminal here and that build hey it builds great i can't remember in the steps if we are supposed to run it at this point or not so let's check uh do save test run the app okay so we'll go ahead and run it dot net run all right so it's running and you can see it is generating fake values um 70 degrees and 99 humidity and there it goes it's just gonna do that every every so often and that's that's all this program does it's just a virtual device that is you know reading temperature and humidity and sending it to iot cloud exciting um we're going to leave it running right now we're going to make a second app though so let's make a new um isn't there a new window no there it is it's at the top make a new visual studio code window same kind of deal and i'm gonna make another console.net new console oh this one's going to be cheese cave operator and we're going to go into the cheese cave operator directory and from in there we are going to add am i still in yep i'm in the right place we're going to add some packages again there's some more dependencies that it has to take advantage of the iot hub libraries there we go and then we'll open this project in our existing visual studio code window all right so oh wait ah there my there my c-sharp extension kicked in this time so we'll select the the cs proj and hopefully let it do its thing so just like before we've got a project file we've got a program file we're going to blow away the contents of the program file and we're going to copy this text in this code and just like before i'm going to go ahead and say just like before i'm going to go ahead and say yeah this code is a little not optimized there's some stuff in in there that that i really wish the the author had done differently um and i would have updated it but because the module supports node as well as c sharp i decided it was best not to rewrite the whole module i so um we're we're kind of paying for um the fact that we're trying to keep things a little generic um just like before though there's a placeholder so what it wants here is our event hub endpoint so i'm going to grab that from my text file go and what does this code do well if we jump down i should have my outline now i do yay all right so if we jump down to main it instantiates that event hub client remember i said we event hub is kind of the underpinnings of iot hub we're just going direct to the event hub to get telemetry this time um and we're getting some runtime information and some information about the partitions and we're connecting to the partitions uh within uh within the iot hub to start receiving messages right and we we just run it as a as a background task one for each partition um what does that receive messages look like well every so often um where i forget where the uh where the i think it's the 100 milliseconds there i think that's milliseconds that milliseconds is that what specifies the time now i forget what's setting the time on this and because we are pressed for time i'm not going to go too deep into it but every so often it pulls for new messages on the event hub partitions and if it gets one it writes it out to the console so we're sending telemetry from from our virtual device and this code is our operator code and it's getting the telemetry from that device and iot hub is sitting up there in the middle uh brokering that information exchange so just like before let's see if it works build and remember if you will that cheese cave device is still running put him over on that side and i'll put cheesecake operator on the other clear my console so we can read it again dot net run so you see it connected to the partitions on the iot hub and now it's displaying the telemetry that's being sent which includes the temperature and humidity values as well as the fact that the device says those are out of range so they've added you remember in the code back for the device there was um it adds temperature alert and humidity alert if it's out of range and that's that's what that looks like so that's working we're sending telemetry from this app going up to iot hub and getting it in this one so what's next well i'm glad you asked so let's go to the next step the next thing that we're going to talk about is how to invoke a direct method so in interest of time i'm going to kind of gloss over this but the remember our device we need to be able to turn on a fan remotely we need our operator uh code either manually by like a like a human or automatically to say oh humidity and temperature about a range let's turn on the fan so what that involves is a method on the device to turn on the fan and some way to invoke it from the operator code so that's what we're going to do so come to this step make sure you have c sharp selected and we're going to add this method this set fan state method to the cheesecake cheesecake device app first so let's go over here and let's stop running there we go terminal for now we're going to come down here to the end of the class and we're just going to paste that method in that method here let's uh shift alt f there's no formatter for c why are my extensions all messed up that's no fun anyway uh that method just sets the fan state right it takes um it takes a a method request that's passed in and it if the the uh uh if the fan state is currently failed throws an error otherwise it looks at what was passed in as is the data uh to uh determine uh whether we're turning the fan on or off and it's just going to display that output that's all that does again this is all just virtual this isn't a real fan um and this what's the content of this method is less important than the fact that the method exists the method exists there it is now we need to wire it up coming back over here so the way we wire it up is one line of code in the main method so let's go to main there's main and it says says to do it after creating the device client so here's where we create the device client s device client equals so right after that we'll put our code that sets that callback now when this code basically says hey when iot hub gives you a set fan state method i want you to invoke the set fan state method right when he requests set fan state you go do the actual method called set fan state that's all that this does let's make sure this builds okay it builds in fact let's go ahead and just run it and we'll come back to it okay so on the service side how are we going to invoke that direct method well the first thing we need to do let's go back to my operator look at this stop that i'm gonna close my terminal first thing we need to do is up top we're gonna add a couple more global variables we have one to hold the iot hub service client and one to hold the iot hub service connection string now why are we using the iot hub client here instead of the event hub endpoint it's because the iot hub client is what's required for doing a direct method invocation the event hub can't do that the event hub you're just reading the events coming from the telemetry device you're not a you're not able to send things going back the iot hub client that we're creating here is what you uh is what allows you to send things back so you remember over on notepad we had our service connection string we're going to put that here too far my usual disclaimer don't hardcode connection strings all right um let's go back to the exercise what else do we have um add the following task perhaps after the main method okay so let's let's add that after the the main method where's my main method ah i have uh i have my outline here we go so we'll put it right here now i can alt shift f and get my code formatting there we go so when this invoke method task is started it's going to create a basically a cloud to device method name that invokes whoops i keep pointing at things that invokes the set fan state direct method and it sets a couple other parameters like how long we're going to wait and we can also set a payload value and for payload we're going to set the fan state to on the other options would be off or or whatever and then we're going to invoke that that device method if it was successful we're going to display a success message and if it was not successful we're going to display a failure message and that's all that does and then the last thing that we need to do so we created this task that does all that we created the the service client variable and the connection string variable the only other thing that we need to do is we need to actually add the code that invokes it right so we're gonna add that to the main method what where did it say creating the receivers to listen for messages ah look there's where we create receivers to listen for messages there we go it's just f so now we're going to invoke that method and wait for it synchronously because this main isn't an async main which if i were refactoring this i would but i didn't refactor it and we're just going to run with it like that even though that's really not a best practice david fowler would slap you all right he wouldn't slap you he's a very nice guy but he he would have things to say okay so this should build right let's find out i should save it first okay so let's put that on one side we over here we're getting telemetry and we have the uh the the thing set up to get a call back on a direct method so let's invoke that by running the operator code hmm direct method failed time out that's interesting live debugging is fun that is interesting indeed what did i break let's see if we can find out i'm going to check my connection strings first because that would be the first thing that i would check so this is my iot hub owner identity my um iot hub endpoint that is all correct i really could not say why it's timing out right now let's run it again yeah it looks like it's timing out again because right away it should come right back and say oh we've turned it on i think it's going to time out again looks like it is well here let's let's run possible that i didn't save my program before i ran it over here which could very likely be the problem try that again there it worked yeah that was my problem i i didn't because i'm not using my c sharp extension on the device it didn't work right and didn't give me warnings and everything but you saw let's let's scroll back up here you saw right here fan set to on when it did the direct method set fan state so that's all it does it when we first start the program up it turns on the fan just to show the direct method invocation now we're pressed for time so i'm going to go through this next part very quickly he says as he struggles with window management the last thing that we're going to talk about is device twins a device twin is just a json document in the cloud and what that document does is it represents the state of your device that is somewhere else your device just updates the twin and says hey the current temperature is x the current humidity is y and then that way when you need that information you don't have to query the device directly all you have to do is query the twin in the cloud it's much faster and hopefully cuts down on communication directly to the device so i'm not going to step through this code entirely i am just going to copy and paste it though so we can see device twins in action this code goes into the back end app at the end of the class so let's pop him up at the end of the class stop execution and what was how do we invoke that i'm sorry i keep forgetting to see where it goes before the lion's creating a service client in maine there we go so there's a service client so that's going to go here so this is the code that says hey connect to the registry mana connect to the digital twin rather and update the properties that code should run now [Music] and then on the device we are going to add at the end of the class and then we need to add a back so the callback just says hey whenever you get properties that are being set on the twin the cloud i want you to go uh make those proper those those uh properties on the twin happen on the actual device um i'm sorry i keep forgetting to look to see where that's supposed to be pasted uh lines after the statements creating a handler for the direct method so let's go to main right there after the direct method call there we go run running the device and run running the back end all right so i can take you now back to our azure portal and i can show you if we go to the device and we go to our device twin this is that json document is updated every time that the the code sends an update uh to update the j the the twin you can see it has the current desired values that were set by the operator code and it has the current reported values which is just echoing back the the desired values in the code right now now in a real scenario the reported values would probably be actual values instead of echoing those values back and funny we should mention that we have a way to do actual values so we're going to bring back from our first workshop cheesecake.net on the raspberry pi okay and we're going to this is i know i'm pressed for time so i'm going to try and do this quickly um we're going to go to this url akka.ms slash.net iot-azure-sample and this is going to take us to github for now later on it'll probably take us to an actual nicely formatted sample in the net samples browser but we didn't have a lot of time um we're we're gonna go to program.cs we're gonna grab all the code from program.cs copy it go back to cheesecave.net and paste it in i don't have time to walk through this code i did refactor it some basically it combines everything from this second module this azure iot hub module it combines that with all these connection strings and stuff combines that with the gpio stuff and uh bme 280 stuff from the earlier module so we actually have a real cheese cave device now all right so we need our device connection string let me grab that uh where did i leave notepad there it is i have too many monitors all right um cheesecake.net there it goes your connection strain here [Music] save we're gonna publish to my raspberry pi that is still on wait what are we doing oh i forgot to add uh i forgot to add i got project references i forgot to add so what i'm going to do is i'm just going to come over here and i'm going to get this cs proj i'm just going to paste it in there we go dotnet publish scp and we don't even need to run the operator program now okay because we have the ability to interact with the device twin in the cloud so i'm not going to bother with the operator program i'm just going to deploy this to my to my raspberry pi so here's here's my console from the raspberry pi we're going to run it that should not be what we saw did that not this is the problem with going too fast what did i forget oh no that is what we should see i believe all right let's find out so there's my current temperature my current humidity off my bme 280. let's go to azure um there we go i delete my azure window i did delete my azure window tab um cave iot devices g-hub retrieving devices come on we have places to be all right let's look at the twin and unfortunately for this demo it is not working right away however uh it was working prior to the demo today we're pressed for time i don't have time to debug it the long story short is this twin should reflect the values of your cheesecake.net device um and you can also come in here and invoke that direct method set fan state you can set fan on or you can set it off and the light will light up accordingly unfortunately this is the wow moment of today's workshop it didn't work but that's how these go sometimes that's okay this is a this is a live stream and a live demo here uh that was amazing cam i really really appreciate you taking extra time to kind of walk through that those extra steps um this was all recorded and will be uploaded on our youtube and all of our amazing channels learn tv uh thank you so much for everyone who was able to join today we really appreciate it cam thank you so much this was amazing uh thank you for our nano uh our wilderness labs folks who came in uh to help give us those other amazing demos and all the things that you can do with iot and net laurent thank you so much for uh moderating and we have um just a quick promo video as we have a huge conference coming up in november called.net conf and you will not want to miss it trust me and uh yeah thank you so much and have a great day folks thanks bye save the date for dot net conf 2021 november 9th through the 11th for the.net 6 launch dot net conf is our annual free 3-day virtual developer event co-organized by the.net community and microsoft sponsored bythe.net foundation and our ecosystem partners for three days you'll learn from featured speakers from the community and net teams at microsoft talking about net 6 c 10 azure visual studio and more this is your chance to learn ask questions live and get inspired for your next software project we've got wall-to-wall content for web mobile cloud desktop games services libraries and a variety of platforms and devices all with dot net everyone is going to learn something no matter if you're just beginning or you're a seasoned engineer don't delay head to www.d-o-t-n-e-t-c-o-n-f dot net that's dot net conf dot net to learn more and tune in [Music] [Music] [Music] [Music] [Music] you
Info
Channel: dotNET
Views: 3,969
Rating: 5 out of 5
Keywords:
Id: sKaSBh1M4M4
Channel Id: undefined
Length: 125min 36sec (7536 seconds)
Published: Thu Sep 30 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.