Read Raspberry PI CPU temperature in Ignition

Hello
I try to read my Raspberry PI CPU temperature in Ignition. I use following command in terminal:

cat /sys/class/thermal/thermal_zone0/temp

I can read temperature by using os.system in Python3 but in ignition the os.system() function always return 1 and system.util.execute only execute command and doesn't return anything.
Can someone tell me how to read Raspberry PI CPU temperature in Ignition environment?

I also try to copy gpiozero library into /usr/local/ignition/user-lib/pylib folder of ignition and use following script(which is tested in python3) to get CPU temperature but getting error which I think is because python 2.7.

import os
from gpiozero import CPUTemperature

cpu = CPUTemperature()
print(cpu.temperature)

The error:

Traceback (most recent call last):
  File "<event:actionPerformed>", line 2, in <module>
  File "C:\Users\User\.ignition\cache\gw192.168.3.69_8088\C1\pylib\gpiozero\__init__.py", line 97, in <module>
    from .output_devices import (
  File "C:\Users\User\.ignition\cache\gw192.168.3.69_8088\C1\pylib\gpiozero\output_devices.py", line 45, in <module>
    from colorzero import Color
ImportError: No module named colorzero
Ignition v8.0.0 (b2019040718)
Java: Azul Systems, Inc. 11.0.2

It’s a pseudo-file. Normally, you should just read it using standard python file access. However, it might be readable only by root. In which case you’ll need a root process to read it for you.

@nader.chinichian did you ever work this out? I was looking to something similar, and getting the same issues. I’ve checked the file permissions on the file and can confirm that everyone has read access to it.

In fact, I went ahead and created a test file in my home directory and can’t read that file either using the execute command which I’ve set up like this:

system.util.execute(['cat', '/home/pi/test'])

Try spelling out the location of the executable, like /usr/bin/cat. Or better, just read the darn file with python.

1 Like

Oh I just noticed what pturmel meant by standard file access, I was thinking of still using cat (which I guess just doesn’t play nice with the execute command) The following code worked just fine

filePath = "/sys/class/thermal/thermal_zone0/temp"
fileIsThere = system.file.fileExists(filePath)
if (fileIsThere):
	cpuTemp = system.file.readFileAsString(filePath)
	cpuTemp = float(cpuTemp ) / 1000
	system.tag.write(Your_Tag_Path, cpuTemp)
1 Like

Technically, the cat command executes just fine with or without spelling out the full location, it’s just that it fails to actually return the contents of the file as one would expect when running cat from the bash prompt. And taking a quick look at the System.util.Execute command I just noticed that it literally returns nothing so how could executing cat ever produce a result, lol.

As you can see from post I made a moment after you, the readFileAsString command worked just fine, thanks :wink:

2 Likes