树莓派使用sgp30传感器的二氧化碳(co2)监测Python空气质量传感器,二氧化碳,GPIO编程,Python,传感器

By etertuuftj

2023-05-21 13:20:23

浏览量3719

已赞6

SGP30传感器,采用IIC总线与处理器通信。树莓派刚好有IIC引脚。且有现成的python包支持。有兴趣可以参考链接:python程序模块 - Python中文网,截至目前网上都是单片机用C语言来写的sgp30传感器检测实例。下面我们将一步步的介绍使用python在树莓派上编写一个sgp30传感器监测co2的实例。

SGP30数据手册

一.连接树莓派和sgp30传感器接口

Image

1.打开树莓派的I2C功能

树莓派默认打开I2C功能,连接好树莓派和SGP30硬件接线后,检查树莓派是否开启了I2C功能:

sudo i2cdetect -y -a 1

如果开启了会显示:

Image

表示SGP30传感器I2C地址:0x58,如果I2C没有打开,可以使用命令

sudo raspi-config进入树莓派功能配置 。

Image

2.安装python的SGP30模块

pip install sgp30

4.修正SGP模块中的一些引用

在包SGP的快速使用示例中看到它从smbus2中import SMBusWrapper。但是smbus2中其实是没有SMBusWrapper的。在sgp30中它也import了SMBusWrapper。因此这个写法就会报错。解决:将import SMBusWrapper改为from smbus2 import SMBus(sgp30中也对应的改)

错误示例:

Image

 5.编辑代码程序:

from smbus2 import SMBus
from sgp30 import Sgp30
import time
import os.path
import matplotlib.pyplot as plt
import datetime
import schedule
def run_recordData():
    print(".",end="")
    print()
    print(sgp.read_measurements())
    TimeNow=datetime.datetime.now().strftime('%H:%M')
    co2Data.append(sgp.read_measurements()[0][0])
    recordTime.append(TimeNow)
def run_show8hourPic():
    plt.close()
    plt.plot(recordTime,co2Data,linestyle='-', linewidth=1, marker='.', markersize=10, label='CO2')
    plt.legend()
    plt.xticks(rotation = 45)
    plt.show(block=False)
    plt.pause(3)
def run_resetData():
    co2Data=[]
    recordTime=[]
co2Data=[]
recordTime=[]
with SMBus(1) as bus:
    sgp=Sgp30(bus,baseline_filename="/tmp/mySGP30_baseline")#这句是sgp示例里面写的,我没仔细去看是用来做什么的,可以删掉
    print("resetting all i2c devices")
    sgp.i2c_geral_call()
    print(sgp.read_features())
    print(sgp.read_serial())
    sgp.init_sgp()
    print(sgp.read_measurements())
    print(sgp.read_measurements()[0][0])
    print("the SGP30 takes at least 15 seconds to warm up, 12 hours before the readigs become really stable")
    schedule.every(10).minutes.do(run_recordData)
    schedule.every(1).hours.do(run_show8hourPic)
    schedule.every(24).hours.do(run_resetData)
    while True:
        schedule.run_pending()  # run_pending:运行所有可以运行的任务
实现:每10分钟采集一次CO2的值,每小时更新一次展示图。每24小时重值归零数据后展示新一天的数据。

Image网上找到的空间环境中二氧化碳对人的影响:

Image

本文章最后由 超级版主2023-11-12 21:26 编辑
发表评论
请先 注册/登录 后参与评论

已有1 发布

默认   热门   正序   倒序
查看更多评论
(6) 分享
分享

扫二维码或复制链接分享该篇文章

取消
已有0次打赏
本站免责声明
1、本站资源,均来自网络或个人用户发布,版权归原作者,所有资源和文章仅限用于学习和研究目的 。
2、不得用于商业或非法用途,否则,一切责任由该用户承担 !

侵权删除请致信 E-Mail:cxg88@qq.com