【树莓派教程 】——获取树莓派当前状态和数据

2016-01-081.5万
AI 快速预览详细 收起
本文介绍了一个Python脚本,用于获取树莓派的CPU温度、内存使用情况和磁盘空间。通过调用os.popen和vcgencmd measure_temp等命令,可以实时监控树莓派的状态。这对于确保树莓派的稳定运行和延长使用寿命非常有帮助。


vi get.py
代码:
  1. import os
  2. # Return CPU temperature as a character string                                    
  3. def getCPUtemperature():
  4.     res = os.popen('vcgencmd measure_temp').readline()
  5.     return(res.replace("temp=","").replace("'C\n",""))
  6. # Return RAM information (unit=kb) in a list                                      
  7. # Index 0: total RAM                                                              
  8. # Index 1: used RAM                                                               
  9. # Index 2: free RAM                                                               
  10. def getRAMinfo():
  11.     p = os.popen('free')
  12.     i = 0
  13.     while 1:
  14.         i = i + 1
  15.         line = p.readline()
  16.         if i==2:
  17.             return(line.split()[1:4])
  18. # Return % of CPU used by user as a character string                              
  19. def getCPUuse():
  20.     return(str(os.popen("top -n1 | awk '/Cpu\(s\):/ {print $2}'").readline().strip()))
  21. # Return information about disk space as a list (unit included)                    
  22. # Index 0: total disk space                                                        
  23. # Index 1: used disk space                                                        
  24. # Index 2: remaining disk space                                                   
  25. # Index 3: percentage of disk used                                                
  26. def getDiskSpace():
  27.     p = os.popen("df -h /")
  28.     i = 0
  29.     while 1:
  30.         i = i +1
  31.         line = p.readline()
  32.         if i==2:
  33.             return(line.split()[1:5])
  34. # CPU informatiom
  35. CPU_temp = getCPUtemperature()
  36. CPU_usage = getCPUuse()
  37. # RAM information
  38. # Output is in kb, here I convert it in Mb for readability
  39. RAM_stats = getRAMinfo()
  40. RAM_total = round(int(RAM_stats[0]) / 1000,1)
  41. RAM_used = round(int(RAM_stats[1]) / 1000,1)
  42. RAM_free = round(int(RAM_stats[2]) / 1000,1)
  43. # Disk information
  44. DISK_stats = getDiskSpace()
  45. DISK_total = DISK_stats[0]
  46. DISK_used = DISK_stats[1]
  47. DISK_perc = DISK_stats[3]
  48. if __name__ == '__main__':
  49.     print('')
  50.     print('CPU Temperature = '+CPU_temp)
  51.     print('CPU Use = '+CPU_usage)
  52.     print('')
  53.     print('RAM Total = '+str(RAM_total)+' MB')
  54.     print('RAM Used = '+str(RAM_used)+' MB')
  55.     print('RAM Free = '+str(RAM_free)+' MB')
  56.     print('')
  57.     print('DISK Total Space = '+str(DISK_total)+'B')
  58.     print('DISK Used Space = '+str(DISK_used)+'B')
  59.     print('DISK Used Percentage = '+str(DISK_perc))
复制代码

然后执行python get.py
输出如图【树莓派教程 】——获取树莓派当前状态和数据图1

测试了一下树莓派再有风扇和没有风扇时候的的温度,差距还是比较大啊,为了保护我们的小派还是装个风扇吧~~


(明天上图吧,网速实在不敢恭维~~是不是DF该升级服务器了呢,哈哈~~)




创作许可协议

本项目采用 None(不开放任何权利,保留所有权利) 进行许可。

评论(4)
吹口琴的钢铁侠
吹口琴的钢铁侠
不能只贴代码啊= = 加点注释也行
凌风清羽
凌风清羽
作者
回复吹口琴的钢铁侠
恩恩,这几天有空就开始改进,
凌风清羽
凌风清羽
作者
回复吹口琴的钢铁侠
恩恩,这几天有空就开始改进,
共4条回复,已加载2条,点击查看更多共4条回复,已加载全部
丄帝De咗臂
丄帝De咗臂
有木有用过pcduino,安卓系统的
凌风清羽
凌风清羽
作者
回复丄帝De咗臂
木有:dizzy:,说rasp也可以装安卓啦
qq815076543322
qq815076543322
很牛掰
qq815076543322
qq815076543322回复孙毅
我也是新手。。。我也是新手。。。
孙毅
孙毅回复qq815076543322
我也是新手。。。
共4条回复,已加载2条,点击查看更多共4条回复,已加载全部
dsweiliang
dsweiliang
获取的是树莓派的数据?
凌风清羽
凌风清羽
作者
回复dsweiliang
对的,树莓派的实时数据~~
- 没有更多了 -