by Mike Jahrig KG5P
This month I will combine code described in previous issues and create a usable weather station application that will run on a Pico W. My intention is to use this weather station during RACES activation. Thus, I don’t want it tethered to my laptop with a USB cable. I intend to build an external power supply using one or two 18650 LiPo batteries. Originally I tried powering the Pico with an external power pack. Unfortunately, there is a problem, if you want to call it that, with commercial power packs. The Pico draws less than 25 ma., and they want to shut down and turn themselves off because they think there is no load connected.
This application will read data output from the BME-280 sensor, reformat it into units familiar to Americans, display it on a HTML client and also log the data as a .csr file. In addition, it will call the NTP pool to obtain the date and time which will also be displayed and logged. The HTML server output will look like shown at left.
Here is the link to the micropython code. I’ve written instructions on how to install Thonny and the BME-280 libraries in previous editions of The Chawed Rag, so I won’t repeat them here.
The entire world, with the exception of the US and one or two other countries, use SI metric units, as does the BME-280. Temperature values it returns are in degrees centigrade and barometric pressure is in hectopascals. So I have included code that converts these values to degrees fahrenheit and inches of mercury respectively.
To get this to work untethered to a computer, you just rename the program to “main.py” and save it on the Pico. Then, when you supply power from an external source, it will automatically boot itself and execute main.py.
When the Thonny IDE is running, you can open the log file and it will look like this in .csv format.
You can now access the log file contents and import it into your favorite spreadsheet application and graphed if desired.
I’d like to make a few comments on the code. You will, of course, need to enter your network WiFi SSID and password at lines 16 and 17. The way I coded it seems to be pretty stable running on Windows 10, 11, and Ubuntu Linux. If you experience timeouts on your HTML client, you might have to lower the content keyword at line 74. On line 88, I coded it to write a .csv log record every 30 minutes. Feel free to change it if you wish to increase or decrease it. On lines 60 and 61, I issue a 60 second sleep which seems to work ok for me. If you experience timeout problems, decrease the sleep time.
One final word: As mentioned earlier, if you name the program main.py, all you have to do is plug in the power source and the Pico will boot and kickoff program execution. One of it’s first steps is to obtain the NTP time and clear the log file so it can write new data to it. So what happens when you plug it into your computer USB com port so you can download data? Same thing. Powering it up will clear your log file.
So the only way around this dilemma is to first start up the Thonny IDE. Then you can plug it into the com port. Thonny will try to connect through the USB. The Pico knows this is going on and will not try to boot and execute the program that clears the log. Now you can open the Pico via Thonny and extract the log file.
I know there are a lot of Picos out there and this is a way to get you feet wet and build something useful. Maybe learn something in the process. I certainly have.