orangepiZero2智能家居(三)添加火灾、socket、语音模块

1.智能家居添加火灾检测模块

1.概述

火灾检测模块大致与灯光模块相同。
不同点:
	火灾模块要读取引脚状态
	pinMode要把output 改为input
	不用打开和关闭

2.代码

1.contrlDevice.h

#include <wiringPi.h>
struct Devices{
	char deviceName[32];
	int status;
	int pinNum;
	int (*open)(int pinNum);
	int (*close)(int pinNum);
	int (*deviceInit)(int pinNum);
	int (*readStatus)(int pinNum);//火灾
	
	int (*changeStatus)(int status);


	struct Devices *next;

};
struct Devices*  addBathroomLightToDeviceLink(struct Devices *phead);
struct Devices*  addUpstairLightToDeviceLink(struct Devices *phead);
struct Devices*  addLivingroomLightToDeviceLink(struct Devices *phead);
struct Devices*  addRestaurantLightToDeviceLink(struct Devices *phead);
struct Devices*  addfireIfOrNotToDeviceLink(struct Devices *phead);

2…fire.c

#include <stdio.h>
#include "contrlDevice.h"
#include "inputCommand.h"



int fireIfOrNotInit(int pinNum){
	pinMode(pinNum,INPUT);

	digitalWrite(pinNum,HIGH);
}
int fireStatusRead(int pinNum){
	
	return digitalRead(pinNum);
}
struct Devices fireIfOrNot={
	.deviceName="fireIfOrNot",
	.pinNum=6,
	
	.deviceInit=fireIfOrNotInit,
	.readStatus=fireStatusRead
		
};


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

2.智能家居添加语音模块和socket模块

1.头文件inputCommand.h

#include <wiringSerial.h>
#include <unistd.h>
#include <stdlib.h>

#include <wiringPi.h>

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

	struct InputCommander *next;
	
};
struct InputCommander *addVoiceContrlToInputCommandLink(struct InputCommander *phead);
struct InputCommander *addSocketContrlToInputCommandLink(struct InputCommander *phead);

2.语音模块

#include "inputCommand.h"

/*
struct InputCommander{
	char commandName[128];
	char deviceName[32];
	char command[32];
	int fd;
	int (*Init)(char *name,char *ipAdress,char *port);
	int (*getCommand)(char *cmd);
	
	char log[1024];

	struct InputCommander *next;
};
*/

int voiceInit(struct InputCommander *voicer,char *ipAdress,char *port){
	int fd=serialOpen(voicer->deviceName,115200);
	if(fd == -1){
		printf("open failed\n");
		exit(-1);
	}
	voicer->fd=fd;
	return fd;
	
}

int voiceGetCommand(struct InputCommander *voicer){
	int nread=0;
	nread=read(voicer->fd,voicer->command,sizeof(voicer->command));
	if(nread==0){
		printf("over time\n");
	}
	return nread;

}



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


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

}

3.socket模块

#include "inputCommand.h"
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>

/*
struct InputCommander{
	char commandName[128];
	char deviceName[32];
	char command[32];
	int fd;
	int (*Init)(char *name,char *ipAdress,char *port);
	int (*getCommand)(char *cmd);
	
	char log[1024];

	struct InputCommander *next;
};
*/

int socketInit(struct InputCommander *socketMes,char *ipAdress,char *port){
	struct sockaddr_in addr;
	
	memset(&addr,0,sizeof(struct sockaddr_in));
	/*   socket */
	int s_fd;
	s_fd=socket(AF_INET,SOCK_STREAM,0);

	if(s_fd==-1){
		perror("socket");
		exit(-1);
	}
	
	addr.sin_family=AF_INET;
	addr.sin_port= htons(atoi(socketMes->port));
	inet_aton(socketMes->ipAddress,&addr.sin_addr);
	bind(s_fd,(struct sockaddr*)&addr,sizeof(struct sockaddr_in));
	
	
		 
	listen(s_fd,10);
	socketMes->sfd=s_fd;
	return s_fd;
	
}

int socketGetCommand(struct InputCommander *socketMes){
	int c_fd;
	int nread;
	struct sockaddr_in addr1;
	memset(&addr1,0,sizeof(struct sockaddr_in));
	int size=sizeof(struct sockaddr_in);
	c_fd=accept(socketMes->sfd,(struct sockaddr*)&addr1,&size);
	if(c_fd==-1){
		perror("accept");
	}
					
	printf("celinte IP:%s\n",inet_ntoa(addr1.sin_addr));

	memset(socketMes->command,'\0',sizeof(socketMes->command));
    nread=read(c_fd,socketMes->command,sizeof(socketMes->command));
	if(nread<0){
		perror("read");
	}
	else if(nread>0){
		printf("read %d, %s\n",nread,socketMes->command);
	}
	else{
		printf("client is quit\n");
	}

	return nread;

}



struct InputCommander socketContrl = {
	.commandName="socketServer",
	.command={'\0'},
	.Init=socketInit,
	.port="8088",
	.ipAddress="172.20.10.4",
	.getCommand=socketGetCommand,
	.log={'\0'},
	.next=NULL
};


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

}

3.编写主程序验证代码

#include <stdio.h>
#include <string.h>
#include "contrlDevice.h"
#include "inputCommand.h"
#include <wiringPi.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <errno.h>

struct Devices *pDeviceHead=NULL;
struct Devices *tmp=NULL;
struct InputCommander *pCommandHead=NULL;

struct InputCommander *socketHander=NULL;


int c_fd;

struct Devices *findDeviceByName(char *name,struct Devices *phead){
	struct Devices *ptmp=phead;
	if(ptmp==NULL){
		printf("none\n");
		return NULL;
	}else{
		while(ptmp!=NULL){
			if(strcmp(ptmp->deviceName,name)==0){

				return ptmp;
			}
			ptmp=ptmp->next;
		}
		return NULL;
	}

}

struct InputCommander *findCommandByName(char *name,struct InputCommander*phead){
	struct InputCommander*ptmp=phead;
	if(ptmp==NULL){
		printf("none\n");
		return NULL;
	}else{
		while(ptmp!=NULL){
			if(strcmp(ptmp->commandName,name)==0){

				return ptmp;
			}
			ptmp=ptmp->next;
		}
		return NULL;
	}

}
void *voice_Thread(void *datas){
	int fd;
	int n_read;
	
	struct InputCommander *voiceHander=NULL;
	voiceHander=findCommandByName("voice",pCommandHead);
	if(voiceHander==NULL){
		printf("find voiceHander error\n");
		pthread_exit(NULL);
	}
	fd=voiceHander->Init(voiceHander,NULL,NULL);
	if(fd < 0){
		printf("voice init error\n");
		pthread_exit(NULL);
	}else{
		printf("voice init success\n");
	}
	while(1){
		n_read=voiceHander->getCommand(voiceHander);
		if(n_read==0){
			printf("not command\n");
		}else{
			printf("read %d bytes %s\n",n_read,voiceHander->command);

		}
	}
}
void *read_Thread(void *datas){
	int n_read;
	memset(socketHander->command,'\0',sizeof(socketHander->command));
	n_read=read(c_fd,socketHander->command,sizeof(socketHander->command));
	if(n_read<0){
			perror("read");
	}
	else if(n_read>0){
		printf("read %d, %s\n",n_read,socketHander->command);
	}
	else{
		printf("client is quit\n");
	}

}
void *socket_Thread(void *datas){
	/* struct InputCommander *socketHander=NULL;*/
	pthread_t readThread;

	
	struct sockaddr_in addr1;
	
	socketHander=findCommandByName("socketServer",pCommandHead);
	if(socketHander==NULL){
		printf("not find\n");
		pthread_exit(NULL);
	}
	socketHander->Init(socketHander,NULL,NULL);
	
	memset(&addr1,0,sizeof(struct sockaddr_in));
	int size=sizeof(struct sockaddr_in);
	while(1){
		c_fd=accept(socketHander->sfd,(struct sockaddr*)&addr1,&size);
		printf("celinte IP:%s\n",inet_ntoa(addr1.sin_addr));
		pthread_create(&readThread,NULL,read_Thread,NULL);

	}
}	
int main(){
	if(wiringPiSetup()==-1){
		return -1;
	}
	char name[32]={'\0'};

	
	

	pthread_t voiceThread;
	pthread_t socketThread;
	//设备工厂初始化
	pDeviceHead=addBathroomLightToDeviceLink(pDeviceHead);
	pDeviceHead=addUpstairLightToDeviceLink(pDeviceHead);
	pDeviceHead=addLivingroomLightToDeviceLink(pDeviceHead);
	pDeviceHead=addRestaurantLightToDeviceLink(pDeviceHead);
	pDeviceHead=addfireIfOrNotToDeviceLink(pDeviceHead);

	//指令工厂初始化

	pCommandHead=addVoiceContrlToInputCommandLink(pCommandHead);
	pCommandHead=addSocketContrlToInputCommandLink(pCommandHead);
	/*
	while(1){
		printf("input deviceName:bathroomLight upstairLight livingroomLight restaurantLight\n");
		scanf("%s",name);
		tmp=findDeviceByName(name,pDeviceHead);
	
		if(tmp!=NULL){
			tmp->deviceInit(tmp->pinNum);
			tmp->open(tmp->pinNum);
		}
		else{
			printf("not find\n");
		}
		memset(name,'\0',sizeof(name));
	}
	
*/

	//创建线程池
		//socket
	pthread_create(&voiceThread,NULL,voice_Thread,NULL);
	pthread_create(&socketThread,NULL,socket_Thread,NULL);
	pthread_join(voiceThread,NULL);
    pthread_join(socketThread,NULL);
		//语音
		//摄像头
		//火灾检测
	return 0;
}

打开网络调试助手验证socket server是否可以接受数据

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值