基于树莓派的智能家居

一、项目功能介绍

本项目主要采用了简单工厂模式的设计方式,通过链表将各个模块连接起来,当要使用某个模块时,只需要使用链表遍历,找到所需模块去调用,各模块功能稳定性,拓展性更强,提高了整个项目系统代码的稳定性和拓展性。项目主要分为设备工厂和指令工厂,实现网络和语音控制家电。

项目主要功能:

1、通过手机端网络调试助手,语音识别,对单独房间灯光、一、二楼灯光,全部灯光进行综合智能远程控制。

2、通过火焰报警、视频监控等功能保证家庭的安全。(可加入震动报警、烟雾报警,红外感应人体提示、原理都大同小异)

3、通过433M无线射频在无网络情况下对部分家电进行控制。(时间原因,本次只对游泳池进行控制)

4、通过翔云平台提供的人脸对比识别服务,实现人脸识别功能,可用于人脸识别开锁功能。

硬件部分包含主控芯片树莓派3B,SU-03T离线语音模块,时光公寓模型,继电器组,继电器,433M无线射频模块,蜂鸣器,树莓派摄像头,电源,LED灯,火焰传感器,电源。

二、实物效果

整体效果

基于树莓派的智能家居_第1张图片

火灾测试

基于树莓派的智能家居_第2张图片

无线射频控制测试

基于树莓派的智能家居_第3张图片

三、设计思路

流程图:

基于树莓派的智能家居_第4张图片

程序构建(建议用source insight写完传输上树莓派)

基于树莓派的智能家居_第5张图片

四、程序设计

主程序mainPro.c

#include 
#include "contrlDevices.h"
#include "inputCommand.h"
#include 
#include 
#include 
#include 
#include           
#include 
#include 
#include 
//433M引脚定义
#define D0 4
#define D1 5

int c_fd;
struct Devices *pdeviceHead = NULL;
struct InputCommander *pCommandHead = NULL;
struct Devices *deviceTmp;
struct InputCommander *commandTmp;

struct InputCommander *socketHandler;
//寻找设备工厂设备
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;
    }


}
//寻找指令工厂指令
struct InputCommander *findCommandByName(char *name,struct InputCommander *phead)
{
    struct InputCommander *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 *voice_thread(void *datas)
{
    struct InputCommander *voiceHandler;
    int nread;//接收串口字节数
    
    
    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);
        }else{
            //初始化语音
            printf("%s init success\n",voiceHandler->commandName);
        }
        //不断接收数据命令
        while(1){
            nread = voiceHandler->getCommand(voiceHandler);
            printf("do divece contrl:0x%s\n",voiceHandler->command);
    
            if(nread == 0){
                printf("nodata from voice\n");
            }else if(strstr(voiceHandler->command,"P") != NULL){
                deviceTmp=findDeviceByName("bathroomLight",pdeviceHead);
                deviceTmp->deviceInit(deviceTmp->pinNum);
                deviceTmp->open(deviceTmp->pinNum);//开浴室灯
            }else if(strstr(voiceHandler->command,"Q") != NULL){
                deviceTmp=findDeviceByName("livingroomLight",pdeviceHead);
                deviceTmp->deviceInit(deviceTmp->pinNum);
                deviceTmp->open(deviceTmp->pinNum);//开客厅灯
            }else if(strstr(voiceHandler->command,"R") != NULL){
                deviceTmp=findDeviceByName("upstairLight",pdeviceHead);
                deviceTmp->deviceInit(deviceTmp->pinNum);
                deviceTmp->open(deviceTmp->pinNum);//开卧室灯
            }else if(strstr(voiceHandler->command,"S") != NULL){
                deviceTmp=findDeviceByName("restaurantLight",pdeviceHead);
                deviceTmp->deviceInit(deviceTmp->pinNum);
                deviceTmp->open(deviceTmp->pinNum);//开餐厅灯
            }else if(strstr(voiceHandler->command,"T") != NULL){
                deviceTmp=findDeviceByName("poolLight",pdeviceHead);
                deviceTmp->deviceInit(deviceTmp->pinNum);
                deviceTmp->open(deviceTmp->pinNum);//开泳池灯
            }else if(strstr(voiceHandler->command,"U") != NULL){
                deviceTmp=findDeviceByName("bathroomLight",pdeviceHead);
                deviceTmp->deviceInit(deviceTmp->pinNum);
                deviceTmp->open(deviceTmp->pinNum);
                //开全部灯
                deviceTmp=findDeviceByName("livingroomLight",pdeviceHead);
                deviceTmp->deviceInit(deviceTmp->pinNum);
                deviceTmp->open(deviceTmp->pinNum);

                deviceTmp=findDeviceByName("upstairLight",pdeviceHead);
                deviceTmp->deviceInit(deviceTmp->pinNum);
                deviceTmp->open(deviceTmp->pinNum);

                deviceTmp=findDeviceByName("restaurantLight",pdeviceHead);
                deviceTmp->deviceInit(deviceTmp->pinNum);
                deviceTmp->open(deviceTmp->pinNum);
                
                deviceTmp=findDeviceByName("poolLight",pdeviceHead);
                deviceTmp->deviceInit(deviceTmp->pinNum);
                deviceTmp->open(deviceTmp->pinNum);
            }else if(strstr(voiceHandler->command,"V") != NULL){
                deviceTmp=findDeviceByName("bathroomLight",pdeviceHead);
                deviceTmp->close(deviceTmp->pinNum);
                    //关全部灯
                deviceTmp=findDeviceByName("livingroomLight",pdeviceHead);
                deviceTmp->close(deviceTmp->pinNum);

                deviceTmp=findDeviceByName("upstairLight",pdeviceHead);
                deviceTmp->close(deviceTmp->pinNum);

                deviceTmp=findDeviceByName("restaurantLight",pdeviceHead);
                deviceTmp->close(deviceTmp->pinNum);
                
                deviceTmp=findDeviceByName("poolLight",pdeviceHead);
                deviceTmp->close(deviceTmp->pinNum);
            }else if(strstr(voiceHandler->command,"W") != NULL){
                deviceTmp=findDeviceByName("bathroomLight",pdeviceHead);
                deviceTmp->deviceInit(deviceTmp->pinNum);
                deviceTmp->open(deviceTmp->pinNum);
                //开二楼灯
                deviceTmp=findDeviceByName("upstairLight",pdeviceHead);
                deviceTmp->deviceInit(deviceTmp->pinNum);
                deviceTmp->open(deviceTmp->pinNum);
                
            }else if(strstr(voiceHandler->command,"X") != NULL){
                deviceTmp=findDeviceByName("bathroomLight",pdeviceHead);
                deviceTmp->close(deviceTmp->pinNum);
                //关二楼灯
                deviceTmp=findDeviceByName("upstairLight",pdeviceHead);
                deviceTmp->close(deviceTmp->pinNum);
                
            }else if(strstr(voiceHandler->command,"Y") != NULL){
                deviceTmp=findDeviceByName("livingroomLight",pdeviceHead);
                deviceTmp->deviceInit(deviceTmp->pinNum);
                deviceTmp->open(deviceTmp->pinNum);
                //开一楼灯
                deviceTmp=findDeviceByName("restaurantLight",pdeviceHead);
                deviceTmp->deviceInit(deviceTmp->pinNum);
                deviceTmp->open(deviceTmp->pinNum);
                
            }else if(strstr(voiceHandler->command,"`") != NULL){
                deviceTmp=findDeviceByName("livingroomLight",pdeviceHead);
                deviceTmp->close(deviceTmp->pinNum);
                //关一楼灯
                deviceTmp=findDeviceByName("restaurantLight",pdeviceHead);
                deviceTmp->close(deviceTmp->pinNum);
                
            }
            
            
        }
    }
}
//网络控制线程
void *read_thread(void * datas)
{
    int n_read;
    //struct InputCommander *socketHandler=NULL;
    //清空命令接收区,方便下次检测
    memset(socketHandler->command, '\0', sizeof(socketHandler->command));
    n_read=read(c_fd,socketHandler->command,sizeof(socketHandler->command));
    printf("nget: %d,%s\n",n_read,socketHandler->command);    
    
    if(n_read == -1){
        perror("read");            
    }else if(strcmp(socketHandler->command,"bath") == 0){
                deviceTmp=findDeviceByName("bathroomLight",pdeviceHead);
                deviceTmp->deviceInit(deviceTmp->pinNum);
                deviceTmp->open(deviceTmp->pinNum);//开浴室灯
    }else if(strcmp(socketHandler->command,"bath1") == 0){
                deviceTmp=findDeviceByName("bathroomLight",pdeviceHead);
                deviceTmp->close(deviceTmp->pinNum);//关浴室灯
    }else if(strcmp(socketHandler->command,"live") == 0){
                deviceTmp=findDeviceByName("livingroomLight",pdeviceHead);
                deviceTmp->deviceInit(deviceTmp->pinNum);
                deviceTmp->open(deviceTmp->pinNum);//开客厅灯
    }else if(strcmp(socketHandler->command,"live1") == 0){
                deviceTmp=findDeviceByName("livingroomLight",pdeviceHead);
                deviceTmp->close(deviceTmp->pinNum);//关浴室灯
    }else if(strcmp(socketHandler->command,"upstair") == 0){
                deviceTmp=findDeviceByName("upstairLight",pdeviceHead);
                deviceTmp->deviceInit(deviceTmp->pinNum);
                deviceTmp->open(deviceTmp->pinNum);//开卧室灯
    }else if(strcmp(socketHandler->command,"upstair1") == 0){
                deviceTmp=findDeviceByName("upstairLight",pdeviceHead);
                deviceTmp->close(deviceTmp->pinNum);//关卧室灯
    }else if(strcmp(socketHandler->command,"rest") == 0){
                deviceTmp=findDeviceByName("restaurantLight",pdeviceHead);
                deviceTmp->deviceInit(deviceTmp->pinNum);
                deviceTmp->open(deviceTmp->pinNum);//开客厅灯
    }else if(strcmp(socketHandler->command,"rest1") == 0){
                deviceTmp=findDeviceByName("restaurantLight",pdeviceHead);
                deviceTmp->close(deviceTmp->pinNum);//关客厅灯
    }else if(strcmp(socketHandler->command,"pool")  == 0){
                deviceTmp=findDeviceByName("poolLight",pdeviceHead);
                deviceTmp->deviceInit(deviceTmp->pinNum);
                deviceTmp->open(deviceTmp->pinNum);//开泳池灯
    }else if(strcmp(socketHandler->command,"pool1") == 0){
                deviceTmp=findDeviceByName("poolLight",pdeviceHead);
                deviceTmp->close(deviceTmp->pinNum);//关泳池灯
    }else if(strcmp(socketHandler->command,"allon") == 0){
                deviceTmp=findDeviceByName("bathroomLight",pdeviceHead);
                deviceTmp->deviceInit(deviceTmp->pinNum);
                deviceTmp->open(deviceTmp->pinNum);//开所有灯

                deviceTmp=findDeviceByName("livingroomLight",pdeviceHead);
                deviceTmp->deviceInit(deviceTmp->pinNum);
                deviceTmp->open(deviceTmp->pinNum);

                deviceTmp=findDeviceByName("upstairLight",pdeviceHead);
                deviceTmp->deviceInit(deviceTmp->pinNum);
                deviceTmp->open(deviceTmp->pinNum);

                deviceTmp=findDeviceByName("restaurantLight",pdeviceHead);
                deviceTmp->deviceInit(deviceTmp->pinNum);
                deviceTmp->open(deviceTmp->pinNum);
                
                deviceTmp=findDeviceByName("poolLight",pdeviceHead);
                deviceTmp->deviceInit(deviceTmp->pinNum);
                deviceTmp->open(deviceTmp->pinNum);
    }else if(strcmp(socketHandler->command,"alloff") == 0){
                deviceTmp=findDeviceByName("bathroomLight",pdeviceHead);
                deviceTmp->close(deviceTmp->pinNum);//关所有灯

                deviceTmp=findDeviceByName("livingroomLight",pdeviceHead);
                deviceTmp->close(deviceTmp->pinNum);

                deviceTmp=findDeviceByName("upstairLight",pdeviceHead);
                deviceTmp->close(deviceTmp->pinNum);

                deviceTmp=findDeviceByName("restaurantLight",pdeviceHead);
                deviceTmp->close(deviceTmp->pinNum);
                
                deviceTmp=findDeviceByName("poolLight",pdeviceHead);
                deviceTmp->close(deviceTmp->pinNum);
    }else if(strcmp(socketHandler->command,"second") == 0){
                deviceTmp=findDeviceByName("bathroomLight",pdeviceHead);
                deviceTmp->deviceInit(deviceTmp->pinNum);
                deviceTmp->open(deviceTmp->pinNum);//开二楼灯

                deviceTmp=findDeviceByName("upstairLight",pdeviceHead);
                deviceTmp->deviceInit(deviceTmp->pinNum);
                deviceTmp->open(deviceTmp->pinNum);
                
    }else if(strcmp(socketHandler->command,"second1") == 0){
                deviceTmp=findDeviceByName("bathroomLight",pdeviceHead);
                deviceTmp->close(deviceTmp->pinNum);//关二楼灯

                deviceTmp=findDeviceByName("upstairLight",pdeviceHead);
                deviceTmp->close(deviceTmp->pinNum);
                
    }else if(strcmp(socketHandler->command,"first") == 0){
                deviceTmp=findDeviceByName("livingroomLight",pdeviceHead);
                deviceTmp->deviceInit(deviceTmp->pinNum);
                deviceTmp->open(deviceTmp->pinNum);//开一楼灯

                deviceTmp=findDeviceByName("restaurantLight",pdeviceHead);
                deviceTmp->deviceInit(deviceTmp->pinNum);
                deviceTmp->open(deviceTmp->pinNum);
                
    }else if(strcmp(socketHandler->command,"first1") == 0){
                deviceTmp=findDeviceByName("livingroomLight",pdeviceHead);
                deviceTmp->close(deviceTmp->pinNum);//关一楼灯

                deviceTmp=findDeviceByName("restaurantLight",pdeviceHead);
                deviceTmp->close(deviceTmp->pinNum);
                
    }else{
        printf("client quit\n");
        
    }        
}


void *socket_thread(void * datas)
{
    
    
    int n_read = 0;
    pthread_t readthread;
    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(&readthread, NULL , read_thread ,NULL);

    }
}
//视频监控线程
void *monitor_thread(void * datas)
{
    //用system函数运行监控脚本,开启监控
    system("/home/pi/mjpg-streamer/mjpg-streamer-experimental/start.sh");
    pthread_exit(NULL);
}
//火灾报警线程
void *fireAlarm_thread(void * datas)
{
    int firestatus;
    struct Devices *fireTmp = NULL;
    struct Devices *alarmTmp = NULL;

    fireTmp = findDeviceByName("fireIfOrNot",pdeviceHead);//从设备工厂找到火灾设备
    alarmTmp = findDeviceByName("alarm",pdeviceHead);//从设备工厂找到警报设备

    fireTmp->deviceInit(fireTmp->pinNum);//初始化火灾设备
    alarmTmp->deviceInit(alarmTmp->pinNum);//初始化警报设备
    
    while(1){
        firestatus = fireTmp->readStatus(fireTmp->pinNum);//读取火灾设备的引脚电平
        if(firestatus == 0){    
            alarmTmp->deviceInit(alarmTmp->pinNum);
            alarmTmp->open(alarmTmp->pinNum);
            printf("fire alarm!!!\n");
            delay(1000);
        }else if(firestatus == 1){
            printf("No fire!!!\n");
            alarmTmp->close(alarmTmp->pinNum);
            delay(1000);
        }



    }
    
}
//无线射频控制线程
void *RFID_thread(void * datas)
{
    struct Devices *rfidTmp = NULL;
    int rfidStatus1;
    int rfidStatus2;
    //初始化433M射频控制
    wiringPiSetup();
    pinMode(D0,INPUT);
    pinMode(D1,INPUT);

    digitalWrite(D0, LOW);
    digitalWrite(D1, LOW);

    printf("init success!\n");
    
    while(1){
        delay(10);
        rfidStatus1 = digitalRead(D0); //读取433M的引脚电平
        rfidStatus2 = digitalRead(D1);
        if(rfidStatus1 == 1){
            rfidTmp=findDeviceByName("poolLight",pdeviceHead);
            rfidTmp->deviceInit(rfidTmp->pinNum);
            rfidTmp->open(rfidTmp->pinNum);
            
        }else if(rfidStatus2 == 1){
            rfidTmp=findDeviceByName("poolLight",pdeviceHead);
            rfidTmp->close(rfidTmp->pinNum);        
            }
        rfidStatus1 = 10;
        rfidStatus2 = 10;
    }
}
//人脸识别功能线程
void *camera_Thread(void *data)
{
    struct Devices *cameraTemp = NULL;
    cameraTemp = findDeviceByName("camera", pdeviceHead);//从设备工程找到人脸识别

    if (cameraTemp == NULL){
        printf("find camera error\n");
        pthread_exit(NULL);
    }
    cameraTemp->justDoOnce();//找到人脸识别设备调用函数
}

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

    pthread_t voiceThread;
    pthread_t socketThread;
    pthread_t monitorThread;
    pthread_t fireAlarmThread;
    pthread_t RfidThread;
    pthread_t cameraThread;
    //wiring库初始化
    if(wiringPiSetup() == -1){
                    printf("wiringPi setup error!\n");
                    return -1;
    }
    
    
    //1.指令工厂初始化
    pCommandHead = addvoiceContrlLightToDeviceLink(pCommandHead);
    pCommandHead = addsocketContrlLightToDeviceLink(pCommandHead);
    //2.设备控制初始化
    pdeviceHead = addBathroomLightToDeviceLink(pdeviceHead);
    pdeviceHead = addUpstairLightToDeviceLink(pdeviceHead);
    pdeviceHead = addLivingroomLightToDeviceLink(pdeviceHead);
    pdeviceHead = addRestaurantLightToDeviceLink(pdeviceHead);
    pdeviceHead = addFireToDeviceLink(pdeviceHead);
    pdeviceHead = addpoolLightToDeviceLink(pdeviceHead);
    pdeviceHead = addAlarmToDeviceLink(pdeviceHead);
    pdeviceHead = addcameraToDeviceLink(pdeviceHead);
    //3.线程池建立
    //3.1 语音线程
    pthread_create(&voiceThread, NULL , voice_thread ,NULL);

    //3.2 socket线程
    pthread_create(&socketThread, NULL , socket_thread ,NULL);
    //3.3 监控摄像头线程
    pthread_create(&monitorThread, NULL , monitor_thread ,NULL);
    //3.4 火灾线程
    pthread_create(&fireAlarmThread, NULL , fireAlarm_thread ,NULL);
    //3.5 433M线程
    pthread_create(&RfidThread, NULL , RFID_thread ,NULL);
    //3.6 人脸识别线程
    pthread_create(&cameraThread, NULL , camera_Thread ,NULL);
    
    //等待线程退出
    pthread_join(voiceThread,NULL);    
    pthread_join(socketThread,NULL);
    pthread_join(monitorThread,NULL);
    pthread_join(fireAlarmThread,NULL);    
    pthread_join(RfidThread,NULL);    
    pthread_join(cameraThread,NULL);    
        
    
    return  0;
}

设备工厂头文件controlDevice.h

#include 
#include 
#include 
//C语言中没有布尔类型,这里需要用typedef定义布尔类型
typedef unsigned int bool;


struct Devices
{
    char deviceName[128];
    int status;
    int pinNum;
    int pinNum1;
    int (*open)(int pinNum);//函数指针
    int (*close)(int pinNum);
    int (*deviceInit)(int pinNum);
    
    int (*readStatus)(int pinNum);
    int (*changeStatus)(int status);

    bool (*justDoOnce)();
    char *(*getFace)();
    char *(*getPicFromOCRBase64)();
    size_t (*readData)();

    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 *addpoolLightToDeviceLink(struct Devices *phead);

struct Devices* addAlarmToDeviceLink(struct Devices* phead);

struct Devices* addcameraToDeviceLink(struct Devices* phead);

命令工厂头文件inputCommand.h

#include 
#include 


struct InputCommander
{
    char commandName[128];
    char deviceName[128];
    char command[32];
    int (*Init)(struct InputCommander *voicer, char *ipAdress, char *port);
    int (*getCommand)(struct InputCommander *voicer);
    char log[1024];
    int fd;
    char port[12];
    char ipAddress[32];
    int sfd;
    struct InputCommander *next;
    
};

//函数声明
struct InputCommander* addvoiceContrlLightToDeviceLink(struct InputCommander* phead);

struct InputCommander* addsocketContrlLightToDeviceLink(struct InputCommander* phead);

浴室灯Bathromlight.c(四个房间小灯类似,这里只展示一个)

#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",
    .pinNum = 22,
    .open = bathroomLightOpen,
    .close = bathroomLightClose,
    .deviceInit = bathroomLightCloseInit,
    .changeStatus = bathroomLightCloseStatus
    
};
//头插法
struct Devices* addBathroomLightToDeviceLink(struct Devices* phead)
{
    if(phead == NULL){
        return &bathroomLight;        
    }else{
    
        bathroomLight.next = phead;
        phead = &bathroomLight;
    }

}


泳池灯PoolLight.c

#include "contrlDevices.h"


int poolLightClose(int pinNum)
{
    
    digitalWrite(pinNum,HIGH);    

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

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

}

struct Devices poolLight = {
    .deviceName = "poolLight",
    .pinNum = 7,
    .open = poolLightOpen,
    .close = poolLightClose,
    .deviceInit = poolLightCloseInit,
    .next = NULL

};

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

}

火灾Fire.c

#include "contrlDevices.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* addFireToDeviceLink(struct Devices* phead)
{
    if(phead == NULL){
        return &fireIfOrNot;        
    }else{
    
        fireIfOrNot.next = phead;
        phead = &fireIfOrNot;
    }

}

警报Alarm.c

#include "contrlDevices.h"


int alarmIfOrNotInit(int pinNum)
{
    pinMode(pinNum,OUTPUT);
    digitalWrite(pinNum,HIGH);

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

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

}


struct Devices alarm = {

    .deviceName = "alarm",
    .pinNum = 2,
    .deviceInit = alarmIfOrNotInit,
    .open = alarmOpen,
    .close = alarmClose
};
struct Devices* addAlarmToDeviceLink(struct Devices* phead)
{
    if(phead == NULL){
        return &alarm;        
    }else{
    
        alarm.next = phead;
        phead = &alarm;
    }

}

人脸识别camera.c

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "contrlDevices.h"

#define true 1
#define false 0

typedef unsigned int bool;

char buf[1024] = {'\0'};//全局变量,用来接收从OCR后台返回的数据
char *getFace1();
char* getPicBase64FromFile(char *filePath);
//回调函数,把从后台的数据拷贝给buf
size_t read_Data( void *ptr, size_t size, size_t nmemb, void *stream)
{
    strncpy(buf,ptr,1024);
    printf("==================================Get data===============================\n");
    printf("%s\n",buf);

}
char *getFace1()
{
        printf("playing photo...\n");
        system("raspistill -q 15 -t 1 -o image.jpg");
        //-q 是图片质量,在0~100之间,我们调成5,压缩图片质量,生成的照片名字为imag.jpg
        //-t 是拍照延时,设定1s后拍照
        while(access("./image.jpg",F_OK) != 0);
        //判断是否拍照完毕
        printf("photo over!\n");

        char *base64BufFaceRec = getPicBase64FromFile("./image.jpg");

        return base64BufFaceRec;//返回刚才拍照的base64
}

char* getPicBase64FromFile(char *filePath)
{
    char *bufPic;
    char cmd[128]={'\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);
    char *bufpic =(char *)malloc(filelen+2);
    memset(bufpic,'\0',filelen+2);
    read(fd,bufpic,filelen);
    close(fd);
    system("rm -f tmpFile");

    return bufpic;
}
bool postUrl()
{
    CURL *curl;
    CURLcode res;
    char *postString;

    char img1[12];
    char img2[12];

    char *key = "***cUojMR5Lbf***";//用自己的
    char *secret = "***0544088f3***";//用自己的
    int typeId = 21;
    char *format = "xml";

    char *bufpic1 = getFace1();
    char *bufpic2 = getPicBase64FromFile("./zyf1.jpg");
    

    int len = strlen(key)+strlen(secret)+strlen(bufpic1)+strlen(bufpic2)+124;
    postString = (char*)malloc(len);//postString是一个指针,不能用sizeof来计算其指向的大小
    memset(postString, '\0' ,len);
    sprintf(postString,"&img1=%s&img2=%s&key=%s&secret=%s&typeId=%d&format=%s",
            bufpic1,bufpic2,key,secret,21,format);

    curl = curl_easy_init();
    if (curl)
    {
        curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "/tmp/cookie.txt"); 
        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, read_Data); 
        res = curl_easy_perform(curl);
    printf("OK:%d\n",res);
    if(strstr(buf,"是") != NULL){
        printf("the same Person\n");
    }else{
        printf("diff Person\n");
    }
        curl_easy_cleanup(curl);
    }
    
    return true;
}


struct Devices camera = {
    .deviceName = "camera",
    .justDoOnce = postUrl,
    .getFace = getFace1,
    .getPicFromOCRBase64 = getPicBase64FromFile,
    .readData = read_Data
};

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

}

网络socket.c

#include 
#include 
#include 
#include 
#include 
#include           
#include 
#include 
#include 
#include 
#include "inputCommand.h"




int socketGetCommand(struct InputCommander *socketMes)
{
    int c_fd;
    int n_read = 0;
    struct sockaddr_in c_addr;
    memset(&c_addr,0,sizeof(struct sockaddr_in));
    int clen=sizeof(struct sockaddr_in);

    c_fd =accept(socketMes->sfd,(struct sockaddr *)&c_addr,&clen);

    n_read=read(c_fd,socketMes->command,sizeof(socketMes->command));
    if(n_read == -1){
        perror("read");            
    }else if(n_read > 0){                
        printf("nget: %d\n",n_read);    
    }else{
        printf("client quit\n");
        
    }                        
    return n_read;
}


int socketInit(struct InputCommander *socketMes, 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("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,10);
    printf("socket Server listening.......\n");
    socketMes->sfd = s_fd;
    
    return s_fd;

}


struct InputCommander socketContrl = {
    .commandName = "socketServer",
    .port = "8080",
    .ipAddress ="192.168.101.43",
    .command = {'\0'},
    .Init = socketInit,
    .getCommand = socketGetCommand,
    .log = {'\0'},
    .next = NULL

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

}

语音控制voiceControl.c

#include 
#include 
#include 
#include 
#include 
#include 
#include "inputCommand.h"

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, char *ipAdress, char *port)
{
    int fd;
    
    if((fd = serialOpen(voicer->deviceName,9600)) == -1){
        perror("./");
        exit(-1);
    }
    voicer->fd = fd;
    return fd;

}



struct InputCommander voiceContrl = {
    .commandName = "voice",
    .deviceName = "/dev/ttyAMA0",
    .command = {'\0'},
    .Init = voiceInit,
    .getCommand = voiceGetCommand,
    .log = {'\0'},
    .next = NULL

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

}

你可能感兴趣的:(智能家居)