1、项目实现的功能:
可以通过手机APP和语音控制家电,支持人脸识别开锁,视频监控防盗,以及内设有防火、防震警报。功能比较简单,因为某些模块的缺失,没有做的那么精致。还一个原因就是因为懒。。。
2、用到的模块:
这是一个简单的智能家居项目,主控芯片是树莓派3B-cortexA53,其它则是一些外设小芯片。有语音识别模块、摄像头、5V低电平触发继电器、继电器组合4只、电磁锁、微型震动传感器、火焰传感器、蜂鸣器、4只小型led灯。。。wemosD1和433M射频和红外编解码模块没用上。
树莓派与wemosD1网络通信参考博文点击这里
433M射频博文参考点击这里
红外编解码模块参考博文点击这里
其他的怎么接线我就不说了,可以自己去查。有实力可以玩玩家里的220V家电,不过要注意用电安全。
3、存在的疑点:
第一个:调用视频监控的时候,网页查看能显示视频监控。但是app的视频监控区显示不出来,说是版本太旧,还要自己去修改一下。
第二个:因为全部都是使用线程,当程序运行启动的时候,所有的线程都会去争夺资源运行。在这里每个线程都可以尝试加一个线程锁。但是当人脸识别线程启动的时候它就会先进行一次拍照然后实现人脸识别开锁功能,感觉这样太浪费了资源了。能不能像在小区门口一样的人脸识别开锁一样,它是人脸识别程序一直在运行,当捕捉到人脸的时候再识别进行开锁。不像自己写的这种还要拍照再进行识别比对开锁功能,有时候拍的照片还识别不出来。
后面有时间再修改下。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include"controlDevices.h"
#include"inputCommand.h"
//全局变量,用到线程不要轻易去传参
struct InputCommander *pcommandHead = NULL;
struct Devices *pdeviceHead = NULL;
struct InputCommander *socketHandler = NULL;
int c_fd;
pthread_mutex_t mutex;//定义互斥锁
pthread_t voiceThread;//语音线程
pthread_t socketThread;//服务器线程
pthread_t cameraThread;//人脸识别线程
pthread_t fireThread;//火灾线程
pthread_t shakeThread;//震动线程
pthread_t monitoringThread;//视频监控线程
//摄像头相关,改变返回值命名,因为C语言中没有这样的返回值
#define true 1
#define false 0
typedef unsigned int bool;
char buf[1024] = {'\0'};
//外设设备查询
struct Devices *findDevicesByName(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 *camera_thread(void *data) //人脸识别线程
{
struct Devices *cameraTemp;
cameraTemp = findDevicesByName("camera", pdeviceHead);
if (cameraTemp == NULL)
{
printf("Face recognition query failed\n");
pthread_exit(NULL);
}
cameraTemp->justDoOnce();
}
void *voice_thread(void *datas)//语音线程
{
struct InputCommander *voiceHandler = NULL;
struct Devices *deviceTmp = NULL;
int nread = 0;
voiceHandler = findCommandByName("voice",pcommandHead);
if(voiceHandler == NULL){
printf("Voice device query failed. Procedure\n");
pthread_exit(NULL);
}else{
if(voiceHandler->Init(voiceHandler,NULL,NULL) <0){
printf("Description Voice module initialization failed\n");
pthread_exit(NULL);
}else{
printf("%s init ok\n",voiceHandler->commandName);
}
pthread_mutex_lock(&mutex);//加锁
//这个锁加的意义好像不是很大,因为其他线程并没有加锁。。。
while(1){
memset(voiceHandler->command,'\0',sizeof(voiceHandler->command));
nread = voiceHandler->getCommand(voiceHandler);
if(nread == 0){
printf("nodata from voice,please say it again\n");
}
else if(strstr(voiceHandler->command, "Please say") != NULL){
printf("Please say the first password--->>>:\n");
}
else if(strstr(voiceHandler->command, "Receive") != NULL){
printf("xiaopapa receive\n");
}
else if(strstr(voiceHandler->command, "KCTD") != NULL){
deviceTmp = findDevicesByName("livingroomLight", pdeviceHead);
deviceTmp->deviceInit(deviceTmp->pinNum);
deviceTmp->open(deviceTmp->pinNum);
printf("The dining room lights are on\n");
}
else if(strstr(voiceHandler->command, "GCTD") != NULL){
deviceTmp = findDevicesByName("livingroomLight", pdeviceHead);
deviceTmp->deviceInit(deviceTmp->pinNum);
deviceTmp->close(deviceTmp->pinNum);
printf("The dining room lights are off\n");
}
else if(strstr(voiceHandler->command, "KELD") != NULL){
deviceTmp = findDevicesByName("upstairLight", pdeviceHead);
deviceTmp->deviceInit(deviceTmp->pinNum);
deviceTmp->open(deviceTmp->pinNum);
printf("The lights are on on the second floor\n");
}
else if(strstr(voiceHandler->command, "GELD") != NULL){
deviceTmp = findDevicesByName("upstairLight", pdeviceHead);
deviceTmp->deviceInit(deviceTmp->pinNum);
deviceTmp->close(deviceTmp->pinNum);
printf("The lights are off on the second floor\n");
}
else if(strstr(voiceHandler->command, "KYSD") != NULL){
deviceTmp = findDevicesByName("bathroomLight", pdeviceHead);
deviceTmp->deviceInit(deviceTmp->pinNum);
deviceTmp->open(deviceTmp->pinNum);
printf("The bathroom light is on\n");
}
else if(strstr(voiceHandler->command, "GYSD") != NULL){
deviceTmp = findDevicesByName("bathroomLight", pdeviceHead);
deviceTmp->deviceInit(deviceTmp->pinNum);
deviceTmp->close(deviceTmp->pinNum);
printf("The bathroom light is off\n");
}
else if(strstr(voiceHandler->command, "KYCD") != NULL){
deviceTmp = findDevicesByName("swimmingpoolLight", pdeviceHead);
deviceTmp->deviceInit(deviceTmp->pinNum);
deviceTmp->open(deviceTmp->pinNum);
printf("The pool light is on\n");
}
else if(strstr(voiceHandler->command, "GYCD") != NULL){
deviceTmp = findDevicesByName("swimmingpoolLight", pdeviceHead);
deviceTmp->deviceInit(deviceTmp->pinNum);
deviceTmp->close(deviceTmp->pinNum);
printf("The pool light is off\n");
}
else if (strstr(voiceHandler->command, "KQBD") != NULL)
{
deviceTmp = findDevicesByName("livingroomLight", pdeviceHead);
deviceTmp->deviceInit(deviceTmp->pinNum);
deviceTmp->open(deviceTmp->pinNum);
deviceTmp = findDevicesByName("upstairLight", pdeviceHead);
deviceTmp->deviceInit(deviceTmp->pinNum);
deviceTmp->open(deviceTmp->pinNum);
deviceTmp = findDevicesByName("bathroomLight", pdeviceHead);
deviceTmp->deviceInit(deviceTmp->pinNum);
deviceTmp->open(deviceTmp->pinNum);
deviceTmp = findDevicesByName("swimmingpoolLight", pdeviceHead);
deviceTmp->deviceInit(deviceTmp->pinNum);
deviceTmp->open(deviceTmp->pinNum);
printf("All the lights are on\n");
}
else if (strstr(voiceHandler->command, "GQBD") != NULL)
{
deviceTmp = findDevicesByName("livingroomLight", pdeviceHead);
deviceTmp->deviceInit(deviceTmp->pinNum);
deviceTmp->close(deviceTmp->pinNum);
deviceTmp = findDevicesByName("upstairLight", pdeviceHead);
deviceTmp->deviceInit(deviceTmp->pinNum);
deviceTmp->close(deviceTmp->pinNum);
deviceTmp = findDevicesByName("bathroomLight", pdeviceHead);
deviceTmp->deviceInit(deviceTmp->pinNum);
deviceTmp->close(deviceTmp->pinNum);
deviceTmp = findDevicesByName("swimmingpoolLight", pdeviceHead);
deviceTmp->deviceInit(deviceTmp->pinNum);
deviceTmp->close(deviceTmp->pinNum);
printf("All the lights are off\n");
}
else if(strstr(voiceHandler->command, "DKMS") != NULL){
deviceTmp = findDevicesByName("lock", pdeviceHead);
deviceTmp->deviceInit(deviceTmp->pinNum);
deviceTmp->open(deviceTmp->pinNum);
printf("Monitor enabled\n");
}
else if(strstr(voiceHandler->command, "GBMS") != NULL){
deviceTmp = findDevicesByName("lock", pdeviceHead);
deviceTmp->deviceInit(deviceTmp->pinNum);
deviceTmp->close(deviceTmp->pinNum);
printf("Monitor turned off\n");
}
}
pthread_mutex_unlock(&mutex);//解锁
}
}
void *monitoring_thread(void *datas)//视频监控线程
{
system("./start_web_video.sh");//视频监控脚本
pthread_exit(NULL);
}
void *read_thread(void *datas)//读取客户端发来数据
{
int n_read;
struct Devices *deviceTmp = NULL;
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("APP client read data:%s\n", socketHandler->command);
if(strstr(socketHandler->command, "KCTD") != NULL){
deviceTmp = findDevicesByName("livingroomLight", pdeviceHead);
deviceTmp->deviceInit(deviceTmp->pinNum);
deviceTmp->open(deviceTmp->pinNum);
printf("The dining room lights are on form clienAPP\n");
}
else if(strstr(socketHandler->command, "GCTD") != NULL){
deviceTmp = findDevicesByName("livingroomLight", pdeviceHead);
deviceTmp->deviceInit(deviceTmp->pinNum);
deviceTmp->close(deviceTmp->pinNum);
printf("The dining room lights are off form clienAPP\n");
}
else if(strstr(socketHandler->command, "KELD") != NULL){
deviceTmp = findDevicesByName("upstairLight", pdeviceHead);
deviceTmp->deviceInit(deviceTmp->pinNum);
deviceTmp->open(deviceTmp->pinNum);
printf("The lights are on on the second floor form clienAPP\n");
}
else if(strstr(socketHandler->command, "GELD") != NULL){
deviceTmp = findDevicesByName("upstairLight", pdeviceHead);
deviceTmp->deviceInit(deviceTmp->pinNum);
deviceTmp->close(deviceTmp->pinNum);
printf("The lights are off on the second floor form clienAPP\n");
}
else if(strstr(socketHandler->command, "KYSD") != NULL){
deviceTmp = findDevicesByName("bathroomLight", pdeviceHead);
deviceTmp->deviceInit(deviceTmp->pinNum);
deviceTmp->open(deviceTmp->pinNum);
printf("The bathroom light is on form clienAPP\n");
}
else if(strstr(socketHandler->command, "GYSD") != NULL){
deviceTmp = findDevicesByName("bathroomLight", pdeviceHead);
deviceTmp->deviceInit(deviceTmp->pinNum);
deviceTmp->close(deviceTmp->pinNum);
printf("The bathroom light is off form clienAPP\n");
}
else if(strstr(socketHandler->command, "KYCD") != NULL){
deviceTmp = findDevicesByName("swimmingpoolLight", pdeviceHead);
deviceTmp->deviceInit(deviceTmp->pinNum);
deviceTmp->open(deviceTmp->pinNum);
printf("The pool light is on form clienAPP\n");
}
else if(strstr(socketHandler->command, "GYCD") != NULL){
deviceTmp = findDevicesByName("swimmingpoolLight", pdeviceHead);
deviceTmp->deviceInit(deviceTmp->pinNum);
deviceTmp->close(deviceTmp->pinNum);
printf("The pool light is off form clienAPP\n");
}
else if (strstr(socketHandler->command, "KQBD") != NULL)
{
deviceTmp = findDevicesByName("livingroomLight", pdeviceHead);
deviceTmp->deviceInit(deviceTmp->pinNum);
deviceTmp->open(deviceTmp->pinNum);
deviceTmp = findDevicesByName("upstairLight", pdeviceHead);
deviceTmp->deviceInit(deviceTmp->pinNum);
deviceTmp->open(deviceTmp->pinNum);
deviceTmp = findDevicesByName("bathroomLight", pdeviceHead);
deviceTmp->deviceInit(deviceTmp->pinNum);
deviceTmp->open(deviceTmp->pinNum);
deviceTmp = findDevicesByName("swimmingpoolLight", pdeviceHead);
deviceTmp->deviceInit(deviceTmp->pinNum);
deviceTmp->open(deviceTmp->pinNum);
printf("All the lights are on form clienAPP\n");
}
else if (strstr(socketHandler->command, "GQBD") != NULL)
{
deviceTmp = findDevicesByName("livingroomLight", pdeviceHead);
deviceTmp->deviceInit(deviceTmp->pinNum);
deviceTmp->close(deviceTmp->pinNum);
deviceTmp = findDevicesByName("upstairLight", pdeviceHead);
deviceTmp->deviceInit(deviceTmp->pinNum);
deviceTmp->close(deviceTmp->pinNum);
deviceTmp = findDevicesByName("bathroomLight", pdeviceHead);
deviceTmp->deviceInit(deviceTmp->pinNum);
deviceTmp->close(deviceTmp->pinNum);
deviceTmp = findDevicesByName("swimmingpoolLight", pdeviceHead);
deviceTmp->deviceInit(deviceTmp->pinNum);
deviceTmp->close(deviceTmp->pinNum);
printf("All the lights are off form clienAPP\n");
}
else if(strstr(socketHandler->command, "DKMS") != NULL){
deviceTmp = findDevicesByName("lock", pdeviceHead);
deviceTmp->deviceInit(deviceTmp->pinNum);
deviceTmp->open(deviceTmp->pinNum);
delay(3000);
deviceTmp->close(deviceTmp->pinNum);
printf("The door lock is unlocked from clienAPP\n");
}
else if(strstr(socketHandler->command, "GBMS") != NULL){
deviceTmp = findDevicesByName("lock", pdeviceHead);
deviceTmp->deviceInit(deviceTmp->pinNum);
deviceTmp->close(deviceTmp->pinNum);
printf("The door lock is closed from clienAPP\n");
}
else if (strstr(socketHandler->command, "DKJK") != NULL){
pthread_create(&monitoringThread, NULL, monitoring_thread, NULL);
printf("Monitor enabled from clienAPP\n");
}
else if (strstr(socketHandler->command, "GBJK") != NULL){
system("killall -TERM mjpg_streamer");
printf("Monitor turned off from clienAPP\n");
}
else if (strstr(socketHandler->command, "RLSBKS") != NULL){
pthread_create(&cameraThread, NULL, camera_thread, NULL);
printf("Facial recognition unlock has been opened from clienAPP\n");
}
}else{
printf("clien quit\n");
pthread_exit(NULL);
}
}
void *socket_thread(void *datas)//套接字线程
{
int nread = 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("Failed to find the socket\n");
pthread_exit(NULL);
}else{
printf("%s init ok\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 *fire_thread(void *datas) //火灾线程
{
int status;
struct Devices *fireDeviceTmp = NULL;
struct Devices *buzzerDeviceTmp1 = NULL;
fireDeviceTmp = findDevicesByName("fire", pdeviceHead);
buzzerDeviceTmp1 = findDevicesByName("buzzer", pdeviceHead);
fireDeviceTmp->deviceInit(fireDeviceTmp->pinNum);
buzzerDeviceTmp1->deviceInit(buzzerDeviceTmp1->pinNum);
printf("The fire init ok\n");
while (1)
{
status = fireDeviceTmp->readStatus(fireDeviceTmp->pinNum);
if (status == 0)
{ delay(1000);
printf("Pay attention!! Possible fire\n");
buzzerDeviceTmp1->open(buzzerDeviceTmp1->pinNum);
delay(3000);
}else{
buzzerDeviceTmp1->close(buzzerDeviceTmp1->pinNum);
}
}
}
void *shake_thread(void *datas) //震动线程
{
int status;
struct Devices *shakeDeviceTmp = NULL;
struct Devices *buzzerDeviceTmp2 = NULL;
shakeDeviceTmp = findDevicesByName("shake", pdeviceHead);
buzzerDeviceTmp2 = findDevicesByName("buzzer", pdeviceHead);
shakeDeviceTmp->deviceInit(shakeDeviceTmp->pinNum);
buzzerDeviceTmp2->deviceInit(buzzerDeviceTmp2->pinNum);
printf("The shock init ok\n");
while (1)
{
status = shakeDeviceTmp->readStatus(shakeDeviceTmp->pinNum);
if (status == 0)
{
delay(1000);
printf("Pay attention!! Feel an earthquake\n");
buzzerDeviceTmp2->open(buzzerDeviceTmp2->pinNum);
delay(3000);
}else{
buzzerDeviceTmp2->close(buzzerDeviceTmp2->pinNum);
}
}
}
int main()
{
int status;
char name[128];
if(-1 == wiringPiSetup()){
printf("Hardware initialization failed!!");
return -1;
}
//指令工厂的初始化
pcommandHead = addVoiceContrlToInputCommandLink(pcommandHead);
pcommandHead = addSocketContrlToInputCommandLink(pcommandHead);
//设备工厂的初始化
pdeviceHead = addBathroomLightToDevicesLink(pdeviceHead);
pdeviceHead = addLivingroomLightToDevicesLink(pdeviceHead);
pdeviceHead = addUpstairLightToDevicesLink(pdeviceHead);
pdeviceHead = addSwimmingpoolLightToDevicesLink(pdeviceHead);
pdeviceHead = addFireToDevicesLink(pdeviceHead);
pdeviceHead = addBuzzerToDevicesLink(pdeviceHead);
pdeviceHead = adddoorLockToDevicesLink(pdeviceHead);
pdeviceHead = addcameraToDeviceLink(pdeviceHead);
pdeviceHead = addshakeToDevicesLink(pdeviceHead);
//语音线程
pthread_create(&voiceThread,NULL,voice_thread,NULL);
//socket线程
pthread_create(&socketThread,NULL,socket_thread,NULL);
//摄像头线程
pthread_create(&cameraThread, NULL, camera_thread, NULL);
//火灾线程
pthread_create(&fireThread, NULL, fire_thread, NULL);
//震动线程
pthread_create(&shakeThread, NULL, shake_thread, NULL);
//线程等待
pthread_join(voiceThread,NULL);
pthread_join(socketThread,NULL);
pthread_join(cameraThread, NULL);
pthread_join(fireThread, NULL);
pthread_join(shakeThread, NULL);
pthread_join(monitoringThread, NULL);
//删除锁
pthread_mutex_destroy(&mutex);
return 0;
}
#include
#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 ipAdress[32];
int sfd;
struct InputCommander *next;
};
struct InputCommander *addVoiceContrlToInputCommandLink(struct InputCommander *phead);
struct InputCommander *addSocketContrlToInputCommandLink(struct InputCommander *phead);
#include
#include
#include
typedef unsigned int bool;
struct Devices
{
char deviceName[128];
int pinNum;
int (*deviceInit)(int pinNum);
int (*open)(int pinNum);
int (*close)(int pinNum);
int (*readStatus)(int pinNum);
void (*justDoOnce)();
char *(*getFace)();
char *(*getPicFromOCRBase64)();
size_t (*readData)();
CURL *curl;
char *key;
char *secret;
int typeId;
char *format;
bool (*cameraInit)(struct Devices *camera);
struct Devices *next;
};
struct Devices *addBathroomLightToDevicesLink(struct Devices *phead);
struct Devices *addLivingroomLightToDevicesLink(struct Devices *phead);
struct Devices *addSwimmingpoolLightToDevicesLink(struct Devices *phead);
struct Devices *addUpstairLightToDevicesLink(struct Devices *phead);
struct Devices *addFireToDevicesLink(struct Devices *phead);
struct Devices *addBuzzerToDevicesLink(struct Devices *phead);
struct Devices *adddoorLockToDevicesLink(struct Devices *phead);
struct Devices *addcameraToDeviceLink(struct Devices *phead);
struct Devices *addshakeToDevicesLink(struct Devices *phead);
#include"inputCommand.h"
#include
#include
#include
#include
#include
#include
#include
#include
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));
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->ipAdress,&s_addr.sin_addr);
bind(s_fd,(struct sockaddr *)&s_addr,sizeof(struct sockaddr_in));
listen(s_fd,10);
printf("socket server listing......\n");
socketMes->sfd = s_fd;
return s_fd;
}
int socketGetCommand(struct InputCommander *socketMes)
{
int c_fd;
int nread = 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);
nread = read(c_fd,socketMes->command,sizeof(socketMes->command));
return nread;
}
struct InputCommander socketContrl = {
.commandName = "socketServer",
.command = {'\0'},
.port = "5795",
.ipAdress = "192.168.31.80",
.Init = socketInit,
.getCommand = socketGetCommand
};
struct InputCommander *addSocketContrlToInputCommandLink(struct InputCommander *phead)
{
if(phead == NULL){
return &socketContrl;
}else{
socketContrl.next = phead;
return &socketContrl;
}
}
#include"inputCommand.h"
#include
#include
#include
int voiceInit(struct InputCommander *voicer,char *ipAdress,char *port)
{
int fd;
if((fd = serialOpen(voicer->deviceName,9600))== -1){
exit(-1);
}
voicer->fd = fd;
return fd;
}
int voiceGetCommand(struct InputCommander *voicer)
{
int nread = 0;
nread = read(voicer->fd,voicer->command,sizeof(voicer->command));
return nread;
}
struct InputCommander voiceContrl = {
.commandName = "voice",
.deviceName = "/dev/ttyAMA0",
.command = {'\0'},
.Init = voiceInit,
.getCommand = voiceGetCommand
};
struct InputCommander *addVoiceContrlToInputCommandLink(struct InputCommander *phead)
{
if(phead == NULL){
return &voiceContrl;
}else{
voiceContrl.next = phead;
return &voiceContrl;
}
}
#include"controlDevices.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);
}
struct Devices livingroomLight = {
.deviceName = "livingroomLight",
.pinNum = 27,
.deviceInit = livingroomLightInit,
.open = livingroomLightOpen,
.close = livingroomLightClose
};
struct Devices *addLivingroomLightToDevicesLink(struct Devices *phead)
{
if(phead == NULL){
return &livingroomLight;
}else{
livingroomLight.next = phead;
phead = &livingroomLight;
}
}
#include"controlDevices.h"
int upstairLightInit(int pinNum)
{
pinMode(pinNum,OUTPUT);
digitalWrite(pinNum,HIGH);
}
int upstairLightOpen(int pinNum)
{
digitalWrite(pinNum,LOW);
}
int upstairLightClose(int pinNum)
{
digitalWrite(pinNum,HIGH);
}
struct Devices upstairLight = {
.deviceName = "upstairLight",
.pinNum = 26,
.deviceInit = upstairLightInit,
.open = upstairLightOpen,
.close = upstairLightClose
};
struct Devices *addUpstairLightToDevicesLink(struct Devices *phead)
{
if(phead == NULL){
return &upstairLight;
}else{
upstairLight.next = phead;
phead = &upstairLight;
}
}
#include"controlDevices.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);
}
struct Devices bathroomLight = {
.deviceName = "bathroomLight",
.pinNum = 29,
.deviceInit = bathroomLightInit,
.open = bathroomLightOpen,
.close = bathroomLightClose
};
struct Devices *addBathroomLightToDevicesLink(struct Devices *phead)
{
if(phead == NULL){
return &bathroomLight;
}else{
bathroomLight.next = phead;
phead = &bathroomLight;
}
}
#include"controlDevices.h"
int swimmingpoolLightInit(int pinNum)
{
pinMode(pinNum,OUTPUT);
digitalWrite(pinNum,HIGH);
}
int swimmingpoolLightOpen(int pinNum)
{
digitalWrite(pinNum,LOW);
}
int swimmingpoolLightClose(int pinNum)
{
digitalWrite(pinNum,HIGH);
}
struct Devices swimmingpoolLight = {
.deviceName = "swimmingpoolLight",
.pinNum = 28,
.deviceInit = swimmingpoolLightInit,
.open = swimmingpoolLightOpen,
.close = swimmingpoolLightClose
};
struct Devices *addSwimmingpoolLightToDevicesLink(struct Devices *phead)
{
if(phead == NULL){
return &swimmingpoolLight;
}else{
swimmingpoolLight.next = phead;
phead = &swimmingpoolLight;
}
}
#include"controlDevices.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
//因为在外接设备的头文件没传参,所以这里重新定义
void postUrl();
size_t readData1(void *ptr, size_t size, size_t nmemb, void *stream);
char *getFace1();
char *getPicFromOCRBase641(char *Filepath);
struct Devices *addcameraToDeviceLink(struct Devices *phead);
char ocrRetBuf[1024] = {'\0'};
#define door_lock 2
size_t readData1(void *ptr, size_t size, size_t nmemb, void *stream)
{
strncpy(ocrRetBuf, ptr, 1024);
printf("ocrRetBuf:%s",ocrRetBuf);
}
char *getFace1()
{
printf("In the picture...\n");
system("raspistill -q 5 -t 1 -o huge3.jpg");//-q 是图片质量,在0~100之间,我们调成5,压缩图片质量,生成的照片名字为huge1.jpg
//-t 是拍照延时,设定1s后拍照
//判断照片是否拍完
while (access("./huge3.jpg", F_OK) != 0)
;
printf("Photo taken successfully\n");
char *base64BufFaceRec = getPicFromOCRBase641("huge3.jpg");
system("rm huge3.jpg");
return base64BufFaceRec;
}
char *getPicFromOCRBase641(char *Filepath)//把拍的照片转换成base64
{
int fd;
int filelen;
char cmd[128] = {'\0'};
sprintf(cmd, "base64 %s > tmpFile", Filepath);
system(cmd);
fd = open("./tmpFile", O_RDWR);
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 + 128);
system("rm -rf tmpFile");
close(fd);
return bufpic;
}
void postUrl()
{
CURL *curl;
CURLcode res;
struct Devices *deviceTmp = NULL;
char *key = "xxxJopRsVNtZ3RVEv6exxx";
char *secret = "xxxa040d9866453b800dd5d62719exxx"; //用自己的
int typeId = 21;
char *format = "xml";
char *base64BufPic1 = getFace1();
char *base64BufPic2 = getPicFromOCRBase641("./huge.jpg");
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, readData1);
res = curl_easy_perform(curl);
printf("OK:%d\n", res);
if (strstr(ocrRetBuf, "是") != NULL)
{
printf("It's the same person\n");
digitalWrite(door_lock, LOW);
delay(3000);
digitalWrite(door_lock, HIGH);
}
else
{
printf("Not the same person\n");
}
curl_easy_cleanup(curl);
}
}
struct Devices camera = {
.deviceName = "camera",
.justDoOnce = postUrl,
.getFace = getFace1,
.getPicFromOCRBase64 = getPicFromOCRBase641,
.readData = readData1
};
struct Devices *addcameraToDeviceLink(struct Devices *phead)
{
if (phead == NULL)
{
return &camera;
}
else
{
camera.next = phead;
phead = &camera;
}
}
#include"controlDevices.h"
int doorLockInit(int pinNum)
{
pinMode(pinNum,OUTPUT);
digitalWrite(pinNum,HIGH);
}
int doorLockOpen(int pinNum)
{
digitalWrite(pinNum,LOW);
}
int doorLockClose(int pinNum)
{
digitalWrite(pinNum,HIGH);
}
struct Devices doorLock = {
.deviceName = "lock",
.pinNum = 2,
.deviceInit = doorLockInit,
.open = doorLockOpen,
.close = doorLockClose
};
struct Devices *adddoorLockToDevicesLink(struct Devices *phead)
{
if(phead == NULL){
return &doorLock;
}else{
doorLock.next = phead;
phead = &doorLock;
}
}
#include"controlDevices.h"
int fireInit(int pinNum)
{
pinMode(pinNum,INPUT);
digitalWrite(pinNum,HIGH);
}
int fireStatusRead(int pinNum)
{
return digitalRead(pinNum);
}
struct Devices fire = {
.deviceName = "fire",
.pinNum = 24,
.deviceInit = fireInit,
.readStatus = fireStatusRead
};
struct Devices *addFireToDevicesLink(struct Devices *phead)
{
if(phead == NULL){
return &fire;
}else{
fire.next = phead;
phead = &fire;
}
}
#include"controlDevices.h"
int shakeInit(int pinNum)
{
pinMode(pinNum,INPUT);
digitalWrite(pinNum,HIGH);
}
int shakeStatusRead(int pinNum)
{
return digitalRead(pinNum);
}
struct Devices shake = {
.deviceName = "shake",
.pinNum = 25,
.deviceInit = shakeInit,
.readStatus = shakeStatusRead
};
struct Devices *addshakeToDevicesLink(struct Devices *phead)
{
if(phead == NULL){
return &shake;
}else{
shake.next = phead;
phead = &shake;
}
}
#include"controlDevices.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 = 22,
.deviceInit = buzzerInit,
.open = buzzerOpen,
.close = buzzerClose
};
struct Devices *addBuzzerToDevicesLink(struct Devices *phead)
{
if(phead == NULL){
return &buzzer;
}else{
buzzer.next = phead;
phead = &buzzer;
}
}
cd ../mjpg-streamer/mjpg-streamer-experimental/
./mjpg_streamer -i "./input_raspicam.so" -o "./output_http.so -w ./www"
写好脚本后,记得给该脚本文件加上可执行权限。