From the course: Introduction to IoT with .NET Core

Advanced tutorial: Extending Blinky with a temperature sensor

From the course: Introduction to IoT with .NET Core

Advanced tutorial: Extending Blinky with a temperature sensor

- Welcome back to the.net with IoT series. In this video, we're going to do an advanced module. We're going to add a temperature sensor to our circuit and make the LED blink based off the reading values. So this is a BME280 temperature sensor. We will be adding this to our circuit and this sensor component can take not only temperature readings, but also humidity and pressure readings. Let's take a look at what the breadboard diagram for this will look like. So we're going to add four new wire connections that will connect to specific ports on the Raspberry Pi, which will help the Raspberry Pi get readings from this temperature sensor. So let's go grab our components and make these connections. So following the flexing diagram, I have my breadboard set up here. It's connected the BME280 sensor to the breadboard. I have made the three connections of the wires onto the Raspberry Pi already. And this is the last and fourth connection that I have to make to the SEK portal. With this complete, this finishes our circuit that we will use for the BME280 pressure sensor. Now that we finished our connections on the Raspberry Pi let's go write some code. So I'm in the dot net core GitHub repo. I've changed the tag from master to release three because we're using dot net code three in our sample. I'm going to go look for our BME sensor folder. Within this folder you will notice that apart from all other files there's a folder called samples. So let's go look at that. At the specifically look at the sample for our sensors. So this is the BME280 sample. So looking at the sample code we are going to now copy over and use the lines of code that we need to change how our Blinky code works. So instead of Blinky always constantly blinking the LED, will change it so it will only blink the LED when we have a certain reading value from the BME sensor. So let's get started. So first we can see that it does some initialization to create an instance of the sensor. So let's go ahead and do that. So we need this constant value. So I'm going to move this over into our code. The next thing is that we're going to create an instance of the BME two sensor. So let's go grab that here and see what's happening. So the first we think we need to pull in the reference. So we're going to pull in the device IO and let's go pull in the device NuGet package. So the first thing that's happening here, is we're creating a connection which uses the Raspberry Pi byte ID and the default address at which the BME two sensor is connected. We then create an instance of this device setting and initialize an instance of the BME two sensor itself. Now that we've created an instance of the class, let's use it. So, using this instance variable, I'm going to move the logic of blinking the LED inside of this using, and now we want the logic to be that the LED is not constantly blinking, but we are instead constantly getting readings from the sensor and then using that reading value to control the blinking of the LED. So let's go ahead and do that. So I'm going to move this while loop out of here and instead, move it up here. So while we constantly get readings from the IC, from the BME280 sensor, let's go set up the variable so we can get that actual reading value. So first we have to turn it on. So, what this does is, it turns on the device, turns on the power mode and then if we hover over here it says performance on measurement, stores the results and then puts the device to sleep. The next thing we're going to do is set the sampling value. So for this sample, we're going to use the humidity value to control the LED. So I'm going to go grab this to set the sampling rate, is currently set too low. Let's change the resolution to something higher, so we get some good results values for this demo. The next thing we're going to do is now read the actual values. So over here I'm going to go grab the humidity value over here. So let's see what's happening here. So if we notice, we'll see that it's setting up a variable called humidity value and it is reading it async. So the advantage of using the IoT device by adding NuGet is that it encapsulates a lot of simple functionality like reading humidity or reading the temperature without me having to go in and, you know, sort of set up the eye-open readings or trying to understand how do I translate the value from the sensor to my Raspberry Pi. So I'm going to go in and so you'll notice there's a word here that's await. So this is happening async. So we need to go and set up our main program reference to adjust for the async task. So to do that, we're going to come in here and add async and add the word task. So this is going to pull in another system reference called threading. So this basically just means that it's going to create an asynchronous reading of the humidity value and grabbing the values for us. So once we have these values coming in all we have to do now is let's print out this value in the console, so we know what's happening. So that's what this console writeline is doing over here. The next step. So we've initialized the BME sensor. We've set the sampling rate and we are reading the value from it. Now let's use this value to control that LED. So if the humidity value is greater than 38, then I want the controller to come in and blink this LEDs. So this is the end of this. I'm going to end this statement, let's format this code a little bit so we understand what's going on. So what we have here, let's do a quick recap. We created an instance of the device settings created a device class and then initialized a BME280 sensor variable. Using that BME280 sensor variable, we set it's sampling mode set the power mode that we want to use, and now we are reading values that the sensor is getting, and here it's specifically using the humidity value. If you look at the GitHub sample the BME sensor can also provide temperature pressure and altitude values. Then we are not putting a check on that humidity value that if it is greater than 38, only then do we want the LED to blink. And the rest of the LED blinking logic, is exactly the same of what we had from Blinky. So we've only added these few lines of code looking at the sample in GitHub to get this code going. So now I'm going to go into my command prompt and copy over this file. So we'll be using Docker again to do this. So I'm going to go into the folder where I have my code which is right over here. And then I'm going to SSH into a Raspberry Pi and create a new folder where we will copy over these files. So let's go into our Pi. Now once I'm in the Pi, I'm going to make a new folder, let's call it BME, and I want to make a instance and go back to where our code is. So now I'm going to copy over the new code files into the Raspberry Pi. So let's go back and use our handy HCP. And I'm going to say, copy Blinky on to the so the Pi at the address. And our folder name was BME. Similarly, I'm going to copy over the Docker file. Oops, looks like I made a typo. Let's go fix that Docker file and copy that over to our Raspberry Pi. And our last file is the actual code file. So let's copy that into our Pi and into the folder called BME. So those are the three files that we need to get started. So let's now go back into the Raspberry Pi. Let's go into our folder, make sure our files are in there. Excellent, I see all of them. So let's go and follow a Docker bill steps. And let's call this Blinky. Now that the code is done building let's run it on a Raspberry Pi. So I'm going to do Docker run and provided the device permission to use the GPI open for the LED, and I'm going to pass in a new device permission to use the I2C port to control the BME280 sensor. Then let's run Blinky. So it's doing a console print of the humidity value, but it's not at our threshold yet. So if I switch over to my camera and put my finger on the sensor, you notice that the LED now starts blinking. And if I go back to the reading you'll see that the humidity value has reached 50% which passes out if else check, and now the LED starts blinking. This is the end of our IoT with.net core video series. Please be sure to check out our other videos on asp.net core C-sharp and.net core videos. I hope you enjoyed the series and I hope you go and blink some LEDs.

Contents