Raspberry Pi Weather Station

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome to another video explaining computers this time i'm going to take this temperature pressure and humidity sensor and use it to turn a raspberry pi into a weather station so let's go and get started right let's take a closer look at our sensor hardware which is based on a bosch bme 280 which is mounted on a breakout board from pymeroni and this sells for 12 pounds 60. so if we ask mr scissors to come in and snip the top of the packaging across like that i think that'll let us in like that when in theory we can now open up our little bag hopefully i got this right yes we have and we can take out what we have here which is the sensor and two five pin headers and we just turn the center around you can see the actual bme 280 component that provides temperature pressure and humidity sensing is the rounded metal box with a hole in it which is only about 2.5 millimeters square so it really is very tiny indeed if we look at the board's web page here on the primary website you can see they don't just sell the module they also support it well which is always great to see there's various links down here so for example there's a python library available for the board so we know we've got the libraries available to get data from the sensor that's clearly very significant and there's also some sample code they label it here a few nice little examples which is again great to see takes us across to github we'll have a look at some of this sample code in the next segment of the video and there's also a link here to the data sheet for the module from bosch so we can learn all about the bme 280. i find it fantastic with this wonderful data sheet for this tiny little module always great to see things like that i like this type of stuff as you've probably gathered and if we go down here you can see the target devices for using this sensor are shown therefore that zoom in a bit on that which includes mobile phones and tablets and navigation systems and cameras and flying toys and watches and also home weather stations which is what we're using the sensor for here it's worth noting that a bme 280 sensor is also available in a slightly different module from adafruit for 14.95 as you can see so the wiring would be slightly different what i'm going to show you here because it's a different configuration of module but the implementation and operation of the project will be exactly the same using this adafruit component talking of wiring i'm going to solder the supplied female right angle header directly onto the sensor board which will allow us to attach it straight onto a raspberry pi's gpio pins one three five seven and nine with the sensor positioned away from the heat generated by the system on a chip but alternatively we could solder some jumper leads onto the module some leads like these and indeed we could use these like these to extend the center away from the pie if we need to do that in the future so i'll get on with some soldering which is always a great thing to try and show you on camera but here we are it seems to be working reasonably well anyway and with it complete we can now mount the sensor onto a raspberry pi and here i've chosen a raspberry pi 3 model b plus which remains an absolutely classic piece of hardware a really nice pie but any model of raspberry pi can be used for this weather station project right i've now got the pi all connected up and running and here in raspberry pi os there's a couple of things we need to do to allow us to access the bme 218 module and the first of these is to enable the i squared c serial communications interface and we can do this by going to a menu here go to preferences we'll do it graphically via raspberry pi configuration and if it comes up there we are we go to interfaces i squared c is there we will enable that and click on ok secondly we need to go to a terminal which i've already opened up here and i've put in the command we need what this command does is to install a couple of libraries specifically the primary bme 280 module library and also a library called sm bus and if you're looking at this code and thinking what's going on it's doing a super user do to execute and install it's also got a pip in here and pip is a package management system for installing python software so that's why we've got sudo pip install rather than just sudo install anyway that's what we need to enter so i'll just execute that and hopefully things will happen and there we are that's all the install which is a pretty good and so now we can go to some sample code to try everything out so i'll close this down and i've got opened up the genie programmers editor here and here is the first piece of sample code provided by primarily caller all values pipe and what we can see here basically it imports some libraries it imports the time library it then imports some functions from sm bus and from the bme 280 library as well you can see it sort of hedges its bets here obviously doesn't know which version of sm bus we might have so it looks for one of two different versions of that particular module it needs and then it prints out what it's doing and then it goes down here to as you can see to initialize the sensor with that code there executes a while loop which is going on forever it's while true true is always true so this loop will go on indefinitely do we crash out of the program and it's going to get values for temperature pressure humidity by using a get command on the bmv 280 and it's going to print them out with a bit of formatting in terms of the decimal places so this is very exciting i've not tried this already sometimes i've tried things in advance in a video i've not tried them here so let's run this code and see what happens and there we are we're reading data from our sensor although it seems to be a bit strange the first line of data looks about right certainly in terms of the temperature in this room and then after the first reading everything seems to got a bit of skew they're not quite sure what is going on but we are at least gathering data from the sensor looks like things can work so i think this gives us a good foundation for the rest of the project right here i am back again it's now the next day i've been doing some experimentation and also writing a slightly different version of the code and for start i've decided to tidy up the importing of libraries at the start of a code you might remember it did look like this where it checked to see whether it would use sm bus or some bus 2 from the smbus library i've discovered it's always sm but it's using so i thought we might as well tidy things up that just kept me rather happy having some slightly neater code i've also discovered that the first reading you get from the sensor is always wrong so what i'm doing here is getting some data from a sensor waiting for a second ignoring that entirely and then getting on with reading some actual data but when we do that let's run it up again we still have issues as yesterday here we've got our temperature our pressure and humidity and the pressure here looks to be about right i've checked that against a weather site i found in nottingham which suggests it should be about a 999 or which is at the location they're testing so the facts we're reading here are 995 that suggest that's pretty good they suggest that the value for humidity is about 99 it's raining most of the time we're not in today that's very different to what we're measuring here but here we're measuring at the moment internal humidity in a room that's warmed up in the winter so you get much much lower humidity over here this is a problem though this is a temperature of 30.85 degrees something like that in this room that's not true this rumor called a thermometer is about 21 degrees so this figure is way off and the reason for that i've worked out and also read about is if you think about it rather obvious it's because the sensor is plugged onto the raspberry pi and it's separated from the system on a chip but not by that much and the pi system on the chip is currently here running at what 48 degrees and it therefore warms the air below the center and there's some conduction of heat around the board and up the gpio pins which also warms the sensor now there's two things we can do about this there's various stuff you can find online which actually is you're dealing with the issue using code here are the primarily bits of sample code there's one here called compensated temperature which builds on a review over here which is of the enviro hat which uses the same sensor another board from primarily a bit more sophisticated board and this basically deals with the issue by taking the pi's temperature and using a scale factor and working out what the actual temperature of the world around the sensor actually is we could do that but to be honest i think that's rather a daft better because you're never going to get the actual best temperature and so what i'm going to do is this yes as you can see we've now got the sensor on a jumper lead separated out from gpio connectors here so the system on a chip generating its warmth can't influence the sensor warm it up and give us incorrect readings and indeed if we look back to the raspberry pi's desktop and run the code we see accurate results the temperature now reflects the temperature on the thermometer in this room and the humidity results reflect what you'd normally expect to see in an inside room in the winter clearly that's gone up significantly because we don't have nervous system on a chip drying the air around the sensor now so far we've been executing the code here in the genie editor but it would be good if we could execute it in the terminal and also do so remotely so we'll close down genie and we'll open up a terminal and we'll do a list ls there to see where we are and we'll change to a python code where i'll keep my python code as you would guess and the file we've been working on most recently is this one bme 280 cjb a a so to make that executable i'll do a ch mod and a plus x and the file name which is a vm e280 and just because some of you want me to do so i'll press the tab key to complete rather than typing the whole lot and press enter and that will have taken place and if we now do a list again you will see it's now in green so it's an executable file and to check it works we'll do that and we'll type again the file name bme and autocomplete and enter and hopefully yes it's running we've once again got our temperature pressure and humidity readings so with that working we'll stop this with a control c and we'll close down the terminal and we'll now set up the pi to be accessed remotely so to do this we'll go to a preferences and pi configuration and first of all we're going to turn on something called ssh means of accessing the pi over a network which is down here so we'll enable ssh and ok there we are and because we've enabled ssh we should really change the pi's default password which at the moment will be raspberry based on the user name pi so we go in here and we go to a change password if you don't change your password after you've turned on ssh you'll get lots of messages telling you to do so you might as well do it anyway and i'm now going to shut down the raspberry pi and take it outside and put it in my garage or my garage depending on how you want to pronounce it and as you can see it's now running headlessly it's just connected to a power supply and so if we go across to my laptop instantaneously i'm now back indoors where it's much warmer and here i've installed a small piece of software called putty which is an ssh client which you can obtain for free from the address i'm showing you on the screen so we launch putty there we are we enter the hostname raspberry pi like that which is the default hostname for the pi it'll be that unless you've changed it and if we click on open there we are it's come up and just before i put my login details in i'll quickly change the font settings so you can see things better on video so we'll log in as a username pi and the password i just entered when i changed the password which was i think that hopefully there we are we're now logged in i'll do a clear just to make the screen nice and neat and if we now do a list as before we change to python code just as we just did on the pi itself list there we can see there's our file we should be able to execute it with a bme can we autocomplete here we can and enter on that and is it going to run it is and as you can see it's a bit colder outside isn't it and seems to be dropping presumably the sanctuary is acclimatizing to where it is but it's now down to 11.8 11.4 or 11.84 i can't read numbers you can see what it is it's clearly colder outside and the humidity is massively higher so clearly we've now got to a point where we can have a raspberry pi remotely potentially lots of raspberry pi's remotely reporting in their data on temperature and pressure and humidity and from that we could try to predict the weather we could see if for example the pressure here which is measured in a hectopascals in case you were you're wondering if the pressure was falling rapidly it means it's likely to rain or potentially snow if it's very cold over here and generally if the humidity is high and rising it's also more likely to rain greetings here i am back again with a monitor keyboard and mouse connected to pi and we're now going to set things up to record weather data over time and because we're working here in a full desktop operating system we happen to have available a spreadsheet in the form of libreoffice calc so we launch up libreoffice calc there we are and what i've done is to create a spreadsheet which i've got down here i've been playing around with error spreadsheets apps to final one and doesn't look very exciting at the moment all it's got at the moment as you can see is five column headings for date time temperature pressure and humidity so what we're going to do is to alter our code so it'll actually put our readings into the spreadsheet where we can look at them manipulate them chart them whatever we wish to do so let's close that down and go back to the genie programming editor there it is and this is our new code let's go back to the top you can see what's going on and first of all we're importing libraries the ones we imported previously but i'm also here importing date time and date time i'm taking dates from that as well which we'll be using in a second and i'm also importing a library called open pi xl and this allows us to manipulate spreadsheets in python and to use this library we first have to install it and so earlier i opened up a terminal and issued the command sudo pip install open pi xl which run through so we had the module on the system anyway here we are back in the code whereas previously we're initializing the sensor taking your first reading and getting rid of it to overcome that first reading being rubbish issue and then after that we're loading in our spreadsheet as you can see and the spreadsheet is here that's whether xlsx sitting in home pipe python code etc and we're setting the sheet to be the first one in the workbook we then get to our loop which is sitting down here where we're going to read the sensor as we did previously although here i'm rounding the values for temperature pressure and humidity when we first take them rounding them to one decimal place which makes life easier in a spreadsheet later on and then we're also collecting the data today and now which is going to be the time i thought we'd then let the news know what is going on we're going to print out we're adding this data to the spreadsheet the date the time and again the things we're actually reading temperature pressure and humidity and then we're going to append the data to the spreadsheet by setting up the row of data which is going to be the date time temperature pressure and humidity and we append it to the spreadsheet we then save the spreadsheet always important to keep saving your spreadsheet when you're adding data and then after that we're going to wait here for 10 minutes which is 600 seconds finally you might notice i've actually put the loop inside a try finally combination and the reason for that is because we're going to have to break into this with control c and we might break in before the spreadsheet's been saved and so because of that was a try finding things set up it means whatever happens it'll execute what's listed on the finally which here is to save the workbook and of course to print goodbye so let's just test this out and because testing doesn't want to take forever i'll change 600 seconds to say i don't know three seconds just to show you what's going on so i'll put that in there and we'll save this and if we execute this code fairly similar to what happened before of course but it basically now tells us it's adding this data to the spreadsheet date time this could be better formatted but it's good enough for now i mainly care about what's going into the worksheet you can see it's adding this stuff in let's do a couple of others another one that'll do and i'll control c to get out of that we'll now go back to our spreadsheet and we'll load it in very exciting isn't it and you will hopefully see yes there we are the data has come into the spreadsheet it's stored date time temperature pressure and humidity and of course we can look at our data here we can chart it if we wanted to this is becoming quite a useful tool for monitoring and potentially predicting the weather so now i'd like to try this out for real so i'm going to select this data and get rid of it select the rows and do a delete effectively reset our spreadsheet we'll save that and we'll come out of this go back to our code and change our time sleep back to 600 for those 10 minute increments file and save and i'm now going to shut down the pi and return it to an outdoor location right the pie is now again out in the cold whilst i'm in the warm inside on my windows laptop where i've logged into the pi navigated to the python code directory and you can see we need to run the code here which is a weatherspread.pi i didn't make that executable before so i'm doing it now here remotely by ssh which should be fine there we are let's just list again and you'll see yet it's turned green so let's now execute that code with that and uh like that and execute and we'll see hopefully the pi is going to be giving us some data in a second there we are it knows what time it is oh look it's not very warm is it 6.4 degrees centigrade anyway we now know that the next measurement won't be taken for 10 minutes and so i'm now going to leave the pie for a few hours to get on collecting data and here i am back again just over three and a half hours later data's being collected consistently it seems all this lovely data and by the magic of filmmaking we'll go across to a spreadsheet with the data in it there of course various ways this could happen we could download the data from the pi over the network we could get pi to save the data to a usb drive and take it off the pi that way or we could boot up the pi with a keyboard and monitor mouse attached and launched libreoffice calc which is what i've done here and i do find this very interesting this is real weather data isn't it what a weather station should be doing it's allowing us to look at trends in data over time and we can see that the temperature has dropped as it's got darker and colder across the afternoon the pressure has been pretty consistent but the humidity the humidity is definitely increasing here i think it's going to rain fairly soon it's gone from what 72 to 89 humidity i think that suggests we're going to see some rain but anyway in terms of our experiment this has clearly worked we've managed to use the pie to collect some real weather data well there we are we've delved into the use of a raspberry pi for monitoring and potentially predicting the weather in a future video i might try and go further and attempt to use a raspberry pi to actually control the weather although i suspect we'll have to wait for the release of the raspberry pi 5 before we're able to do that but now that's it for another video if you've enjoyed it you've seen here please press that like button if you haven't subscribed please subscribe and i hope to talk to you again very soon you
Info
Channel: ExplainingComputers
Views: 211,177
Rating: undefined out of 5
Keywords: Raspberry Pi, weather station, temperature sensor, hummidity sensor, pressure sensor, BME280, Pimoroni BME280, Bosch BME280, Python, spreadsheet, Python spreadsheet library
Id: ChQpD2gsC20
Channel Id: undefined
Length: 21min 42sec (1302 seconds)
Published: Sun Jan 03 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.