BME680 Compensated-temp.py

Hi, I’m trying to use the compensated-temperature.py script in the BME680 repository, but it is resulting in a type error. Here is the code:

from subprocess import PIPE, Popen

# Gets the CPU temperature in degrees C
def get_cpu_temperature():
    process = Popen(['vcgencmd', 'measure_temp'], stdout=PIPE)
    output, _error = process.communicate()
    return float(output[output.index('=') + 1:output.rindex("'")])

cpu_temp = get_cpu_temperature()

The error is :
TypeError: argument should be integer or bytes-like object, not ‘str’

I’m not sure how to interpret this error message, and I would love some help!

I’ve not got a Pi handy to test this on, but are you running this on Python 3? Python 2 and 3 format the output of subprocess differently. Try just printing the output and see how it looks.

The output comes back as an empty binary string, I think.

output = b""

Ah ok, so as far as I can see the output of the Popen command isn’t a string, it’s a bytes object. You need to convert that to a string before using the .index and .rindex string methods on it:

from subprocess import PIPE, Popen

# Gets the CPU temperature in degrees C
def get_cpu_temperature():
    process = Popen(['vcgencmd', 'measure_temp'], stdout=PIPE)
    output, _error = process.communicate()
    output.decode("utf-8")
    return float(output[output.index('=') + 1:output.rindex("'")])

cpu_temp = get_cpu_temperature()