Check out a free preview of the full Nodebots / Hardware course:
The "Reading the Weather Sensor" Lesson is part of the full, Nodebots / Hardware course featured in this preview video. Here's what you'd learn in this lesson:

Steve writes JavaScript to log sensor readings to the console.

Get Unlimited Access Now

Transcript from the "Reading the Weather Sensor" Lesson

[00:00:00]
>> Steve Kinney: So I'm gonna actually write this code back in the playground.js file cuz it's just simpler. I don't wanna like, I'm gonna need that express code later, and I don't wanna delete it. It's really the only reason I have these two different files. So I'm gonna go write in playground cuz there's less stuff going on there.

[00:00:15] So this all stays the same. I miss my RGB LED, but it is no longer with us on the bread board so I'll get rid of this code.
>> Steve Kinney: Very cool. So we'll call this, it's like a weather monitor.
>> Steve Kinney: We'll say a new five and this is gonna be the multi class.

[00:00:39] This is doing a lot of things. Like Johnny five does need a little bit of help though and need to tell it what it is. So we'll say controller and as I mentioned earlier, this is the infamous BME280. You might be like, how am I supposed to remember that?

[00:01:00] The trick is just to have to type it and say it a whole bunch of times. It's also on the component. [LAUGH] That's the other option is it is in small print in the little component on the red board, cool. And then not dissimilar from our input field the monitor is gonna have and change event so every time the temperature, parametric pressure, or relative humidity changes, this will fire an event if your suspicion is won't that be a lot, yes.

[00:01:36] [LAUGH] Yes. It's gonna be, it's gonna be insane. [LAUGH] absolutely, but we'll deal with that shortly. First we're going to let the like, onslaught of information work, cool. So when that happens, whenever something changes, we need to go ahead and just get that data off the controller. So we'll actually say const temperature = monitor.thermometer

[00:02:17]
>> Steve Kinney: .fahrenheit. If you have strong feelings about Celsius, you are welcome to get a Celsius rating instead. I am going to go with Fahrenheit, cool. I will do const pressure. That is different from live coding in front of a bunch of people and live wiring, we're talking about barometric pressure, not just like peer judgement.

[00:02:49] Barometer, the pressure. It's nice to have some auto complete in place.
>> Steve Kinney: He says, making sure that thermometer is spelled correctly. And then we'll do relative, yeah.
>> Speaker 2: Just to remove headache later on, I think you spelled Fahrenheit wrong.
>> Steve Kinney: Thank you so much! [LAUGH]
>> Steve Kinney: You're a hero.

[00:03:21]
>> Steve Kinney: That was definitely, cuz I would have just, I would have gotten a thing undefined. It wouldn't have crashed, but it definitely, I would have had that very confused look on my face and then we would have had to redeploy again so it's awesome that you got that.

[00:03:34] Humidity will be monitor,
>> Steve Kinney: .hygrometer,
>> Steve Kinney: .relative humidity. Cool, so now, we have these three things. What we will go ahead and do is we're just gonna start out by console logging. Those three constants. Temperature, pressure, relativeHumidity. All right, very cool. What I'm gonna do is I'm gonna start the deploy going here and then I'm gonna create a branch and push it up just in case you need to compare notes.

[00:04:16] I would probably wait until we verify that my code works. But I will push it up regardless cuz cool thing about git, you can always push up again. All right, cool. So I'm gonna go over and I will do a t2, run playground and then as that happens, I will make a new branch and I will call it bma 280.

[00:04:46] It's a memorable name.
>> Steve Kinney: The fact that I didn't wanna open a terminal tab made me use the GitHub app, and I actually don't know how to make a branch on that. So I'll do
>> Steve Kinney: We'll push up the change to that real time code. And we'll check out my BME 280 branch.

[00:05:38]
>> Steve Kinney: I've made a mess of git, so I'll rectify that real quick. Live coding, live hardware, and now live git.
>> Steve Kinney: All right, I'm gonna go back over to that tab that was open before. And you can see my data is coming in. And boy, is it? So fun fact is that the temperature in here is around 74 degrees Fahrenheit.

[00:06:31] The pressure is 96 and change. The relative humidity is around 53%. Just to verify that any of this works, I need to find a way to change the temperature around my sensor. It turns out that I brought with me a 98.6 degree heat source. That I'm just going to take.

[00:06:50] Take my finger and place it on the sensor. And we can see that it's kind of rising to 84. I don't know if it's gonna go all the way to 95, but it's slowly going up, verifying that this thing is working. That is a very scientific way of making sure that your readings are somewhat accurate, which is put your finger on it.

[00:07:10] Which is definitely counter intuitive to everything I was taught about electronics as a child. Which is take your fingers off of that, don't put your fork in the socket. But it works in this case. So it's a lot of data, and it's really interesting. And hopefully, there's a bunch of things from some of the technology we looked at today that are kind of floating around in your head.

[00:07:32] Could you take all this data and start pushing out all the web sockets? Yes. [LAUGH] Right? Could you do a whole bunch of really kind of interesting things? Could you post it to a web server, absolutely. I like to try maybe some other things. But could you change the color of different LEDs based on this data?

[00:07:52] Absolutely, you can start to mix and match. For a lot of things we're looking at, is a few things in isolation. But as you begin to mix and match stuff, you can start to figure some of that stuff out.