当前位置:   article > 正文

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

树莓派智能项目设计

前言

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

一、整体系统框图

在这里插入图片描述

二、代码部分

  • main.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
 
#include <sys/types.h>          
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
 
#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主函数/==================================================================
	
 	}

 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444
  • 445
  • 446
  • 447
  • 448
  • 449
  • 450
  • 451
  • 452
  • 453
  • 454
  • 455
  • 456
  • 457
  • 458
  • 459
  • 460
  • 461
  • 462
  • 463
  • 464
  • 465
  • 466
  • 467
  • 468
  • 469
  • 470
  • 471
  • 472
  • 473
  • 474
  • 475
  • 476
  • 477
  • 478
  • 479
  • 480
  • 481
  • 482
  • 483
  • 484
  • 485
  • 486
  • 487
  • 488
  • 489
  • 490
  • 491
  • 492
  • 493
  • 494
  • 495
  • 496
  • 497
  • 498
  • 499
  • 500
  • 501
  • 502
  • 503
  • 504
  • 505
  • 506
  • 507
  • 508
  • 509
  • 510
  • 511
  • 512
  • 513
  • 514
  • 515
  • 516
  • 517
  • 518
  • 519
  • 520
  • 521
  • 522
  • 523
  • 524
  • 525
  • 526
  • 527
  • 528
  • 529
  • 530
  • 531
  • 532
  • 533
  • 534
  • 535
  • 536
  • 537
  • 538
  • 539
  • 540
  • 541
  • 542
  • 543
  • 544
  • 545
  • 546
  • 547
  • 548
  • 549
  • 550
  • 551
  • 552
  • 553
  • 554
  • 555
  • 556
  • 557
  • 558
  • 559
  • 560
  • 561
  • 562
  • 563
  • 564
  • 565
  • 566
  • 567
  • 568
  • 569
  • 570
  • 571
  • 572
  • 573
  • 574
  • 575
  • 576
  • 577
  • 578
  • 579
  • 580
  • 581
  • 582
  • 583
  • 584
  • 585
  • 586
  • 587
  • 588
  • 589
  • 590
  • 591
  • 592
  • 593
  • 594
  • 595
  • 596
  • 597
  • 598
  • 599
  • 600
  • 601
  • 602
  • 603
  • 604
  • 605
  • 606
  • 607
  • 608
  • 609
  • 610
  • 611
  • 612
  • 613
  • 614
  • 615
  • 616
  • 617
  • 618
  • 619
  • 620
  • 621
  • 622
  • 623
  • 624
  • 625
  • 626
  • 627
  • 628
  • 629
  • 630
  • 631
  • socketContrl.c
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <wiringSerial.h>
#include <unistd.h>
 
#include <sys/types.h>          
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
 
 
#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;	
}
 
 
 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • voiceContrl.c
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <wiringSerial.h>
#include <unistd.h>
#include <string.h>
 
#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;	
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 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;
}
 
 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 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;
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 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;
}
 
 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 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;
}
 
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 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;
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 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;
}
 
 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 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;
}
 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • contrlDevices.h
#include <wiringPi.h>
#include <stdio.h>
#include <curl/curl.h>
 
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);


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • inputCommand.h
#include <wiringPi.h>
#include <stdio.h>
 
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);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

二、APP客户端

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

在这里插入图片描述

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


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155

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();

	}
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51

三、实物展示

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/232191?site
推荐阅读
相关标签
  

闽ICP备14008679号