Raspberry pie - Smart Home - expansion is wemosD1

catalogue

1. Main function

mainPro.c 

2. Sub file (all peripheral devices are objects)

bathroomLight.c (bathroom light)

livingroomLight.c (bedroom light)

restaurantLight.c (kitchen light)

upstairLight.c (living room lamp)

buzzer.c (buzzer)

fire.c (flame sensor)

camera.c (camera)

client_wemos.c (raspberry pie is connected to wemos D1 server as a client)

socketContrl.c (socket control)

voiceContrl.c (voice control equipment)

3. Header file

contrlDevices.h (header file of peripheral equipment)

inputCommand.h (header file of control)

This article is just a quick upload of the code, and many details are not mentioned.

The simple factory design mode is used to simplify the main function code, which is easy to read. All control and peripheral devices are made into objects and connected into a control chain list and a chain list of peripheral devices, which is convenient for the addition of functional modules in the future. In order to control them separately, we use multithreading.

Control end:

Voice control, socket control

Hardware equipment:

Raspberry pie, voice module, wemos D1, relay group, 433M RF module, camera, flame sensor, electromagnetic lock and buzzer.

Project Description:

Raspberry pie machine connects the hardware of each module through serial port, detects the speech recognition results, and analyzes the speech recognition results to control household appliances. After the raspberry camera captures the face, it accesses the face recognition scheme of Xiangyun platform through HTTPS and compares the base64 coding of the photos for face recognition. Because the voice module occupies the only serial port bit of raspberry pie, Wemos D1 uses socket to connect with raspberry pie, controls household appliances by reading the data sent by raspberry pie as a client, and completes the interface expansion of raspberry pie to control more devices.

1. Main function

mainPro.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 "contrlDevices.h"
#include "inputCommand.h"

#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>

pthread_t voiceThread; 	//Note: the definition thread does not use pointers to avoid null pointer exceptions
pthread_t socketThread;	//Note: the thread is not recommended to pass parameters (chain header), so it is set as a global variable
pthread_t fireThread;
pthread_t cameraThread;
pthread_t clientWemosThread;

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

pthread_mutex_t mutex;  //Define mutex (lock)
//pthread_cond_t  cond;   // condition 

int c_fd;			    //Note: do not pass parameters easily when multithreading is involved

//Camera related
#define true 1
#define false 0
typedef unsigned int bool;
char buf[1024] = {'\0'};

struct Devices *findDeviceByName(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 *voice_thread(void *arg)
{
	int i = 0;
	int nread;
	struct InputCommander *voiceHandler = NULL;
	struct Devices *deviceTmp = NULL;
	
	voiceHandler = findCommandByName("voice",pCommandHead);
	if(voiceHandler == NULL){
		printf("find voiceHandler error\n");
		pthread_exit(NULL);		
	}else{
		if(voiceHandler->Init(voiceHandler,NULL,NULL) < 0){
			printf("voice init error\n");
			pthread_exit(NULL);  //Exit thread
		}else{
			printf("%s init success\n",voiceHandler->commandName);
		} //Voice initialization complete

pthread_mutex_lock(&mutex);    //Lock

		while(1){			
			memset(voiceHandler->comand,'\0',sizeof(voiceHandler->comand));
		
			nread = voiceHandler->getCommand(voiceHandler);	
			if(nread == 0){
				//printf("noData from voice,please say again\n");
			}else if(strstr(voiceHandler->comand,"all") != NULL){
				printf("close all light\n");
				deviceTmp = findDeviceByName("yu",pdeviceHead);
				deviceTmp->close(deviceTmp->pinNum);
				deviceTmp = findDeviceByName("ke",pdeviceHead);
				deviceTmp->close(deviceTmp->pinNum);
				deviceTmp = findDeviceByName("chu",pdeviceHead);
				deviceTmp->close(deviceTmp->pinNum);
				deviceTmp = findDeviceByName("shui",pdeviceHead);
				deviceTmp->close(deviceTmp->pinNum);
			}
			else{ 
				deviceTmp = findDeviceByName(voiceHandler->comand,pdeviceHead);
				if(deviceTmp == NULL){
					printf("findDeviceByName error\n");
				}
				else{
					printf("findDevice = %s\n",deviceTmp->deviceName);
					deviceTmp->deviceInit(deviceTmp->pinNum);
					deviceTmp->open(deviceTmp->pinNum);
				}
			}			
			
		}
		
pthread_mutex_unlock(&mutex);    //Unlock

	}
}



size_t readData1( void *ptr, size_t size, size_t nmemb, void *stream)
{
	memset(buf,'\0',1024);
	strncpy(buf,ptr,1024);

}

char *getBase641(char *picture)
{
	int fd;
	int len;
	char cmd[256] = {'\0'};	

	sprintf(cmd,"base64 %s > pictureBase64File",picture);
	system(cmd);

	fd = open("./pictureBase64File",O_RDWR);
	len = lseek(fd,0,SEEK_END);
	lseek(fd,0,SEEK_SET);

	char *readBuf = (char *)malloc(len);	//If we don't use dynamic, when we return this readBuf pointer, there will be a segment error
	read(fd,readBuf,len);			//Because readBuf is a local variable and a static memory, the contents will be released as soon as the program runs (C language foundation)
	close(fd);
	system("rm ./pictureBase64File");

	return readBuf;
}

bool cameraContrlPostUrl()
{
	CURL *curl;
	CURLcode res;
	char *postString;
	struct Devices *deviceTmp = NULL;

	char *img1;
	char *img2;
	char *key = "Use your own";
	char *secret = "Use your own";
	int typeId = 21;
	char *format = "xml";

	
	chdir("/home/pi/mjpg-streamer/mjpg-streamer-experimental");
 	system("./start.sh");    //Run camera
	chdir("/home/pi/yu/smartHome5_camera+face");
	system("wget  http://172.20.10.2:8080/?action=snapshot -O ./visitor.jpg");  	// Screenshot of real-time video

	img1 = getBase641("./visitor.jpg");
	img2 = getBase641("./me1.jpg");

	int len = strlen(key)+strlen(secret)+strlen(img1)+strlen(img2)+124;
	postString = (char *)malloc(strlen(key)+strlen(secret)+strlen(img1)+strlen(img2)+124);
	memset(postString,'\0',len);

	sprintf(postString,"&img1=%s&img2=%s&key=%s&secret=%s&typeId=%d&format=%s",  //post���ݵ�ƴ��
			img1,img2,key,secret,typeId,format);

	system("rm visitor.jpg");

	curl = curl_easy_init();

	if (curl)
	{
		curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "/tmp/cookie.txt"); // Specify cookie file
		curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postString);    // ָ Specify the post content and splice it with the $symbol
		curl_easy_setopt(curl, CURLOPT_URL, "https://netocr.com/api/faceliu.do");   //  ָ Specify url
		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, readData1); //Output the returned http header to the file pointed to by fp
		res = curl_easy_perform(curl);
		printf("ok = %d\n",res);

		curl_easy_cleanup(curl);

		
		printf("%s\n",buf);
		
		if(strstr(buf,"yes") != NULL){
				deviceTmp = findDeviceByName("chu",pdeviceHead);
				deviceTmp->open(deviceTmp->pinNum);
				deviceTmp = findDeviceByName("yu",pdeviceHead);
				deviceTmp->open(deviceTmp->pinNum);
				deviceTmp = findDeviceByName("ke",pdeviceHead);
				deviceTmp->open(deviceTmp->pinNum);
				deviceTmp = findDeviceByName("shui",pdeviceHead);	
				deviceTmp->open(deviceTmp->pinNum);					
		}else{
			printf("not a same of man\n");		
		}
		
	}
	return true;	
}

void *read_thread(void *datas)
{
	int n_read;
	struct Devices *deviceTmp = NULL;
	
	while(1){
		memset(socketHandler->comand,'\0',sizeof(socketHandler->comand));

		n_read = read(c_fd,socketHandler->comand,sizeof(socketHandler->comand));
		if(n_read == -1){
			perror("read_thread");
		}else if(n_read > 0){
			//printf("getCommand:%s\n",socketHandler->comand);
		
			//Processing commands read by the client
			if(strstr(socketHandler->comand,"op_shui") != NULL){
				deviceTmp = findDeviceByName("shui",pdeviceHead);
				deviceTmp->deviceInit(deviceTmp->pinNum);
				deviceTmp->open(deviceTmp->pinNum);
			}
			if(strstr(socketHandler->comand,"cl_shui") != NULL){
				deviceTmp = findDeviceByName("shui",pdeviceHead);
				deviceTmp->deviceInit(deviceTmp->pinNum);
				deviceTmp->close(deviceTmp->pinNum);
			}
			if(strstr(socketHandler->comand,"op_yu") != NULL){
				deviceTmp = findDeviceByName("yu",pdeviceHead);
				deviceTmp->deviceInit(deviceTmp->pinNum);
				deviceTmp->open(deviceTmp->pinNum);
			}
			if(strstr(socketHandler->comand,"cl_yu") != NULL){
				deviceTmp = findDeviceByName("yu",pdeviceHead);
				deviceTmp->deviceInit(deviceTmp->pinNum);
				deviceTmp->close(deviceTmp->pinNum);
			}
			if(strstr(socketHandler->comand,"op_ke") != NULL){
				deviceTmp = findDeviceByName("ke",pdeviceHead);
				deviceTmp->deviceInit(deviceTmp->pinNum);
				deviceTmp->open(deviceTmp->pinNum);
			}
			if(strstr(socketHandler->comand,"cl_ke") != NULL){
				deviceTmp = findDeviceByName("ke",pdeviceHead);
				deviceTmp->deviceInit(deviceTmp->pinNum);
				deviceTmp->close(deviceTmp->pinNum);
			}						
			if(strstr(socketHandler->comand,"op_chu") != NULL){
				deviceTmp = findDeviceByName("chu",pdeviceHead);
				deviceTmp->deviceInit(deviceTmp->pinNum);
				deviceTmp->open(deviceTmp->pinNum);
			}
			if(strstr(socketHandler->comand,"cl_chu") != NULL){
				deviceTmp = findDeviceByName("chu",pdeviceHead);
				deviceTmp->deviceInit(deviceTmp->pinNum);
				deviceTmp->close(deviceTmp->pinNum);
			}
			if(strstr(socketHandler->comand,"face") != NULL){
				//deviceTmp = findDeviceByName("face",pdeviceHead);
				//deviceTmp->cameraInit(deviceTmp);
				cameraContrlPostUrl();
			}
			if(strstr(socketHandler->comand,"cl_all") != NULL){
				deviceTmp = findDeviceByName("chu",pdeviceHead);
				deviceTmp->deviceInit(deviceTmp->pinNum);
				deviceTmp->close(deviceTmp->pinNum);
				deviceTmp = findDeviceByName("yu",pdeviceHead);
				deviceTmp->deviceInit(deviceTmp->pinNum);
				deviceTmp->close(deviceTmp->pinNum);
				deviceTmp = findDeviceByName("ke",pdeviceHead);
				deviceTmp->deviceInit(deviceTmp->pinNum);
				deviceTmp->close(deviceTmp->pinNum);
				deviceTmp = findDeviceByName("shui",pdeviceHead);
				deviceTmp->deviceInit(deviceTmp->pinNum);
				deviceTmp->close(deviceTmp->pinNum);				
			}

		}
		else{
			printf("client quit\n");
			exit(-1);				//The client exits and the server program exits
			//pthread_exit(NULL);  // Exit thread
		}
	}
	
}

void *socket_thread(void *datas)
{
	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("socketServer",pCommandHead);
	if(socketHandler == NULL){
		printf("find socketHandler error\n");
		pthread_exit(NULL);		
	}else{
		printf("%s init success\n",socketHandler->commandName);
	}
	
	socketHandler->Init(socketHandler,NULL,NULL);
	
	while(1){
		c_fd = accept(socketHandler->sfd,(struct sockaddr *)&c_addr, &clen);
		pthread_create(&readPthread,NULL,read_thread,NULL);
	}
	
}

void *fire_thread(void *datas)
{
	int status;
	struct Devices *fireDeviceTmp = NULL;
	struct Devices *buzzerDeviceTmp = NULL;

	fireDeviceTmp = findDeviceByName("fire",pdeviceHead);
	buzzerDeviceTmp = findDeviceByName("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);
		}
    }
}

void *camera_thread(void *datas)
{
	
}

void *clientWemos_Thread(void *datas)
{
	char *p;
	struct InputCommander *clientHandler = NULL;

	//Do client connection to wemosD1 server
	clientHandler = findCommandByName("client",pCommandHead);
	if(clientHandler == NULL){
		printf("find clientHandler error\n");
		exit(-1);
	}else{
		clientHandler->Init(clientHandler,NULL,NULL);
	}
	
	while(1){		
		memset(clientHandler,'\0',sizeof(clientHandler));
        printf("input your contrl wemosD1 command::\n");
        fgets(clientHandler->comand,sizeof(clientHandler->comand),stdin);  //Don't get, there are warnings
		if((p = strchr(clientHandler->comand,'\n')) != NULL)
        *p = '\0';		//Manually change \ nthe value at position to 0
        
        write(clientHandler->sfd,clientHandler->comand,strlen(clientHandler->comand));     //Send data to wemosD1
	}
}

int main()
{
	char name[32] = {'\0'};

	//Raspberry pie library initialization
	if(wiringPiSetup() == -1){     
          printf("Hardware interface initialization failed\n");
          return -1;
    }
	
	pthread_mutex_init(&mutex,NULL);  //Initialize mutex (lock)
	//pthread_ cond_ init(&cond,NULL);      // Condition creation (dynamic initialization)

	//1. Instruction factory initialization
	pCommandHead = addVoiceContrlToInputCommanderLink(pCommandHead);
    pCommandHead = addsocketContrlToInputCommanderLink(pCommandHead);
	pCommandHead = addclientContrlToInputCommanderLink(pCommandHead);

	//2. Equipment control engineering initialization
	pdeviceHead = addBathroomLightToDeviceLink(pdeviceHead);
	pdeviceHead = addupStairLightToDeviceLink(pdeviceHead);
	pdeviceHead = addlivingroomLightToDeviceLink(pdeviceHead);
	pdeviceHead = addrestaurantLightToDeviceLink(pdeviceHead);
	pdeviceHead = addFireToDeviceLink(pdeviceHead);
	pdeviceHead = addBuzzerToDeviceLink(pdeviceHead);
	//pdeviceHead = addcameraContrlToDeviceLink(pdeviceHead);


	//int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void *), void *restrict arg);
	//3. Establishment of thread pool
		//3.1 voice thread
	pthread_create(&voiceThread,NULL,voice_thread,NULL); //Parameter 2: thread property, which is generally set to NULL; parameter 3: function of thread working; parameter 4: to voice_thread transfers data inside the thread.
		//3.2. socket server thread
	pthread_create(&socketThread,NULL,socket_thread,NULL); 
		//3.3 fire accident
	pthread_create(&fireThread,NULL,fire_thread,NULL); 
		//3.4 camera thread
	//pthread_create(&cameraThread,NULL,camera_thread,NULL); 
		//3.5. Connect to wemosD1 server as a client
	pthread_create(&clientWemosThread,NULL,clientWemos_Thread,NULL); 


	//Wait thread
	pthread_join(voiceThread,NULL);	
	pthread_join(socketThread,NULL);	
	pthread_join(fireThread,NULL);	
	pthread_join(cameraThread,NULL);	
	pthread_join(clientWemosThread,NULL);

	pthread_mutex_destroy(&mutex);	//Destroy mutex
	//pthread_ cond_ destroy(&cond);        // Destruction of conditions


	/*Start creating debug code for the device factory
	while(1){
		printf("input:\n");
		memset(name,'\0',sizeof(name));
		gets(name);

		tmp = findDeviceByName(name,pdeviceHead);
		if(tmp != NULL){
			tmp->deviceInit(tmp->pinNum);
			tmp->open(tmp->pinNum);
		}
		
	}
*/	

	return 0;
}

2. Sub file (all peripheral devices are objects)

bathroomLight.c (bathroom light)

#include "contrlDevices.h"

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

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

int bathroomLightCloseInit(int pinNum)
{
	pinMode (pinNum, OUTPUT);
	digitalWrite (pinNum,HIGH);
}

int bathroomLightCloseStatus(int status)
{

}

struct Devices bathroomLight = {
	//.deviceName = "bathroomLight",
	.deviceName = "yu",
	.pinNum = 22,
	.open = bathroomLightOpen,
	.close = bathroomLightClose,
	.deviceInit = bathroomLightCloseInit,
	.changStatus = bathroomLightCloseStatus
};

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

livingroomLight.c (bedroom light)

#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 livingroomLight = {
	//.deviceName = "livingroomLight",
	.deviceName = "shui",
	.pinNum = 21,
	.open = livingroomLightOpen,
	.close = livingroomLightClose,
	.deviceInit = livingroomLightCloseInit,
	.changStatus = livingroomLightCloseStatus
};

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


restaurantLight.c (kitchen light)

#include "contrlDevices.h"

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

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

int restaurantLightCloseInit(int pinNum)
{
	pinMode (pinNum, OUTPUT);
	digitalWrite (pinNum,HIGH);
}

int restaurantLightCloseStatus(int status)
{

}

struct Devices restaurantLight = {
	//.deviceName = "restaurantLight",
	.deviceName = "chu",
	.pinNum = 23,
	.open = restaurantLightOpen,
	.close = restaurantLightClose,
	.deviceInit = restaurantLightCloseInit,
	.changStatus = restaurantLightCloseStatus
};

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


upstairLight.c (living room lamp)

#include "contrlDevices.h"

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

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

int upStairLightCloseInit(int pinNum)
{
	pinMode (pinNum, OUTPUT);
	digitalWrite (pinNum,HIGH);
}

int upStairLightCloseStatus(int status)
{

}

struct Devices upStairLight = {
	//.deviceName = "upStairLight",
	.deviceName = "ke",
	.pinNum = 24,
	.open = upStairLightOpen,			
	.close = upStairLightClose,
	.deviceInit = upStairLightCloseInit,
	.changStatus = upStairLightCloseStatus
};

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


buzzer.c (buzzer)

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

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


fire.c (flame sensor)

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

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



camera.c (camera)

#include "contrlDevices.h"

#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>

#define true 1
#define false 0
typedef unsigned int bool;

char resultBuf[1024] = {'\0'};

size_t readData( void *ptr, size_t size, size_t nmemb, void *stream)
{
	strncpy(resultBuf,ptr,1024);
	//printf("%s\n",resultBuf);
}	

char *getBase64(char *picture)
{
	int fd;
	int len;
	char cmd[256] = {'\0'};	

	sprintf(cmd,"base64 %s > pictureBase64File",picture);
	system(cmd);

	fd = open("./pictureBase64File",O_RDWR);
	len = lseek(fd,0,SEEK_END);
	lseek(fd,0,SEEK_SET);

	char *readBuf = (char *)malloc(len);	//��������ö�̬�������ǽ����readBufָ�뷵��ȥ�����δ���
	read(fd,readBuf,len);			//�� Ϊ ��readBuf� Ǿֲ ��������� ̬ � ڴ Out door һ ������ ϣ ��������� ݱ �� ͷţ �C��� Ի �����

	close(fd);
	system("rm ./pictureBase64File");

	return readBuf;
}

bool cameraContrl_postUrl(struct Devices *camera)
{
	CURL *curl;
	CURLcode res;
	char *postString;

	char *img1;
	char *img2;

	system("raspistill -w 700 -h 525 -o ./visitor.jpg");

	img1 = getBase64("./visitor.jpg");
	img2 = getBase64("./me1.jpg");

	int len = strlen(camera->key)+strlen(camera->secret)+strlen(img1)+strlen(img2)+124;
	postString = (char *)malloc(strlen(camera->key)+strlen(camera->secret)+strlen(img1)+strlen(img2)+124);
	memset(postString,'\0',len);

	sprintf(postString,"&img1=%s&img2=%s&key=%s&secret=%s&typeId=%d&format=%s",  //post���ݵ�ƴ��
			img1,img2,camera->key,camera->secret,camera->typeId,camera->format);

	system("rm visitor.jpg");

	curl = curl_easy_init();

	if (curl)
	{
		curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "/tmp/cookie.txt"); // ָ��cookie�ļ�
		curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postString);    // ָ��post���ݣ���$����ƴ��
		curl_easy_setopt(curl, CURLOPT_URL, "https://netocr.com/api/faceliu.do");   // ָ��url
		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, readData); //�����ص�httpͷ�����fpָ����ļ�
		res = curl_easy_perform(curl);
		//printf("ok = %d\n",res);
	


		printf("%s\n",resultBuf);
		if(strstr(resultBuf,"��") != NULL){
				printf("ok\n");
		}else{
				printf("no\n");
		}

		curl_easy_cleanup(curl);
	}
	return true;
}


struct Devices cameraContrl = {
	.deviceName = "camera",
	.key = "Use your own",
	.secret = "Use your own",
	.typeId = 21,
	.format = "xml",
	.cameraInit = cameraContrl_postUrl,

	.next = NULL
};

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


client_wemos.c (Raspberry pie connects to wemos D1 server as a client)

#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 clientInit(struct InputCommander *client,char *ipAdress,char *port)
{
        struct sockaddr_in addr;
 
        memset(&addr,0,sizeof(struct sockaddr_in));
 
        //1.�����׽���
        int 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(client->port));
        inet_aton(client->ipAdress,&addr.sin_addr);
 
        //2.���ӷ�����
        if(connect(s_fd,(struct sockaddr *)&addr,sizeof(struct sockaddr))==-1)  //����wemosD1
        {
                perror("connect");
                exit(-1);
        }
        printf("client wemosD1 connect....\n");

		client->sfd = s_fd;
		return s_fd;

}

struct InputCommander clientContrl = {
	.commandName = "client",
	.comand = {'\0'},
	.port = "8888",            
	.ipAdress = "172.20.10.12",
	.Init = clientInit,
	.next = NULL
};

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

socketContrl.c (socket control)

#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->comand,sizeof(socketMes->comand));
	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,char *ipAdress,char *port)
{
	/*�β���Ȼ�����ˣ��ò��ϣ��۲���*/

	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;
	return s_fd;

}


struct InputCommander socketContrl = {
	.commandName = "socketServer",
	.comand = {'\0'},
	.port = "8083",             //�˿ں����ַ������ڰ�֮ǰatoi������תΪ16��������
	.ipAdress = "172.20.10.2",
	.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 (voice control equipment)

#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->comand,'\0',sizeof(voicer->comand));

	 nread = read(voicer->fd,voicer->comand,sizeof(voicer->comand));

	 return nread;

}

int voiceInit(struct InputCommander *voicer,char *ipAdress,char *port)
{
	//�β���Ȼ�����ˣ��ò��ϣ��۲���
	
	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";
	.comand = {'\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;	
}


3. Header file

contrlDevices.h (header file of peripheral equipment)

#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 *addBathroomLightToDeviceLink(struct Devices *phead);
struct Devices *addupStairLightToDeviceLink(struct Devices *phead);
struct Devices *addlivingroomLightToDeviceLink(struct Devices *phead);
struct Devices *addrestaurantLightToDeviceLink(struct Devices *phead);
struct Devices *addFireToDeviceLink(struct Devices *phead);
struct Devices *addBuzzerToDeviceLink(struct Devices *phead);
struct Devices *addcameraContrlToDeviceLink(struct Devices *phead);






inputCommand.h (header file of control)

#include <wiringPi.h>
#include <stdio.h>

struct InputCommander
{
	char commandName[128];		//�����߳���
	char deviceName[128];		//*
	char comand[32];			//����

	int (*Init)(struct InputCommander *voicer,char *ipAdress,char *port);	//��ʼ���׽���
	int (*getCommand)(struct InputCommander *voicer);						//��ȡ�����������

	char log[1024];
	int fd;
	char port[12];     //�˿ں�
	char ipAdress[32]; //ip��ַ
	int sfd;
	int cfd;		   //����wemosD1��
	
	struct InputCommander *next;
};

struct InputCommander *addVoiceContrlToInputCommanderLink(struct InputCommander *phead);
struct InputCommander *addsocketContrlToInputCommanderLink(struct InputCommander *phead);
struct InputCommander *addclientContrlToInputCommanderLink(struct InputCommander *phead);


Added by newbie_07 on Sat, 15 Jan 2022 00:20:49 +0200