智能家居 (5) ——前四章内容整合

   日期:2021-03-25     浏览:106    评论:0    
核心提示:1、main.c 文件(主函数):2、往期文章讲解:【智能家居 (1) ——工厂模式继电器控制灯】【智能家居 (2) ——工厂模式火焰报警器】【智能家居 (3) ——语音识别控制(多线程)】【智能家居 (4) ——网络控制(多线程)】...

目录:

    • 1、main.c 文件(主函数):
    • 2、分文件:
        • (1) equipment.h 文件(设备类头文件):
        • (2) bathroomLight.c 文件(浴室灯):
        • (3) secondfloorLight.c 文件(二楼灯):
        • (4) livingroomLight.c 文件(客厅灯):
        • (5) restaurantLight.c 文件(餐厅灯):
        • (6) fireDetection.c 文件(火焰传感器):
        • (7) buzzer.c 文件(蜂鸣器):
        • (8) command.h 文件(指令控制类头文件):
        • (9) voiceControl.c 文件(语音控制):
        • (10) socketControl.c(网络端口控制):
    • 3、往期文章讲解:

1、main.c 文件(主函数):

#include <stdio.h>
#include <string.h>
#include "equipment.h"
#include "command.h"
#include <pthread.h>
#include <unistd.h>

struct Equipment *findEquipByName(char *name,struct Equipment *phead);		//一些函数声明
struct Command *findCommandByName(char *name,struct Command *phead);
void *voiceControlThread(void *data);
void *socketControlThread(void *data);
void *socketReadThread(void *data);
void *fireAlarmThread(void *data);

struct Equipment *equiphead = NULL;			//设备工厂链表头节点
struct Command *cmdhead = NULL;				//指令控制工厂链表节点头
struct Command *socketHandler = NULL;		//“网络控制线程”执行的函数使用到的全局变量


int main()
{ 
	if(wiringPiSetup() == -1){ 					//使用wiringPi库需要初始化
		printf("wiringPiSetup failed!\n");
		return -1; 
	}

	equiphead = addBathroomLightToLink(equiphead);			//各设备加入设备工厂
	equiphead = addSecondfloorLightToLink(equiphead);	
	equiphead = addLivingroomLightToLink(equiphead);
	equiphead = addRestaurantLightToLink(equiphead);
	equiphead = addFireDetectionToLink(equiphead);
	equiphead = addBuzzerToLink(equiphead);

	cmdhead = addVoiceControlToLink(cmdhead);				//各指令控制加入指令控制工厂
	cmdhead = addSocketControlToLink(cmdhead);

	struct Equipment *tmpequiphead = equiphead;
	while(tmpequiphead != NULL){ 						//设备工厂所有设备初始化
		tmpequiphead->Init(tmpequiphead->pinNum);
		tmpequiphead = tmpequiphead->next;
	}

	pthread_t voiceControl_thread;
	pthread_t socketControl_thread;
	pthread_t fireAlarm_thread;
	pthread_create(&voiceControl_thread,NULL,voiceControlThread,NULL);		//创建线程:语音控制
	pthread_create(&socketControl_thread,NULL,socketControlThread,NULL);	//创建线程:网络控制
	pthread_create(&fireAlarm_thread,NULL,fireAlarmThread,NULL);			//创建线程:火灾报警系统


	pthread_join(voiceControl_thread, NULL);		//主函数等待线程退出
	pthread_join(socketControl_thread, NULL);		//主函数等待线程退出
	pthread_join(fireAlarm_thread, NULL);			//主函数等待线程退出
	return 0;
}


void *voiceControlThread(void *data)			//“语音控制线程”执行的函数
{ 
	int nread;
	char *temName = NULL;
	struct Command *voiceHandler = NULL;
	struct Equipment *linkHandler;


	voiceHandler = findCommandByName("voiceControl",cmdhead);		//寻找“语音控制”所在节点,返回给voiceHandler
	if(voiceHandler == NULL){ 
		printf("find voiceHandler error\n");
		pthread_exit(NULL);
	}
	if(voiceHandler->Init(voiceHandler) < 0){ 				//“语音控制”功能初始化
		printf("voiceControl init error\n");
		pthread_exit(NULL);
	}


	while(1){ 
		nread = voiceHandler->getCommand(voiceHandler);			//获取指令
		if(nread == 0){ 											//没有获取到指令
			printf("No command received\n");
		}else{ 													//获取到指令
			printf("Get voice command:%s\n",voiceHandler->command);

												//以下为根据不用指令执行相应操作

			if(strcmp("OpBaLi",voiceHandler->command) == 0){ 
				linkHandler = findEquipByName("bathroomLight",equiphead);
				linkHandler->open(linkHandler->pinNum);
			}

			if(strcmp("ClBaLi",voiceHandler->command) == 0){ 
				linkHandler = findEquipByName("bathroomLight",equiphead);
				linkHandler->close(linkHandler->pinNum);
			}

			if(strcmp("OpSeLi",voiceHandler->command) == 0){ 
				linkHandler = findEquipByName("secondfloorLight",equiphead);
				linkHandler->open(linkHandler->pinNum);
			}

			if(strcmp("ClSeLi",voiceHandler->command) == 0){ 
				linkHandler = findEquipByName("secondfloorLight",equiphead);
				linkHandler->close(linkHandler->pinNum);
			}

			if(strcmp("OpLiLi",voiceHandler->command) == 0){ 
				linkHandler = findEquipByName("livingroomLight",equiphead);
				linkHandler->open(linkHandler->pinNum);
			}

			if(strcmp("ClLiLi",voiceHandler->command) == 0){ 
				linkHandler = findEquipByName("livingroomLight",equiphead);
				linkHandler->close(linkHandler->pinNum);
			}

			if(strcmp("OpReLi",voiceHandler->command) == 0){ 
				linkHandler = findEquipByName("restaurantLight",equiphead);
				linkHandler->open(linkHandler->pinNum);
			}

			if(strcmp("ClReLi",voiceHandler->command) == 0){ 
				linkHandler = findEquipByName("restaurantLight",equiphead);
				linkHandler->close(linkHandler->pinNum);
			}

			if(strcmp("OpAlLi",voiceHandler->command) == 0){ 
				linkHandler = findEquipByName("bathroomLight",equiphead);
				linkHandler->open(linkHandler->pinNum);
			
				linkHandler = findEquipByName("secondfloorLight",equiphead);
				linkHandler->open(linkHandler->pinNum);
				
				linkHandler = findEquipByName("livingroomLight",equiphead);
				linkHandler->open(linkHandler->pinNum);
				
				linkHandler = findEquipByName("restaurantLight",equiphead);
				linkHandler->open(linkHandler->pinNum);
			}

			if(strcmp("ClAlLi",voiceHandler->command) == 0){ 
				linkHandler = findEquipByName("bathroomLight",equiphead);
				linkHandler->close(linkHandler->pinNum);
			
				linkHandler = findEquipByName("secondfloorLight",equiphead);
				linkHandler->close(linkHandler->pinNum);
				
				linkHandler = findEquipByName("livingroomLight",equiphead);
				linkHandler->close(linkHandler->pinNum);
				
				linkHandler = findEquipByName("restaurantLight",equiphead);
				linkHandler->close(linkHandler->pinNum);
			}
			
		}
	}
}


void *socketControlThread(void *data)				//“网络控制线程”执行的函数
{ 
	int c_fd;
	struct sockaddr_in c_addr;
	memset(&c_addr,0,sizeof(struct sockaddr_in));
	socklen_t clen = sizeof(struct sockaddr_in);
	pthread_t socketRead_thread;


	socketHandler = findCommandByName("socketControl",cmdhead);		//寻找“网络控制”所在节点,返回给socketHandler
	if(socketHandler == NULL){ 
		printf("find socketHandler error\n");
		pthread_exit(NULL);
	}
	if(socketHandler->Init(socketHandler) < 0){ 				//“网络控制”功能初始化
		printf("socketControl init error\n");
		pthread_exit(NULL);
	}


	while(1){ 
		c_fd = accept(socketHandler->s_fd,(struct sockaddr*)&c_addr,&clen);		//接收连接请求,阻塞至有客户端完成三次握手
		socketHandler->fd = c_fd;					//将套接字描述符返回给“网络控制”链表节点

		pthread_create(&socketRead_thread,NULL,socketReadThread,NULL);			//创建新线程:用于读取TCP端口指令
	}
}


void *socketReadThread(void *data)				//“读取tcp端口指令线程”执行的函数
{ 
	int nread;
	struct Equipment *linkHandler;
	memset(socketHandler->command,'\0',sizeof(socketHandler->command));		//将指令存放的空间置空
	nread = read(socketHandler->fd,socketHandler->command,sizeof(socketHandler->command));		//读取指令
	if(nread == 0){ 
		printf("No command received\n");			//没有读取到指令
	}else{ 
		printf("Get net command:%s\n",socketHandler->command);		//读取到指令

											//以下为根据不用指令执行相应操作

		if(strcmp("OpBaLi",socketHandler->command) == 0){ 
			linkHandler = findEquipByName("bathroomLight",equiphead);
			linkHandler->open(linkHandler->pinNum);
		}

		if(strcmp("ClBaLi",socketHandler->command) == 0){ 
			linkHandler = findEquipByName("bathroomLight",equiphead);
			linkHandler->close(linkHandler->pinNum);
		}

		if(strcmp("OpSeLi",socketHandler->command) == 0){ 
			linkHandler = findEquipByName("secondfloorLight",equiphead);
			linkHandler->open(linkHandler->pinNum);
		}

		if(strcmp("ClSeLi",socketHandler->command) == 0){ 
			linkHandler = findEquipByName("secondfloorLight",equiphead);
			linkHandler->close(linkHandler->pinNum);
		}

		if(strcmp("OpLiLi",socketHandler->command) == 0){ 
			linkHandler = findEquipByName("livingroomLight",equiphead);
			linkHandler->open(linkHandler->pinNum);
		}

		if(strcmp("ClLiLi",socketHandler->command) == 0){ 
			linkHandler = findEquipByName("livingroomLight",equiphead);
			linkHandler->close(linkHandler->pinNum);
		}

		if(strcmp("OpReLi",socketHandler->command) == 0){ 
			linkHandler = findEquipByName("restaurantLight",equiphead);
			linkHandler->open(linkHandler->pinNum);
		}

		if(strcmp("ClReLi",socketHandler->command) == 0){ 
			linkHandler = findEquipByName("restaurantLight",equiphead);
			linkHandler->close(linkHandler->pinNum);
		}

		if(strcmp("OpAlLi",socketHandler->command) == 0){ 
			linkHandler = findEquipByName("bathroomLight",equiphead);
			linkHandler->open(linkHandler->pinNum);
			
			linkHandler = findEquipByName("secondfloorLight",equiphead);
			linkHandler->open(linkHandler->pinNum);
				
			linkHandler = findEquipByName("livingroomLight",equiphead);
			linkHandler->open(linkHandler->pinNum);
				
			linkHandler = findEquipByName("restaurantLight",equiphead);
			linkHandler->open(linkHandler->pinNum);
		}

		if(strcmp("ClAlLi",socketHandler->command) == 0){ 
			linkHandler = findEquipByName("bathroomLight",equiphead);
			linkHandler->close(linkHandler->pinNum);
		
			linkHandler = findEquipByName("secondfloorLight",equiphead);
			linkHandler->close(linkHandler->pinNum);
				
			linkHandler = findEquipByName("livingroomLight",equiphead);
			linkHandler->close(linkHandler->pinNum);
				
			linkHandler = findEquipByName("restaurantLight",equiphead);
			linkHandler->close(linkHandler->pinNum);
		}
		
	}
}


void *fireAlarmThread(void *data)				//“火灾报警器线程”执行的函数
{ 
	int status;
	struct Equipment *firetmp = NULL;
	struct Equipment *buztmp = NULL;

	firetmp = findEquipByName("fireDetection",equiphead);		//寻找“火焰传感器”链表节点,返回给firetmp
	buztmp = findEquipByName("buzzer",equiphead);				//寻找“蜂鸣器”链表节点,返回给buztmp

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

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


struct Equipment *findEquipByName(char *name,struct Equipment *phead)		//根据名字寻找设备工厂链表链节函数,并返回链节
{ 
	struct Equipment *tmp = phead;

	if(phead == NULL){ 
		return NULL;
	}

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


struct Command *findCommandByName(char *name,struct Command *phead)			//根据名字寻找指令控制工厂链表链节函数,并返回链节
{ 
	struct Command *tmp = phead;

	if(phead == NULL){ 
		return NULL;
	}

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

2、分文件:

(1) equipment.h 文件(设备类头文件):

#include <wiringPi.h> //wiringPi库
#include <stdio.h>
#include <stdlib.h>

struct Equipment								//设备工厂链表节点定义
{ 
	char equipName[128];						//设备名
	int pinNum;									//引脚号
	int status;									//“初始化设备”函数指针
	int (*Init)(int pinNum);					//“打开设备”函数指针
	int (*open)(int pinNum);					//“关闭设备”函数指针
	int (*close)(int pinNum);

	int (*readStatus)(int pinNum);				//“读取设备状态”函数指针
	int (*changeStatus)(int status);			//“改变设备状态函数指针”

	struct Equipment *next;
};

struct Equipment *addBathroomLightToLink(struct Equipment *phead);			//“浴室灯”设备节点加入设备工厂链表函数声明
struct Equipment *addSecondfloorLightToLink(struct Equipment *phead);		//“二楼灯”设备节点加入设备工厂链表函数声明
struct Equipment *addLivingroomLightToLink(struct Equipment *phead);		//“客厅灯”设备节点加入设备工厂链表函数声明
struct Equipment *addRestaurantLightToLink(struct Equipment *phead);		//“餐厅灯”设备节点加入设备工厂链表函数声明
struct Equipment *addFireDetectionToLink(struct Equipment *phead);			//“火焰传感器”设备节点加入设备工厂链表函数声明
struct Equipment *addBuzzerToLink(struct Equipment *phead);					//“蜂鸣器”设备节点加入设备工厂链表函数声明

(2) bathroomLight.c 文件(浴室灯):

#include "equipment.h"

int bathroomLightInit(int pinNum);				//一些函数声明
int bathroomLightOpen(int pinNum);
int bathroomLightClose(int pinNum);
struct Equipment *addBathroomLightToLink(struct Equipment *phead);


struct Equipment bathroomLight = { 		//“浴室灯”设备链表节点
	.equipName = "bathroomLight",
	.pinNum = 21,						//树莓派gpio引脚21
	.Init = bathroomLightInit,
	.open = bathroomLightOpen,
	.close = bathroomLightClose,
};


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

int bathroomLightOpen(int pinNum)			//打开函数
{ 
	digitalWrite(pinNum,LOW);
}

int bathroomLightClose(int pinNum)			//关闭函数
{ 
	digitalWrite(pinNum,HIGH);
}


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

(3) secondfloorLight.c 文件(二楼灯):

#include "equipment.h"

int secondfloorLightInit(int pinNum);				//一些函数声明
int secondfloorLightOpen(int pinNum);
int secondfloorLightClose(int pinNum);
struct Equipment *addSecondfloorLightToLink(struct Equipment *phead);


struct Equipment secondfloorLight = { 		//“二楼灯”设备链表节点
	.equipName = "secondfloorLight",
	.pinNum = 22,							//树莓派gpio引脚22
	.Init = secondfloorLightInit,
	.open = secondfloorLightOpen,
	.close = secondfloorLightClose,
	.changeStatus = secondfloorLightChangeStatus,
};


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

int secondfloorLightOpen(int pinNum)			//打开函数
{ 
	digitalWrite(pinNum,LOW);
}

int secondfloorLightClose(int pinNum)			//关闭函数
{ 
	digitalWrite(pinNum,HIGH);
}


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

(4) livingroomLight.c 文件(客厅灯):

#include "equipment.h"

int livingroomLightInit(int pinNum);				//一些函数声明
int livingroomLightOpen(int pinNum);
int livingroomLightClose(int pinNum);
struct Equipment *addLivingroomLightToLink(struct Equipment *phead);


struct Equipment livingroomLight = { 		//“客厅灯”设备链表节点
	.equipName = "livingroomLight",
	.pinNum = 23,							//树莓派gpio引脚23
	.Init = livingroomLightInit,
	.open = livingroomLightOpen,
	.close = livingroomLightClose,
};


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

int livingroomLightOpen(int pinNum)				//打开函数
{ 
	digitalWrite(pinNum,LOW);
}

int livingroomLightClose(int pinNum)			//关闭函数
{ 
	digitalWrite(pinNum,HIGH);
}


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

(5) restaurantLight.c 文件(餐厅灯):

#include "equipment.h"

int restaurantLightInit(int pinNum);				//一些函数声明
int restaurantLightOpen(int pinNum);
int restaurantLightClose(int pinNum);
struct Equipment *addRestaurantLightToLink(struct Equipment *phead);


struct Equipment restaurantLight = { 		//“餐厅灯”设备链表节点
	.equipName = "restaurantLight",
	.pinNum = 24,							//树莓派gpio引脚24
	.Init = restaurantLightInit,
	.open = restaurantLightOpen,
	.close = restaurantLightClose,
};


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

int restaurantLightOpen(int pinNum)				//打开函数
{ 
	digitalWrite(pinNum,LOW);
}

int restaurantLightClose(int pinNum)			//关闭函数
{ 
	digitalWrite(pinNum,HIGH);
}


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

(6) fireDetection.c 文件(火焰传感器):

#include "equipment.h"

int fireDetectionInit(int pinNum);					//一些函数声明
int readFireDetectionStatus(int pinNum);
struct Equipment *addFireDetectionToLink(struct Equipment *phead);


struct Equipment fireDetection = { 			//“火焰传感器”设备链表节点
	.equipName = "fireDetection",
	.pinNum = 25,							//树莓派gpio引脚25
	.Init = fireDetectionInit,
	.readStatus = readFireDetectionStatus,
};


int fireDetectionInit(int pinNum)			//初始化函数
{ 
	pinMode(pinNum,INPUT);					//配置引脚为输入引脚
	digitalWrite(pinNum,HIGH);				//引脚输出高电平,即默认为关闭状态
}

int readFireDetectionStatus(int pinNum)		//读取“火焰传感器”状态函数
{ 
	return digitalRead(pinNum);
}


struct Equipment *addFireDetectionToLink(struct Equipment *phead)
{ 
	if(phead == NULL){ 
		return &fireDetection;
	}else{ 
		fireDetection.next = phead;
		phead = &fireDetection;
		return phead;
	}
}

(7) buzzer.c 文件(蜂鸣器):

#include "equipment.h"

int buzzerInit(int pinNum);					//一些函数声明
int buzzerOpen(int pinNum);
int buzzerClose(int pinNum);
struct Equipment *addBuzzerToLink(struct Equipment *phead);


struct Equipment buzzer = { 			//“蜂鸣器”设备链表节点
	.equipName = "buzzer",
	.pinNum = 29,					//树莓派gpio引脚29
	.Init = buzzerInit,
	.open = buzzerOpen,
	.close = buzzerClose,
};


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 Equipment *addBuzzerToLink(struct Equipment *phead)			//头插法将设备节点加入设备工厂链表函数
{ 
	if(phead == NULL){ 
		return &buzzer;
	}else{ 
		buzzer.next = phead;
		phead = &buzzer;
		return phead;
	}
}

(8) command.h 文件(指令控制类头文件):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wiringPi.h>
#include <wiringSerial.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

struct Command									//指令控制工厂链表节点定义
{ 
	char commandName[128];						//“控制方式”名字
	char deviceFilesName[128];					//存放初始化功能需要打开的文件的路径
	char command[32];							//存放指令
	int fd;										//存放文件描述符
	int (*Init)(struct Command *file);			//“初始化”函数指针
	int s_fd;									//存放套接字描述符
	char ipAdress[32];							//存放IP地址
	char port[12];								//存放端口号
	int (*getCommand)(struct Command *cmd);		//“获取指令”函数指针
	char log[1024];								//日志(暂未使用)

	struct Command *next;
};

struct Command *addVoiceControlToLink(struct Command *phead);		//“语音控制”加入指令控制工厂链表函数声明
struct Command *addSocketControlToLink(struct Command *phead);		//“网络控制”加入指令控制工厂链表函数声明

(9) voiceControl.c 文件(语音控制):

#include "command.h"
#include <unistd.h>

int voiceControlInit(struct Command *file);							//“语音控制”功能初始化函数声明
int voiceControlGetCommand(struct Command *cmd);					//“获取指令”函数声明
struct Command *addVoiceControlToLink(struct Command *phead);		//“语音控制”加入指令控制工厂链表函数声明


struct Command voiceControl = { 				//“语音控制”链表节点
	.commandName = "voiceControl",
	.deviceFilesName = "/dev/ttyAMA0",
	.command = { '\0'},
	.Init = voiceControlInit,
	.getCommand = voiceControlGetCommand,
	.log = { '\0'},
};


int voiceControlInit(struct Command *file)
{ 
	int fd;
	if((fd = serialOpen(file->deviceFilesName,9600)) == -1){ 		//打开树莓派串口,波特率为9600
		exit(-1);
	}
	file->fd = fd;				//打开串口文件成功,返回“文件描述符”到“语音控制”链表节点中
}


int voiceControlGetCommand(struct Command *cmd)					//“获取指令”函数
{ 
	int nread = 0;
	memset(cmd->command,'\0',sizeof(cmd->command));					//读取串口
	nread = read(cmd->fd,cmd->command,sizeof(cmd->command));		//返回读取到数据的字节数
	return nread;
}


struct Command *addVoiceControlToLink(struct Command *phead)		//头插法将“语音控制”链表节点加入指令控制工厂链表函数
{ 
	if(phead == NULL){ 
		return &voiceControl;
	}else{ 
		voiceControl.next = phead;
		phead = &voiceControl;
		return phead;
	}
}

(10) socketControl.c(网络端口控制):

#include "command.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>


int socketControlInit(struct Command *file);					//“网络控制”功能初始化函数声明
struct Command *addSocketControlToLink(struct Command *phead);	//“网络控制”加入指令控制工厂链表函数声明


struct Command socketControl = { 		//“网络控制”链表节点
	.commandName = "socketControl",
	.command = { '\0'},
	.Init = socketControlInit,
	.ipAdress = "192.168.43.97",		//树莓派连接网络时的IP地址
	.port = "8888",						//树莓派打开待外界连接的端口号
	.log = { '\0'},
};


int socketControlInit(struct Command *file)
{ 
	int s_fd;											//套接字描述符
	struct sockaddr_in s_addr;
	memset(&s_addr,0,sizeof(struct sockaddr_in));

	s_fd = socket(AF_INET,SOCK_STREAM,0);				//创建套接字
    if(s_fd == -1){ 										//创建套接字失败时
            perror("socketControl error");
            exit(-1);
    }

	s_addr.sin_family = AF_INET;
    s_addr.sin_port = htons(atoi(file->port));
    inet_aton(file->ipAdress,&s_addr.sin_addr);
	if(bind(s_fd,(struct sockaddr*)&s_addr,sizeof(struct sockaddr_in)) == -1){ 		//套接字与端口号绑定
    	perror("bind error");
    	exit(-1);
    }

	if(listen(s_fd,10) == -1){ 				//打开监听
    	perror("listen error");
    	exit(-1);
    }

	file->s_fd = s_fd;						//套接字描述符返回到“网络控制”链表节点
}


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

3、往期文章讲解:

【智能家居 (1) ——工厂模式继电器控制灯】

【智能家居 (2) ——工厂模式火焰报警器】

【智能家居 (3) ——语音识别控制(多线程)】

【智能家居 (4) ——网络控制(多线程)】

 
打赏
 本文转载自:网络 
所有权利归属于原作者,如文章来源标示错误或侵犯了您的权利请联系微信13520258486
更多>最近资讯中心
更多>最新资讯中心
更多>相关资讯中心
0相关评论

推荐图文
推荐资讯中心
点击排行
最新信息
新手指南
采购商服务
供应商服务
交易安全
关注我们
手机网站:
新浪微博:
微信关注:

13520258486

周一至周五 9:00-18:00
(其他时间联系在线客服)

24小时在线客服