打造自己的树莓派监控系统1--CPU监控-matplotlib显示数据
发布日期:2022-03-18 18:19:18
浏览次数:2
分类:技术文章
本文共 1918 字,大约阅读时间需要 6 分钟。
系列文章:
-
文章1:
-
文章2:
-
文章3:
-
GitHub仓库:
代码编写
树莓派的CPU温度是存放在一个文件里,使用如下命令查看CPU温度:
cat /sys/class/thermal/thermal_zone0/temp
输出的是五位的整数,除以1000就是CPU的温度了。
python代码如下:def get_temperature(): try: cpu_temp_file = open("/sys/class/thermal/thermal_zone0/temp") cpu_temp = cpu_temp_file.read() return cpu_temp except Exception as e: print(e) finally: cpu_temp_file.close()
然后我使用Python自带的sqlite数据库存储数据:
def create(): global conn conn = sqlite3.connect('data.db') conn.execute(""" create table if not exists temperature( id INTEGER PRIMARY KEY , temperature INTEGER DEFAULT NULL, time INTEGER DEFAULT NULL)""") conn.commit()def save(temperature): # 将数据保存至本地 global conn command1 = "insert into temperature \ (temperature,time) values (?,?);" try: temp = (temperature, int(round(time.time() * 1000))) conn.execute(command1, temp) except Exception as e: print(e) print("insert error!") conn.rollback() conn.commit()
最后是画图:
def cpu(): import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt global conn temperature = cpu_get() ID = len(temperature) past = datetime.datetime.now()-datetime.timedelta(minutes=ID) x = [past+datetime.timedelta(minutes=i) for i in range(ID)] plt.title("time and cpu temperature", fontsize=25) plt.xlabel("time", fontsize=15) plt.ylabel("cpu temperature", fontsize=15) plt.plot(x, temperature) plt.ylim(20 if 20 < min(temperature) else min(temperature), 100 if 100 > max(temperature) else max(temperature)) plt.gcf().autofmt_xdate() plt.savefig('static/temperature.jpg')
运行
此项目的GitHub地址:
运行如下命令:git clone https://github.com/zhang0peter/raspberry-pi-monitor.gitcd raspberry-pi-monitor/screen -S raspberry-pi-monitorbash main.sh
然后在浏览器中打开即可看到树莓派CPU温度-时间图:
转载地址:https://zhang0peter.blog.csdn.net/article/details/84071442 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
逛到本站,mark一下
[***.202.152.39]2024年03月11日 15时57分46秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
php date 函数用法,php中date()日期时间函数使用方法
2019-04-21
php除法获取整数和余数,PHP除法取整和取余数
2019-04-21
java迷宫路径,Java中的迷宫路径查找器
2019-04-21
php substr cnblog,php中substr用法示例
2019-04-21
php链接怎么截取,PHP 截取网页中的固定种子链接
2019-04-21
iis运行不起来php报500,解决IIS上安装thinkphp6运行报500错误
2019-04-21
matlab 图中的legend,matlab中legend加图示命令的使用
2019-04-21
PHP exec xargs 不执行,Linux中的xargs命令及示例
2019-04-21
php 枚举cookie内容,php设置和获取cookie
2019-04-21
oracle 坏块查找,检测oracle数据库坏块的方法
2019-04-21
怎么对oracle 进行重装,重装oracle的方法
2019-04-21
php eol 没有换行,php_eol没有换行怎么办
2019-04-21
oracle数据对象缓存,oracle优化缓存对象与数据 -电脑资料
2019-04-21
linux系统如何连接wf,Linux下如何通过命令连接wifi
2019-04-21
linux网卡传输数据卡死,linux – 网卡正在接收,但根本没有传输?
2019-04-21