让树莓派说出自己的“体温”--cpu温度+gpu温度

  • Chen Yang
  • LV3工程师
  • |      2015-02-12 11:40:11
  • 浏览量 1135
  • 回复:0
事先说明:
这个也是比较贴近实用的文章,虽然大家的树莓派不一定是用来大规模集群计算的,但是知道树莓派的温度也可以预防树莓派烧掉啊。

材料准备:
一个可以正常运行的联网的树莓派+一块连接树莓派的显示器或者显示板(就是那种不能显示图形的lcd显示屏)

技术准备:
1.熟练掌握或者精通linux系统以及使用sudo命令
2.会建立python脚本以及存储到文件夹中
3.能让python脚本运行

4.能编辑/boot/boot.rc文件


正文如下:

1.复制以下文本保存到python脚本文件(命名为wendu.py)中,并且放置在类似这样的目录当中(/home/pi/Code/wendu.py



import commands

  

def get_cpu_temp():

    tempFile = open( "/sys/class/thermal/thermal_zone0/temp" )

    cpu_temp = tempFile.read()

    tempFile.close()

    return float(cpu_temp)/1000

    # Uncomment the next line if you want the temp in Fahrenheit

    #return float(1.8*cpu_temp)+32

  

def get_gpu_temp():

    gpu_temp = commands.getoutput( \'/opt/vc/bin/vcgencmd measure_temp\' ).replace( \'temp=\', \'\' ).replace( \'\'C\', \'\' )

    return  float(gpu_temp)

    # Uncomment the next line if you want the temp in Fahrenheit

    # return float(1.8* gpu_temp)+32

  

def main():

    print "CPU temp: ", str(get_cpu_temp())

    print "GPU temp: ", str(get_gpu_temp())

  

if __name__ == \'__main__\':

    main()



这里面有两个命令:get_cpu_tempget_gpu_temp,看名字就知道了,一个对应cpu的温度,一个是对应gpu的温度的,它们俩都会返回一个float型的摄氏温度值。
注意:这个脚本里面输出的是摄氏温度℃的,如果想要使用华氏度℉的话,就把里面用#注释掉的两端代码打开(就是删掉前面的3号)就好了。


2.使这个脚本运行

sudo chmod +x mailer.py

3.设置开机运行
如果大家觉得有必要把这个脚本 开机启动的话就需要编辑 /boot/boot.rc 文件了(假定已经重命名了这个rc文件
在这个boot.rc文件的最后,修改路径为放置wendu.py文件的路径,比如:


#Script to email ip address upon reboot

python /home/pi/Code/wendu.py


如果你们使用的是 Rasbian 系统的话,那就是没有这个rc文件的,但是可以加在这个 /etc/rc.local 文件中:


sudo nano /etc/rc.local


修改文件,加上最后两行代码,像这个样子:


# rc.local

#

# This script is executed at the end of each multiuser runlevel.

# Make sure that the script will "exit 0" on success or any other

# value on error.

#

# In order to enable or disable this script just change the execution

# bits.

#

# By default this script does nothing.

# Print the IP address if it doesn\'t work ad sleep 30 before all your code

 

python /home/pi/Code/mailer.py

exit 0



教程结束。

  • 0
  • 收藏
  • 举报
  • 分享
我来回复

登录后可评论,请 登录注册

所有回答 数量:0
x
收藏成功!点击 我的收藏 查看收藏的全部帖子