赞
踩
基于树莓派的智能家居。智能家居用到的硬件有:树莓派3B+、SU-03语音识别模块、pi 摄像头、继电器组、小灯、火焰传感器、蜂鸣器、电磁锁、超声波测距模块、DHT11温湿度检测模块,433M射频编解码模块或者红外模块,面包板等。
采用了简单工厂模式的一个设计方式。稳定,拓展性更强。通过工厂创建一个通用的接口,集中管理设备和指令。
创建了两个工厂:设备工厂和指令工厂。
设备工厂:用于管理卧室灯,餐厅灯,楼梯灯,浴室灯,火焰传感器,蜂鸣器,电磁锁,超声波,摄像头等设备。初始化的时候,通过链表将各个模块连接起来(头插法)。在要使用某个模块时,只需要使用链表遍历,找到所需模块去调用功能。
指令工厂:管理串口输入输出和socket客户端的指令输入和输出。
具体功能如下:
1、可通过SU-03语音模块的口令模式,口令+具体控制,通过串口把控制指令传给树莓派,来控制客厅、餐厅、二楼、浴室的灯
2、可通过SU-03语音模块的口令模式,通过串口把控制指令传给树莓派,树莓派再传递给射频模块(或者红外模块)控制窗帘,泳池灯,空调等。
3、可以通过socket客户端来发指令来控制客厅、餐厅、二楼、浴室的灯,可以在socket客户端看到摄像头实时监控画面,并且可以在客户端实时看到温度,湿度,火灾检测等数据。
4、火灾报警,当火焰传感器检测到火焰的时候,蜂鸣器会报警。
5、视频监控采用开源mjpg-Streamer来实现的,设置摄像头开机自启动,监控画面可在http://192.168.1.3:8080/?action=stream去看到
6、人脸识别开锁和监控,人脸识别功能是使用的翔云平台的人脸识别解决方案,需要安装libcurl 和 openSSl库来支持https协议,通过系统调用wget http://192.168.1.3:8080/?action=snapshot获取;
InputCommand.h:指令工厂
- #include <stdlib.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <string.h>
- #include <wiringPi.h>
-
- struct InputCommander
- {
- char commandName[128];
- char deviceName[128];//打开的设备名称
- char command[32];//获取指令
- char log[1024];
- int baud;//波特率
- int fd;
- int s_fd;
- char port[12];//端口号
- char ipAddress[32];//ip地址
- char temp[12];
- char hum[12];
- char fireIfOrNot[32];
- int (*Init)(struct InputCommander *pInputCommand);
- int (*getCommand)(struct InputCommander *pInputCommand);
- int (*sendCommand)(struct InputCommander *pInputCommand);
- struct InputCommander *next;
- };
-
- struct InputCommander * addSocketContrlToInputCommand(struct InputCommander * phead);
- struct InputCommander * addVoiceContrlToInputCommand(struct InputCommander * phead);
-
contrlDevices.h(设备工厂):
- #include <wiringPi.h>
-
- 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 (*changeStatus)(int pinNum);
-
- struct Devices *next;
- };
-
- struct Devices * addBathroomLightToDeviceLink(struct Devices *phead);
- struct Devices * addLivingroomLightToDeviceLink(struct Devices *phead);
- struct Devices * addUpstairLightToDeviceLink(struct Devices *phead);
- struct Devices * addRestaurantLightToDeviceLink(struct Devices *phead);
- struct Devices * addFireIfOrNotToDeviceLink(struct Devices *phead);
- struct Devices * addAlertToDeviceLink(struct Devices *phead);
- struct Devices * addLockToDeviceLink(struct Devices *phead);
- struct Devices * addCameraToDeviceLink(struct Devices *phead);
- struct Devices * addCsbToDeviceLink(struct Devices *phead);
- struct Devices * addDht11ToDeviceLink(struct Devices *phead);
socketContrl.c
- #include "InputCommand.h"
- #include <wiringSerial.h>
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <stdlib.h>
- #include <string.h>
-
- 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("socket");
- exit(-1);
- }
- s_addr.sin_family = AF_INET;
- s_addr.sin_port = htons(atoi(socketMes->port));
- inet_aton(socketMes->ipAddress,&s_addr.sin_addr);
- //2. bind
- bind(s_fd,(struct sockaddr *)&s_addr,sizeof(struct sockaddr_in));
- //3. listen
- listen(s_fd,20);
- printf("socket server listening...\n");
- socketMes->s_fd = s_fd;
- return s_fd;
- }
-
-
- struct InputCommander socketContrl={
- .commandName = "socketServer",
- .command={'\0'},
- .port="8888",
- .ipAddress="192.168.1.3",
- .log={'\0',},
- .Init =socketInit,
- .temp={'\0'},
- .hum={'\0'},
- .fireIfOrNot={'\0'}
-
- };
-
- struct InputCommander * addSocketContrlToInputCommand(struct InputCommander * phead)
- {
- if(phead ==NULL){
- return &socketContrl;
- }else{
- socketContrl.next = phead;
- return &socketContrl;
- }
- }
voiceContrl.c
- #include "InputCommand.h"
- #include <wiringSerial.h>
- #include <unistd.h>
- #include <string.h>
- #include <stdlib.h>
- #include <stdio.h>
- #define SIZE 7
-
- char Light_ON[7]={0xFD,0x03,0x55,0x55,0xC0,0x73,0xDF};
- char Light_OFF[7]={0xFD,0x03,0x55,0x55,0x30,0x73,0xDF};
- char CUR_ON[7]={0xFD,0x03,0x55,0x55,0x0C,0x73,0xDF};
- char CUR_OFF[7]={0xFD,0x03,0x55,0x55,0x03,0x73,0xDF};
-
- int voiceGetCommand(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,voicer->baud))==-1){
- perror("serial open error\n");
- exit(-1);
- }
- voicer->fd = fd;
- return fd;
- }
- int voiceSendCommand(struct InputCommander *voicer)
- {
- int n_write=0;
- if(!strcmp(voicer->command,"s o")){
- n_write=write(voicer->fd,Light_ON,7);
-
- }else if(!strcmp(voicer->command,"s c")){
- n_write=write(voicer->fd,Light_OFF,7);
-
- }else if(!strcmp(voicer->command,"c o")){
- n_write=write(voicer->fd,CUR_ON,7);
-
- }else if(!strcmp(voicer->command,"c c")){
- n_write=write(voicer->fd,CUR_OFF,7);
-
- }
- return n_write;
- }
-
- struct InputCommander voiceContrl={
- .commandName = "voice",
- .deviceName="/dev/ttyAMA0",
- .baud=9600,
- .command={'\0'},//指令清零
- .log={'\0'},
- .Init =voiceInit,
- .getCommand = voiceGetCommand,
- .sendCommand= voiceSendCommand,
- .next =NULL
- };
-
- struct InputCommander * addVoiceContrlToInputCommand(struct InputCommander * phead)
- {
- if(phead ==NULL){
- return &voiceContrl;
- }else{
- voiceContrl.next = phead;
- return &voiceContrl;
- }
- }
bathroomLight.c: livingroomLight.c,restaurantLight.c,upstairLight.c同
- #include "contrlDevices.h"
- #include <stdlib.h>
-
- int bathroomLightOpen(int pinNum)
- {
- digitalWrite(pinNum,LOW);
- }
- int bathroomLightClose(int pinNum)
- {
- digitalWrite(pinNum,HIGH);
- }
- int bathroomLightInitCloseInit(int pinNum)
- {
- pinMode(pinNum,OUTPUT);
- digitalWrite(pinNum,HIGH);
- }
-
- struct Devices bathroomLight=
- {
- .deviceName="bathroomLight",
- .pinNum=21,
- .open = bathroomLightOpen,
- .close =bathroomLightClose,
- .deviceInit=bathroomLightInitCloseInit,
- };
-
- struct Devices * addBathroomLightToDeviceLink(struct Devices *phead)
- {
- if(phead == NULL){
- return &bathroomLight;
- }else{
- bathroomLight.next=phead;
- return &bathroomLight;
- }
- }
lock.c
- #include "contrlDevices.h"
- #include <stdlib.h>
-
- int lockOpen(int pinNum)
- {
- pinMode(pinNum,OUTPUT);
- //通电开锁,断电关锁;
- digitalWrite(pinNum,HIGH);
- digitalWrite(pinNum,LOW);
- }
- int lockClose(int pinNum)
- {
- pinMode(pinNum,OUTPUT);
- digitalWrite(pinNum,LOW);
- }
- int lockInit(int pinNum)
- {
- pinMode(pinNum,OUTPUT);
- digitalWrite(pinNum,LOW);
- }
- int lockReadStatus(int pinNum)
- {
- //高电平开,低电平为关
- return digitalRead(pinNum);
- }
- struct Devices lock=
- {
- .deviceName="lock",
- .pinNum=29,
- .open = lockOpen,
- .close =lockClose,
- .deviceInit=lockInit,
- .readStatus=lockReadStatus
- };
-
- struct Devices * addLockToDeviceLink(struct Devices *phead)
- {
- if(phead == NULL){
- return &lock;
- }else{
- lock.next=phead;
- return &lock;
- }
- }
fire.c
- #include "contrlDevices.h"
- #include <stdlib.h>
- #include <unistd.h>
-
- int fireIfOrNotInit(int pinNum)
- {
- pinMode(pinNum,INPUT);
- digitalWrite(pinNum,HIGH);
- }
- int fireStatusRead(int pinNum)
- {
- return digitalRead(pinNum);
- //低电平为有火灾,高电平为无火灾
- }
-
- struct Devices fireIfOrNot=
- {
- .deviceName="fireIfOrNot",
- .pinNum=25,
- .deviceInit=fireIfOrNotInit,
- .readStatus=fireStatusRead,
- };
-
- struct Devices * addFireIfOrNotToDeviceLink(struct Devices *phead)
- {
- if(phead == NULL){
- return &fireIfOrNot;
- }else{
- fireIfOrNot.next=phead;
- return &fireIfOrNot;
- }
- }
csb.c
- #include "contrlDevices.h"
- #include <stdlib.h>
- #include <stdio.h>
- #include <sys/time.h>
-
- #define Trig 27
- #define Echo 28
-
- int csbInit(int pinNum)
- {
- pinMode(Trig,OUTPUT);
- pinMode(Echo,INPUT);
- }
- int getDistance(int pinNum)
- {
- //测量距离:2-400cm;
- struct timeval tv1; //timeval是time.h中的预定义结构体 其中包含两个一个是秒,一个是微秒
-
- struct timeval tv2;
- long start, stop;
- float dis;
-
- digitalWrite(Trig, LOW);
- delayMicroseconds(2);
-
- digitalWrite(Trig, HIGH);
- delayMicroseconds(10); //发出超声波脉冲
- digitalWrite(Trig, LOW);
-
- while(digitalRead(Echo) != 1);
-
- gettimeofday(&tv1, NULL); //获取当前时间 开始接收到返回信号的时候
-
- while(digitalRead(Echo) != 0);
- gettimeofday(&tv2, NULL); //获取当前时间 最后接收到返回信号的时候
-
- start = tv1.tv_sec * 1000000 + tv1.tv_usec; //微秒级的时间
- stop = tv2.tv_sec * 1000000 + tv2.tv_usec;
-
- dis = (float)(stop - start) / 1000000 * 34000 / 2; //计算时间差求出距离
- printf("dis=%.2f cm\n",dis);
- if(dis>100){
- return 1;
- }else{
- return 0;
- }
-
- }
- struct Devices csb=
- {
- .deviceName="csb",
- .deviceInit=csbInit,
- .readStatus=getDistance//大于1米,返回1,小于1米 返回0;
- };
-
- struct Devices * addCsbToDeviceLink(struct Devices *phead)
- {
- if(phead == NULL){
- return &csb;
- }else{
- csb.next=phead;
- return &csb;
- }
- }
dht11.c
- #include "contrlDevices.h"
- #include <stdlib.h>
- #include <stdio.h>
- #include <unistd.h>
-
- unsigned long int databuf;
-
- int DHT11_start(int pinNum)
- {
- pinMode(pinNum, OUTPUT); // set mode to output
- digitalWrite(pinNum, HIGH); // output a high level
-
- sleep(1);
- pinMode(pinNum, OUTPUT);
- digitalWrite(pinNum, LOW);
-
- delay(25);
-
- digitalWrite(pinNum, HIGH);
- pinMode(pinNum, INPUT);
- pullUpDnControl(pinNum, PUD_UP); //当引脚被配置为输入(INPUT)模式,使用函数pullUpDnControl来激活其内部的上拉电阻或下拉电阻
-
- delayMicroseconds(27);
-
- return 1;
- }
-
- /*
- //主机接受数据
- 1.主机接受到从机回复的响应信号
- 2.格式0——54us的低电平+23到27us的高电平
- 格式1——54us的低电平+68到74us的高电平
- 3.思路:从识别到低电平开始,然后去除掉掉前面54秒的低电平还有
- */
- int DHT11_read(int pinNum)
- {
- int crc, i;
-
- if (0 == digitalRead(pinNum)) //主机接收到从机发送的响应信号(低电平)
- {
- while(!digitalRead(pinNum)); //主机接收到从机发送的响应信号(高电平)
-
- for (i = 0; i < 32; i++)
- {
- while(digitalRead(pinNum)); //数据位开始的54us低电平
- while(!digitalRead(pinNum)); //数据位开始的高电平就开始
-
- delayMicroseconds(32); //跳过位数据,32us已经是数据0和数据1的差距点
-
- databuf *= 2;
-
- if (digitalRead(pinNum) == 1)
- {
- databuf++;
- }
- }
-
- for (i = 0; i < 8; i++)
- {
- while (digitalRead(pinNum));
- while (!digitalRead(pinNum));
-
- delayMicroseconds(32);
-
- crc *= 2;
- if (digitalRead(pinNum) == 1)
- {
- crc++;
- }
- }
- return 1;
- }
- else
- {
- return 0;
- }
- }
-
- struct Devices dht11=
- {
- .deviceName="dht11",
- .pinNum=0,
- .deviceInit=DHT11_start,
- .readStatus=DHT11_read
- };
-
- struct Devices * addDht11ToDeviceLink(struct Devices *phead)
- {
- if(phead == NULL){
- return &dht11;
- }else{
- dht11.next=phead;
- return &dht11;
- }
- }
camera.c
- #include "contrlDevices.h"
- #include <stdlib.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <curl/curl.h>
- #include <string.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
-
-
- char buf[10240]={'\0'};
-
- size_t readData( void *ptr, size_t size, size_t nmemb, void *stream)
- {
-
- strncpy(buf,ptr,1024);
- }
-
- char * base64Img(char *filename)
- {
- char cmd[128];
- char *bufPic;
- sprintf(cmd,"base64 %s >tmpFile",filename);
- system(cmd);
-
- int fd = open("./tmpFile",O_RDWR);
- int filesize = lseek(fd,0,SEEK_END);
- lseek(fd,0,SEEK_SET);
- bufPic=(char *)malloc(sizeof(char)*filesize+2);
- memset(bufPic,'\0',filesize+2);
-
- read(fd,bufPic,filesize);
- close(fd);
- system("rm -f tmpFile");
- return bufPic;
-
- }
- int postUrl(int pinNum)
- {
- CURL *curl;
- CURLcode res;
- int status;
- char *postString;
- char *key="PkRgwW5TLdU2kqLDpncZvz";
- char *secret="2977c55e431f487194e5eea44a804328";
- int typeId = 21;
- char *format="xml";
-
- char *img1=base64Img("./person1.jpg");
- char *img2 = base64Img("./person2.jpg");
- int len = strlen(key)+strlen(secret)+strlen(img1)+strlen(img2)+strlen(format);
- postString =(char *)malloc(sizeof(char)*(len+124));
- memset(postString,'\0',len+124);
-
- sprintf(postString,"&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_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);
- res = curl_easy_perform(curl);
-
- if(strstr(buf,"是")!=NULL){
- printf("camera:same person\n");
- status=1;
- }else{
- printf("camera:diff person\n");
- status =0;
- }
- curl_easy_cleanup(curl);
- }
- return status;
- }
-
- int camera_readStatus(int pinNum)
- {
- int status;
- system(" wget -O ./person1.jpg http://192.168.1.3:8080/?action=snapshot");
- printf("take ptoto success\n");
- status = postUrl(pinNum);
- return status;
- }
-
-
- struct Devices camera=
- {
- .deviceName="camera",
- .readStatus=camera_readStatus//1是同一个人,开锁,0不是同一个人,关锁;
- };
-
- struct Devices * addCameraToDeviceLink(struct Devices *phead)
- {
- if(phead == NULL){
- return &camera;
- }else{
- camera.next=phead;
- return &camera;
- }
- }
主程序MainPro.c
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <arpa/inet.h>
- #include "contrlDevices.h"
- #include "InputCommand.h"
- #include <wiringSerial.h>
-
-
- extern unsigned long int databuf;
-
- struct Devices *pdeviceHead = NULL;
- struct InputCommander *pCommandHead=NULL;
- struct InputCommander *socketHandler;
- struct InputCommander *voiceHandler;
- struct Devices *fireHandler;
- int c_fd;
-
- struct Devices * findDevicesByName(char *str,struct Devices * phead)
- {
- struct Devices *tmp = phead;
- if(tmp == NULL){
- printf("find devices error\n");
- return NULL;
- }else{
- while(tmp!=NULL){
- if(!strcmp(tmp->deviceName,str)){
- return tmp;
- }
- tmp=tmp->next;
- }
- }
- }
- struct InputCommander * findInputCommanderByName(char *str,struct InputCommander * phead)
- {
- struct InputCommander *tmp = phead;
- if(tmp == NULL){
- printf("find InputCommander error\n");
- return NULL;
- }else{
- while(tmp!=NULL){
- if(!strcmp(tmp->commandName,str)){
- return tmp;
- }
- tmp=tmp->next;
- }
- }
- }
-
- void openDevices(char *deviceName)
- {
- struct Devices *deviceHandler;
- deviceHandler=findDevicesByName(deviceName,pdeviceHead);
- deviceHandler->deviceInit(deviceHandler->pinNum);
- deviceHandler->open(deviceHandler->pinNum);
- }
- void closeDevices(char *deviceName)
- {
- struct Devices *deviceHandler;
- deviceHandler=findDevicesByName(deviceName,pdeviceHead);
- deviceHandler->deviceInit(deviceHandler->pinNum);
- deviceHandler->close(deviceHandler->pinNum);
- }
- void cmdContrlLight(char cmd[12])
- {
- if(!strcmp(cmd,"b o")){
- openDevices("bathroomLight");
- }else if(!strcmp(cmd,"b c")){
- closeDevices("bathroomLight");
- }else if(!strcmp(cmd,"l o")){
- openDevices("livingroomLight");
- }else if(!strcmp(cmd,"l c")){
- closeDevices("livingroomLight");
- }else if(!strcmp(cmd,"r o")){
- openDevices("restaurantLight");
- }else if(!strcmp(cmd,"r c")){
- closeDevices("restaurantLight");
- }else if(!strcmp(cmd,"u o")){
- openDevices("upstairLight");
- }else if(!strcmp(cmd,"u c")){
- closeDevices("upstairLight");
- }
- }
-
- void *read_thread(void *datas)
- {
- int n_read;
- 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");
- }else if(n_read>0){
- printf("socket get cmd: %s\n",socketHandler->command);
- cmdContrlLight(socketHandler->command);
- }else{
- printf("client quit\n");
- break;
- }
- }
- }
- void *write_thread(void *datas)
- {
- struct Devices *dht11Handler;
- int n_write;
- char message[128]={'\0'};
- dht11Handler = findDevicesByName("dht11",pdeviceHead);
-
- while(1){
-
- dht11Handler->deviceInit(dht11Handler->pinNum);
- dht11Handler->readStatus(dht11Handler->pinNum);
-
- memset(socketHandler->hum,'\0',12);
- memset(socketHandler->temp,'\0',12);
- memset(socketHandler->fireIfOrNot,'\0',32);
- //低电平为有火灾,高电平为无火灾
- if(fireHandler->readStatus(fireHandler->pinNum)){
- strcpy(socketHandler->fireIfOrNot,"fire watching...");
- }else{
- strcpy(socketHandler->fireIfOrNot,"fire warning!!!Attention!");
- }
-
- sprintf(socketHandler->temp,"%d.%d°C", (databuf >> 8) & 0xff, databuf & 0xff);
- sprintf(socketHandler->hum,"%d.%d%rh",(databuf >> 24) & 0xff, (databuf >> 16) & 0xff);
-
- sprintf(message,"%s,%s,%s",socketHandler->temp,socketHandler->hum,socketHandler->fireIfOrNot);
- printf("message:%s\n",message);
- write(c_fd,message,strlen(message));
- sleep(10);
- }
- }
-
- void *socket_thread(void *datas)
- {
- pthread_t readThread;
- pthread_t writeThread;
-
- struct sockaddr_in c_addr;
-
- int n_read = 0;
- int clen = sizeof(struct sockaddr_in);
- memset(&c_addr,0,clen);
- socketHandler=findInputCommanderByName("socketServer",pCommandHead);
- if(socketHandler==NULL){
- printf("find socketHandler error\n");
- pthread_exit(NULL);
- }else{
- socketHandler->Init(socketHandler);
- printf("%s init success\n",socketHandler->commandName);
- while(1){
- //不断接收新的客户端的接入,接入新的客户端后创建新的读线程和写线程:
- c_fd = accept(socketHandler->s_fd,(struct sockaddr *)&c_addr,&clen);
- if(c_fd == -1){
- perror("accept");
- }
- pthread_create(&readThread,NULL,read_thread,NULL);
- pthread_create(&writeThread,NULL,write_thread,NULL);
- }
- }
-
- }
-
- void *voice_thread(void *datas)
- {
-
- int nread;
- voiceHandler=findInputCommanderByName("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);
- }
- while(1){
- nread=voiceHandler->getCommand(voiceHandler);
- if(nread==0){
- //printf("no data from voice\n");
- }else{
- printf("voice get cmd: %s\n",voiceHandler->command);
- voiceHandler->sendCommand(voiceHandler);
- cmdContrlLight(voiceHandler->command);
- }
- }
-
- }
- }
- void *fire_thread(void *datas)
- {
-
- struct Devices *alertHandler;
-
- fireHandler=findDevicesByName("fireIfOrNot",pdeviceHead);
- alertHandler=findDevicesByName("alert", pdeviceHead);
- fireHandler->deviceInit(fireHandler->pinNum);
- alertHandler->deviceInit(alertHandler->pinNum);
-
- while(1)
- {
- int status;
- status = fireHandler->readStatus(fireHandler->pinNum);
- if(!status){
- //有火灾,打开报警器:
- alertHandler->open(alertHandler->pinNum);
- }else{
- //无火灾:关闭报警器:
- alertHandler->close(alertHandler->pinNum);
- }
- }
- }
- void *camera_thread(void * datas)
- {
-
- struct Devices *cameraHandler;
- struct Devices *lockHandler;
- struct Devices *csbHandler;
-
- cameraHandler=findDevicesByName("camera",pdeviceHead );
- lockHandler=findDevicesByName("lock",pdeviceHead);
- csbHandler=findDevicesByName("csb",pdeviceHead);
-
- lockHandler->deviceInit(lockHandler->pinNum);
- csbHandler->deviceInit(csbHandler->pinNum);
-
-
- while(1){
- //距离大于1米,返回1;
- while(! csbHandler->readStatus( csbHandler->pinNum)){
-
- printf("dis<1m,taking photo...\n");
- lockHandler->status=lockHandler->readStatus(lockHandler->pinNum);//检测锁的状态
- cameraHandler->status=cameraHandler->readStatus(cameraHandler->pinNum);//对比人脸;
-
- //1:是同一个人,开 锁,0:不是同一个人,关锁;
- //1:锁是开状态,0:锁是关闭状态;
- if( cameraHandler->status && !lockHandler->status){
- lockHandler->open(lockHandler->pinNum);
- printf("open lock\n");
- }
-
- }
- sleep(10);//每10秒检测一次距离,距离小于1米就拍照对比
- }
-
- }
-
- int main()
- {
- pthread_t voiceThread;
- pthread_t socketThread;
- pthread_t fireIfOrNotThread;
- pthread_t cameraThread;
-
-
- if(wiringPiSetup()==-1){
- printf("wiringPi init error\n");
- exit(-1);
- }
- //1.指令工厂初始化
- pCommandHead= addSocketContrlToInputCommand(pCommandHead);
- pCommandHead=addVoiceContrlToInputCommand(pCommandHead);
-
- //2.设备控制工厂初始化
- pdeviceHead=addBathroomLightToDeviceLink(pdeviceHead);
- pdeviceHead=addLivingroomLightToDeviceLink(pdeviceHead);
- pdeviceHead=addUpstairLightToDeviceLink(pdeviceHead);
- pdeviceHead=addRestaurantLightToDeviceLink(pdeviceHead);
- pdeviceHead=addFireIfOrNotToDeviceLink(pdeviceHead);
- pdeviceHead=addAlertToDeviceLink(pdeviceHead);
- pdeviceHead=addLockToDeviceLink(pdeviceHead);
- pdeviceHead=addCameraToDeviceLink(pdeviceHead);
- pdeviceHead=addCsbToDeviceLink(pdeviceHead);
- pdeviceHead=addDht11ToDeviceLink(pdeviceHead);
-
- //3.线程池建立
- //3.1语音线程
- pthread_create(&voiceThread,NULL, voice_thread,NULL);
- //3.2socket 线程
- pthread_create(&socketThread,NULL, socket_thread,NULL);
- //3.3摄像头线程
- pthread_create(&cameraThread,NULL,camera_thread,NULL);
- //3.4火灾线程
- pthread_create(&fireIfOrNotThread,NULL,fire_thread,NULL);
-
- //等待线程退出:
- pthread_join(voiceThread,NULL);
- pthread_join(socketThread,NULL);
- pthread_join(fireIfOrNotThread,NULL);
- pthread_join(cameraThread,NULL);
-
- return 0;
- }
(37条消息) 智能家居Android设计_lelebanaba的博客-CSDN博客w
智能家居
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。