一、功能介紹
基于樹莓派的智能家居。智能家居用到的硬件有:樹莓派3B+、SU-03語音識別模塊、pi 攝像頭、繼電器組、小燈、火焰?zhèn)鞲衅?、蜂鳴器、電磁鎖、超聲波測距模塊、DHT11溫濕度檢測模塊,433M射頻編解碼模塊或者紅外模塊,面包板等。
采用了簡單工廠模式的一個設(shè)計方式。穩(wěn)定,拓展性更強(qiáng)。通過工廠創(chuàng)建一個通用的接口,集中管理設(shè)備和指令。
創(chuàng)建了兩個工廠:設(shè)備工廠和指令工廠。
設(shè)備工廠:用于管理臥室燈,餐廳燈,樓梯燈,浴室燈,火焰?zhèn)鞲衅?,蜂鳴器,電磁鎖,超聲波,攝像頭等設(shè)備。初始化的時候,通過鏈表將各個模塊連接起來(頭插法)。在要使用某個模塊時,只需要使用鏈表遍歷,找到所需模塊去調(diào)用功能。
指令工廠:管理串口輸入輸出和socket客戶端的指令輸入和輸出。
具體功能如下:
1、可通過SU-03語音模塊的口令模式,口令+具體控制,通過串口把控制指令傳給樹莓派,來控制客廳、餐廳、二樓、浴室的燈
2、可通過SU-03語音模塊的口令模式,通過串口把控制指令傳給樹莓派,樹莓派再傳遞給射頻模塊(或者紅外模塊)控制窗簾,泳池?zé)?,空調(diào)等。
3、可以通過socket客戶端來發(fā)指令來控制客廳、餐廳、二樓、浴室的燈,可以在socket客戶端看到攝像頭實時監(jiān)控畫面,并且可以在客戶端實時看到溫度,濕度,火災(zāi)檢測等數(shù)據(jù)。
4、火災(zāi)報警,當(dāng)火焰?zhèn)鞲衅鳈z測到火焰的時候,蜂鳴器會報警。
5、視頻監(jiān)控采用開源mjpg-Streamer來實現(xiàn)的,設(shè)置攝像頭開機(jī)自啟動,監(jiān)控畫面可在http://192.168.1.3:8080/?action=stream去看到
6、人臉識別開鎖和監(jiān)控,人臉識別功能是使用的翔云平臺的人臉識別解決方案,需要安裝libcurl 和 openSSl庫來支持https協(xié)議,通過系統(tǒng)調(diào)用wget???http://192.168.1.3:8080/?action=snapshot獲?。?/p>
二、設(shè)計框圖
三、程序?qū)崿F(xiàn)
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];//打開的設(shè)備名稱
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(設(shè)備工廠):
#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);
//通電開鎖,斷電關(guān)鎖;
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)
{
//高電平開,低電平為關(guān)
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);
//低電平為有火災(zāi),高電平為無火災(zāi)
}
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中的預(yù)定義結(jié)構(gòu)體 其中包含兩個一個是秒,一個是微秒
struct timeval tv2;
long start, stop;
float dis;
digitalWrite(Trig, LOW);
delayMicroseconds(2);
digitalWrite(Trig, HIGH);
delayMicroseconds(10); //發(fā)出超聲波脈沖
digitalWrite(Trig, LOW);
while(digitalRead(Echo) != 1);
gettimeofday(&tv1, NULL); //獲取當(dāng)前時間 開始接收到返回信號的時候
while(digitalRead(Echo) != 0);
gettimeofday(&tv2, NULL); //獲取當(dāng)前時間 最后接收到返回信號的時候
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); //當(dāng)引腳被配置為輸入(INPUT)模式,使用函數(shù)pullUpDnControl來激活其內(nèi)部的上拉電阻或下拉電阻
delayMicroseconds(27);
return 1;
}
/*
//主機(jī)接受數(shù)據(jù)
1.主機(jī)接受到從機(jī)回復(fù)的響應(yīng)信號
2.格式0——54us的低電平+23到27us的高電平
格式1——54us的低電平+68到74us的高電平
3.思路:從識別到低電平開始,然后去除掉掉前面54秒的低電平還有
*/
int DHT11_read(int pinNum)
{
int crc, i;
if (0 == digitalRead(pinNum)) //主機(jī)接收到從機(jī)發(fā)送的響應(yīng)信號(低電平)
{
while(!digitalRead(pinNum)); //主機(jī)接收到從機(jī)發(fā)送的響應(yīng)信號(高電平)
for (i = 0; i < 32; i++)
{
while(digitalRead(pinNum)); //數(shù)據(jù)位開始的54us低電平
while(!digitalRead(pinNum)); //數(shù)據(jù)位開始的高電平就開始
delayMicroseconds(32); //跳過位數(shù)據(jù),32us已經(jīng)是數(shù)據(jù)0和數(shù)據(jù)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內(nèi)容
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不是同一個人,關(guān)鎖;
};
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);
//低電平為有火災(zāi),高電平為無火災(zāi)
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){
//不斷接收新的客戶端的接入,接入新的客戶端后創(chuàng)建新的讀線程和寫線程:
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){
//有火災(zāi),打開報警器:
alertHandler->open(alertHandler->pinNum);
}else{
//無火災(zāi):關(guān)閉報警器:
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);//檢測鎖的狀態(tài)
cameraHandler->status=cameraHandler->readStatus(cameraHandler->pinNum);//對比人臉;
//1:是同一個人,開 鎖,0:不是同一個人,關(guān)鎖;
//1:鎖是開狀態(tài),0:鎖是關(guān)閉狀態(tài);
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.設(shè)備控制工廠初始化
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火災(zāi)線程
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設(shè)計_lelebanaba的博客-CSDN博客w文章來源:http://www.zghlxwxcb.cn/news/detail-717042.html
五、項目演示
智能家居文章來源地址http://www.zghlxwxcb.cn/news/detail-717042.html
到了這里,關(guān)于基于樹莓派的智能家居項目及代碼的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!