Categories
HowTo's VSCP

Monitor CPU temp of a Raspberry Pi #rpi #vscp #iot

The CPU temperature of a Raspberry Pi can be checked with

  cat /sys/class/thermal/thermal_zone0/temp

what you get is the CPU temperature in degrees Celsius times 1000. A typical output would therefore be

  44388

which is 44.388 degrees Celsius.

If we want to react on this temperature or collect it or diagram we need to send it to the VSCP daemon.  There is many ways to do this but the probably easiest way is to send it to the VSCP daemon over the TCP/IP interface. If you like MQTT more or websocket or a REST interface you can choose any of them instead as they all are supported by the VSCP daemon.

On a Raspberry Pi we usually have Python so we do this with a Python script for simplicity.  We can add this python script to cron and as we will see later get the CPU temperature every minute.

The script by the way is general. It can send any temperature times 1000 expressed in degrees Celsius and stored in a file to the VSCP daemon. It is possible to have the conversion as an input parameter but we skip that in this case.

The script is easy and looks like this

You use it like this

./send_pi_cpu_temp.py 192.168.1.6 admin secret –

First remember to make it executable (chmod a+x send_pi_cpu_temp.py ).

The parameters are.

  • IP address to server (192.168.1.9) where VSCP daemon resides.
  • User name for TCP/IP connection, obviously you should use anther user than the admin user in most cases.
  • Passsword for TCP/IP connection.
  • GUID to use for the temperature event. This is an optional parameter and if not given “-” wil be used which is the same as a GUID with all zeros and mans that the event will have the GUID of the interface. It is better to give an explicit GUID but this works for now.

So issuing this and watching it in VSCP Works

now we can add this to a cron script to get the temperature sent to the VSCP daemon every minute.

We add a script send_cpu_temp to /etc/cron.d looking like this

* * * * * root cd /root;./send_pi_cpu_temp.py 192.168.1.6 admin secret 00:00:00:00:00:00:00:00:00:00:00:00:00:01:00:02

Note that a GUID has been assigned here for the sensor. The

00:00:00:00:00:00:00:00:00:00:00:00:xx:xx:xx:xx

can be used for lab usage and I put a id for some hardware in byte 2/3 in this case 00:01 and index in byte 0/1 ( 00:02). We could have used the MAC address or the IP address  of the Raspberry Pi as a base for this or a privately assigned GUID series. You can read more about the GUID’s here.

Thats it. You can now alarm yourself when the temperature reach critical levels or just diagram the data or collect it in a database.

A note
VSCP base measurements on the SI system.  So a measurement has a base unit that is standardized and well specified. For convenience you can often use other units also. As an example, for temperature, the base unit (0) is Kelvin. But you can send the temperature data as degrees Celsius (unit=1) or degrees Fahrenheit (unit=2) also. The important thing is that it will always be well specified.

The format of the data is also well specified. In VSCP there is no questions raised about big/little endian or decimal point formats and such things. The number can come in many formats (string/integer/float…) but they are all well specified and have a clear conversion path between each other.

Well why? you may ask. What can we gain from this?

The thing is that as soon as we have the data in a common format routines for handling one type of measurement will work equally well for another type of measurements. Write once use many times.  You can even see this in the above sample. Only the unit and the class type will have a new meaning if we wanted to send a sound level measurement instead of a temperature measurement to the VSCP daemon.

In this case all VSCP Level I is used. That means max. 8 bytes of data.  Even the smallest device out there can handle this. It’s both practical and economical.

And all this is true for turning on/off things etc also of course.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.