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

[可燃气体传感器 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;
}

模块测试

相关推荐
云山工作室2 天前
智能家居环境监测系统设计(论文+源码)
单片机·嵌入式硬件·毕业设计·智能家居·毕设
Stanford_11062 天前
物联网智能项目之——智能家居项目的实现!
物联网·学习·微信小程序·智能家居·微信公众平台·twitter·微信开放平台
深圳启明云端科技3 天前
ESP32无线通信智能屏方案,设备触摸人机交互,高性能控制应用
网络·物联网·人机交互·智能家居·乐鑫
艾格北峰13 天前
STM32 物联网智能家居 (五) 设备子系统之点亮LED灯
arm开发·stm32·单片机·嵌入式硬件·物联网·架构·智能家居
智哪儿14 天前
智能家居企业如何通过设计师渠道打造第二曲线?
智能家居
深圳启明云端科技16 天前
ESP RainMaker轻量级云平台方案,产品私有云部署,物联网无线应用
网络·物联网·wifi·智能家居
不能只会打代码17 天前
32单片机综合应用案例——智能家居灯光控制系统(二)(内附详细代码讲解!!!)
单片机·嵌入式硬件·智能家居·语音识别·32单片机
艾格北峰18 天前
STM32 物联网智能家居 (二)-开发环境及工程搭建(STM32CubeMX)
arm开发·stm32·单片机·嵌入式硬件·物联网·架构·智能家居
freewzx200520 天前
VMware虚拟机安装Home Assistant智能家居平台并实现远程访问保姆级教程
智能家居
沐欣工作室_lvyiyi20 天前
基于STM32的智能家居蓝牙系统(论文+源码)
stm32·单片机·嵌入式硬件·物联网·毕业设计·智能家居