智能家居(2)—— 网络操控以及引入人脸识别模块

准备工作

设备需要

设备名称作用
蜂鸣器提醒烟雾报警
MQ-2烟雾报警器检测甲烷、液化气、可燃气体,信号发送给SBC开发板
全志H616开发板SBC开发板,控制各类外设
继电器组控制灯光或其他电器
SG90舵机控制开关门
SU-03T语音模块发送指令,控制电器
USB摄像头人脸识别验证

准备工作

libcurl库下载及编译

在linux下用c语言做HTTP的编程有一种方法是依赖于这个libcurl库,以后做跨平台网络协议相关的开发,第一个要想到的就是它。

libcurl是一个跨平台的网络协议库,支持http, https, ftp等协议,libcurl同样支持:

(1)HTTPS证书授权
(2)HTTP POST, HTTP PUT, FTP 上传
(3)HTTP基本表单上传,代理,cookies,和用户认证

库下载地址: Release 7.71.1 · curl/curl · GitHub
加压下载好的库,先去处理openSSL,最后编译libcurl库

tar xvf curl-7.71.1.tar.bz2

编译openSSL支持libcurl的https访问

wget https://www.openssl.org/source/openssl-1.1.1a.tar.gz
tar xvf openssl-1.1.1a.tar.gz
cd /openssl-1.1.1a
./config
make
sudo make install

回到/curl-7.71.1/目录下重新进行配置:

./configure --prefix=$PWD/_install --with-ssl
make
make install

项目效果

嵌入式智能家居项目(语音控制、网络控制)

代码实现

单独建立一个文件夹用于舵机开关

mkdir sg90
cd sg90
gcc ./SG90.c -lwiringPi
编译方法
gcc *.c -lwiringPi -lwiringPiDev -lpthread -lm -lcrypt -lrt -I …/https/curl-7.71.1/_install/include/ -L …/https/curl-7.71.1/_install/lib/ -lcurl
sudo ./a.out

SG90.c

#include <stdio.h>
#include <sys/time.h>
#include <stdlib.h>
#include <signal.h>
#include <wiringPi.h>
#include <unistd.h>
#define SG90Pin 10
int jd;
int count = 0;
static int i = 0;
void signal_handler(int signum)
{
	if(i <= jd){
		digitalWrite(SG90Pin, HIGH);
	}else{
		digitalWrite(SG90Pin, LOW);
	}
	if(i == 40){
		i = 0;
        count++;
	}
	i++;
}
int main(int argc,char** argv)
{
    int flag = 1;
	if(argc != 2)
		printf("sudo ./a.out **\n");
	struct itimerval itv;
	jd = 0;
	wiringPiSetup();
	pinMode(SG90Pin, OUTPUT);
	//设定定时时间
	itv.it_interval.tv_sec = 0;
	itv.it_interval.tv_usec = 500;
	//设定开始生效,启动定时器的时间
	itv.it_value.tv_sec = 1;
	itv.it_value.tv_usec = 0;
	//设定定时方式
	if( -1 == setitimer(ITIMER_REAL, &itv, NULL)){
		perror("error");
		exit(-1);
	}
	//jd = atoi(argv[1]);
	//printf("%d",jd);
	//信号处理
	signal(SIGALRM,signal_handler);
	while(flag){
		jd = atoi(argv[1]);
        if(count >= 100)
        flag = 0;
		//printf("input jd: 1-0 2-45 3-90 4-135 \n");
		//scanf("%d",&jd);
	}
	return 0;
}


main.c

#include <pthread.h>
#include "ControlDevice.h"
#include "InputCommand.h"

struct InputCommand *pcommandHead = NULL;				              
struct Devices      *pdeviceHead  = NULL;				              
struct InputCommand *socketHandler = NULL;
char ocrRetBuf[1024] = {'\0'}; 
struct Devices* findDeviceByName(char *name, struct Devices *phead)
{
	struct Devices *tmp =phead;
	if(phead == NULL){
		return NULL;
	}else{
		while(tmp != NULL){
			if(strcmp(tmp->deviceName,name)==0){
				return tmp;
			}
			tmp = tmp->next;
		}
		return NULL;
	}
}
char *strstr2(char *str, char *strSearch){
	char *strTemp;
	char *strSearchTemp;
	while(*str!='\0'){
		strTemp=str;
		strSearchTemp=strSearch;
		while((*strTemp==*strSearchTemp)&&(*strTemp!='\0')&&(*strSearchTemp!='\0')){
			strTemp++;strSearchTemp++;
		}
		if(*strSearchTemp=='\0')
			return str;
		unsigned char code = *str;
		if(code>=0x80)
			str += 2;
		else
			str++;
	}
	return NULL;
}  
struct InputCommand* findCommandByName(char *name, struct InputCommand *phead)
{
	struct InputCommand *tmp =phead;
	if(phead == NULL){
		return NULL;
	}else{
		while(tmp != NULL){
			if(strcmp(tmp->commandName,name)==0){
				return tmp;
			}
			tmp = tmp->next;
		}
		return NULL;
	}
}

void Command(struct InputCommand* CmdHandler)
{
	struct Devices *tmp =NULL;

	if(strcmp("CSHALL",CmdHandler->command) == 0){
		tmp = findDeviceByName("smokeAlarm",pdeviceHead);
		if(tmp != NULL)  tmp->Init(tmp->pinNum);
		tmp = findDeviceByName("buzzer",pdeviceHead);
		if(tmp != NULL)  tmp->Init(tmp->pinNum);
		tmp = findDeviceByName("livingroomLight",pdeviceHead);
		if(tmp != NULL)  tmp->Init(tmp->pinNum); 
		tmp = findDeviceByName("restaurantLight",pdeviceHead);
		if(tmp != NULL)  tmp->Init(tmp->pinNum);
		tmp = findDeviceByName("bedroomLight",pdeviceHead);
		if(tmp != NULL)  tmp->Init(tmp->pinNum);
		tmp = findDeviceByName("bathroomLight",pdeviceHead);
		if(tmp != NULL)  tmp->Init(tmp->pinNum);
		tmp = findDeviceByName("swimpoolLight",pdeviceHead);
		if(tmp != NULL)  tmp->Init(tmp->pinNum);
		tmp = findDeviceByName("lock",pdeviceHead);
		if(tmp != NULL)  tmp->Init(tmp->pinNum);
		printf("Initial device has been done.\n");
	}
	if(strcmp("OL1",CmdHandler->command) == 0){
		tmp = findDeviceByName("livingroomLight",pdeviceHead);
		if(tmp != NULL){
			tmp->open(tmp->pinNum);
			printf("Open livingroom Light.\n");
		}
	}
	if(strcmp("CL1",CmdHandler->command) == 0){
		tmp = findDeviceByName("livingroomLight",pdeviceHead);
		if(tmp != NULL){
			tmp->close(tmp->pinNum);
			printf("Close livingroom Light.\n");
		}
	}
	if(strcmp("OL2",CmdHandler->command) == 0){
		tmp = findDeviceByName("restaurantLight",pdeviceHead);
		if(tmp != NULL){
			tmp->open(tmp->pinNum);
			printf("Open restaurant Light.\n");
		}
	}
	if(strcmp("CL2",CmdHandler->command) == 0){
		tmp = findDeviceByName("restaurantLight",pdeviceHead);
		if(tmp != NULL){
			tmp->close(tmp->pinNum);
			printf("Close restaurant Light.\n");
		}
	}
	if(strcmp("OL3",CmdHandler->command) == 0){
		tmp = findDeviceByName("bedroomLight",pdeviceHead);
		if(tmp != NULL){
			tmp->open(tmp->pinNum);
			printf("Open bedroom Light.\n");
		}
	}
	if(strcmp("CL3",CmdHandler->command) == 0){
		tmp = findDeviceByName("bedroomLight",pdeviceHead);
		if(tmp != NULL){
			tmp->close(tmp->pinNum);
			printf("Close bedroom Light.\n");
		}
	}
	if(strcmp("OL4",CmdHandler->command) == 0){
		tmp = findDeviceByName("bathroomLight",pdeviceHead);
		if(tmp != NULL){
			tmp->open(tmp->pinNum);
			printf("Open bathroom Light.\n");
		}
	}
	if(strcmp("CL4",CmdHandler->command) == 0){
		tmp = findDeviceByName("bathroomLight",pdeviceHead);
		if(tmp != NULL){
			tmp->close(tmp->pinNum);
			printf("Close bathroom Light.\n");
		}
	}
	if(strcmp("OD",CmdHandler->command) == 0){
		tmp = findDeviceByName("lock",pdeviceHead);
		if(tmp != NULL){
			tmp->open(tmp->pinNum);
			printf("Open door.\n");
		}
	}
	if(strcmp("CD",CmdHandler->command) == 0){
		tmp = findDeviceByName("lock",pdeviceHead);
		if(tmp != NULL){
			tmp->close(tmp->pinNum);
			printf("Close door.\n");
		}
	}
	if(strcmp("OLALL",CmdHandler->command) == 0){
		tmp = findDeviceByName("livingroomLight",pdeviceHead);
		if(tmp != NULL)  tmp->open(tmp->pinNum);
		tmp = findDeviceByName("restaurantLight",pdeviceHead);
		if(tmp != NULL)  tmp->open(tmp->pinNum);
		tmp = findDeviceByName("bedroomLight",pdeviceHead);
		if(tmp != NULL)  tmp->open(tmp->pinNum);
		tmp = findDeviceByName("bathroomLight",pdeviceHead);
		if(tmp != NULL)  tmp->open(tmp->pinNum);
		tmp = findDeviceByName("swimpoolLight",pdeviceHead);
		if(tmp != NULL)  tmp->open(tmp->pinNum);
		printf("Open all Light.\n");
	}
	if(strcmp("CLALL",CmdHandler->command) == 0){
		tmp = findDeviceByName("livingroomLight",pdeviceHead);
		if(tmp != NULL)  tmp->close(tmp->pinNum);
		tmp = findDeviceByName("restaurantLight",pdeviceHead);
		if(tmp != NULL)  tmp->close(tmp->pinNum);
		tmp = findDeviceByName("bedroomLight",pdeviceHead);
		if(tmp != NULL)  tmp->close(tmp->pinNum);
		tmp = findDeviceByName("bathroomLight",pdeviceHead);
		if(tmp != NULL)  tmp->close(tmp->pinNum);
		tmp = findDeviceByName("swimpoolLight",pdeviceHead);
		if(tmp != NULL)  tmp->close(tmp->pinNum);
		printf("Close all Light.\n");
	}
	if(strcmp("OCR",CmdHandler->command) == 0){
		tmp = findDeviceByName("camera",pdeviceHead);
		if(tmp != NULL){
			tmp->justDoOnce();
			if(strstr2(ocrRetBuf,"否") != NULL){   
				printf("人脸识别失败\n");
                unsigned char VoiceSendCmd[5] = {0xAA,0x55,0x02,0x55,0xAA};
                int nwrite = write(CmdHandler->fd,VoiceSendCmd,5*sizeof(VoiceSendCmd[0]));
                //if(nwrite ==-1)
                printf("Send voice cmd %d bytes.\n",nwrite);
			}else{
				printf("人脸识别成功\n");
                unsigned char VoiceSendCmd[5] = {0xAA,0x55,0x01,0x55,0xAA};
                int nwrite = write(CmdHandler->fd,VoiceSendCmd,5*sizeof(VoiceSendCmd[0]));
                //if(nwrite ==-1)
                printf("Send voice cmd %d bytes.\n",nwrite);
                tmp = findDeviceByName("lock",pdeviceHead);
				if(tmp != NULL){
					tmp->open(tmp->pinNum);
					printf("开门\n");
					delay(3000);
					tmp->close(tmp->pinNum);
				}
			}
		}
	}
}

void *voiceControlThread(void *data)			
{   
	int nread;
	struct InputCommand *voiceHandler = NULL;
	voiceHandler =  findCommandByName("voice", pcommandHead);

	if(voiceHandler == NULL){
		printf("find voiceHandler error\n");
		pthread_exit(NULL);
	}else{
		if(voiceHandler->Init(voiceHandler) < 0){       
			printf("voiceControl init error\n");
			pthread_exit(NULL);
		}else{
			printf("voiceControl init success\n");
		}
		while(1){
			memset(voiceHandler->command,'\0',sizeof(voiceHandler->command));
			nread = voiceHandler->getCommand(voiceHandler);
			if(nread == 0){                                 
				printf("No voiceCommand received\n");
			}else{											
				printf("Get VoiceCommand -->%s\n",voiceHandler->command);
				Command(voiceHandler);
			}
		}   
	}
}

void *socketReadThread(void *data)				
{
	int n_read;
	printf("Connect success\n");
	while(1){
		memset(socketHandler->command,'\0',sizeof(socketHandler->command));
		n_read=read(socketHandler->fd,socketHandler->command,sizeof(socketHandler->command));
		if(n_read == -1){
			perror("read:");
		}else{
			printf("Get SocketCommand-->%s\n",socketHandler->command);
			Command(socketHandler);
		}
	}
}


void *socketControlThread(void *data)           
{   
	int c_fd;                           
	struct sockaddr_in c_addr;
	memset(&c_addr,0,sizeof(struct sockaddr_in));
	int clen = sizeof(struct sockaddr_in);

	pthread_t socketRead_thread; 

	socketHandler =  findCommandByName("socket", pcommandHead);

	if(socketHandler == NULL){
		printf("find socketHandler error\n");
		pthread_exit(NULL);
	}
	if(socketHandler->Init(socketHandler) < 0){   
		printf("socketControl init error\n");
		pthread_exit(NULL);
	}else{
		printf("socketControl init success\n");
	}
	while(1){
		
		c_fd = accept(socketHandler->s_fd,(struct sockaddr *)&c_addr,&clen);	
		socketHandler->fd = c_fd;					                            
		pthread_create(&socketRead_thread,NULL,socketReadThread,NULL);			
	}
}

void *smokeAlarmThread(void *data)               
{
	int smokeStatus;												
	struct Devices *tmp = NULL;

	while(1){
		tmp = findDeviceByName("smokeAlarm", pdeviceHead);
		if(tmp != NULL){
			smokeStatus = tmp->readStatus(tmp->pinNum);
			delay(100);
			tmp = findDeviceByName("buzzer", pdeviceHead);
			if(tmp != NULL){
				if( smokeStatus == 0 ){
					tmp->open(tmp->pinNum);
				}else{
					tmp->close(tmp->pinNum);
				}           
			}
		}
	}
}

int main()
{
	if (wiringPiSetup () == -1) { 
		fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ; 
		return 1 ; 
	}

	pthread_t voiceControl_thread;
	pthread_t socketControl_thread;
	pthread_t smokeAlarm_thread;

	pcommandHead = addVoiceControlToInputCommandLink(pcommandHead);
	pcommandHead = addSocketControlToInputCommandLink(pcommandHead);

	pdeviceHead = addBathroomLightToDeviceLink(pdeviceHead);            
	pdeviceHead = addBedroomLightToDeviceLink(pdeviceHead);
	pdeviceHead = addRestaurantLightToDeviceLink(pdeviceHead);
	pdeviceHead = addLivingroomLightToDeviceLink(pdeviceHead);
	pdeviceHead = addSmokeAlarmToDeviceLink(pdeviceHead);
	pdeviceHead = addBuzzerToDeviceLink(pdeviceHead);
	pdeviceHead = addSwimpoolLightToDeviceLink(pdeviceHead);
	pdeviceHead = addLockToDeviceLink(pdeviceHead);
	pdeviceHead = addCameraToDeviceLink(pdeviceHead);

	
	pthread_create(&voiceControl_thread,NULL,voiceControlThread,NULL);
	pthread_create(&socketControl_thread,NULL,socketControlThread,NULL);
	pthread_create(&smokeAlarm_thread,NULL,smokeAlarmThread,NULL);

	pthread_join(voiceControl_thread, NULL);		
	pthread_join(socketControl_thread, NULL);		
	pthread_join(smokeAlarm_thread, NULL);			

	return 0;
}

InputCommand.h

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>      
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <wiringPi.h>
#include <wiringSerial.h>
 
struct InputCommand
{
    char commandName[128];                              
    char deviceName[128];               
    char command[32];                                   
    int fd;                                             
    int s_fd;									        
    char port[12];								        
    char ipAdress[32];							        
    char log[1024];                                     
    int (*Init)(struct InputCommand *voice);            
    int (*getCommand)(struct InputCommand *voice);      
 
    struct InputCommand *next;
};
 
struct InputCommand* addVoiceControlToInputCommandLink(struct InputCommand *phead);	
struct InputCommand* addSocketControlToInputCommandLink(struct InputCommand *phead);	

ControlDevice.h

#include <stdio.h>
#include <stdlib.h>
#include <wiringPi.h>					
 
 
extern char ocrRetBuf[1024];
 
struct Devices                          
{
    char deviceName[128];               
    int status;                        
    int pinNum;							
 
    int (*Init)(int pinNum);			
	int (*open)(int pinNum);			
	int (*close)(int pinNum);			
    int (*readStatus)(int pinNum);	
	int (*changeStatus)(int status);	
    void (*justDoOnce)();
 
    struct Devices *next;
};

 
struct Devices* addBathroomLightToDeviceLink(struct Devices *phead);		
struct Devices* addBedroomLightToDeviceLink(struct Devices *phead);	        
struct Devices* addRestaurantLightToDeviceLink(struct Devices *phead);		
struct Devices* addLivingroomLightToDeviceLink(struct Devices *phead);		
struct Devices* addSmokeAlarmToDeviceLink(struct Devices *phead);           
struct Devices* addBuzzerToDeviceLink(struct Devices *phead);		        
struct Devices* addLockToDeviceLink(struct Devices *phead);
struct Devices* addCameraToDeviceLink(struct Devices *phead);

camera.c

#include "ControlDevice.h"
#include <stdio.h>
#include <curl/curl.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
 
size_t readData(void *ptr, size_t size, size_t nmemb, void *stream)
{
        strncpy(ocrRetBuf,ptr,1024);
}
 
char* getBase64FromFile(char* filePath)
{
        char* base64Buf = NULL;
        char cmd[256] = {'\0'};
 
        sprintf(cmd,"base64 %s > tmpFile",filePath);     
        system(cmd);
 
        int fd = open("./tmpFile",O_RDWR);
        int fileLen = lseek(fd,0,SEEK_END);        
        lseek(fd,0,SEEK_SET);                      
 
        base64Buf = (char* )malloc(fileLen+8);
        memset(base64Buf,'\0',fileLen+8);
 
        read(fd,base64Buf,fileLen+8);              
        close(fd);
        system("rm -f tmpFile");
 
        return base64Buf;
}
 
char* getFace()
{
	printf("人脸数据采集中...\n");
	system("sudo fswebcam -d /dev/video0 --no-banner -r 1280x720 -S 5 ./image.jpg");
	
	while(access("./image.jpg",F_OK) != 0);
	
	printf("数据采集完毕\n");
	
	char* base64BufFaceRec = getBase64FromFile("./image.jpg");
	system("rm image.jpg");  
	return base64BufFaceRec;  
}
 
void postUrl()       
{
        CURL *curl;
        CURLcode res;
        
        
        char* base64BufPic1 = getFace();       
        char* base64BufPic2 = getBase64FromFile("./Person1.jpg");
        char* key    = "需要自行前往翔云平台购买";
        char* secret = "需要自行前往翔云平台购买";
        int   typeId = 21;
        char* format = "xml"; 
        
        int len = strlen(key)+strlen(secret)+strlen(base64BufPic1)+strlen(base64BufPic2)+128;
        char* postString = (char* )malloc(len);
        memset(postString,'\0',len);
	      sprintf(postString,"img1=%s&img2=%s&key=%s&secret=%s&typeId=%d&format=%s",base64BufPic1,base64BufPic2,key,secret,typeId,format);
 
        curl = curl_easy_init();
        if(curl){
                
                curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postString);
                curl_easy_setopt(curl, CURLOPT_URL, "https://netocr.com/api/faceliu.do");
                curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,readData); 
                res = curl_easy_perform(curl);
                printf("OK:%d\n",res);
                curl_easy_cleanup(curl);
        }
}

struct Devices camera = {
	.deviceName = "camera",
    .justDoOnce = postUrl
};
 
struct Devices* addCameraToDeviceLink(struct Devices* phead)
{
	if(phead == NULL){
		return &camera;
	}else{
		camera.next = phead; 
		phead = &camera;      
 	 return phead;
	}
}

SocketControl.c

#include "InputCommand.h"
 
int socketInit(struct InputCommand *socketMsg)
{
 
    int s_fd;                                       
	struct sockaddr_in s_addr;
	memset(&s_addr,0,sizeof(struct sockaddr_in));   
 
    
	s_fd=socket(AF_INET,SOCK_STREAM,0);             
	if(s_fd == -1){                                 
		perror("socket:");
		exit(-1);
	}
 
    s_addr.sin_family = AF_INET;                        
	s_addr.sin_port = htons(atoi(socketMsg->port));     
	inet_aton(socketMsg->ipAdress,&s_addr.sin_addr);   
 
	bind(s_fd,(struct sockaddr *)&s_addr,sizeof(struct sockaddr_in));
 
    
	listen(s_fd,10);
    printf("socket Server listening ...\n");
 
    socketMsg->s_fd = s_fd;						   
    return s_fd;
}
 
struct InputCommand socketControl = {
    .commandName = "socket",
    .command = '\0',
    .port = "8080",
    .ipAdress = "192.168.31.111",
    .Init = socketInit,
    .log = {'\0'},
    .next = NULL
};
 
struct InputCommand* addSocketControlToInputCommandLink(struct InputCommand *phead)
{
	if(phead == NULL){
		return &socketControl;
	}else{
		socketControl.next = phead;
		phead = &socketControl;
		return phead;
	}
}

bedroom.c

#include "ControlDevice.h"
 
int bedroomLightInit(int pinNum)            
	pinMode(pinNum,OUTPUT);					
	digitalWrite(pinNum,HIGH);			
}
 
int bedroomLightOpen(int pinNum)
{
	digitalWrite(pinNum,LOW);				
}
 
int bedroomLightClose(int pinNum)
{
	digitalWrite(pinNum,HIGH);			
 
int bedroomLightStatus(int status)
{
	
}
 
struct Devices bedroomLight = {			
	.deviceName = "bedroomLight",		
	.pinNum = 7,						
	.Init = bedroomLightInit,			
	.open = bedroomLightOpen,			
	.close = bedroomLightClose,			
    .changeStatus = bedroomLightStatus
};
 
struct Devices* addBedroomLightToDeviceLink(struct Devices *phead)		
{
	if(phead == NULL){
		return &bedroomLight;
	}else{
		bedroomLight.next = phead;  
		phead = &bedroomLight;     
		return phead;
	}
}

livingroom.c

#include "ControlDevice.h"			
 
int livingroomLightInit(int pinNum)         
{
	pinMode(pinNum,OUTPUT);					
	digitalWrite(pinNum,HIGH);				
}
 
int livingroomLightOpen(int pinNum)
{
	digitalWrite(pinNum,LOW);				
}
 
int livingroomLightClose(int pinNum)
{
	digitalWrite(pinNum,HIGH);				
}
 
int livingroomLightStatus(int status)
{
	
}
 
struct Devices livingroomLight = {			//定义客厅灯(对象)
	.deviceName = "livingroomLight",		//名字
	.pinNum = 8,							//香橙派 16号(wPi)引脚
	.Init = livingroomLightInit,			//指定初始化函数
	.open = livingroomLightOpen,			//指定“打开灯”函数
	.close = livingroomLightClose,			//指定“关闭灯”函数
    .changeStatus = livingroomLightStatus
};
 
struct Devices* addLivingroomLightToDeviceLink(struct Devices *phead)		//客厅灯(对象)加入设备链表函数
{
	if(phead == NULL){
		return &livingroomLight;
	}else{
		livingroomLight.next = phead;  //以前的头变成.next
		phead = &livingroomLight;      //更新头
		return phead;
	}
}

restaurant.c

#include "ControlDevice.h"			//自定义设备类的文件
 
int restaurantLightInit(int pinNum)         //C语言必须要传参,JAVA不用,可直接访问变量的值
{
	pinMode(pinNum,OUTPUT);					//配置引脚为输出模式
	digitalWrite(pinNum,HIGH);				//引脚置高电平,断开继电器
}
 
int restaurantLightOpen(int pinNum)
{
	digitalWrite(pinNum,LOW);				//引脚置低电平,闭合继电器
}
 
int restaurantLightClose(int pinNum)
{
	digitalWrite(pinNum,HIGH);				//引脚置高电平,断开继电器
}
 
int restaurantLightStatus(int status)
{
	
}
 
struct Devices restaurantLight = {			//定义餐厅灯(对象)
	.deviceName = "restaurantLight",		//名字
	.pinNum = 5,							//香橙派 13号(wPi)引脚
	.Init = restaurantLightInit,			//指定初始化函数
	.open = restaurantLightOpen,			//指定“打开灯”函数
	.close = restaurantLightClose,			//指定“关闭灯”函数
    .changeStatus = restaurantLightStatus
};
 
struct Devices* addRestaurantLightToDeviceLink(struct Devices *phead)		//餐厅灯(对象)加入设备链表函数
{
	if(phead == NULL){
		return &restaurantLight;
	}else{
		restaurantLight.next = phead;  //以前的头变成.next
		phead = &restaurantLight;      //更新头
		return phead;
	}
}

bathroom.c

#include "ControlDevice.h"			
 
int bathroomLightInit(int pinNum) {       
	pinMode(pinNum,OUTPUT);					
	digitalWrite(pinNum,HIGH);			
}
 
int bathroomLightOpen(int pinNum)
{
	digitalWrite(pinNum,LOW);				
}
 
int bathroomLightClose(int pinNum)
{
	digitalWrite(pinNum,HIGH);				
}
 
int bathroomLightStatus(int status)
{
	
}
 
struct Devices bathroomLight = {			
	.deviceName = "bathroomLight",		
	.pinNum = 2,							
	.Init = bathroomLightInit,				
	.open = bathroomLightOpen,				
	.close = bathroomLightClose,			
    .changeStatus = bathroomLightStatus
};
 
struct Devices* addBathroomLightToDeviceLink(struct Devices *phead)		
{
	if(phead == NULL){
		return &bathroomLight;
	}else{
		bathroomLight.next = phead;  
		phead = &bathroomLight;     
		return phead;
	}
}

buzzer.c

#include "ControlDevice.h"			
 
int buzzerInit(int pinNum)
{
	pinMode(pinNum,OUTPUT);						
	digitalWrite(pinNum,HIGH);					
}
 
int buzzerOpen(int pinNum)
{
	digitalWrite(pinNum,LOW);					
}
 
int buzzerClose(int pinNum)
{
	digitalWrite(pinNum,HIGH);				
}
 
struct Devices buzzer = {						
	.deviceName = "buzzer",						
	.pinNum = 11,								
	.Init = buzzerInit,							
	.open = buzzerOpen,							
	.close = buzzerClose,						
};
 
 
struct Devices* addBuzzerToDeviceLink(struct Devices *phead)		
{
	if(phead == NULL){
		return &buzzer;
	}else{
		buzzer.next = phead;
		phead = &buzzer;
		return phead;
	}
}

lock.c

#include <stdio.h>
#include <sys/time.h>
#include <stdlib.h>
#include <signal.h>
#include <wiringPi.h>
#include "ControlDevice.h"

static int count = 0;
int angle;
int lockInit(int pinNum)         
{
	pinMode(pinNum,OUTPUT);					
	digitalWrite(pinNum,HIGH);				
 system("sudo ./sg90/a.out 3");
}
 
int lockOpen(int pinNum)
{
   system("sudo ./sg90/a.out 1");
}
 
int lockClose(int pinNum)
{
   system("sudo ./sg90/a.out 3");

} 
int lockStatus(int status)
{
	
}
 
struct Devices lock = {			//定义客厅灯(对象)
	.deviceName = "lock",		//名字
	.pinNum = 10,							//香橙派 16号(wPi)引脚
	.Init = lockInit,			//指定初始化函数
	.open = lockOpen,			//指定“打开灯”函数
	.close = lockClose,			//指定“关闭灯”函数
  .changeStatus = lockStatus
};
 
struct Devices* addLockToDeviceLink(struct Devices *phead)		//客厅灯(对象)加入设备链表函数
{
	if(phead == NULL){
		return &lock;
	}else{
		lock.next = phead;  //以前的头变成.next
		phead = &lock;      //更新头
		return phead;
	}
}

firewarn.c

#include "ControlDevice.h"			        
 
int smokeAlarmInit(int pinNum)              
{
	pinMode(pinNum,INPUT);				    
	//digitalWrite(pinNum,HIGH);			
}
 
int smokeAlarmReadStatus(int pinNum)
{
	return digitalRead(pinNum);
}
 
int smokeAlarmStatus(int status)
{
 
}
 
struct Devices smokeAlarm = {			
	.deviceName = "smokeAlarm",			
	.pinNum = 6,						
	.Init = smokeAlarmInit,				
    .readStatus = smokeAlarmReadStatus,
    .changeStatus = smokeAlarmStatus
};
 
struct Devices* addSmokeAlarmToDeviceLink(struct Devices *phead)		
{
	if(phead == NULL){
		return &smokeAlarm;
	}else{
		smokeAlarm.next = phead;  
		phead = &smokeAlarm;      
		return phead;
	}
}

注意事项

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值