基于树莓派的智能家居设计

文章目录

  • 前言
  • 一、整体系统框图
  • 二、代码部分
  • 二、APP客户端
    • 1.MainActivity.java
    • 2.NetUtils.java
  • 三、实物展示

前言

一个简易的小项目,以下是代码部分和实物效果展示。

一、整体系统框图

基于树莓派的智能家居设计_第1张图片

二、代码部分

  • main.c
#include 
#include 
#include 
#include 
#include 
 
#include           
#include 
#include 
#include 
#include 
#include 
#include 
#include 
 
#include "contrlDevices.h"
#include "inputCommand.h"
 
pthread_t voiceThread; 	//注意:定义线程不使用指针以免空指针异常
pthread_t socketThread;	//注意:不建议线程传参(链表头)所以定为全局变量
pthread_t fireThread;
pthread_t cameraThread;
pthread_t clientWemosThread;
 
struct InputCommander *pCommandHead = NULL;  
struct Devices		  *pdeviceHead = NULL;
struct InputCommander *socketHandler = NULL; 
struct InputCommander *clientHandler = NULL;


pthread_mutex_t mutex;  //定义互斥量(锁)
//pthread_cond_t  cond;   //条件 
 
int c_fd;			    //注意:涉及到多线程不要轻易的去传参
 
//摄像头相关,改变返回值命名,因为C语言中没有这样的返回值
#define true 1
#define false 0
typedef unsigned int bool;
//char buf[1024] = {'\0'};

char* faceRes = NULL;
 
struct Devices *findEquipByName(char *name,struct Devices *phead)    //查询设备
{
	if(phead == NULL){
		return NULL;
	}
	while(phead != NULL){
		if(strstr(phead->deviceName,name) != NULL){
			return phead;
		}
		phead = phead->next;
	}
	
	return NULL;
}
 
struct InputCommander *findCommandByName(char *name,struct InputCommander *phead)  //查询控制
{
	if(phead == NULL){
		return NULL;
	}
	while(phead != NULL){
		if(strcmp(phead->commandName,name) == 0){
			return phead;
		}
		phead = phead->next;
	}
	return NULL;
}
 
//=======================================================摄像头======================================================================
void *camera_thread(void *datas)    //摄像头线程
{
	const char* openCamera = "/home/pi/mjpg-streamer/mjpg-streamer-experimental/start.sh";
	system(openCamera);	//调用start.sh可执行文件
}
//=======================================================/end/======================================================================
//=======================================================人脸识别开锁=================================================================

size_t handle(void *ptr, size_t size, size_t nmemb, void *stream){
	//拷贝返回来的结果字段
	int ssize = strlen(ptr) + 1;
	faceRes = (char*)malloc(ssize);
	memset(faceRes, '\0', ssize);
	strncpy(faceRes, ptr, ssize);
}
char* getBase64(char* photoPath){// 获取照片(jpg格式)的Base64的值
	char cmd[256] = {'\0'};

	sprintf(cmd, "base64 %s > photoBase64File", photoPath);
	system(cmd);// 通过执行"base64"这个指令即可得到照片的Base64值,在这里将得到的Base64值存放在photoBase64File文件中

	int fd,size;
	fd = open("./photoBase64File", O_RDWR);
	size = lseek(fd, 0, SEEK_END) + 1;
	lseek(fd, 0, SEEK_SET);
	
	char* res = (char*)malloc(size);
	memset(res, '\0', size);
	read(fd, res, size);// 从photoBase64File文件中读取照片的Base64值
	close(fd);
	
	system("rm photoBase64File");

	return res;
}
bool cameraContrlPostUrl()
{
	CURL *curl;
	CURLcode res;
	struct Devices *linkHandler  = NULL;
	char* message = NULL;

	// 调用getBase64()自定的函数获取存放在当前文件夹下的两个进行识别的图片的Base64值
	char* img1 = getBase64("./host.jpg");

/*************************添加的代码**************************************/
	
	//openCamera为start.sh可执行文件的绝对地址,请根据自己的路径改
	//const char* openCamera = "/home/pi/mjpg-streamer/mjpg-streamer-experimental/start.sh";
	//system(openCamera);	//调用start.sh可执行文件

	//获取访问者的照片
	system("wget http://192.168.137.104:8080/?action=snapshot -O ./visitor.jpg");

/************************************************************************/
	char* img2 = getBase64("./visitor.jpg");
	
	// key值和secret值是在翔云官网->个人中心的OCR Key和OCR secret两个的值
	char* key = "QYRbM22W52hMrHaUWkdKEN";
	char* secret = "30e80f34e0b04a2f863ff4ff615c2661";
	int typeId = 21;
	char* format = "xml";

	int size = strlen(img1)+strlen(img2)+strlen(key)+strlen(secret)+strlen(format)+3;
	message = (char*)malloc(size);
	memset(message, '\0', size);

	sprintf(message, "&img1=%s&img2=%s&key=%s&secret=%s&typeId=%d&format=%s",
		img1, img2, key, secret, typeId, format);

	curl = curl_easy_init(); // 初始化
	if (curl)
	{
		curl_easy_setopt(curl, CURLOPT_URL, "https://netocr.com/api/faceliu.do");// 指定url
		curl_easy_setopt(curl, CURLOPT_POSTFIELDS, message);// 指定post内容
		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, handle);// 当拿到结果后,指定调用handle()该自定的函数进行处理
		res = curl_easy_perform(curl);// 进行访问官网、进行人脸识别的操作
		curl_easy_cleanup(curl);// 执行完后,对curl_easy_init()进行清理
		



		printf("%s\n",faceRes);// 打印出人脸识别后返回来的结果字段

		printf("shibiechenggong\n");
		
		if(strstr(faceRes, "是") != NULL){// 如果返回来的结果字段中有“是”这个字眼,代表是同一个人
			
			printf("tongyigeren\n");
			linkHandler = findEquipByName("lock",pdeviceHead);
			linkHandler->open(linkHandler->pinNum);
			sleep(3);
			linkHandler->close(linkHandler->pinNum);

		}
		
		else{

			printf("bushitongyigeren\n");
			
			
		
			}
	printf("shibiejieshu\n");
	}

	

	
	
	free(faceRes);
	free(img1);
	free(img2);

}
 

//=======================================================/end人脸识别/================================================================

//=======================================================语音模块=====================================================================

void *voice_thread(void *arg)    //语音线程
{
	int i = 0;
	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) < 0){    //语音模块初始化
				printf("voice init error\n");
				pthread_exit(NULL);  //退出线程
			}else{
				printf("%s init success\n",voiceHandler->commandName);
				} //语音初始化完成
	 
//pthread_mutex_lock(&mutex);    //加锁【有待研究】   
                               //为什么加这个锁呢,我的想法是在语音读取一级指令的时候,为了避免其它线程对于 紧接着读取二级指令的干扰
 
		while(1){			
			memset(voiceHandler->command,'\0',sizeof(voiceHandler->command));
		
			nread = voiceHandler->getCommand(voiceHandler);	    //读取来自语音模块的串口数据
			if(nread == 0){
				printf("noData from voice,please say again\n");
				
			}

				else{													//获取到指令
					printf("Get voice command:%s\n",voiceHandler->command);

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

					//语音模块串口传出来的后面带\r\n,不加对比不出来
						if(strcmp("kycd\r\n",voiceHandler->command) == 0){//开泳池灯
							linkHandler = findEquipByName("yongchilight",pdeviceHead);
							linkHandler->open(linkHandler->pinNum);
							printf("open bathroomlight\n");
						}

						if(strcmp("gycd\r\n",voiceHandler->command) == 0){//关泳池灯
							linkHandler = findEquipByName("yongchilight",pdeviceHead);
							linkHandler->close(linkHandler->pinNum);
							printf("已关闭浴室灯\n");
						}

						if(strcmp("kwsd\r\n",voiceHandler->command) == 0){//开卧室灯
							linkHandler = findEquipByName("bedroomlight",pdeviceHead);
							linkHandler->open(linkHandler->pinNum);
						}

						if(strcmp("gwsd\r\n",voiceHandler->command) == 0){//关卧室灯
							linkHandler = findEquipByName("bedroomlight",pdeviceHead);
							linkHandler->close(linkHandler->pinNum);
						}

						if(strcmp("kktd\r\n",voiceHandler->command) == 0){//开客厅灯
							linkHandler = findEquipByName("livingroomLight",pdeviceHead);
							linkHandler->open(linkHandler->pinNum);
						}

						if(strcmp("gktd\r\n",voiceHandler->command) == 0){//关客厅灯
							linkHandler = findEquipByName("livingroomLight",pdeviceHead);
							linkHandler->close(linkHandler->pinNum);
						}

						if(strcmp("kfs\r\n",voiceHandler->command) == 0){//开风扇
							linkHandler = findEquipByName("fan",pdeviceHead);
							linkHandler->open(linkHandler->pinNum);
						}

						if(strcmp("gfs\r\n",voiceHandler->command) == 0){//关风扇
							linkHandler = findEquipByName("fan",pdeviceHead);
							linkHandler->close(linkHandler->pinNum);
						}
						if(strcmp("km\r\n",voiceHandler->command) == 0){//开门
						
							cameraContrlPostUrl();   //调用人脸识别
							//linkHandler = findEquipByName("lock",pdeviceHead);
							//linkHandler->open(linkHandler->pinNum);
						}

						if(strcmp("gm\r\n",voiceHandler->command) == 0){//关门
							linkHandler = findEquipByName("lock",pdeviceHead);
							linkHandler->close(linkHandler->pinNum);
						}

						if(strcmp("kqbd\r\n",voiceHandler->command) == 0){//打开全部灯
							linkHandler = findEquipByName("yongchilight",pdeviceHead);
							linkHandler->open(linkHandler->pinNum);

							linkHandler = findEquipByName("bedroomlight",pdeviceHead);
							linkHandler->open(linkHandler->pinNum);

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

							}
							if(strcmp("face",voiceHandler->command) == 0){    //进行人脸识别
								//deviceTmp = findDeviceByName("face",pdeviceHead);
								//deviceTmp->cameraInit(deviceTmp);
								cameraContrlPostUrl();   //调用人脸识别

			
							}
/*
						//以下指令发送到wemos服务端
						if(strcmp("dkds\r\n",voiceHandler->command) == 0){//打开电视
								memset(clientHandler,'\0',sizeof(clientHandler));
                                strcpy(clientHandler->command,"1");
                                write(clientHandler->sfd,clientHandler->command,strlen(clientHandler->command));    

							}
						if(strcmp("gbds\r\n",voiceHandler->command) == 0){//关闭电视
								memset(clientHandler,'\0',sizeof(clientHandler));
                                strcpy(clientHandler->command,"2");
                                write(clientHandler->sfd,clientHandler->command,strlen(clientHandler->command)); 

							}
						if(strcmp("dkkt\r\n",voiceHandler->command) == 0){//打开空调
							memset(clientHandler,'\0',sizeof(clientHandler));
                                strcpy(clientHandler->command,"3");
                                write(clientHandler->sfd,clientHandler->command,strlen(clientHandler->command)); 

							}
						if(strcmp("gbkt\r\n",voiceHandler->command) == 0){//关闭空调
								memset(clientHandler,'\0',sizeof(clientHandler));
                                strcpy(clientHandler->command,"4");
                                write(clientHandler->sfd,clientHandler->command,strlen(clientHandler->command)); 
		
							}*/
				}
			}


//pthread_mutex_unlock(&mutex);    //解锁
 
	}
}


//========================================================/end语音/====================================================================


//========================================================火焰警报=====================================================================
void *fire_thread(void *datas)    //火灾线程
{
	int status;
	struct Devices *fireDeviceTmp = NULL;
	struct Devices *buzzerDeviceTmp = NULL;
 
	fireDeviceTmp = findEquipByName("fire",pdeviceHead);    //在设备工厂找到火灾模块
	buzzerDeviceTmp = findEquipByName("buzzser",pdeviceHead);
	    
	fireDeviceTmp->deviceInit(fireDeviceTmp->pinNum);        //火灾模块初始化
	buzzerDeviceTmp->deviceInit(buzzerDeviceTmp->pinNum);
	printf("fire_thread init\n");
 
   	while(1){
		delay(2000);
		status = fireDeviceTmp->readStatus(fireDeviceTmp->pinNum);    //读取火灾模块实时数据
		//printf("fire get data = %d\n",status);
 
		if(status == 0){
			buzzerDeviceTmp->open(buzzerDeviceTmp->pinNum);
		}else{
			buzzerDeviceTmp->close(buzzerDeviceTmp->pinNum);
		}
    }
}


//========================================================/end火焰/====================================================================


//========================================================socket网络控制===============================================================
void *read_thread(void *datas)    //通过socket读取客户端发来的数据
{
	int n_read;
	struct Devices *linkHandler  = NULL;
	
	while(1){
		memset(socketHandler->command,'\0',sizeof(socketHandler->command));
 
		n_read = read(c_fd,socketHandler->command,sizeof(socketHandler->command));    //读取客户端发来的数据
		if(n_read == -1){
			perror("read_thread");
		}else if(n_read > 0){
			printf("getCommand:%s\n",socketHandler->command);
		
			//处理客户端读到的命令
			if(strcmp("kycd",socketHandler->command) == 0){
				linkHandler = findEquipByName("yongchilight",pdeviceHead);
				linkHandler->open(linkHandler->pinNum);
				
			
			}

			if(strcmp("gycd",socketHandler->command) == 0){
				linkHandler = findEquipByName("yongchilight",pdeviceHead);
				linkHandler->close(linkHandler->pinNum);
			}

			if(strcmp("kwsd",socketHandler->command) == 0){
				linkHandler = findEquipByName("bedroomlight",pdeviceHead);
				linkHandler->open(linkHandler->pinNum);
			}

			if(strcmp("gwsd",socketHandler->command) == 0){
				linkHandler = findEquipByName("bedroomlight",pdeviceHead);
				linkHandler->close(linkHandler->pinNum);
			}

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

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

			if(strcmp("kfs",socketHandler->command) == 0){
				linkHandler = findEquipByName("fan",pdeviceHead);
				linkHandler->open(linkHandler->pinNum);
			}

			if(strcmp("gfs",socketHandler->command) == 0){
				linkHandler = findEquipByName("fan",pdeviceHead);
				linkHandler->close(linkHandler->pinNum);
			}
			if(strcmp("km",socketHandler->command) == 0){
				linkHandler = findEquipByName("lock",pdeviceHead);
				linkHandler->open(linkHandler->pinNum);
			}

			if(strcmp("gm",socketHandler->command) == 0){
				linkHandler = findEquipByName("lock",pdeviceHead);
				linkHandler->close(linkHandler->pinNum);
			}


			if(strcmp("kqbd",socketHandler->command) == 0){
				linkHandler = findEquipByName("yongchilight",pdeviceHead);
				linkHandler->open(linkHandler->pinNum);

				linkHandler = findEquipByName("bedroomlight",pdeviceHead);
				linkHandler->open(linkHandler->pinNum);

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

			
			}
			if(strcmp("gqbd",socketHandler->command) == 0){
				linkHandler = findEquipByName("yongchilight",pdeviceHead);
				linkHandler->close(linkHandler->pinNum);

				linkHandler = findEquipByName("bedroomlight",pdeviceHead);
				linkHandler->close(linkHandler->pinNum);

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

			
			}

			if(strcmp("gqbdq",socketHandler->command) == 0){
				linkHandler = findEquipByName("bedroomlight",pdeviceHead);
				linkHandler->close(linkHandler->pinNum);

				linkHandler = findEquipByName("yongchilight",pdeviceHead);
				linkHandler->close(linkHandler->pinNum);

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

				linkHandler = findEquipByName("fan",pdeviceHead);
				linkHandler->close(linkHandler->pinNum);
				
				linkHandler = findEquipByName("lock",pdeviceHead);
				linkHandler->close(linkHandler->pinNum);
			}
			//if(strcmp("face",socketHandler->command) == 0){    //进行人脸识别
				//deviceTmp = findDeviceByName("face",pdeviceHead);
				//deviceTmp->cameraInit(deviceTmp);
				//cameraContrlPostUrl();   //调用人脸识别

			//
			//	}
		}
		else{
			printf("client quit\n");
			exit(-1);				//客户端退出,服务器程序退出
			//pthread_exit(NULL);  //退出线程
		}
	}
	
}

 void *socket_thread(void *datas)    //开启socket服务端,并将socket服务端初始化
{
	int n_read = 0;
	pthread_t readPthread;
	
	struct sockaddr_in c_addr;
	memset(&c_addr,0,sizeof(struct sockaddr_in));
	int clen = sizeof(struct sockaddr_in);

	
	socketHandler = findCommandByName("socketContrl",pCommandHead);    //在控制工厂找到socket
	if(socketHandler == NULL){
		printf("find socketHandler error\n");
		pthread_exit(NULL);		
	}else{
		printf("%s init success\n",socketHandler->commandName);
	}
	if(socketHandler->Init(socketHandler) < 0){				//“网络控制”功能初始化
		printf("socketControl init error\n");
		pthread_exit(NULL);
	}    
	//socketHandler->Init(socketHandler,NULL,NULL);    //初始化socket
	
	while(1){
		c_fd = accept(socketHandler->sfd,(struct sockaddr *)&c_addr, &clen);
		pthread_create(&readPthread,NULL,read_thread,NULL);
	}
	
}

 //========================================================/end网络/====================================================================
//=========================================================/连接wemos服务端/============================================================
/*
void *clientWemos_Thread(void *datas)    //连接wemos线程
{
	char *p;
	//struct InputCommander *clientHandler = NULL;    //放到全局,因为我要在socket那里接收用户客户端client的数据,然后发给wemos
 
	//做客户端连接wemosD1服务器
	clientHandler = findCommandByName("client",pCommandHead);    //在控制工厂找到客户连接端

	if(clientHandler == NULL){
		printf("find clientHandler error\n");
		exit(-1);
	}else{
		clientHandler->Init(clientHandler);        //client初始化
	}
	
	while(1){		//获取服务端输入的命令数据,发送到wemos执行,这里只是调试作用,实际上我们需要接收的是客户端发来的数据。
		memset(clientHandler,'\0',sizeof(clientHandler));
        printf("input your contrl wemosD1 command::\n");
        fgets(clientHandler->command,sizeof(clientHandler->command),stdin);  //不用gets,有警告
		if((p = strchr(clientHandler->command,'\n')) != NULL)
        *p = '\0';		//手动将\n位置处的值变为0
        
        write(clientHandler->sfd,clientHandler->command,strlen(clientHandler->command));     //向wemosD1发送数据
	}
}

*/

//=========================================================/end/=======================================================================

 //========================================================主函数=======================================================================
int main()
{
	char name[32] = {'\0'};
 
	//树莓派库初始化
	if(wiringPiSetup() == -1){     
          printf("硬件接口初始化失败\n");
          return -1;
    }
	
	pthread_mutex_init(&mutex,NULL);  //初始化互斥量(锁)
	//pthread_cond_init(&cond,NULL);      //条件的创建(动态初始化)
 
	//1、指令工厂初始化
	pCommandHead = addVoiceContrlToInputCommanderLink(pCommandHead);
    pCommandHead = addsocketContrlToInputCommanderLink(pCommandHead);
	//pCommandHead = addclientContrlToInputCommanderLink(pCommandHead);
	
	//2、设备控制工程初始化
	pdeviceHead = addyongchilightToDeviceLink(pdeviceHead);
	pdeviceHead = addfanToDeviceLink(pdeviceHead);
	pdeviceHead = addlivingroomLightToDeviceLink(pdeviceHead);
	pdeviceHead = addbedroomlightToDeviceLink(pdeviceHead);
	pdeviceHead = addFireToDeviceLink(pdeviceHead);
	pdeviceHead = addBuzzerToDeviceLink(pdeviceHead);
	pdeviceHead = addlockToDeviceLink(pdeviceHead);
	//pdeviceHead = addcameraContrlToDeviceLink(pdeviceHead);
 
	struct Devices *tmpequiphead = pdeviceHead;
	while(tmpequiphead != NULL){						//设备工厂所有设备初始化
		tmpequiphead->deviceInit(tmpequiphead->pinNum);
		tmpequiphead = tmpequiphead->next;
	}
	
	
	//int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void *), void *restrict arg);
	//3、线程池的建立
		//3.1、语音线程
	pthread_create(&voiceThread,NULL,voice_thread,NULL); //参数2:线程属性,一般设置为NULL,参数3:线程干活的函数,参数4:往voice_thread线程里面传送数据。
		//3.2、socket服务器线程
	pthread_create(&socketThread,NULL,socket_thread,NULL); 
		//3.3、火灾线程
	
	pthread_create(&fireThread,NULL,fire_thread,NULL); 
		//3.4、摄像头线程
	pthread_create(&cameraThread,NULL,camera_thread,NULL); 
		//3.5、作为客户端连接wemosD1服务器
	//pthread_create(&clientWemosThread,NULL,clientWemos_Thread,NULL); 
 
 
	//等待线程退出
	pthread_join(voiceThread,NULL);	
	pthread_join(socketThread,NULL);	
	pthread_join(fireThread,NULL);	
	pthread_join(cameraThread,NULL);	
	//pthread_join(clientWemosThread,NULL);

	pthread_mutex_destroy(&mutex);	//销毁互斥量
	//pthread_cond_destroy(&cond);        //条件的销毁
 
 
 
	return 0;
	 //========================================================/end主函数/==================================================================
	
 	}

 

  • socketContrl.c
#include 
#include 
#include 
#include 
#include 
 
#include           
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
 
 
#include "inputCommand.h"
 
int socketgetCommand(struct InputCommander *socketMes)
{
	int c_fd;
	int n_read;
	
	struct sockaddr_in c_addr;
	memset(&c_addr,0,sizeof(struct sockaddr_in));
	int clen = sizeof(struct sockaddr_in);
 
	//4.accept 
	c_fd = accept(socketMes->sfd,(struct sockaddr *)&c_addr, &clen);
 
	n_read = read(c_fd,socketMes->command,sizeof(socketMes->command));
	if(n_read == -1){
		perror("read");
	}else if(n_read > 0){
		printf("\nget:%d\n",n_read);
	}else{
		printf("client quit\n");
	}
 
	return n_read;
}	 
 
int socketInit(struct InputCommander *socketMes)
{


	int s_fd;
	struct sockaddr_in s_addr;
 
	memset(&s_addr,0,sizeof(struct sockaddr_in));			
 
	//1.socket 
	s_fd = socket(AF_INET,SOCK_STREAM,0);
	if(s_fd == -1){
		perror("socked");
		exit(-1);
	}
 
	s_addr.sin_family = AF_INET;
	s_addr.sin_port = htons(atoi(socketMes->port));
	inet_aton(socketMes->ipAdress,&s_addr.sin_addr);
 
	//2.bind  
	bind(s_fd, (struct sockaddr *)&s_addr,sizeof(struct sockaddr_in));
 
	//3.listen  
	listen(s_fd,10);
	printf("socket Server listening......\n");
 
	socketMes->sfd = s_fd;
	

 
}
 
 
struct InputCommander socketContrl = {
	.commandName = "socketContrl",
	.command = {'\0'},
	.port = "8085",             
	.ipAdress = "192.168.137.104",
	.Init = socketInit,
	.getCommand = socketgetCommand,
	.log = {'\0'},
	.next = NULL
};
 
struct InputCommander *addsocketContrlToInputCommanderLink(struct InputCommander *phead)
{	
	if(phead == NULL){
		return &socketContrl;
	}else{
		socketContrl.next = phead;
		phead = &socketContrl;
	}
	return phead;	
}
 
 
 

  • voiceContrl.c
#include 
#include 
#include 
#include 
#include 
#include 
 
#include "inputCommand.h"
 
int getCommand(struct InputCommander *voicer)
{
	int nread = 0;
	memset(voicer->command,'\0',sizeof(voicer->command));
 
	 nread = read(voicer->fd,voicer->command,sizeof(voicer->command));
 
	 return nread;
 
}
 
int voiceInit(struct InputCommander *voicer)
{
	/*形参虽然定多了,用不上,咱不管*/
	
	int fd;
 
	 if((fd = serialOpen(voicer->deviceName,9600)) == -1){
		exit(-1);
	 }
	 voicer->fd = fd;
	 
	 return fd;
}
 
 
struct InputCommander voiceContrl = {
	.commandName = "voice",
	.deviceName = "/dev/ttyAMA0",
									//.boTelv = "9600";
	.command= {'\0'},
	.Init = voiceInit,
	.getCommand = getCommand,
	.log = {'\0'},
	.next = NULL
};
 
struct InputCommander *addVoiceContrlToInputCommanderLink(struct InputCommander *phead)
{	
	if(phead == NULL){
		return &voiceContrl;
	}else{
		voiceContrl.next = phead;
		phead = &voiceContrl;
	}
	return phead;	
}
 
 
  • bedroomlight.c
 
#include "contrlDevices.h"
 
int bedroomlightOpen(int pinNum)
{
	digitalWrite (pinNum,LOW);
}
 
int  bedroomlightClose(int pinNum)
{
	digitalWrite (pinNum,HIGH);
} 
 
int bedroomlightCloseInit(int pinNum)
{
	pinMode (pinNum, OUTPUT);
	digitalWrite (pinNum,HIGH);
}
 
int bedroomlightCloseStatus(int status)
{
 
}
 
struct Devices bedroomlight = {
	.deviceName = "bedroomlight",
	//.deviceName = "chu",
	.pinNum = 27,
	.open = bedroomlightOpen,
	.close = bedroomlightClose,
	.deviceInit = bedroomlightCloseInit,
	.changStatus = bedroomlightCloseStatus,
	.next = NULL
};
 
struct Devices *addbedroomlightToDeviceLink(struct Devices *phead)
{
	if(phead == NULL){
		return &bedroomlight;
	}else{
		bedroomlight.next = phead;
		phead = &bedroomlight;
	}
	return phead;
}
 
 

  • buzzer.c
 
#include "contrlDevices.h"
 
int buzzerOpen(int pinNum)
{
	digitalWrite (pinNum,LOW);
}
 
int buzzerClose(int pinNum)
{
	digitalWrite (pinNum,HIGH);
}
 
int buzzerInit(int pinNum)
{
	pinMode (pinNum, OUTPUT);
	digitalWrite (pinNum,HIGH);
}
 
 
struct Devices buzzer = {
	.deviceName = "buzzser",
	.pinNum = 7,
	.open = buzzerOpen,
	.close = buzzerClose,
	.deviceInit = buzzerInit,
	.next = NULL
};
 
struct Devices *addBuzzerToDeviceLink(struct Devices *phead)
{
	if(phead == NULL){
		return &buzzer;
	}else{
		buzzer.next = phead;
		phead = &buzzer;
	}
	return phead;
}
 
 
  • fan.c
 
#include "contrlDevices.h"
 
int fanOpen(int pinNum)
{
	digitalWrite (pinNum,LOW);
}
 
int  fanClose(int pinNum)
{
	digitalWrite (pinNum,HIGH);
} 
 
int fanCloseInit(int pinNum)
{
	pinMode (pinNum, OUTPUT);
	digitalWrite (pinNum,HIGH);
}
 
int fanCloseStatus(int status)
{
 
}
 
struct Devices fan = {
	.deviceName = "fan",
	//.deviceName = "chu",
	.pinNum = 28,
	.open = fanOpen,
	.close = fanClose,
	.deviceInit = fanCloseInit,
	.changStatus = fanCloseStatus,
	.next = NULL
};
 
struct Devices *addfanToDeviceLink(struct Devices *phead)
{
	if(phead == NULL){
		return &fan;
	}else{
		fan.next = phead;
		phead = &fan;
	}
	return phead;
}
 
 

  • fire.c
 
#include "contrlDevices.h"
 
int fireInit(int pinNum)
{
	pinMode (pinNum,INPUT);
	digitalWrite (pinNum,HIGH);
}
 
int readFireStatus(int pinNum)
{
	return digitalRead(pinNum);
}
 
 
struct Devices fire = {
	.deviceName = "fire",
	.pinNum = 25,
	.deviceInit = fireInit,
	.readStatus = readFireStatus,
	.next = NULL
};
 
struct Devices *addFireToDeviceLink(struct Devices *phead)
{
	if(phead == NULL){
		return &fire;
	}else{
		fire.next = phead;
		phead = &fire;
	}
	return phead;
}
 
 
 
  • livingroomLight.c
 
#include "contrlDevices.h"
 
int livingroomLightOpen(int pinNum)
{
	digitalWrite (pinNum,LOW);
}
 
int  livingroomLightClose(int pinNum)
{
	digitalWrite (pinNum,HIGH);
} 
 
int livingroomLightCloseInit(int pinNum)
{
	pinMode (pinNum, OUTPUT);
	digitalWrite (pinNum,HIGH);
}
 
int livingroomLightCloseStatus(int status)
{
 
}
 
struct Devices livingroomLight2 = {
	.deviceName = "livingroomLight",
	//.deviceName = "shui",
	.pinNum = 26,
	.open = livingroomLightOpen,
	.close = livingroomLightClose,
	.deviceInit = livingroomLightCloseInit,
	.changStatus = livingroomLightCloseStatus,
	.next = NULL
	
};
 
struct Devices *addlivingroomLightToDeviceLink(struct Devices *phead)
{
	if(phead == NULL){
		return &livingroomLight2;
	}else{
		livingroomLight2.next = phead;
		phead = &livingroomLight2;
	}
	return phead;
}
 
 
  • lock.c
 
#include "contrlDevices.h"
 
int lockOpen(int pinNum)
{
	digitalWrite (pinNum,HIGH);
}
 
int  lockClose(int pinNum)
{
	digitalWrite (pinNum,LOW);
} 
 
int lockCloseInit(int pinNum)
{
	pinMode (pinNum, OUTPUT);
	digitalWrite (pinNum,LOW);
}
 
int lockCloseStatus(int status)
{
 
}
 
struct Devices lock = {
	.deviceName = "lock",
	
	.pinNum = 5,
	.open = lockOpen,
	.close = lockClose,
	.deviceInit = lockCloseInit,
	.changStatus = lockCloseStatus,
	.next = NULL
};
 
struct Devices *addlockToDeviceLink(struct Devices *phead)
{
	if(phead == NULL){
		return &lock;
	}else{
		lock.next = phead;
		phead = &lock;
	}
	return phead;
}
 
 

  • yongchilight.c
 
#include "contrlDevices.h"
 
int yongchilightOpen(int pinNum)
{
	digitalWrite (pinNum,LOW);
}
 
int  yongchilightClose(int pinNum)
{
	digitalWrite (pinNum,HIGH);
} 
 
int yongchilightCloseInit(int pinNum)
{
	pinMode (pinNum, OUTPUT);
	digitalWrite (pinNum,HIGH);
}
 
int yongchilightCloseStatus(int status)
{
 
}
 
struct Devices yongchilight = {
	.deviceName = "yongchilight",
	//.deviceName = "yu",
	.pinNum = 29,
	.open = yongchilightOpen,
	.close = yongchilightClose,
	.deviceInit = yongchilightCloseInit,
	.changStatus = yongchilightCloseStatus,
	.next = NULL
};
 
struct Devices *addyongchilightToDeviceLink(struct Devices *phead)
{
	if(phead == NULL){
		return &yongchilight;
	}else{
		yongchilight.next = phead;
		phead = &yongchilight;
	}
	return phead;
}
 

  • contrlDevices.h
#include 
#include 
#include 
 
typedef unsigned int bool;
 
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 (*changStatus)(int status);    //改变数据函数指针
 
	//摄像头相关的
	CURL *curl;
	char *key;
	char *secret;
	int typeId;
	char *format;
	bool (*cameraInit)(struct Devices *camera);
	int yesNum;
	int noNum;
 
	struct Devices *next;
};
 
//每个设备加到链表函数的声明
struct Devices *addyongchilightToDeviceLink(struct Devices *phead);
struct Devices *addfanToDeviceLink(struct Devices *phead);
struct Devices *addlivingroomLightToDeviceLink(struct Devices * phead);
struct Devices *addlockToDeviceLink(struct Devices *phead);
struct Devices *addbedroomlightToDeviceLink(struct Devices *phead);
struct Devices *addFireToDeviceLink(struct Devices *phead);
struct Devices *addBuzzerToDeviceLink(struct Devices *phead);
struct Devices *addcameraContrlToDeviceLink(struct Devices *phead);


  • inputCommand.h
#include 
#include 
 
struct InputCommander
{
	char commandName[128];		//socket名
	char deviceName[128];		//串口名
	char command[32];			//控制命令
 
	int (*Init)(struct InputCommander *voicer);	//socket初始化
	int (*getCommand)(struct InputCommander *voicer);						//读取数据
 
	char log[1024];
	int fd;
	char port[12];     //端口号
	char ipAdress[32]; //IP地址
	int sfd;
	int cfd;		   
	
	struct InputCommander *next;
};
 
//每个控制添加到控制链表的函数声明
struct InputCommander *addVoiceContrlToInputCommanderLink(struct InputCommander *phead);
struct InputCommander *addsocketContrlToInputCommanderLink(struct InputCommander *phead);
struct InputCommander *addclientContrlToInputCommanderLink(struct InputCommander *phead);

二、APP客户端

参考博文:https://blog.csdn.net/bhbhhyg/article/details/115694774?spm=1001.2014.3001.5501

基于树莓派的智能家居设计_第2张图片

1.MainActivity.java

package com.example.smarthome;
import java.io.OutputStream;
import java.net.Socket;




import android.os.Bundle;
import android.app.Activity;


import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
	Button kycd;
	Button gycd;
	Button kws;
	Button gws;
	Button kkt;
	Button gkt;
	Button kfs;
	Button gfs;
	Button km;
	Button gm;
	Button kqbd;
	Button gqbd;
	Button ljms;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		     
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		  setContentView(R.layout.activity_main);
       WebView wb = (WebView) findViewById(R.id.web);
        wb.setWebViewClient(new WebViewClient());
        String s="http://192.168.137.104:8080/?action=stream";
        wb.loadUrl(s);
  
       
        kycd=(Button)findViewById(R.id.kycd);
        gycd=(Button)findViewById(R.id.gycd);
    	kws=(Button)findViewById(R.id.kws);
    	gws=(Button)findViewById(R.id.gws);
    	kkt=(Button)findViewById(R.id.kkt);
    	gkt=(Button)findViewById(R.id.gkt);
    	kfs=(Button)findViewById(R.id.kfs);
    	gfs=(Button)findViewById(R.id.gfs);
    	km=(Button)findViewById(R.id.km);
    	gm=(Button)findViewById(R.id.gm);
    	kqbd = (Button)findViewById(R.id.kqbd);
    	gqbd = (Button)findViewById(R.id.gqbd); 
    	ljms=(Button)findViewById(R.id.ljms);
    	
    	
    	
    	kycd.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
			  new NetUtils("kycd").sendMessage();
			}
		});
    	gycd.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				 new NetUtils("gycd").sendMessage();
			}
		});
    	kws.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				 new NetUtils("kwsd").sendMessage();
			}
		});
    	gws.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				 new NetUtils("gwsd").sendMessage();
			}
		});
    	kkt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				 new NetUtils("kktd").sendMessage();
			}
		});
    	gkt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				 new NetUtils("gktd").sendMessage();
			}
		});
    	kfs.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				 new NetUtils("kfs").sendMessage();
			}
		});
    	gfs.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				 new NetUtils("gfs").sendMessage();
			}
		});
    	km.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				 new NetUtils("km").sendMessage();
			}
		});
    	gm.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				 new NetUtils("gm").sendMessage();
			}
		});
    	kqbd.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				 new NetUtils("kqbd").sendMessage();
			}
		});
    	gqbd.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				 new NetUtils("gqbd").sendMessage();
			}
		});
    	ljms.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				 new NetUtils("gqbdq").sendMessage();
			}
		});
    }}


2.NetUtils.java

package com.example.smarthome;


import java.io.OutputStream;
import java.net.Socket;

import android.os.Handler;



public class NetUtils {

	public String  message;
	public String  reTurnMes;
	public Handler handler;

	public NetUtils(String message) {
		this.message = message;
	}

	public NetUtils(String message, Handler handler) {
		this.message = message;
		this.handler = handler;
	}

	public void sendMessage() {

		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					 Socket client = new Socket("192.168.137.104",8085);//("192.168.137.166",8085);
					//Socket client = new Socket(StartActivity.IP, Integer.parseInt(StartActivity.Port));
					OutputStream out = client.getOutputStream();
					out.write(message.getBytes());				
					//out.close();
					//client.close();
					

				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}).start();

	}
}

三、实物展示

基于树莓派的智能家居设计_第3张图片

你可能感兴趣的:(树莓派开发,智能家居,c语言,c++,开发语言,eclipse,语音识别)