sudo i2cdetect -y -a 1
pip install sgp30
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小时重值归零数据后展示新一天的数据。