0

I can get the temperatue of Raspberry pi using the python command:

os.popen("vcgencmd measure_temp").readline()

But when I am running this command inside a lambda function (python 2.7) on deployed greengrass on the device, it gives me error:

VCHI initialization failed

I believe this is because lambda function is running in a container is not cognizant about the raspberry pi it is running on.

How can I get the temperature of raspberry pi from lambda function running on greengrass?

Bharthan
  • 1,458
  • 2
  • 17
  • 29

1 Answers1

0

There are two ways to read the CPU temperature - one using vcgencmd and one using the file interface. It may be that greengrass prevents you from running vcgencmd, it may be that it doesn't let you access the file interface either but it's worth giving it a try. The file is located at /sys/class/thermal/thermal_zone0/temp.

One way is using gpiozero's CPUTemperature class:

from gpiozero import CPUTemperature

cpu = CPUTemperature()

print(cpu.temperature)

Alternatively, read the file directly, and extract the temperature (as gpiozero does underneath):

def cpu_temp():
    sensor_file = '/sys/class/thermal/thermal_zone0/temp'

    with io.open(sensor_file, 'r') as f:
        return float(f.readline().strip()) / 1000

print(cpu_temp())
ben_nuttall
  • 859
  • 10
  • 20