我一直在从事一个项目,通过该项目我可以整理来自MQ-135空气质量传感器的空气质量信息,以检测我工作场所空气质量恶化的情况。然而,MQ-135空气质量传感器的位置在从传感器收集原始信息时具有巨大的重要性。因此,我决定通过开发一个用户友好的界面,通过选定的角度制作一个可移动的传感器底座。为了实现这一目标并添加一些确凿的功能(例如可调节背景光),我使用Python中的guizero模块为我的RaspberryPi开发了一个GUI,名为AirQualityModule。通过空气质量模块,您可以通过选择给定角度之一-0、30、45、90、135、180-来控制连接到迷你云台套件的两个伺服电机,并以RGB格式调整背景光-255,255,255.而且,最重要的是,您可以监控MQ-135传感器生成的当前空气质量范围:如果空气质量恶化,GUI会通过警告消息通知您。本项目中提到的一些产品和组件:树莓派3板壳3B树莓派4B/3B+SD卡32GB树莓派3BMicro-HDMIVGA转换器电缆补给品:树莓派3型号B+×1DigitSpaceRaspberryPi3板壳×1SD卡32GB×1DigitSpaceRaspberryPi3BMicro-HDMIVGA转换线×1AdafruitMCP3008×1MQ-135空气质量传感器×1PimoroniPan-Tilt帽子×1SG90微型伺服电机×2RGB扩散共阳极×1SparkFun可焊接面包板-迷你×2电阻220欧姆×3跳线(通用)×1在这个项目中,你将学到:如何使用RaspberryPi的角度控制伺服电机如何使用RaspberryPi调整RGBLED的颜色如何使用MCP3008和RaspberryPi读取模拟传感器如何使用guizero模块开发GUI如何编写范围函数来排列值,如Arduino中的map函数第1步:使用MCP3008读取模拟传感器与Arduino相比,RaspberryPi没有内置的ADC(模数转换器),我们可以通过它轻松地从模拟传感器收集信息。因此,我们需要在我们的电路中实现一个外部ADC,以整理来自模拟传感器(如MQ-135空气质量传感器)的数据。我使用了MCP30088通道ADC,因为它效率高且使用简单。我使用RaspberryPi上的AdafruitCircuitPythonMCP3xxx库来读取带有MCP3008的模拟传感器。但是,您需要安装在Python中提供CircuitPython支持的Adafruit_Blinka库才能使用上述库。sudopip3installadafruit-blinka如果需要,您必须在执行命令之前在RaspberryPiConfigurationSettings上启用SPI。完成后,从命令行运行以下命令来安装AdafruitCircuitPythonMCP3xxx库。sudopip3installadafruit-circuitpython-mcp3xxx然后,按如下方式进行引脚连接。尽管在项目原理图和代码中对引脚连接进行了很好的解释,但除此之外,我将保留MCP3008的以下引脚连接原理图。MCP3008CLK转PiSCLKMCP3008DOUT转PiMISOMCP3008DIN转PiMOSIMCP3008CS转PiD5MCP3008VDD至Pi3.3VMCP3008VREF至Pi3.3VMCP3008AGND到PiGNDMCP3008DGND到PiGNDMCP3008CH0到模拟传感器的信号引脚CH0指的是MCP3008上的第一个输入引脚-从0到7。第2步:开发GUI(空气质量模块)和编程RaspberryPi为了能够在Python中创建GUI,您需要在RaspberryPi上安装guizero模块。然后,您可以使用模块提供的所有小部件和事件。从您的命令行运行以下命令来安装guizero模块。sudopip3installguizero导入所需的库和模块。不要忘记包含所有GUI小部件-App、Box、Text、TextBox、PushButton、ButtonGroup、MenuBar、info、yesno、warn。创建SPI总线、cs(片选)和mcp对象。创建连接到MCP3008输入引脚的模拟输入通道——我只使用了通道0。定义GPIO引脚设置-GPIO.setmode(GPIO.BCM)。定义RGB引脚并设置PWM频率-100。在100-OFF处启动RGBPWM引脚。定义伺服电机引脚并设置PWM频率-50。在0-OFF处启动伺服底座和臂销。定义菜单栏选项命令(功能):在Tutorial()函数中,如果需要,打开一个yesno消息以转到项目教程页面。在Components()函数中,打开一条信息消息以显示组件。在About()函数中,打开一条信息消息以显示电梯间距。定义小部件命令和功能:在_range()函数中,复制Arduino映射函数以在Python中排列给定范围内的值。在evaluateSensorValue()函数中,从测试范围内的MQ-135空气质量中获取空气质量值-从0-60000到0-1023。测试您的模块,然后在本例中定义0到60000之间的值范围。如果超过阈值(300),则通过警告消息通知-空气质量恶化-并将状态更改为危险。在adjust_color()函数中,通过使用ChangeDutyCycle()更改PWM频率(在0到100之间)来调整RGBLED的颜色。将输入的颜色值从0-255排列到0-100。在base_tilt_move()和arm_tilt_move()函数中,通过选择的角度控制伺服电机-0,30,45,90,135,180-运行ChangeDutyCycle(selected_angle)。2到12之间的循环值精确地适用于0到180之间的角度。调整伺服电机位置后不要忘记执行以下行以关闭伺服PWM引脚。改变工作周期(0)创建名为AirQualityModule的GUI应用程序。定义菜单栏选项-教程、组件、关于。使用box小部件设计界面RGB颜色接口使用布局网格对齐部件。将相关命令(功能)分配给调整按钮-adjust_color。空气质量界面定义传感器和状态文本变量。使用repeat()函数每秒更新MQ-135空气质量传感器生成的传感器值。注意:在执行app.display()函数时,While循环不起作用。迷你云台底座接口在按钮组小部件中定义角度-0、30、45、90、135、180。分配相关命令(函数)-base_tilt_move。迷你云台臂接口在按钮组小部件中定义角度-0、30、45、90、135、180。分配相关的命令(函数)——arm_tilt_move。启动循环app.display()。特征1)改变仪器的背景颜色(RGB)。2)通过选择中间角度-0、30、45、90、135、180来调整迷你云台套件上伺服电机的位置。3)当传感器(MQ-135)检测到空气质量恶化时收到通知-状态:危险。在菜单栏上:4)进入教程页面。5)检查组件。6)显示电梯间距。连接引脚连接在代码和下面的项目示意图中得到了很好的解释。将树莓派连接到屏幕。将MCP3008和RGBLED连接到迷你面包板上。在将MCP3008连接到RaspberryPi之前,我使用了一个电位器来测试值范围。组装迷你云台套件并将MQ-135空气质量传感器粘在迷你云台套件(臂)上。总结完成所有步骤后,我将所有部件固定在一个旧塑料盒上,并将设备放在我的桌子上,以收集空气质量数据并使用树莓派上的空气质量模块(GUI)调整其背景颜色示意图代码#RaspberryPiAdjustableAirQualityDetectorControlledviaGUI##RaspberryPi3B+##ByKutluhanAktar##LearnhowtodevelopaGUI,namedAirQualityModule,tocontrolaminipan-tiltkitandgetinformationfromanMQ-135AirQualitySensor.#Also,youcanchangethebackgroundlight(RGB)viatheGUI.##Getmoreinformationontheprojectpage:#https://theamplituhedron.com/projects/Raspberry-Pi-Adjustable-Air-Quality-Detector-Controlled-via-GUI/fromguizeroimportApp,Box,Text,TextBox,PushButton,ButtonGroup,MenuBar,info,yesno,warnfromtimeimportsleepfromsubprocessimportcallimportRPi.GPIOasGPIOimportbusioimportdigitalioimportboardimportadafruit_mcp3xxx.mcp3008asMCPfromadafruit_mcp3xxx.analog_inimportAnalogIn#CreatetheSPIbusspi=busio.SPI(clock=board.SCK,MISO=board.MISO,MOSI=board.MOSI)#Createthecs(chipselect)cs=digitalio.DigitalInOut(board.D5)#Createthemcpobjectmcp=MCP.MCP3008(spi,cs)#CreateanaloginputsconnectedtotheinputpinsontheMCP3008.channel_0=AnalogIn(mcp,MCP.P0)#DefineRGBpinssettingsandPWMfrequenciesGPIO.setmode(GPIO.BCM)red_pin=2green_pin=3blue_pin=4GPIO.setup(red_pin,GPIO.OUT)GPIO.setup(green_pin,GPIO.OUT)GPIO.setup(blue_pin,GPIO.OUT)red_value=GPIO.PWM(red_pin,100)blue_value=GPIO.PWM(blue_pin,100)green_value=GPIO.PWM(green_pin,100)red_value.start(100)blue_value.start(100)green_value.start(100)#DefineservomotorpinsettingsandPWMfrequenciesservo_base_pin=20servo_arm_pin=21GPIO.setup(servo_base_pin,GPIO.OUT)GPIO.setup(servo_arm_pin,GPIO.OUT)servo_base_value=GPIO.PWM(servo_base_pin,50)servo_arm_value=GPIO.PWM(servo_arm_pin,50)servo_base_value.start(0)servo_arm_value.start(0)#Definemenubaroptions'commands(functions).defTutorial():#Opentheprojectpageifrequested.go_to_tutorial=yesno("OpenTutorial","Getmoreinformationabouttheproject!")ifgo_to_tutorial==True:command="chromium-browserhttps://theamplituhedron.com/projects/"call([command],shell=True)print("ProjectTutorial!")else:warn("Close","Returntotheapplication!")defComponents():info("Components","RaspberryPi3B+\nMQ-135Sensor\nMiniPan-TiltKit\n2xServoMotor\nRGBLED\n2xMiniBreadboard\nMCP3008")defAbout():info("About","DevelopaGUI,namedAirQualityModule,tocontrolaminipan-tiltkitandgetinformationfromanMQ-135AirQualitySensor.Also,youcanchangethebackgroundlight(RGB)ontheGUI.")#Defineapplicationcommandsandfeatures:def_range(x,in_min,in_max,out_min,out_max):returnint((x-in_min)*(out_max-out_min)/(in_max-in_min)+out_min)defevaluateSensorValue():#Testyourmodule,thendefinethevaluerange-inthiscasebetween0and60000.sensorValue=_range(channel_0.value,0,60000,0,1023)sensor_value.value=sensorValue#Thresholdif(sensorValue>300):status_text.value="Status:DANGER"status_text.text_color="yellow"warn("!!!DANGER!!!","AirQualityDeteriorating!")else:status_text.value="Status:OK"status_text.text_color="green"defadjust_color():red=_range(int(r_input.value),0,255,1,100)green=_range(int(g_input.value),0,255,1,100)blue=_range(int(b_input.value),0,255,1,100)red_value.ChangeDutyCycle(101-red)blue_value.ChangeDutyCycle(101-blue)green_value.ChangeDutyCycle(101-green)defbase_tilt_move():#Cyclevaluesbetween2and12areworkingprecisely:selected_angle=2+(int(base_angle.value)/18)servo_base_value.ChangeDutyCycle(selected_angle)sleep(0.5)servo_base_value.ChangeDutyCycle(0)defarm_tilt_move():selected_angle=2+(int(arm_angle.value)/18)servo_arm_value.ChangeDutyCycle(selected_angle)sleep(0.5)servo_arm_value.ChangeDutyCycle(0)#CreatetheGUIapplication.appWidth=1200appHeight=500app=App(title="AirQualityModule",bg="#eb2e00",width=appWidth,height=appHeight)#Definemenubaroptions.menubar=MenuBar(app,toplevel=["Tutorial","Components","About"],options=[[["View",Tutorial]],[["Inspect",Components]],[["About",About]]])#Designtheinterfaceusingtheboxwidget.top=Box(app,width="fill",height=appHeight/2,align="top")bottom=Box(app,width="fill",height=appHeight/2,align="bottom")color_interface=Box(top,width=appWidth/2,align="left",layout="grid",border=True)quality_interface=Box(top,width=appWidth/2,align="right")base_interface=Box(bottom,width=appWidth/2,align="left")arm_interface=Box(bottom,width=appWidth/2,align="right")#RGBColorInterfacecolor_header=Text(color_interface,text="AdjustRGBBackgroundColor",color="#002699",size=20,grid=[0,0])r_label=Text(color_interface,text="R:",color="#1a53ff",size=15,grid=[0,1])r_input=TextBox(color_interface,grid=[1,1])r_input.bg="#ff5c33"r_input.text_color="#1a53ff"g_label=Text(color_interface,text="G:",color="#1a53ff",size=15,grid=[0,2])g_input=TextBox(color_interface,grid=[1,2])g_input.bg="#ff5c33"g_input.text_color="#1a53ff"b_label=Text(color_interface,text="B:",color="#1a53ff",size=15,grid=[0,3])b_input=TextBox(color_interface,grid=[1,3])b_input.bg="#ff5c33"b_input.text_color="#1a53ff"adjust_button=PushButton(color_interface,grid=[2,4],width="20",text="Adjust",command=adjust_color)adjust_button.bg="#002699"adjust_button.text_color="white"#AirQualityInterfacequality_header=Text(quality_interface,text="AirQualitySensor",color="#002699",size=20,align="top")sensor_value=Text(quality_interface,text="TEST",color="#002699",size=120)status_text=Text(quality_interface,text="Status:OK",color="green",size=15,align="bottom")#Updatethesensorvalue.sensor_value.repeat(1000,evaluateSensorValue)#MiniPan-TiltBaseInterfacebase_header=Text(base_interface,text="Pan-TiltBase",color="#002699",size=20)base_angle=ButtonGroup(base_interface,options=["0","30","45","90","135","180"],selected="0",width=20,command=base_tilt_move)base_angle.text_size=15base_angle.text_color="white"#MiniPan-TiltArmInterfacearm_header=Text(arm_interface,text="Pan-TiltArm",color="#002699",size=20)arm_angle=ButtonGroup(arm_interface,options=["0","30","45","90","135","180"],selected="0",width=20,command=arm_tilt_move)arm_angle.text_size=15arm_angle.text_color="white"#Starttheloop.app.display()*以上内容翻译自网络,原作者:KutluhanAktar,如涉及侵权可联系删除。