weewx-cmon doesn't read cpu_temp of Raspberri PI

438 views
Skip to first unread message

Luc Heijst

unread,
May 7, 2020, 10:48:53 AM5/7/20
to weewx-user
Recently I downloaded the latest version of weewx-cmon (v 0.20) which was converted to use with python3.
This version, and also elder versions of cmon.py, did not read the cpu-temp of my Raspberri PI systems (models 1B, 2B, 3B and 3B+).

On my raspberry PI the following statement is true: os.path.exists(tdir), still a cpu-temp could not be found in this section
The elif os.path.exists(tfile): statement caused the bottem section to be skipped and that is the section that reads the RPI cpu-temp.

See the modifications in yellow which fixed this problem.

Luc

--- snipped of cmon.py ---
        # read cpu temperature
        tdir = '/sys/class/hwmon/hwmon0/device'
        # rpi keeps cpu temperature in a different location
        tfile = '/sys/class/thermal/thermal_zone0/temp'
        temp_found = False
        if os.path.exists(tdir):
            try:
                for f in os.listdir(tdir):
                    if f.endswith('_input'):
                        s = self._readproc_line(os.path.join(tdir, f))
                        if s and len(s):
                            temp_found = True
                            n = f.replace('_input', '')
                            t_C = int(s) / 1000 # degree C
                            record['cpu_' + n] = t_C
            except Exception as e:
                logdbg("read failed for %s: %s" % (tdir, e))
        ### elif os.path.exists(tfile):  ### original statement
        if not temp_found and os.path.exists(tfile):
            try:
                s = self._readproc_line(tfile)
                t_C = int(s) / 1000 # degree C
                record['cpu_temp'] = t_C
            except Exception as e:
                logdbg("read failed for %s: %s" % (tfile, e))

-----------

Tom Keffer

unread,
May 7, 2020, 11:15:06 AM5/7/20
to weewx-user
Looks simple enough, but cmon is not part of weewx. You should submit as a PR to weewx-cmon.



--
You received this message because you are subscribed to the Google Groups "weewx-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to weewx-user+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/weewx-user/db0db811-94ac-4762-ad31-9eb39b561bb9%40googlegroups.com.

Mike Revitt

unread,
May 8, 2020, 4:41:39 AM5/8/20
to weewx-user
I have this working on my Rasberry pi at https://weather.cougar.eu.com/telemetry.html and this is how I did it

in weewx.conf 

In
[Engine]
 
[[Services]]

set

data_services = user.cputemp.AddCpuTemp


Then add this Python program to the bin user directory where your Rasberry Pi code is.

#    Copyright (c) 2009-2020 Mike Revitt


#    See the file LICENSE.txt for your rights.


"""Gets the CPU temperature on a Rasberry Pi"""




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







You can then access the Rasberry Pi CPU temperature as the variable extraTemp1 from within your HTML files 

steeple ian

unread,
May 8, 2020, 9:37:16 AM5/8/20
to weewx...@googlegroups.com
Works perfectly.

--
You received this message because you are subscribed to the Google Groups "weewx-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to weewx-user+...@googlegroups.com.

Mike Revitt

unread,
May 9, 2020, 6:24:09 AM5/9/20
to weewx-user
Glad I could help

Jonathan Zitelman

unread,
Jun 9, 2020, 10:35:28 PM6/9/20
to weewx-user
Mike, I have updated my weewx.conf file.  Could you offer a little more clarification on the new file?  To confirm this is the /usr/bin directory?  Does the filename matter?

-Jonathan

Kevin Chapman

unread,
Nov 12, 2020, 6:19:45 PM11/12/20
to weewx-user
Ok,  I am not familiar with Python at all.  I managed to work through the instructions above and make the changes to my weewx.conf file and then create cputemp.py in my /usr/share/weewx/user directory.  This eliminated the errors in the my syslog.  I am not seeing the cpu temp on the charts.  How would I look up the variables in python to see if they are registering at all. 

vince

unread,
Nov 12, 2020, 7:23:52 PM11/12/20
to weewx-user
We'd have to see the code you're running to look at it in detail.
Where did you get 'cputemp.py' and how can we get a look at the exact version you're running ?

FWIW - you check the temperature on a pi by running "vcgencmd measure_temp" which reads the right thing under /proc and formats it for output

Kevin Chapman

unread,
Nov 12, 2020, 9:23:32 PM11/12/20
to weewx...@googlegroups.com
So below is the code I used from Mike Revitt.  I have no idea if it comes from a package. I followed Mike’s instructions as well as I could.  He was not clear what to name it or where to put it.  I created the python script in the ‘user/share/weewx/user folder.  At first, I named it AddCpuTemp.py but that generated errors starting weewx.  Then I named it cputemp.py and the startup errors went away.  I hope that makes sense.  



On Thu, Nov 12, 2020 at 6:23 PM vince <vince...@gmail.com> wrote:
We'd have to see the code you're running to look at it in detail.
Where did you get 'cputemp.py' and how can we get a look at the exact version you're running ?

FWIW - you check the temperature on a pi by running "vcgencmd measure_temp" which reads the right thing under /proc and formats it for output

On Thursday, November 12, 2020 at 3:19:45 PM UTC-8 kdch...@gmail.com wrote:
Ok,  I am not familiar with Python at all.  I managed to work through the instructions above and make the changes to my weewx.conf file and then create cputemp.py in my /usr/share/weewx/user directory.  This eliminated the errors in the my syslog.  I am not seeing the cpu temp on the charts.  How would I look up the variables in python to see if they are registering at all. 

You can then access the Rasberry Pi CPU temperature as the variable extraTemp1 from within your HTML files 

--
-------------------------------------------------------------------------------------------------
Kevin Chapman
(713) 245-9368

Life is a food chain.  Be at the top or be on the menu!
-------------------------------------------------------------------------------------------------

vince

unread,
Nov 12, 2020, 10:01:57 PM11/12/20
to weewx-user
I would look for values in extraTemp1 in your database, as it seems that's where the python code is putting it.

A query that would report the last 10 readings of this sorted with most recent at the top is:

    echo "select datetime(dateTime,'unixepoch','localtime'),dateTime,extraTemp1 from archive order by rowid desc limit 10;" | sqlite3 weewx.sdb

What this is doing is saying 'give me a human readable date+time, the dateTime element, and the extraTemp1 element' from the archive table in the weewx.sdb database...and reverse sort it, showing me the 10 most recent readings (whew).

The location of weewx.sdb differs depending on how you installed weewx and what os you are running.   See http://weewx.com/docs/usersguide.htm#Where_to_find_things for what's where in the various variants of operating systems etc.

Kevin Chapman

unread,
Nov 12, 2020, 10:04:34 PM11/12/20
to weewx...@googlegroups.com
Ok cool. I’ll check the database. Thanks for taking the time explaining it. 

--
You received this message because you are subscribed to a topic in the Google Groups "weewx-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/weewx-user/Iny0WMj78cU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to weewx-user+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/weewx-user/8a08f5b0-73be-43ff-aaca-0ec102e5aa2dn%40googlegroups.com.

Mike Revitt

unread,
Dec 27, 2020, 7:00:14 AM12/27/20
to weewx...@googlegroups.com

Sorry it has taken so long to respond to this, but I have just finished documenting how I added the CPU Temperature to my website, this is all documented here https://www.cougar.eu.com/useful-guides/weewx-guides/rasberry-pi/read-cpu-temp.html

 

 

Mike Revitt

Database Migration Specialist – Database Freedom

Amazon Web Services

erevi...@amazon.com | p+44.7825.063.466w:  aws.amazon.com

 

signature_1577675884

 

Kevin Chapman

unread,
Dec 27, 2020, 5:06:15 PM12/27/20
to weewx...@googlegroups.com
Thank you.  I will check it out. 

Graham Eddy

unread,
Dec 27, 2020, 6:44:12 PM12/27/20
to weewx...@googlegroups.com
an alternative to repurposing an existing data_type is to define an xtype (e.g. see https://github.com/g-eddy/weewx-vitalstats)

Tom Keffer

unread,
Dec 27, 2020, 7:41:22 PM12/27/20
to weewx-user
That's a novel use of xtypes, but I'm not sure it's always a good idea. In this case, I think you'd be better off simply adding the type to the LOOP packet or archive record. 

The intended purpose of xtypes is to calculate derived variables. It's entirely possible it could be called many times during a reporting cycle, depending on the template used. Something involving I/O could be very expensive.

Having said that, xtypes also do database accesses, which can also be very expensive, so my advice is inconsistent. So, not saying never do it, but think it through.

-tk

--
You received this message because you are subscribed to the Google Groups "weewx-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to weewx-user+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/weewx-user/6B658B72-0127-4CA6-BC85-60A52CB0A9E7%40gmail.com.

Graham Eddy

unread,
Dec 27, 2020, 8:55:04 PM12/27/20
to weewx...@googlegroups.com
hmm, the ‘novelty’ i think is having an xtype calculated with values from outside weewx (eg cpu temperature), and the expense of the calculation is not really at issue - though normally they would be light-weight in case they are used often (eg barometer)

architecturally anything external is supposed to brought into weewx via a service and made available via weewx packet. (the *providing* of an xtype is now positioned as a service, but the xtypes themselves are not.)

okay, i’ll recast my vitalstats from xtypes_service to data_service and not bypass the packets...

Graham Eddy

unread,
Dec 27, 2020, 11:05:04 PM12/27/20
to weewx...@googlegroups.com
i forgot to acknowledge the design consideration you raised that by calculating value once and storing it in the packet, unlike an xtype it is not re-calculated each time it is referenced
Reply all
Reply to author
Forward
0 new messages