简体   繁体   English

如何获取Raspberry Pi B温度作为Python3变量?

[英]How to get a Raspberry Pi B temperature as a Python3 variable?

Is there any way to get the temperature of a Raspberry Pi, specifically the B model as a Python3 variable. 有没有办法获取Raspberry Pi的温度,特别是将B模型作为Python3变量。

The current OS is Raspbian. 当前的操作系统是Raspbian。

I do not have the onboard Sensor on my RPi, but according to this thread , you require this line: /opt/vc/bin/vcgencmd measure_temp 我的RPi上没有板载Sensor,但是根据此线程 ,您需要以下行: /opt/vc/bin/vcgencmd measure_temp

Keep in mind that this doesn't provide a float (as I had previously thought). 请记住,这并不能提供浮点数(就像我之前所想的那样)。 However, you can use the subprocess module to acquire the data and format/strip it from there: 但是,您可以使用subprocess模块来获取数据并从那里格式化/打包数据:

import subprocess
temp = subprocess.check_output(["/opt/vc/bin/vcgencmd measure_temp | cut -c6-9"], shell=True)[:-1]

I have read that using subprocess.Popen() is a better approach - failed to find out why though (perhaps someone else can elaborate on this?). 我已经读到使用subprocess.Popen()是更好的方法-尽管无法找出原因(也许其他人可以对此进行详细说明?)。

It would look something like this: 它看起来像这样:

tmp = subprocess.Popen(["/opt/vc/bin/vcgencmd", "measure_temp"], stdout=subprocess.PIPE)

You can read up more on the subprocess module here . 您可以在此处阅读有关subprocess模块的更多信息

Edit : I've updated the code; 编辑 :我已经更新了代码; I recently found a pyscript returning the temperature of the RPi. 我最近发现一个返回RPi温度的pyscript。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM