BearPi-HM_Nano开发板传感器驱动开发——MQ2读取烟雾浓度
本示例将演示如何在BearPi-HM_Nano开发板上使用E53_SF1读取烟雾浓度,当烟雾浓度超标时蜂鸣器发出警报
E53_SF1 API分析
本案例主要使用了以下API完成烟雾浓度读取
1、Init_E53_SF1()
/***************************************************************
* 函数名称: Init_E53_SF1
* 说 明: 初始化E53_SF1扩展板
* 参 数: 无
* 返 回 值: 无
***************************************************************/
void Init_E53_SF1(void);
描述:
初始化E53_SF1
2、MQ2_PPM_Calibration()
/***************************************************************
* 函数名称: MQ2_PPM_Calibration
* 说 明: 传感器校准函数
* 参 数: 无
*
* 返 回 值: 无
***************************************************************/
void MQ2_PPM_Calibration(void);
描述: MQ2传感器校准
3、GetMQ2PPM()
/***************************************************************
* 函数名称: GetMQ2PPM
* 说 明: 获取PPM函数
* 参 数: 无
*
* 返 回 值: ppm
***************************************************************/
float GetMQ2PPM(void);
描述:
获取烟雾浓度ppm
硬件设计
本案例将用到 E53_SF1 智慧烟感扩展板与 BearPi-HM_Nano 开发板,其中E53_SF1扩展板原理图如下,ADC输出引脚为第五脚,将E53_SF1扩展板插在 BearPi-HM_Nano 开发板上后,该ADC输出引脚与GPIO_13相连接,通过查看芯片手册可知GPIO_13对应的是 ADC Channel 6 ,所以需要编写软件去读取ADC Channel 6的电压实现对烟雾浓度的读取。
E53_SF1 智慧烟感扩展板与 BearPi-HM_Nano 开发板安装
软件设计
主要代码分析
首先调用 Init_E53_SF1()
函数初始化E53_SF1所接的引脚的功能,再等待1s后进行校准,获取正常环境下传感器的参数,然后循环调用 GetMQ2PPM()
函数读取ppm值并通过串口打印出来,当ppm大于15时触发蜂鸣器报警,小于15时关闭报警。
项目目录结构:
yuchuanhuaying@yuchuanhuaying:~/MasterData/PublicSharedData/BearPiCode/BearPiIoTCode/applications/sample/BearPi/BearPi-HM_Nano/sample/C1_YuchuanE53_SF1_MQ2$ tree
.
├── BUILD.gn
├── include
│ └── E53_SF1.h
├── src
│ └── E53_SF1.c
└── yuchuan_e53_sf1_mq2.c
2 directories, 4 files
yuchuanhuaying@yuchuanhuaying:~/MasterData/PublicSharedData/BearPiCode/BearPiIoTCode/applications/sample/BearPi/BearPi-HM_Nano/sample/C1_YuchuanE53_SF1_MQ2$
yuchuan_e53_sf1_mq2.c
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <math.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "E53_SF1.h"
#define E53_TASK_CB_MEM_SIZE 0U
#define E53_TASK_STACK_MEM_SIZE 1024 * 4
#define E53_TASK_PRIORITY 25
static void E53Sf1Mq2Task(void *argument)
{
(void)argument;
float ppm;
//初始化E53接口
Init_E53_SF1();
/***传感器校准***/
//开机1s后进行校准
usleep(1000000);
//校准传感器
MQ2_PPM_Calibration();
while (1)
{
/* code */
printf("====================================================\r\n");
printf("*************Yuchuan E53_SF1 Task Example***********\r\n");
printf("====================================================\r\n");
//获取PPM的值
ppm = GetMQ2PPM();
printf("PPM is value : %.3f\n",ppm);
if (ppm > 15)
{
//设置状态
BeepStatuSet(ON);
printf("PPM ON Status!!!\n");
}
else
{
//设置状态
BeepStatuSet(OFF);
printf("PPM OFF Status!!!\n");
}
usleep(3000000);
}
}
static void YuchuanE53Sf1Mq2Entry(void)
{
osThreadAttr_t threadAttr;
threadAttr.attr_bits = 0U;
threadAttr.cb_mem = NULL;
threadAttr.cb_size = E53_TASK_CB_MEM_SIZE;
threadAttr.stack_mem = NULL;
threadAttr.stack_size = E53_TASK_STACK_MEM_SIZE;
threadAttr.priority = E53_TASK_PRIORITY;
threadAttr.name = "YuchuanE53SF1MQ2Task";
if (osThreadNew((osThreadFunc_t)E53Sf1Mq2Task,NULL,&threadAttr) == NULL)
{
/* code */
printf("Falied to Create YuchuanE53SF1MQ2Task!!!\n");
}
}
SYS_RUN(YuchuanE53Sf1Mq2Entry);
include/E53_SF1.h
#ifndef __E53_SF1_H__
#define __E53_SF1_H__
typedef enum{
OFF = 0,
ON,
}E53_SF1_Status_ENUM;
/**初始化E53接口**/
void Init_E53_SF1(void);
/**校准传感器**/
void MQ2_PPM_Calibration(void);
/**获取PPM**/
float GetMQ2PPM(void);
/**设置状态**/
void BeepStatuSet(E53_SF1_Status_ENUM status);
#endif /* __E53_SF1_H__ */
src/E53_SF1.c
#include <stdio.h>
#include <math.h>
#include <string.h>
#include "E53_SF1.h"
#include "iot_gpio.h"
#include "iot_gpio_ex.h"
#include "iot_pwm.h"
#include "iot_adc.h"
#include "iot_errno.h"
#define WIFI_IOT_GPIO_8 8
#define WIFI_IOT_PWM_PORT_PWM1 1
#define WIFI_IOT_ADC_CHANNEL 5
//RL阻值
#define RL 1
//校准环境中PPM值
#define CAL_PPM 20
//原件在洁净空气中的阻值
static float R0;
/*初始化E53接口*/
void Init_E53_SF1(void)
{
//初始化GPIO_8
IoTGpioInit(WIFI_IOT_GPIO_8);
//设置引脚复用功能为PWM
IoTGpioSetFunc(WIFI_IOT_GPIO_8,IOT_GPIO_FUNC_GPIO_8_PWM1_OUT);
//设置GPIO的方向为输出方向
IoTGpioSetDir(WIFI_IOT_GPIO_8,IOT_GPIO_DIR_OUT);
//初始化PWM
IoTPwmInit(WIFI_IOT_PWM_PORT_PWM1);
}
/**获取电压值的函数**/
static float GetVoltage(void)
{
unsigned int ret;
unsigned short data;
//unsigned int IoTAdcRead(unsigned int channel, unsigned short *data, IotAdcEquModelSel equModel,IotAdcCurBais curBais, unsigned short rstCnt);
ret = IoTAdcRead(WIFI_IOT_ADC_CHANNEL, &data, IOT_ADC_EQU_MODEL_8, IOT_ADC_CUR_BAIS_DEFAULT, 0xff);
if (ret != IOT_SUCCESS)
{
/* code */
printf("ADC Read Voltage Falied !!!\n");
}
return (float)data * 1.8 * 4 / 4096.0;
}
/**校准传感器**/
void MQ2_PPM_Calibration(void)
{
float voltage = GetVoltage();
//计算RS
float RS = (5 - voltage) / voltage * RL;
//计算ppm
R0 = RS / pow(CAL_PPM / 613.9f, 1 / -2.074f);
}
/**获取PPM的函数**/
float GetMQ2PPM(void)
{
float voltage, RS, ppm;
voltage = GetVoltage();
//计算RS
RS = (5 - voltage) / voltage * RL;
//计算ppm
ppm = 613.9f * pow(RS / R0, -2.074f);
return ppm;
}
/**蜂鸣器报警与否**/
void BeepStatuSet(E53_SF1_Status_ENUM status)
{
if (status == ON)
{
//输出PWM波
//unsigned int IoTPwmStart(unsigned int port, unsigned short duty, unsigned int freq);
IoTPwmStart(WIFI_IOT_PWM_PORT_PWM1, 50, 4000);
}
if (status == OFF)
{
//停止PWM波
//unsigned int IoTPwmStop(unsigned int port);
IoTPwmStop(WIFI_IOT_PWM_PORT_PWM1);
}
}
编译调试
//applications/sample/BearPi/BearPi-HM_Nano/sample/C1_YuchuanE53_SF1_MQ2/BUILD.gn
static_library("YuchuanE53SF1Mq2"){
sources = [
"yuchuan_e53_sf1_mq2.c",
"src/E53_SF1.c",
]
include_dirs = [
"//utils/native/lite/include",
"//device/bearpi/bearpi_hm_nano/hi3861_adapter/kal/cmsis",
"//base/iot_hardware/peripheral/interfaces/kits",
"include",
]
}
修改 BUILD.gn 文件
修改applications\BearPi\BearPi-HM_Nano\sample
路径下 BUILD.gn 文件,指定 YuchuanE53SF1Mq2 参与编译。
# Copyright (c) 2020 Nanjing Xiaoxiongpai Intelligent Technology Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import("//build/lite/config/component/lite_component.gni")
lite_component("sample") {
features = [
#"A1_kernal_thread:thread_example",
#"A2_kernel_timer:timer_example",
#"A3_kernel_event:event_example",
#"A4_kernel_mutex:mutex_example",
#"A5_kernel_semaphore:semaphore_example",
#"A6_kernel_message:message_example",
#"B1_basic_led_blink:led_example",
#"B2_basic_button:button_example",
#"B3_basic_pwm_led:pwm_example",
#"B4_basic_adc:adc_example",
#"B5_basic_i2c_nfc:i2c_example",
#"B6_basic_uart:uart_example",
#"C1_e53_sf1_mq2:e53_sf1_example",
#"C2_e53_ia1_temp_humi_pls:e53_ia1_example",
#"C3_e53_sc1_pls:e53_sc1_example",
#"C4_e53_sc2_axis:e53_sc2_example",
#"C5_e53_is1_infrared:e53_is1_example",
#"D1_iot_wifi_ap:wifi_ap",
#"D2_iot_wifi_sta_connect:wifi_sta_connect",
#"D3_iot_udp_client:udp_client",
#"D4_iot_tcp_server:tcp_server",
#"D5_iot_mqtt:iot_mqtt",
#"D6_iot_cloud_oc:oc_mqtt",
#"D7_iot_cloud_onenet:onenet_mqtt",
#"D8_iot_cloud_oc_smoke:cloud_oc_smoke",
#"D9_iot_cloud_oc_light:cloud_oc_light",
#"D10_iot_cloud_oc_manhole_cover:cloud_oc_manhole_cover",
#"D11_iot_cloud_oc_infrared:cloud_oc_infrared",
#"D12_iot_cloud_oc_agriculture:cloud_oc_agriculture",
#"D13_iot_cloud_oc_gps:cloud_oc_gps",
#"B1_YuchuanBasicLEDBlink:yuchuanLED",
#"B2_YuchuanBasicButton:yuchuanButton",
"C1_YuchuanE53_SF1_MQ2:YuchuanE53SF1Mq2"
]
}
运行结果
示例代码编译烧录代码后,按下开发板的RESET按键,通过串口助手查看日志,会打印以下信息,对着烟雾探头制作烟雾,ppm会升高蜂鸣器发出报警。
ready to OS start
sdk ver:Hi3861V100R001C00SPC025 2020-09-03 18:10:00
FileSystem mount ok.
wifi init success!
hiview init success.00 00:00:00 0 132 D 0/HIVIEW: log limit init success.
00 00:00:00 0 132 I 1/SAMGR: Bootstrap core services(count:3).
00 00:00:00 0 132 I 1/SAMGR: Init service:0x4b06d0 TaskPool:0xe4b3c
00 00:00:00 0 132 I 1/SAMGR: Init service:0x4b06dc TaskPool:0xe4b5c
00 00:00:00 0 132 I 1/SAMGR: Init service:0x4b0ba8 TaskPool:0xe4b7c
00 00:00:00 0 8 I 1/SAMGR: Init service 0x4b06dc <time: 0ms> success!
00 00:00:00 0 164 I 1/SAMGR: Init service 0x4b06d0 <time: 10ms> success!
00 00:00:00 0 108 I 1/SAMGR: Init service 0x4b0ba8 <time: 10ms> success!
00 00:00:00 0 108 I 1/SAMGR: Initialized all core system services!
00 00:00:00 0 164 I 1/SAMGR: Bootstrap system and application services(count:0).
00 00:00:00 0 164 I 1/SAMGR: Initialized all system and application services!
00 00:00:00 0 164 I 1/SAMGR: Bootstrap dynamic registered services(count:0).
====================================================
*************Yuchuan E53_SF1 Task Example***********
====================================================
PPM is value : 20.000
PPM ON Status!!!
====================================================
*************Yuchuan E53_SF1 Task Example***********
====================================================
PPM is value : 20.000
PPM ON Status!!!
====================================================
*************Yuchuan E53_SF1 Task Example***********
====================================================
PPM is value : 20.237
PPM ON Status!!!
====================================================
*************Yuchuan E53_SF1 Task Example***********
====================================================
PPM is value : 20.237
PPM ON Status!!!
====================================================
*************Yuchuan E53_SF1 Task Example***********
====================================================
PPM is value : 20.237
PPM ON Status!!!