【智能家居】二、添加火灾检测模块(烟雾报警功能点)

[可燃气体传感器 MQ-2 和 蜂鸣器](#可燃气体传感器 MQ-2 和 蜂鸣器)
代码段

  • controlDevice.h(设备控制)
  • smokeAlarm.c(烟雾报警器)
  • buzzer.c(蜂鸣器)
  • mainPro.c(主函数)
  • 运行结果

可燃气体传感器 MQ-2 和 蜂鸣器


代码段

controlDevice.h(设备类)

c 复制代码
#include <wiringPi.h>					//wiringPi库
#include <stdio.h>
#include <stdlib.h>
 
struct Devices                          //设备类
{
    char deviceName[128];               //设备名
    int status;                         //状态
    int pinNum;							//引脚号
 
    int (*Init)(int pinNum);			//"初始化设备"函数指针
	int (*open)(int pinNum);			//"打开设备"函数指针
	int (*close)(int pinNum);			//"关闭设备"函数指针
    int (*readStatus)(int pinNum);		//"读取设备状态"函数指针  为火灾报警器准备
	int (*changeStatus)(int status);	//"改变设备状态"函数指针
 
    struct Devices *next;
};
 
struct Devices* addBathroomLightToDeviceLink(struct Devices *phead);		//"浴室灯"加入设备链表函数声明      2
struct Devices* addBedroomLightToDeviceLink(struct Devices *phead);	        //"卧室灯"加入设备链表函数声明      8
struct Devices* addRestaurantLightToDeviceLink(struct Devices *phead);		//"餐厅灯"加入设备链表函数声明      13
struct Devices* addLivingroomLightToDeviceLink(struct Devices *phead);		//"客厅灯"加入设备链表函数声明      16
struct Devices* addSmokeAlarmToDeviceLink(struct Devices *phead);           //"烟雾报警器"加入设备链表函数声明  6
struct Devices* addBuzzerToDeviceLink(struct Devices *phead);		        //"蜂鸣器"加入设备链表函数声明      9

smokeAlarm.c(烟雾报警器)

c 复制代码
#include "controlDevice.h"			        //自定义设备类的文件
 
int smokeAlarmInit(int pinNum)              //C语言必须要传参,JAVA不用,可直接访问变量的值
{
	pinMode(pinNum,INPUT);				    //配置引脚为输入模式
	//digitalWrite(pinNum,HIGH);			//引脚置高电平,断开继电器
}
 
int smokeAlarmReadStatus(int pinNum)
{
	return digitalRead(pinNum);
}
 
int smokeAlarmStatus(int status)
{
	
}
 
struct Devices smokeAlarm = {			//定义烟雾报警器(对象)
	.deviceName = "smokeAlarm",			//名字
	.pinNum = 6,						//香橙派 6号(wPi)引脚
	.Init = smokeAlarmInit,				//指定初始化函数
    .readStatus = smokeAlarmReadStatus,
    .changeStatus = smokeAlarmStatus
};
 
struct Devices* addSmokeAlarmToDeviceLink(struct Devices *phead)		//烟雾报警器(对象)加入设备链表函数
{
	if(phead == NULL){
		return &smokeAlarm;
	}else{
		smokeAlarm.next = phead;  //以前的头变成.next
		phead = &smokeAlarm;      //更新头
		return phead;
	}
}

buzzer.c(蜂鸣器)

c 复制代码
#include "controlDevice.h"			//自定义设备类的文件
 
int buzzerInit(int pinNum)
{
	pinMode(pinNum,OUTPUT);						//配置引脚为输出模式
	digitalWrite(pinNum,HIGH);					//引脚置高电平,蜂鸣器关闭
}
 
int buzzerOpen(int pinNum)
{
	digitalWrite(pinNum,LOW);					//引脚置低电平,蜂鸣器开启
}
 
int buzzerClose(int pinNum)
{
	digitalWrite(pinNum,HIGH);					//引脚置高电平,蜂鸣器关闭
}
 
struct Devices buzzer = {						//定义蜂鸣器(对象)
	.deviceName = "buzzer",						//名字
	.pinNum = 9,								//香橙派 9号(wpi)引脚
	.Init = buzzerInit,							//指定初始化函数
	.open = buzzerOpen,							//指定"开启蜂鸣器"函数
	.close = buzzerClose,						//指定"关闭蜂鸣器"函数
};
 
 
struct Devices* addBuzzerToDeviceLink(struct Devices *phead)		//蜂鸣器(对象)加入设备链表函数
{
	if(phead == NULL){
		return &buzzer;
	}else{
		buzzer.next = phead;
		phead = &buzzer;
		return phead;
	}
}

mainPro.c(主函数)

c 复制代码
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "controlDevice.h"
 
struct Devices* findDeviceByName(char *name, struct Devices *phead)
{
    struct Devices *tmp =phead;
    if(phead == NULL){
        return NULL;
    }else{
        while(tmp != NULL){
            if(strcmp(tmp->deviceName,name)==0){
                return tmp;
            }
            tmp = tmp->next;
        }
        return NULL;
    }
}
 
int main()
{
    char *smokeName = "smokeAlarm";
    char *buzzerName = "buzzer";
    struct Devices *tmp = NULL;
    int smokeStatus;												//存放"烟雾传感器"状态
 
    if (wiringPiSetup () == -1) { 
        fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ; 
        return 1 ; 
    }
 
    struct Devices *pdeviceHead = NULL;				                    //定义初始链表头
    //pdeviceHead = addBathroomLightToDeviceLink(pdeviceHead);            //"浴室灯"加入设备链表
    //pdeviceHead = addBedroomLightToDeviceLink(pdeviceHead);
    //pdeviceHead = addRestaurantLightToDeviceLink(pdeviceHead);
    //pdeviceHead = addLivingroomLightToDeviceLink(pdeviceHead);
    pdeviceHead = addSmokeAlarmToDeviceLink(pdeviceHead);
    pdeviceHead = addBuzzerToDeviceLink(pdeviceHead);
 
    while(1){
        tmp = findDeviceByName(smokeName, pdeviceHead);
        if(tmp != NULL){
            tmp->Init(tmp->pinNum);
            smokeStatus = tmp->readStatus(tmp->pinNum);
            tmp = findDeviceByName(buzzerName, pdeviceHead);
            if(tmp != NULL){
                if( smokeStatus == 0 ){
                    tmp->Init(tmp->pinNum);
                    tmp->open(tmp->pinNum);
                }else{
                    tmp->Init(tmp->pinNum);
                    tmp->close(tmp->pinNum);
                }           
            }
        }
    }
 
    return 0;
}

模块测试

相关推荐
BugShare1 天前
怎么一步步实现小米智能家居之玄关篇
网络·智能路由器·智能家居
BugShare2 天前
怎么一步步实现小米智能家居之洗手间篇
智能家居
三佛科技-134163842122 天前
HN15N10DA_TO-252封装100V 15A 增强MOSFET场效应管详细分析(HN15N10DA在小家电的应用)
嵌入式硬件·物联网·智能家居·pcb工艺
远翔调光芯片^138287988722 天前
FP7125停产替代选型指南:FP7135参数详解及应用适配全攻略
科技·物联网·智能家居·能源
AdMergeX2 天前
前沿观察 | 2026智能家居行业发展黄金期:全栈智能变现方案如何为开发者打造增长新引擎?
人工智能·智能家居
BugShare2 天前
怎么一步步实现小米智能家居之客厅篇
智能家居
BugShare3 天前
怎么一步步实现小米智能家居之厨房篇
智能家居
BugShare3 天前
怎么一步步实现小米智能家居之卧室篇
智能家居
yhdata17 天前
68.72亿元!智能家居芯片市场规模锁定,技术迭代催生行业新增长极
大数据·人工智能·智能家居
点灯小铭1 个月前
基于单片机的智能家居智能雨水自动关窗控制系统设计
单片机·嵌入式硬件·毕业设计·智能家居·课程设计·期末大作业