Rasberry Pi Guides >>>
Reading the Rasberry PI CPU Temperature
One of the challenges with the rasberry Pi is keeping the CPU cool, there are a number of ways of doing this, but in the end I opted for one of the excellant FLIRC cases that acts as the heat sink
But in addition to that I wanted to know what temperature my Rasberry Pi was running at, so to do that I needed to make a couple of changes to my WeeWX setup.
First Capture the Rasberry Pi CPU Temperature
This is a relatively easy task and requires a simple Python3 script that reads the CPU Temperature from the gpiozero library and uses that data to populate the extraTemp1 record, the code is replicated below and downloadable from here
For this code to work you need to have the weewx and gpiozero libraries installed
If you have gotten this far I will assume you have already installed weewx; And gpiozero is a standard Rasberry Pi library, however if it is missing then the instructions to install it can be found here
pi@weewx: $ vi cputemp.py import weewx from weewx.engine import StdService from gpiozero import CPUTemperature class AddCpuTemp(StdService): def __init__(self, engine, config_dict): # Initialize my superclass first: super(AddCpuTemp, self).__init__(engine, config_dict) # Bind to any new archive record events: self.bind(weewx.NEW_ARCHIVE_RECORD, self.new_archive_record) def new_archive_record(self, event): cpu = CPUTemperature() if event.record['usUnits'] == weewx.US: event.record['extraTemp1'] = ( cpu.temperature * 1.8 ) + 32 else: event.record['extraTemp1'] = cpu.temperature
Put your Python Script into the Correct Directory
WeeWX scans a nuymber of directories looking for scripts and files to run, one of those directories is a user directory where you can put all of your WeeWX extensions and this is where we are going to put this python file. Full documentation on customizing WeeWX can be found here
Copy your new Python script into the bin user directory under your WeeWX home directory
in my case this is /home/weewx
pi@weewx: $ cp cputemp.py /home/weewx/bin/user/ pi@weewx: $ ls -al /home/weewx/bin/user/ total 48 drwxr-xr-x 3 pi root 4096 Mar 23 2020 . drwxr-xr-x 12 pi root 4096 Mar 28 2020 .. -rw-r--r-- 1 pi root 829 Mar 28 2020 cputemp.py -rw-r--r-- 1 pi root 541 Mar 14 2020 extensions.py -rw-r--r-- 1 pi root 306 Mar 14 2020 __init__.py drwxr-xr-x 2 pi root 4096 Mar 29 2020 __pycache__ -rwxr-xr-x 1 pi pi 7327 Mar 28 2020 s3backup.py -rwxr-xr-x 1 pi pi 11041 Mar 28 2020 s3upload.py -rwxr-xr-x 1 pi pi 3992 Mar 28 2020 stats.py
Now configure weewx.conf
Before your WeeWX installation will start reading the temperature of your Rasberry Pi, you have to tell WeeWX to run your new Python script.
This is done by modifying the Services section of your weewx configuration file and I know there are some fancy ways of doing this programatically, but I simply edit the file by adding a new entry for my new Python script
The syntax of this new entry is critical and is explained below
data_services = user.cputemp.AddCpuTemp
- data_services: Is an existing service that we use to add data to the database
- user: Is the directory under bin where the new Python script is located
- cputemp: Is the name of the new Python Script
- AddCpuTemp: Is the name of the class within the new Python Script
pi@weewx: $ vi weewx.conf # This section configures the internal weewx engine. [Engine] [[Services]] # This section specifies the services that should be run. They are # grouped by type, and the order of services within each group # determines the order in which the services will be run. prep_services = weewx.engine.StdTimeSynch data_services = user.cputemp.AddCpuTemp process_services = weewx.engine.StdConvert, weewx.engine.StdCalibrate, weewx.engine.StdQC, weewx.wxservices.StdWXCalculate archive_services = weewx.engine.StdArchive restful_services = weewx.restx.StdStationRegistry, weewx.restx.StdWunderground, weewx.restx.StdPWSweather, weewx.restx.StdCWOP, weewx.restx.StdWOW, weewx.restx.StdAWEKAS report_services = weewx.engine.StdPrint, weewx.engine.StdReport
Set Heading Text
This file also contains the labels that are used as the headings for each graph, and they are defined in the Labels section
Look for the value extraTemp1 and set the text to a value of your choice, I have used CPU Temperature
pi@weewx: $ vi weewx.conf # The labels to be used for each observation type [[[Labels]]] ... # Generic labels, keyed by an observation type. [[[[Generic]]]] barometer = Barometer ... windvec = Wind Vector extraTemp1 = CPU Temperature extraTemp2 = Temperature2 extraTemp3 = Temperature3
Add CPU Temperature Graph
The creation of graphs is controlled in skin.conf and the one is use is the new Seasons file and this is the modification I will be descrbing
As with the other configuartion files I manually edit this configuration file, adding a new entry between %tempin and %rain to generate this new graph as shown below
Note that we are creating four graphs here, daily, weekly, monthly and yearly
pi@weewx: $ vi skin.conf [[[daytemp]]] yscale = None, None, 0.5 [[[[extraTemp1]]]] [[[weektemp]]] yscale = None, None, 0.5 [[[[extraTemp1]]]] [[[monthtemp]]] yscale = None, None, 0.5 [[[[extraTemp1]]]] [[[yeartemp]]] yscale = None, None, 0.5 [[[[extraTemp1]]]]
Finally Display the new Graph
Creating the graph is only half the battle, you also have to tell WeeWX to display the new graph, I have chosen to display my graph in the Sensor Status section and that is the example that I will be using below
First you need to locate the template that is used to generate the WEB pages, for the Sensor Status this is telemetry.html.tmpl
As with the previous changes, formatting is critical and in this file you are telling the WeeWX engine which files to display and the name you use in this file must match the name you generated in the skin.conf file, in my case this is daytemp, weektemp, monthtemp and yeartemp
pi@weewx: $ vi telemetry.html.tmpl #def period_plots($label) <div id="history_${label}" class="plot_containter" style="display:none"> #set $rximg = "%srx.png" % ($label) #set $voltimg = "%svolt.png" % ($label) #set $tempimg = "%stemp.png" % ($label) <img src="$rximg" alt="Signal Quality"/> <img src="$voltimg" alt="Console Battery"/> <img src="$tempimg" alt="CPU Temp"/> </div> #end def
And that is it, you should now get a graph that looks something like this