基于树莓派的智能家居项目整理

文章目录


一、功能介绍
二、设计框图
三、实物展示
四、程序

一、功能介绍
基于树莓派的智能家居。智能家居用到的硬件有:树莓派4B、LD3320语音识别模块、pi 摄像头、继电器组、小灯、火焰传感器、蜂鸣器、电磁锁
采用了简单工厂模式的一个设计方式。稳定,拓展性更强,在C语言中,因为没有接口、类这一说法,所以这里采用了结构体来“等效替换”。有四个灯,所以我创建了四个灯控制.c程序。每一个程序文件中,都有一个设备结构体,每个程序文件的函数实现方法不同,当有新设备进入只需要在创建一个.c文件,改变函数实现方法即可。初始化的时候,通过链表将各个模块连接起来(头插法)。在要使用某个模块时,只需要使用链表遍历,找到所需模块去调用功能
具体功能是:
1、可通过ld3320语音模块的口令模式,口令+具体控制,通过串口把控制指令传给树莓派,来控制客厅、餐厅、二楼、浴室的灯
2、也可以通过socket客户端来发指令来控制灯的开关
3、火灾报警,当火焰传感器检测到火焰的时候,蜂鸣器会报警。
4、视频监控采用开源mjpg-Streamer来实现的,程序执行时创建一个视频监控的线程,用system函数调用启动脚本运行,监控画面可在http://172.20.10.8:8080去看到
5、人脸识别开锁,人脸识别功能是使用的翔云平台的人脸识别解决方案,需要安装libcurl 和 openSSl库来支持https协议,通过系统调用wget +http://172.20.10.8:8080/?action=snapshot -O ./huyu1.jpg 指令到树莓派的监控页面"去截取一帧保存到本地,获取图片的base64编码,工程文件夹下也有一张照片,huyu.jpg格式,相当于采集的人脸。也是获取图片的base64编码,通过sprintf函数将访问翔云需要的两张图片的base64编码与Key、secret、typeId、format拼接在一起,通过https协议去访问翔云平台, 识别成功后会将识别结果返回,通过回调函数readData将返回的字符串读到readBuff里,通过strstr去readbuff里找有没有字符’是’,如果识别成功就去控制电磁锁打开。

二、设计框图
基于树莓派的智能家居项目整理_第1张图片
*效果展示:*https://www.bilibili.com/video/BV1uD4y1C7PH?share_source=copy_web&vd_source=5e457ac82f63b0e36b8b9e2708807f31

三、实物展示
基于树莓派的智能家居项目整理_第2张图片
基于树莓派的智能家居项目整理_第3张图片
基于树莓派的智能家居项目整理_第4张图片
四、程序

1、InputCommand.h

#include 
#include 

struct InputCommander
{
	char commandName[128];
	char deviceName[128];
	char command[32];
	int (*Init)(struct InputCommander *voicer,char *ipAdress,char *port);
	int (*getCommand)(struct InputCommander *voicer);
	char log[1024];
	int fd;
	char port[12];
	char ipAddress[32];
	int sfd;
	struct InputCommander *next;
};

struct InputCommander *addVoiceControlToInputCommandLink(struct InputCommander *phead);

struct InputCommander *addSocketControlToInputCommandLink(struct InputCommander *phead);

2、controlDevices .h

#include 
#include 



struct Devices 
{
	char deviceName[128];
	int status;
	int pinNum;

	int (*open)(int pinNum);
	int (*close)(int pinNum);
	int (*deviceInit)(int pinNum);

	int (*readStatus)(int pinNum);
	int (*changeStatus)(int status);
	

	
	void (*faceRecognition)(); //人脸识别
	char *(*takePictureInit)();//用于摄像头
	char *(*getPicBase64)(); //用于摄像头
	size_t (*readData)(); //用于摄像头

	struct Devices *next;

};

struct Devices *addBathroomLightToDeviceLink(struct Devices *phead);
struct Devices *addUpstairLightToDeviceLink(struct Devices *phead);
struct Devices *addDiningroomLightToDeviceLink(struct Devices *phead);
struct Devices *addLivingroomLightToDeviceLink(struct Devices *phead);
struct Devices *addFireAlarmToDeviceLink(struct Devices *phead);
struct Devices* addBeepToDeviceLink(struct Devices *phead);
struct Devices *addLookToDeviceLink(struct Devices *phead);


struct Devices *addCameraToDeviceLink(struct Devices *phead);

3、mainPro.c

#include 
#include 
#include 
#include 
#include 
#include        
#include 
#include 
#include 


#include "controlDevices.h"
#include "InputCommand.h"



struct InputCommander *pCommandHead = NULL;
struct Devices 		  *pdeviceHead = NULL;

struct InputCommander *socketHandler = NULL;

int c_fd;
char *name = NULL;

pthread_t cameraThread;



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;
		}

}

struct InputCommander *findCommandByName(char *name,struct InputCommander *phead)
{
	struct InputCommander *tmp = phead;
	if(phead == NULL)
		{
			return NULL;

		}
	else
		{
			while(tmp != NULL){
				if(strcmp(tmp->commandName,name) == 0)
					{
						return tmp;
					}
				tmp = tmp->next;
				}
			return NULL;
		}

}

void *camera_thread(void *datas)	//摄像头线程
{
	
    struct Devices *cameraTemp;

    cameraTemp = findDeviceByName("camera", pdeviceHead); //设备都要从工厂里面取出来

    if (cameraTemp == NULL)
    { 	//防止段错误的必需判断,当给指针赋值是,一定要考虑NULL的情况
        printf("find camera error\n");
        pthread_exit(NULL); //在线程中不用return
    	}

    cameraTemp->faceRecognition(); //调用人脸识别函数
	

}
void * voice_thread(void *datas)	//语音线程
{
	int nread;
	struct InputCommander *voiceHandler = NULL;

	struct Devices *linkHandler = NULL;
	
	voiceHandler = findCommandByName("voice",pCommandHead);
	
	if(voiceHandler == NULL){
		printf("find voicehandler error\n");
		pthread_exit(NULL);

	}else{
		if(voiceHandler->Init(voiceHandler,NULL,NULL)<0){
			printf("voice init error\n");
			pthread_exit(NULL);
		}else{
				printf("%s init success!\n",voiceHandler->commandName);
		}
		while(1){
			memset(voiceHandler->command,'\0',sizeof(voiceHandler->command));
			nread = voiceHandler->getCommand(voiceHandler);	//读取来自语音模块的消息
			printf(" %s\n",voiceHandler->command);
			if(nread > 1){
				
					 if(strstr(voiceHandler->command,"KEL") != NULL){	//开二楼灯
						linkHandler = findDeviceByName("upstairLight",pdeviceHead);
						linkHandler->deviceInit(linkHandler->pinNum);
						linkHandler->open(linkHandler->pinNum);
						printf("open upstairLight\n");
					}else if(strstr(voiceHandler->command,"GEL") != NULL){		//关二楼灯
						linkHandler = findDeviceByName("upstairLight",pdeviceHead);
						linkHandler->close(linkHandler->pinNum);
						printf("close upstairLight\n");
					}else if(strstr(voiceHandler->command,"KYS") != NULL){	//开浴室灯
						linkHandler = findDeviceByName("bathroomLight",pdeviceHead);
						linkHandler->deviceInit(linkHandler->pinNum);
						linkHandler->open(linkHandler->pinNum);
						printf("open bathroomlight\n");
					}else if(strstr(voiceHandler->command,"GYS") != NULL){	//关浴室灯
						linkHandler = findDeviceByName("bathroomLight",pdeviceHead);
						linkHandler->close(linkHandler->pinNum);
						printf("open bathroomlight\n");
					}else if(strstr(voiceHandler->command,"KKT") != NULL){	//开客厅灯
						linkHandler = findDeviceByName("livingroomLight",pdeviceHead);
						linkHandler->deviceInit(linkHandler->pinNum);
						linkHandler->open(linkHandler->pinNum);
						printf("open livingroomLight\n");
 					}else if(strstr(voiceHandler->command,"GKT") != NULL){	//关客厅灯
						linkHandler = findDeviceByName("livingroomLight",pdeviceHead);
						linkHandler->close(linkHandler->pinNum);
						printf("open livingroomLight\n");
					}else if(strstr(voiceHandler->command,"KCT") != NULL){//开餐厅灯
						linkHandler = findDeviceByName("diningroomLight",pdeviceHead);
						linkHandler->deviceInit(linkHandler->pinNum);
						linkHandler->open(linkHandler->pinNum);
						printf("open diningroomLight\n");
					}else if(strstr(voiceHandler->command,"GCT") != NULL){//关餐厅灯
						linkHandler = findDeviceByName("diningroomLight",pdeviceHead);
						linkHandler->close(linkHandler->pinNum);
						printf("open diningroomLight\n");
					}else if(strstr(voiceHandler->command,"KM") != NULL){//开门
						pthread_create(&cameraThread,NULL,camera_thread,NULL);
				
					}else if(strstr(voiceHandler->command,"DQK") != NULL){//打开全部灯
						linkHandler = findDeviceByName("upstairLight",pdeviceHead);
						linkHandler->deviceInit(linkHandler->pinNum);
						linkHandler->open(linkHandler->pinNum);

						linkHandler = findDeviceByName("bathroomLight",pdeviceHead);
						linkHandler->deviceInit(linkHandler->pinNum);
						linkHandler->open(linkHandler->pinNum);

						linkHandler = findDeviceByName("livingroomLight",pdeviceHead);
						linkHandler->deviceInit(linkHandler->pinNum);
						linkHandler->open(linkHandler->pinNum);

						linkHandler = findDeviceByName("diningroomLight",pdeviceHead);
						linkHandler->deviceInit(linkHandler->pinNum);
						linkHandler->open(linkHandler->pinNum);
						printf("All closed\n");

					}else if(strstr(voiceHandler->command,"DQG") != NULL){//关闭全部灯
						linkHandler = findDeviceByName("upstairLight",pdeviceHead);
						linkHandler->close(linkHandler->pinNum);

						linkHandler = findDeviceByName("bathroomLight",pdeviceHead);
						linkHandler->close(linkHandler->pinNum);

						linkHandler = findDeviceByName("livingroomLight",pdeviceHead);
						linkHandler->close(linkHandler->pinNum);

						linkHandler = findDeviceByName("diningroomLight",pdeviceHead);
						linkHandler->close(linkHandler->pinNum);
						printf("All open\n");

					}else if(nread == 0){
						printf("nodata from voice......\n");
					}
					
					//printf("%d\n",strlen(voiceHandler->command));
					

			
				}
		}
	}
}	



void * read_thread(void *datas)
{


	int n_read;
	struct Devices *linkHandler1 = NULL;

	memset(socketHandler->command,'\0',sizeof(socketHandler->command));
	n_read = read(c_fd,socketHandler->command,sizeof(socketHandler->command));
   	if(n_read>0){
    	printf("get message:%d,%s\n",n_read,socketHandler->command);
    
			if(strstr(socketHandler->command,"KEL") != NULL){	//开二楼灯
				linkHandler1 = findDeviceByName("upstairLight",pdeviceHead);
				linkHandler1->deviceInit(linkHandler1->pinNum);
				linkHandler1->open(linkHandler1->pinNum);
				printf("open upstairLight\n");
			}else if(strstr(socketHandler->command,"GEL") != NULL){		//关二楼灯
				linkHandler1 = findDeviceByName("upstairLight",pdeviceHead);
				linkHandler1->close(linkHandler1->pinNum);
				printf("close upstairLight\n");
			}else if(strstr(socketHandler->command,"KYS") != NULL){	//开浴室灯
				linkHandler1 = findDeviceByName("bathroomLight",pdeviceHead);
				linkHandler1->deviceInit(linkHandler1->pinNum);
				linkHandler1->open(linkHandler1->pinNum);
				printf("open bathroomlight\n");
			}else if(strstr(socketHandler->command,"GYS") != NULL){	//关浴室灯
				linkHandler1 = findDeviceByName("bathroomLight",pdeviceHead);
				linkHandler1->close(linkHandler1->pinNum);
				printf("close bathroomlight\n");
			}else if(strstr(socketHandler->command,"KKT") != NULL){	//开客厅灯
				linkHandler1 = findDeviceByName("livingroomLight",pdeviceHead);
				linkHandler1->deviceInit(linkHandler1->pinNum);
				linkHandler1->open(linkHandler1->pinNum);
				printf("open livingroomLight\n");
 			}else if(strstr(socketHandler->command,"GKT") != NULL){	//关客厅灯
				linkHandler1 = findDeviceByName("livingroomLight",pdeviceHead);
				linkHandler1->close(linkHandler1->pinNum);
				printf("close livingroomLight\n");
			}else if(strstr(socketHandler->command,"KCT") != NULL){//开餐厅灯
				linkHandler1 = findDeviceByName("diningroomLight",pdeviceHead);
				linkHandler1->deviceInit(linkHandler1->pinNum);
				linkHandler1->open(linkHandler1->pinNum);
				printf("open diningroomLight\n");
			}else if(strstr(socketHandler->command,"GCT") != NULL){//关餐厅灯
				linkHandler1 = findDeviceByName("diningroomLight",pdeviceHead);
				linkHandler1->close(linkHandler1->pinNum);
				printf("close diningroomLight\n");
			}else if(strstr(socketHandler->command,"KM") != NULL){//开门
				//linkHandler1 = findDeviceByName("camera",pdeviceHead);
				//linkHandler1->takePictureInit();
				//linkHandler1->faceRecognition();
				pthread_create(&cameraThread,NULL,camera_thread,NULL);

				
			}else if(strstr(socketHandler->command,"GM") != NULL){//关门
				linkHandler1 = findDeviceByName("lock",pdeviceHead);
				linkHandler1->close(linkHandler1->pinNum);
			}else if(strstr(socketHandler->command,"DQK") != NULL){//打开全部灯
				linkHandler1 = findDeviceByName("upstairLight",pdeviceHead);
				linkHandler1->deviceInit(linkHandler1->pinNum);
				linkHandler1->open(linkHandler1->pinNum);

				linkHandler1 = findDeviceByName("bathroomLight",pdeviceHead);
				linkHandler1->deviceInit(linkHandler1->pinNum);
				linkHandler1->open(linkHandler1->pinNum);

				linkHandler1 = findDeviceByName("livingroomLight",pdeviceHead);
				linkHandler1->deviceInit(linkHandler1->pinNum);
				linkHandler1->open(linkHandler1->pinNum);

				linkHandler1 = findDeviceByName("diningroomLight",pdeviceHead);
				linkHandler1->deviceInit(linkHandler1->pinNum);
				linkHandler1->open(linkHandler1->pinNum);
				printf("All open\n");
			}else if(strstr(socketHandler->command,"DQG") != NULL){		//关闭全部灯
				linkHandler1 = findDeviceByName("upstairLight",pdeviceHead);
				linkHandler1->close(linkHandler1->pinNum);

				linkHandler1 = findDeviceByName("bathroomLight",pdeviceHead);
				linkHandler1->close(linkHandler1->pinNum);

				linkHandler1 = findDeviceByName("livingroomLight",pdeviceHead);
				linkHandler1->close(linkHandler1->pinNum);

				linkHandler1 = findDeviceByName("diningroomLight",pdeviceHead);
				linkHandler1->close(linkHandler1->pinNum);
				printf("All closed\n");
			}else if(n_read == -1){
    	        perror("read");
    		}	
	}
		
}


void * socket_thread(void *datas)	//socket线程
{
	int n_read = 0;
	pthread_t readThread;
	
	struct sockaddr_in c_addr;
	memset(&c_addr,0,sizeof(struct sockaddr_in));
	int clen = sizeof(struct sockaddr_in);
	
	
	
	socketHandler = findCommandByName("socketServer",pCommandHead);
	if(socketHandler == NULL){
		printf("find socketHandler error\n");
		pthread_exit(NULL);
	}else{
			printf("%s init success!\n",socketHandler->commandName);
		}
	socketHandler->Init(socketHandler,NULL,NULL);
	while(1){
		c_fd = accept(socketHandler->sfd,(struct sockaddr *)&c_addr,&clen);
		pthread_create(&readThread,NULL,read_thread,NULL);
	}
}

void * video_thread(void *datas)	//视频监控线程
{
    system("/home/pi/mjpg-streamer/mjpg-streamer-experimental/start.sh");
	pthread_exit(NULL);	
}
	


void * fireAlarm_thread(void *datas)	//火灾报警线程
{
	int status;
	struct Devices *firetmp = NULL;
	struct Devices *beeptmp = NULL;

	firetmp = findDeviceByName("fireAlarm",pdeviceHead);		//寻找“火焰传感器”链表节点,返回给firetmp
	beeptmp = findDeviceByName("beep",pdeviceHead);				//寻找“蜂鸣器”链表节点,返回给buztmp

	while(1){
		status = firetmp->readStatus(firetmp->pinNum);			//读取“火焰传感器”状态
	
		if(status == 0){	
			beeptmp->deviceInit(beeptmp->pinNum);								//检测到火焰或强光源
			beeptmp->open(beeptmp->pinNum);		//打开蜂鸣器
			printf("Warning of fire!!!\n");
			delay(1000);	//延时1000毫秒=1秒
		}

		if(status == 1){						//未检测到火焰、强光源或解除警报
			beeptmp->close(beeptmp->pinNum);		//关闭蜂鸣器
		}
	}


}

int main()

{
	//char name[128] = {'\0'};
	pthread_t voiceThread;
	pthread_t socketThread;
	pthread_t fireAlarmThread;
	pthread_t videoThread;
	pthread_t cameraThread;
	
	
	struct Devices *tmp = NULL;

	if(wiringPiSetup() == -1)
		{
			return -1;
		}
	

	//1. 指令工厂初始化
	pCommandHead = addVoiceControlToInputCommandLink(pCommandHead);
	pCommandHead = addSocketControlToInputCommandLink(pCommandHead);

	//2. 设备控制工厂初始化
	pdeviceHead = addBathroomLightToDeviceLink(pdeviceHead);
	pdeviceHead = addUpstairLightToDeviceLink(pdeviceHead);
	pdeviceHead = addLivingroomLightToDeviceLink(pdeviceHead);
	pdeviceHead = addDiningroomLightToDeviceLink(pdeviceHead);
	pdeviceHead = addFireAlarmToDeviceLink(pdeviceHead);
	pdeviceHead = addBeepToDeviceLink(pdeviceHead);
	pdeviceHead = addLookToDeviceLink(pdeviceHead);
	pdeviceHead = addCameraToDeviceLink(pdeviceHead);

	struct Devices *tmpequiphead = pdeviceHead;
	if(tmpequiphead != NULL){						//设备工厂所有设备初始化
		tmpequiphead->deviceInit(tmpequiphead->pinNum);
		
		tmpequiphead = tmpequiphead->next;
	}


	
	//3.池建立
	//3.1 语音线程
	//int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void *), void *restrict arg);
	pthread_create(&voiceThread,NULL,voice_thread,NULL);

	//3.2 socket线程
	
	pthread_create(&socketThread,NULL,socket_thread,NULL);

	
	//3.3 摄像头线程
	//printf("camera_thread1===============\n");
	pthread_create(&cameraThread,NULL,camera_thread,NULL);
	
	
	//3.4 火灾线程
	pthread_create(&fireAlarmThread,NULL,fireAlarm_thread,NULL);
	
	//3.视频监控 
	pthread_create(&videoThread, NULL, video_thread, NULL);
	
	pthread_join(voiceThread,NULL);
	pthread_join(socketThread,NULL);
	pthread_join(cameraThread,NULL);
	pthread_join(fireAlarmThread,NULL);
	pthread_join(videoThread,NULL);

	return 0;
}

4、upstairLight.c

#include "controlDevices.h"
#include 

/*struct Devices 
{
	char deviceName[128];
	int status;

	int (*open)();
	int (*close)();
	int (*deviceInit)();

	int (*readStatus)()
	int (*changeStatus)(int status);

	struct Devices *next;

};
*/

int upstairLightOpen(int pinNum)
{
	digitalWrite(pinNum,HIGH);
	
}

int upstairLightClose(int pinNum)
{
	digitalWrite(pinNum,LOW);
	
}

int upstairLightInit(int pinNum)
{
	pinMode(pinNum,OUTPUT);
	digitalWrite(pinNum,LOW);
	
}

int  upstairLightStatus(int status)
{


}



struct Devices upstairLight = {

	.deviceName = "upstairLight",
	.pinNum = 21,
	.open = upstairLightOpen,
	.close = upstairLightClose,
	.deviceInit = upstairLightInit,
	.changeStatus = upstairLightStatus,
	.next = NULL
};

struct Devices *addUpstairLightToDeviceLink(struct Devices *phead)
{
	if(phead == NULL){
		return &upstairLight;	
	}	
	else
	{
		upstairLight.next = phead;
		phead = &upstairLight;
		
		return phead;
	}
}

5、bathroomLight.c

#include "controlDevices.h"
#include 

/*struct Devices 
{
	char deviceName[128];
	int status;

	int (*open)();
	int (*close)();
	int (*deviceInit)();

	int (*readStatus)()
	int (*changeStatus)(int status);

	struct Devices *next;

};
*/

int bathroomLightOpen(int pinNum)
{
	 digitalWrite(pinNum,HIGH);
	 
	
}
int bathroomLightClose(int pinNum)
{
	digitalWrite(pinNum,LOW);
	
}
int bathroomLightInit(int pinNum)
{
	pinMode(pinNum,OUTPUT);
	digitalWrite(pinNum,LOW);
	
	

}

int  bathroomLightStatus(int status)
{


}



struct Devices bathroomLight = {

	.deviceName = "bathroomLight",
	.pinNum = 22,
	.open = bathroomLightOpen,
	.close = bathroomLightClose,
	.deviceInit = bathroomLightInit,
	.changeStatus = bathroomLightStatus,
	.next = NULL
};

struct Devices *addBathroomLightToDeviceLink(struct Devices *phead)
{
	if(phead == NULL){
		return &bathroomLight;	
	}	
	else
	{
		bathroomLight.next = phead;
		phead = &bathroomLight;
		return phead;

	}
}

6、livingroomLight.c

#include "controlDevices.h"
#include 

/*struct Devices 
{
	char deviceName[128];
	int status;

	int (*open)();
	int (*close)();
	int (*deviceInit)();

	int (*readStatus)()
	int (*changeStatus)(int status);

	struct Devices *next;

};
*/

int livingroomLightOpen(int pinNum)
{
	digitalWrite(pinNum,HIGH);
	
}

int livingroomLightClose(int pinNum)
{

	digitalWrite(pinNum,LOW);
	
}
int livingroomLightInit(int pinNum)
{
	pinMode(pinNum,OUTPUT);
	digitalWrite(pinNum,LOW);
	

}

int  livingroomLightStatus(int status)
{


}



struct Devices livingroomLight = {

	.deviceName = "livingroomLight",
	.pinNum = 23,
	.open = livingroomLightOpen,
	.close = livingroomLightClose,
	.deviceInit = livingroomLightInit,
	.changeStatus = livingroomLightStatus,
	.next = NULL
};

struct Devices *addLivingroomLightToDeviceLink(struct Devices *phead)
{
	if(phead == NULL){
		return &livingroomLight;	
	}	
	else
	{
		livingroomLight.next = phead;
		phead = &livingroomLight;
		
		return phead;
	}
}


7、diningroomLight.c

#include "controlDevices.h"
#include 

/*struct Devices 
{
	char deviceName[128];
	int status;

	int (*open)();
	int (*close)();
	int (*deviceInit)();

	int (*readStatus)()
	int (*changeStatus)(int status);

	struct Devices *next;

};
*/

int diningroomLightOpen(int pinNum)
{

	digitalWrite(pinNum,HIGH);
	
}

int diningroomLightClose(int pinNum)
{

	digitalWrite(pinNum,LOW);
}

int diningroomLightInit(int pinNum)
{
	pinMode(pinNum,OUTPUT);
	digitalWrite(pinNum,LOW);

}

int  diningroomLightStatus(int status)
{


}



struct Devices diningroomLight = {

	.deviceName = "diningroomLight",
	.pinNum = 24,
	.open = diningroomLightOpen,
	.close = diningroomLightClose,
	.deviceInit = diningroomLightInit,
	.changeStatus = diningroomLightStatus,
	.next = NULL
};

struct Devices *addDiningroomLightToDeviceLink(struct Devices *phead)
{
	if(phead == NULL){
		return &diningroomLight;	
	}	
	else
	{
		diningroomLight.next = phead;
		phead = &diningroomLight;
		
		return phead;
	}
}


8、fire.c

#include "controlDevices.h"
#include 


int fireAlarmInit(int pinNum)
{
	pinMode(pinNum,INPUT);
	digitalWrite(pinNum,HIGH);

}

int  fireStatusRead(int pinNum)
{
	return digitalRead(pinNum);
}

struct Devices fireAlarm = {

	.deviceName = "fireAlarm",
	.pinNum = 25,
	.deviceInit = fireAlarmInit,
	.readStatus = fireStatusRead,
	.next = NULL
};

struct Devices *addFireAlarmToDeviceLink(struct Devices *phead)
{
	if(phead == NULL){
		return & fireAlarm;	
	}	
	else
	{
		 fireAlarm.next = phead;
		phead = & fireAlarm;

	}
}


9、beep.c

#include "controlDevices.h"

struct Devices *addBeepToDeviceLink(struct Devices *phead);





int beepInit(int pinNum)					//初始化函数
{
	pinMode(pinNum,OUTPUT);					//配置引脚为输出引脚
	digitalWrite(pinNum,LOW);				//引脚输出低电平,即默认为关闭状态
}

int beepOpen(int pinNum)					//打开蜂鸣器函数
{
	digitalWrite(pinNum,HIGH);
}

int beepClose(int pinNum)					//关闭蜂鸣器函数
{
	digitalWrite(pinNum,LOW);
}

struct Devices beep = {			//蜂鸣器设备链表节点
	.deviceName = "beep",
	.pinNum = 27,					//树莓派gpio引脚27
	.deviceInit = beepInit,
	.open = beepOpen,
	.close = beepClose,
	.next = NULL
};


struct Devices* addBeepToDeviceLink(struct Devices *phead)			//头插法将设备节点加入设备工厂链表函数
{
	if(phead == NULL){
		return &beep;
	}else{
		beep.next = phead;
		phead = &beep;
		return phead;
	}
}

10、voiceControl.c

#include "InputCommand.h"
#include 
#include 
#include 
#include 
#include  
#include 

int voiceGetCommand(struct InputCommander *voicer)
{
	int nread = 0;
	//memset(voicer->command,'\0',sizeof(voicer->command));

	nread = read(voicer->fd,voicer->command,sizeof(voicer->command));
	if(nread == 0){
		printf("usart for voice read over time!\n");
	}else{
		return nread;	//读取到的指令是存放到voicer.command的
	}
}

int voiceInit(struct InputCommander *voicer,char *ipAdress,char *port)
{
	int fd;
	//printf("test init1\n");
	
	if((fd = serialOpen(voicer->deviceName,9600))== -1)	//初始化串口,波特率9600
		{
			printf("open serial error\n");
			exit(-1);
		}
	voicer->fd = fd;
	return fd;

}
struct InputCommander voiceControl = {
	.commandName = "voice",
	.deviceName = "/dev/ttyAMA0",
	.command = {'\0'},
	.Init = voiceInit,
	.getCommand = voiceGetCommand,
	.log = {'\0'},
	.next = NULL
};

struct InputCommander *addVoiceControlToInputCommandLink(struct InputCommander *phead)
{
	if(phead == NULL){
		return &voiceControl;	
	}	
	else
	{
		voiceControl.next = phead;
		phead = &voiceControl;

	}
}


11、socketControl.c

你可能感兴趣的:(智能家居,智能家居,语音识别,人工智能,网络协议,https)