PI PICO based WEATHER STATION using a BME280

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in my last video I showed you how you could put together a simple weather station using a Raspberry Pi Z and a BME 280 this time I want to do the same thing but use a Raspberry Pi Pico it's a very different ecosystem to the zero and so we'll have to do a few things differently but let's get to [Music] it hello once again P geek and techno nerds all around the world my name is Jeff and I'm an IT professional who's been in the industry for over 30 years in my spare time I like nothing more than playing around with raspberry pies I'm sure you do too if you like what you see here please hit that Thumbs Up Button subscribe if you want to see more and hit that notification Bell so you can be told when I put new videos out and also let me know in the comments what your favorite raspberry piie projects are and if you've got any ideas for projects that you'd like to see me do leave those in the comments as well as well in my last video I put together a simple weather station using a Raspberry Pi 0 and a bme280 they work great but the pi Z is a little bit overkill for this it's a general purpose computer orbe it quite a restricted one and for this kind of task we can make use of the Raspberry Pi Pico now it's a microcontroller rather than a general purpose computer what this means is when you first Power It Up it immediately ately starts executing a particular task and it will just keep on doing that but there are some drawbacks coding for the Pico is a lot more difficult than the pi zero because you don't get so much for free there is no operating system as such so you don't have immediate Wi-Fi support or a web server or anything like that so there's way more that we've got to do for ourselves but that can kind of be an interesting challenge in itself so let's go and take a look at the kit that you'll need for this project now there are a lot of options with the kit list for this particular project so I'll go through some of this now and you can then just go ahead and buy what you want the first thing you're going to need to buy is the Pico itself now the version of P Pico that we want for this project is the Pico W this is because it has support for wireless networking built into it but you can see there are two flavors here there's the p w and the pwh the wh comes with the header pins pre sold it onto the board whereas with the PW you have to buy and solder on the header pins yourself now the header pins are readily available pimer only have an option here for £180 or you can buy big packs of header pins such as this which work out a lot cheaper but you need to figure out what the best option is for you specifically if you're only doing this project in isolation then you may well find that the pwh is the best solution for you now the next next thing you'll need is the BME 280 board as you can see here on Pimon there are several options available again for the purpose of this video I purchased the Adafruit version but this was really only because the poni version was out of stock at the time as you can see it is a little bit cheaper and it's also easier to program for as we'll see later just like with the Pico you'll need header pins in order to be able to connect the bme280 to the picco itself so just bear that in mind now the next thing you'll need to think about is just how you want to connect the picco to the bme280 the easiest way to do this is just by using some female to- female jumper wires and these then just simply connect the pins from one device to the other and it ends up looking like this it's a little bit messy but it is functional an alternative and neater solution is to use this stuff this is PCB board and as you can see you can get it in a whole variety of shapes and sizes you can then buy header pins such as these that are specifically designed for the PCO and solder them onto the PCB board or alternatively you could buy a pack of sockets such as this one although I did buy a set of these and I found that they were actually too narrow for the pi Pico so I had to cut them in order to make enough space for the Pico to fit but as you can see here the end result can actually be very satisfying I've got a pair of P sockets that I can plug the Raspberry Pi Pico into and then I've got another smaller set of pins that I can plug the bme280 into if I look at the reverse side you can see that I've just soldered wires between the relevant pins to provide the connectivity that I need now it may not be pretty but it is functional and once the p and the bme280 are slotted into place you can see that it makes actually quite a pleasing little circuit now obviously just showing you that soldered board or the mess of the jumper wires connecting the PCO to the bme280 don't really show what the connectivity is so I've put that into a diagram that you can see here the VIN Port of the bme280 connects into Port 36 of the Pico this provides a 3.3 volt power supply the ground Port of the bme280 connects to pin 38 of the picco and this is just an arbitrary ground pin I then connect theck port on the bme280 to pin two of the Pico and this is labeled i s CSL the last pin connection is the sd1 pin out of the bme280 and I connect that to pin one of the Pico and this is labeled I 2 C SDA now I just want to talk quickly about the subject of soldering of course it's quite a dangerous thing not only is the soldering iron itself very very HT so you got to be very careful when you're using it but also there's a solder itself now I use only un Leed solder now this has a higher melting point than leaded solder so it's a bit more tricky to use but it is worth it lead is a toxic substance and it is a carcinogen and especially this week with the news that Steve from chronic fix put out if you've never seen Steve in his channel he is absolutely brilliant he does all kinds of electronic repairs but this week he put out a community post saying that he's just been diagnosed with non Hodgkins lymphoma now Non-Hodgkins Lymphoma is a form of blood cancer and one thing that has been related to it is the use of lead now I'm not saying for a second that it was the lead solder that Steve uses that has caused this for him he may well just have been unlucky but I just want to stress that if you are going to use Le solder please consider using gloves or at the very least wash your hands really really thoroughly when you're finished and make sure that you are always in a very very well ventilated area whenever you're doing any kind of soldering activities Electronics projects are really really great fun I enjoy them a hell of a lot but just be safe while you're doing it but of course right now my heart goes out to Steve I hope you have a very very speedy recovery now back onto the happier subject of the project the last thing that you'll need is actually software and this is thy it's a python IDE that has built-in support for talking to a Raspberry Pi Pico and other microcontroller Hardware as you can see it's available for Windows Mac and Linux so it doesn't matter what you're running they've got you covered now we have all of our basic building blocks we can start to put all of this together before the Pico can really do anything new useful you need to install some firmware onto it now you can get firmware to do all kinds of things on the PCO but what we're going to install is micro python this is a very lightweight python runtime environment and is specifically designed for microcontrollers you can pick it up from this website here and I'll put a link to it along with everything else you see here in the description below if you scroll down just a little way on the page there are a couple of download links just follow the of the Raspberry Pi PW this will download a firmware file that has a uf2 extension we can then go about copying this onto our PI PCO in order to copy micropython onto the PCO you need to connect the power to the Pico while you're holding the button that's just located on the top of it just as you can see me doing here now once you've done this you'll then be able to mount the Raspberry Pi Pico as a new file system on your computer once you've got the file system mounted in exactly the same way as if you had attached a USB thumb drive or an SD card you'll see the file system appear after this you can just go to your downloads directory select the micro python firmware file and copy it and then paste that onto the Raspberry Pi Pico once the copying is complete the PCO will unmount itself and restart now if you start up funny you'll see a display like what you see here if you don't see that triple Chevron prompt then just check in the bottom right hand corner that it's reporting that it is connected if it's not just click the button in the bottom corner and you'll get all of the different connectivity options just select the one for the micropython Raspberry Pi Pico that should then connect just fine now we've got this we can actually run some fairly simple python commands just to prove that we can actually execute python on the Pico this code is actually executing on the PCO itself it's not running on the local computer now there are a whole lot of libraries that are already built for micropython that you can install all you have to do is go to the tools menu and then click on manage packages in here you can just search for packages that you want right now I'm just going to look for one called micro Pro python python machine and now it's found it I'm just going to install that now that's installed on the PCO and I can see that if I go into this lip directory you can see it's imported machine and a whole load of other stuff as well and now I can write some very very simple code to turn on the LED on the Pico and here you can see that the LED has come on and indeed I can call led to off to switch it off again now before we can get the Pico talking to our bme280 certainly for the Adafruit version anyway there's one little Annoying compatibility issue that we need to fix if you double click on lib and then into machine there's a file in here called uncore netore dopy if you open that up then we need to comment Out Lines two and three so just put a hash or a pound character in at the start of those lines and then click the icon to save that now the next step is to get the PCO talking to the BME 280 if you get the Pimon version of the bme280 and maybe others as well this becomes really easy you can just go back to the manage packages window and then search for micro python py e bme280 and you can see there's a library available for it now unfortunately the Ada fruit version of the bme280 doesn't work with this Library it's a whole lot more complicated to set it up but if you don't have the Ada fruit version just go with this library to start with and just see if it works as I have the Ada fruit version there's a whole lot of extra software that I have to install and set up first you can get all of that from this site here again I'll put a link in the description below if you scroll down there are a couple of links to software here the latest blinker library from Ada fruit and the latest platform detect Library both of these link through to GitHub repositories and in both cases you want to download the source code tar.gz file a little further down there's then a link through to the Circuit python Library bundle now if you select this and then scroll down there are a number of download links available the one that you want is the one called python Source bundle just click on the provided link and download that zip file now I've provided a load of scripts and software myself to get all of this working all you have to do is either clone this repository or you can use the download zip option and once you have it you just want to open a terminal and go into the PCO directory so I have all of this owned in this directory here now I'm going to make a directory in here called prepare Ada fruit Libs and I'm going to move into that I'm now going to copy over all of those libraries that I downloaded from Ada fruit and you can see I have those in there now I'm now going to copy in a setup script that I've got in my GitHub and I need to make that executable now if if I run this this will extract all of the relevant libraries that the micropython needs to be able to talk from the PCO to the ad fruit version of the bme280 now that's done it cleans up after itself so I'm just left with the libraries themselves and if I go back to thy now I can copy all of these libraries onto the Pico all I have to do is doubleclick on the lib option and then go to prepare Adafruit Libs select everything I need except for the setup script itself rightclick and then select upload to/ lib this will then go ahead and upload all of these libraries onto the Pico now we've got all of that set up we can go ahead and get the Pico actually talking to the bme280 now the first thing to do here is in the lower window just click Raspberry p Pico in the upper window click on picco and then open up bme280 reader. py now if you've got the non-ad fruit version of bme280 you could just try running this it's a class I've written just to wrap up the functionality of the bme280 and you can create an instance of the class just by providing the numbers of the pins that the bme280 is connected to just try running this now in my case this doesn't work because I've got the Ada fruit version but on your bme280 it may well work just fine if it does brilliant if it doesn't and you've got the odor fruit version then use this version of the class instead bme280 reader A.P this is a specialized version of the same class that is designed to work with the Ada fruit version if I run this then this should work just fine for my case and yes and there you can see that the temperature humidity and pressure dat is coming through just fine so now let's go and take a look at some of the other python code that I've had to write for this the next class I want to talk about is the Wi-Fi class now as I mentioned earlier the peod doesn't really have an operating system as such and so you don't configure Wi-Fi in the same way as you would on a pi0 or any other Raspberry Pi model instead you've kind of got a code it in order to do this I've prepared this class here and it goes through everything the Pico needs to do in order to connect to your Wi-Fi network now I've made a couple of modifications here just for the purpose of the video so it doesn't display what my Wi-Fi's SSID or password are for obvious reasons you configure these in this file wior creds dojon now this can take an array of network connections if you happen to have more than one wi-fi network available if you don't just reduce this down to one set and then against SSID you just put in the name of your network and in password you put in the password that you use to connect to your network when you've got that configured just copy it onto your PCO by right clicking on the file and saying upload to slash in my case I've got a prepared version that I've got under this directory here and I'm just going to upload that now that's in place I should just be able to run this wi-fi class and it will then try to connect to my wireless network and there you can see it's been able to connect successfully the next class I want to talk about is ntp client.py now the pi Pico does have a realtime clock chip installed on it however it doesn't have any battery so when the Pico gets reset it's got no way of knowing what the time was so it just sets back to 1970 so now we've got a way to set up a Wi-Fi connection I can use the protocol ntp to call out to an ntp host server and then retrieve what the current time is Once that's been retrieved I can then call into the real time clock to set what our current date is and I've got some code here that will just test that out it makes use of the Wi-Fi module we were just talking about and then it calls out to pa. ntp.org to get what the local time is it then calls this method to set that time now before we can use this we need to copy our Wi-Fi module onto our Raspberry Pi Pico and so I can just do that by right clicking on wi-fi. piy and saying upload to slash and there you can see it appears now I should be able to run this test code for ntp client with no problem and here you can see that it's now been able to retrieve and set the time successfully the only downside was it took a little while before it could connect to the Wi-Fi in the first place but once it did it was fine now we've successfully tested the time module I'm going to copy that onto the picco as well just by right clicking and saying upload to for Slash and there you can see it's uploaded onto the Raspberry Pi Pico the next class is coloral dopy now anyone who saw my last video will know what this is all about this is just to provide a colorful background for the temperature sensor so that for any given temperature the background will change now just to give you an idea about what this looks like this is some test code that I ran against it so depending on what the temperature is that is measured I can pass it into this library and it will give me a color value back based upon that temperature so you can see it starts out in deep blue and then moves through the blues into the greens as the temperature rises then off into the yellows oranges and ultimately the Reds since in the UK we tend not to have an awful lot of temperatures that are below minus 10 or above 30 I've limited it to that range after those points the colors will stay just dark blue or red respectively and now if we go back to our code I can copy that onto our P Pico the next thing that we need to copy onto the Pico of course is the actual BME 280 Reader Library so I'm going to pick my Ada fruit version and then copy that over to the Pico now all of our basic building blocks have been copied over and we can actually look at the main code that's in weatherstation dopy now this is quite a complex class and it actually brings everything together so it sets up the Wi-Fi connection reads in the time and sets the clock it configures our bme280 reader and sets up the color Cal class so that we can display things properly if we look at the main section of the class that's this serve method here now here what we actually do is we set up our own little mini web server following on from setting up the Wi-Fi connection I then set up a socket to listen on our IP address on Port 80 the HTTP Port it then just waits for connections to come in from browsers and for those browsers then to send in requests for pages I chop up that request so that I can get hold of the actual path that was specified in the request so just as examples here if slash data is provided then this is kind of equivalent to in the last video where on the pi zero I was using flask and a rout called slash data in order to get my application to go off and retrieve the data from the bme280 I do the same thing here and if I get a request coming in for slash data and then just make it call the get data method which constructs this Json data containing all of the weather information and send that back to the client there are then some specific files that I pass for as well if the user requests the default page for a website I then retrieve my index.html page I've also got handlers for the jQuery text fill Library weather station CSS file for all the formatting and my weatherstation doj file which is the JavaScript for all of those they end up calling this method here send file this is ask the name of the file that we need to render what the mime type of the return is and the client that requested the data the first thing I do is I go off and find out how big the file is that I need to return I then open that file up and I read the contents in I then construct this HTTP response string you can see I've got to specify all of the headers myself here including what the MIM type is and the size of the data and I then send that back to the client now all of the file files themselves I've hidden into this web page directory so in order for these to be served out by my software I need to upload this onto the PCO I can do that just by right clicking on web page and saying upload slash now my little mini web server here just runs around in a loop just waiting over and over again for requests to come in from various browsers so I'm going to reset my Pico here and then we can try running this so firstly it's connecting to Wi-Fi then once it's got its Wi-Fi connection it will go off and set the time and then it will start waiting for browser web requests and here you can see some are coming in already and let's go and take a look at the browser to see what it's doing and here you can see that the browser window is updating night nicely now we have all of this running that's really great but the problem is we don't want to have to have the Pico always connected to our thy IDE in order for it to be able to work instead we want it to be able to start up automatically and it turns out that that's really easy to do so let's go and take a look at that now just before we move any further I've just reset the Pico again just by disconnecting the USB cable and plugging it back in again I've also hit the stop button on thony now firstly let's just go ahead and upload the weather station python file onto the Pico now when the Pico starts up by default it's going to look for a file called main.py in the root of the file system I've prepared one here you can see all it really does is it starts up one of these weather server objects and then in an infinite Loop it just calls the serve method so it's just constantly waiting for the connections coming in from the browsers and then serving out the responses in order to make the Pico auto run all I need to do is upload this onto the Pico and then reset its power again so let's do that right now upload to slash and then main.py is there and available now what I'm going to do is I'm going to completely shut down thony and I'll pull our little view of our bme280 into the middle of the screen now the USB connector that you can see there is connected into my PC what I'm going to do is I'm going to replace that with a direct power connector so there's absolutely no connection to the PC whatsoever now when I connect this there's obviously no logging or anything like that that we can see on a status display so to help us out I put some code into the Wi-Fi module so that when the Wi-Fi is connected the LED on the Pico will light up so now I'll just go ahead and swap over these cables there now we can see the light has illuminated on the PCO and that means it's running and it's connected to our Wi-Fi it should now be serving up the web page so if I switch over to the browser you can see that indeed it is now getting data and what's really great about this is it means that I could put this Pico and its bme280 literally anywhere so long as I could get a power supply to it and that includes outside I could take a weatherproof Project Box Mount the Pico inside it and then I could use that to measure the temperature outside or I could put them all over the house and then extend the browser display so that it showed me the temperature everywhere and each one of these little units can cost us as little as about £20 when you consider the cost of the Pico and the bme280 itself now of course there are other ways that you could get devices to show you temperature and pressure and humidity that would have their own little displays but the problem is you can't see all of those at the same time they're not web enabled so to me the extra cost is worthwhile since you have absolute control of what you want the end product to look like Plus you get the satisfaction that you put it all together and so now we've been able to put together a rudimentary weather station both using a Raspberry Pi zero and a pi Pico in my next video I'll be taking this a stage further by adding a device called an anemometer or windspeed sensor to the weather station I'll be doing this against the Pico version but you can do this with the zero in exactly the same way so stay tuned for that one once again if you like what you've seen here please click that Thumbs Up Button subscribe if you want to see more and hit that notification Bell to be notified when I put out another video and let me know in the comments what you thought about this project and again if you've got any ideas for projects that You' like to see me do in the future let me know there as well thanks so much for watching till the end and until next time bye for [Music] now
Info
Channel: Jeffs Pi in the Sky
Views: 805
Rating: undefined out of 5
Keywords: weather, rain, sun, snow, wind, forecast, measure, electronics, pi, raspberry pi, pi zero, zero w, zero 2w, zero wh, pi3, pi4, pi5, bme280, adafruit, wires, cables, temperature, thermometer, hygrometer, barometer
Id: f_iGvii9u8g
Channel Id: undefined
Length: 28min 30sec (1710 seconds)
Published: Sun Feb 11 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.