10097浏览
查看: 10097|回复: 14

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

[复制链接]
本帖最后由 凌风清羽 于 2016-1-25 20:02 编辑

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该升级服务器了呢,哈哈~~)




dsweiliang  初级技神

发表于 2016-1-9 08:00:31

获取的是树莓派的数据?
回复

使用道具 举报

大连林海  初级技神

发表于 2016-1-9 08:53:44

很牛掰
回复

使用道具 举报

凌风清羽  中级技匠
 楼主|

发表于 2016-1-9 12:59:34

dsweiliang 发表于 2016-1-9 08:00
获取的是树莓派的数据?

对的,树莓派的实时数据~~
回复

使用道具 举报

凌风清羽  中级技匠
 楼主|

发表于 2016-1-9 12:59:51


新手请教~~~
回复

使用道具 举报

大连林海  初级技神

发表于 2016-1-9 14:08:29


额 我是新手
回复

使用道具 举报

孙毅  初级技匠

发表于 2016-1-9 16:41:21


我也是新手。。。
回复

使用道具 举报

大连林海  初级技神

发表于 2016-1-9 20:42:42

孙毅 发表于 2016-1-9 16:41
我也是新手。。。

我也是新手。。。我也是新手。。。
回复

使用道具 举报

丄帝De咗臂  高级技匠

发表于 2016-1-10 20:03:11

有木有用过pcduino,安卓系统的
回复

使用道具 举报

凌风清羽  中级技匠
 楼主|

发表于 2016-1-10 20:43:56

丄帝De咗臂 发表于 2016-1-10 20:03
有木有用过pcduino,安卓系统的

木有:dizzy:,说rasp也可以装安卓啦
回复

使用道具 举报

吹口琴的钢铁侠  初级技匠

发表于 2016-1-14 15:39:23

不能只贴代码啊= = 加点注释也行
回复

使用道具 举报

凌风清羽  中级技匠
 楼主|

发表于 2016-1-14 17:13:32

吹口琴的钢铁侠 发表于 2016-1-14 15:39
不能只贴代码啊= = 加点注释也行

恩恩,这几天有空就开始改进,
回复

使用道具 举报

凌风清羽  中级技匠
 楼主|

发表于 2016-1-14 17:13:33

吹口琴的钢铁侠 发表于 2016-1-14 15:39
不能只贴代码啊= = 加点注释也行

恩恩,这几天有空就开始改进,
回复

使用道具 举报

凌风清羽  中级技匠
 楼主|

发表于 2016-1-14 17:13:34

吹口琴的钢铁侠 发表于 2016-1-14 15:39
不能只贴代码啊= = 加点注释也行

恩恩,这几天有空就开始改进,
回复

使用道具 举报

凌风清羽  中级技匠
 楼主|

发表于 2016-1-14 17:13:36

吹口琴的钢铁侠 发表于 2016-1-14 15:39
不能只贴代码啊= = 加点注释也行

恩恩,这几天有空就开始改进,
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail