有哪些做短租的网站好,做网站如何让用户注册,中国外贸导航网,万网域名跳转到指定网站一、功能介绍 二、设计框图 三、实物展示 四、程序
一、功能介绍硬件#xff1a;树莓派3B、LD3320语音识别模块、pi 摄像头、继电器组、小灯、火焰传感器、蜂鸣器、电 磁锁
项目框架#xff1a; 采用了简单工厂模式的一个设计方式。稳定#xff0c;拓展性…一、功能介绍 二、设计框图 三、实物展示 四、程序
一、功能介绍硬件树莓派3B、LD3320语音识别模块、pi 摄像头、继电器组、小灯、火焰传感器、蜂鸣器、电 磁锁
项目框架 采用了简单工厂模式的一个设计方式。稳定拓展性更强在C语言中因为没有接口、类这一说法所以这里采用了结构体来“等效替换”。有四个灯所以我创建了四个灯控制.c程序。每一个程序文件中都有一个设备结构体每个程序文件的函数实现方法不同当有新设备进入只需要在创建一个.c文件改变函数实现方法即可。初始化的时候通过链表将各个模块连接起来头插法。在要使用某个模块时只需要使用链表遍历找到所需模块去调用功能具体功能是 1、可通过ld3320语音模块的口令模式口令具体控制通过串口把控制指令传给树莓派来控 制客厅、餐厅、二楼、浴室的灯以及 人脸识别功能。 2、也可以通过socket客户端来发指令来控制灯的开关电磁锁 3、火灾报警当火焰传感器检测到火焰的时候蜂鸣器会报警。 4、视频监控采用开源mjpg-Streamer来实现的程序执行时创建一个视频监控的线程用system函数调用启动脚本运行监控画面可在http://172.20.10.8:8080去看到 5、人脸识别开锁人脸识别功能是使用的翔云平台的人脸识别解决方案需要安装libcurl 和 openSSl库来支持https协议通过系统调用wget http://172.20.10.8:8080/?actionsnapshot -O ./huyu1.jpg 指令到树莓派的监控页面去截取一帧保存到本地获取图片的base64编码工程文件夹下也有一张照片huyu.jpg格式相当于采集的人脸。也是获取图片的base64编码通过sprintf函数将访问翔云需要的两张图片的base64编码与Key、secret、typeId、format拼接在一起通过https协议去访问翔云平台 识别成功后会将识别结果返回通过回调函数readData将返回的字符串读到readBuff里通过strstr去readbuff里找有没有字符’是’如果识别成功就去控制电磁锁打开。
二、设计框图 四、程序 control Device
#include wiringPi.h
#include stdio.h
#include stdlib.hstruct Devices
{char name[128];int status;int pinName;int (*open)(int pinName);int (*close)(int pinName);int (*deviceInit)(int pinName);void (*justDoOnce)();char* (*getFace)();char* (*getPicFromOCRBase64)(); int (*readStaus)(int pinName);int (*changeStatus)(int status);struct Devices* next;
};struct Devices* addbathroomLink(struct Devices* head);
struct Devices* addupstairLink(struct Devices* head);
struct Devices* addrestaurantLink(struct Devices* head);
struct Devices* addlivingroomLink(struct Devices* head);
struct Devices* addcameraToDeviceLink(struct Devices *head);
struct Devices* addfiretoLink(struct Devices* head);
struct Devices* addBeepToDeviceLink(struct Devices *phead) ; inoutcommand
#include stdlib.h
#include wiringPi.hstruct inputcommander{char commandName[128]; char deviceName[128];char command[32];int (*init)(struct inputcommander*voicer ,char* ipAddress,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* addvoiceControlInputLink(struct inputcommander* phead);struct inputcommander* addsockControlLink(struct inputcommander* phead);
bathroom
#include controDevice.hint bathroomLightopen(int pinName){digitalWrite(pinName,LOW);}int bathroomLightclose(int pinName){digitalWrite(pinName,HIGH);}int bathroomLightInit(int pinName){pinMode(pinName,OUTPUT);digitalWrite(pinName,HIGH);
}struct Devices bathroomLight {.namebathroomLight,.pinName22, .openbathroomLightopen,.closebathroomLightclose,.deviceInitbathroomLightInit};struct Devices* addbathroomLink(struct Devices* head){if(headNULL){return bathroomLight;}else{bathroomLight.nexthead;headbathroomLight;return head;}
}
livinglight
#include controDevice.hint livingroomLightopen(int pinName){digitalWrite(pinName,LOW);}int livingroomLightclose(int pinName){digitalWrite(pinName,HIGH);}int livingroomLightInit(int pinName){pinMode(pinName,OUTPUT);digitalWrite(pinName,HIGH);}int livingroomLightChangestatus(int status){}struct Devices livingroomLight {.namelivingroomLight,.pinName24, .openlivingroomLightopen,.closelivingroomLightclose,.deviceInitlivingroomLightInit,.changeStatuslivingroomLightChangestatus
};struct Devices* addlivingroomLink(struct Devices* head){if(headNULL){return livingroomLight;}else{livingroomLight.nexthead;headlivingroomLight;return head;}
}
restraut light
#include controDevice.hint restaurantLightopen(int pinName){digitalWrite(pinName,LOW);}int restaurantLightclose(int pinName){digitalWrite(pinName,HIGH);}int restaurantLighttInit(int pinName){pinMode(pinName,OUTPUT);digitalWrite(pinName,HIGH);}int restaurantLightChangestatus(int status){}struct Devices restaurantLight {.namerestaurantLight,.pinName23, .openrestaurantLightopen,.closerestaurantLightclose,.deviceInitrestaurantLighttInit,.changeStatusrestaurantLightChangestatus
};struct Devices* addrestaurantLink(struct Devices* head){if(headNULL){return restaurantLight;}else{restaurantLight.nexthead;headrestaurantLight;return head;}
}upstair light
#include controDevice.hint upstairLightopen(int pinName){digitalWrite(pinName,LOW);}int upstairLightclose(int pinName){digitalWrite(pinName,HIGH);}int upstairLightInit(int pinName){pinMode(pinName,OUTPUT);digitalWrite(pinName,HIGH);}int upstairLightChangestatus(int status){}struct Devices upstairLight {.nameupstairLight,.pinName21, .openupstairLightopen,.closeupstairLightclose,.deviceInitupstairLightInit,.changeStatusupstairLightChangestatus
};struct Devices* addupstairLink(struct Devices* head){if(headNULL){return upstairLight;}else{upstairLight.nexthead;headupstairLight;return head;}
} filre
#include controDevice.hint firetoInit(int pinName){ //初始化函数pinMode(pinName,INPUT); //配置引脚为输入引脚digitalWrite(pinName,HIGH); //引脚输出高电平即默认为关闭状态}int firetostatusread(int pinName){ //读取火焰传感器状态函数return digitalRead(pinName); //读取高低电平返回0或1}struct Devices fireto { //火焰传感器设备链表节点.namefire,.pinName25, .deviceInitfiretoInit,.readStausfiretostatusread
};struct Devices* addfiretoLink(struct Devices* head){ //头插法将设备节点加入设备工厂链表函数if(headNULL){return fireto;}else{fireto.nexthead;headfireto;return head;}
}bee
#include controDevice.hint beepInit(int pinName) //初始化函数
{pinMode(pinName,OUTPUT); //配置引脚为输出引脚digitalWrite(pinName,HIGH); //引脚输出高电平即默认为关闭状态
}int beepOpen(int pinName) //打开蜂鸣器函数
{digitalWrite(pinName,LOW);
}int beepClose(int pinName) //关闭蜂鸣器函数
{digitalWrite(pinName,HIGH);
}struct Devices beep { //蜂鸣器设备链表节点.name beep,.pinName 7, //树莓派gpio引脚29.deviceInit beepInit,.open beepOpen,.close beepClose
};struct Devices* addBeepToDeviceLink(struct Devices *phead) //头插法将设备节点加入设备工厂链表函数
{if(phead NULL){return beep;}else{beep.next phead;phead beep;return phead;}
} camera
#include controDevice.h
#include stdio.h
#include unistd.h
#include string.h
#include stdlib.h
#include curl/curl.h
#include sys/types.h
#include sys/stat.h
#include fcntl.h
#include sys/types.h
#include unistd.h
#define SWITCH1 21char ocrRetBuf[1024] {\0};//全局变量用来接收从OCR后台返回的数据size_t readData1(void *ptr, size_t size, size_t nmemb, void *stream)
//回调函数把从后台的数据拷贝给ocrRetBuf
{strncpy(ocrRetBuf,ptr,1024);printf(data reviver\n);
}char *getPicFromOCRBase641(char *Filepath)
{int fd;int filelen;char cmd[128]{\0};sprintf(cmd,base64 %s tmpFile,Filepath);system(cmd);fdopen(./tmpFile,O_RDWR);filelenlseek(fd,0,SEEK_END);lseek(fd,0,SEEK_SET);char *bufpic(char *)malloc(filelen2);memset(bufpic,\0,filelen2);read(fd,bufpic,filelen128);system(rm -rf tmpFile);close(fd);return bufpic;}char*getFace1(){printf(pai zhao zhong\n);system(raspistill -q 5 -t 1 -o pic.jpg);while(access(./pic.jpg,F_OK) ! 0); //判断是否拍照完毕printf(paizhao wan bi\n);char* base64BufFaceRec getPicFromOCRBase641(./pic.jpg);system(rm pic.jpg);return base64BufFaceRec; //返回刚才拍照的base64}void postUrl(){CURL *curl;CURLcode res;//分开定义然后字符串拼接char* key P5bruv7dU4YRH7JHNxuCeb; //翔云平台购买人脸识别后的keychar* secret 0c4c02a1161e43bf9de539d6487260c8; //翔云平台购买人脸识别后的secretint typeId 21; char* format xml;char* base64BufPic1 getFace1();char* base64BufPic2 getPicFromOCRBase641(PYD.jpg);int len strlen(key)strlen(secret)strlen(base64BufPic1)strlen(base64BufPic2)128;//分配空间不够会导致栈溢出char* postString (char* )malloc(len);memset(postString,\0,len);//因为postString是一个指针不能用sizeof来计算其指向的大小sprintf(postString,img1%simg2%skey%ssecret%stypeId%dformat%s,base64BufPic1,base64BufPic2,key,secret,typeId,format);//根据平台的传参格式编写curl curl_easy_init();if(curl){curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postString); //指定post内容传入参数 curl_easy_setopt(curl, CURLOPT_URL, https://netocr.com/api/faceliu.do);// 指定urlcurl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,readData1); //回调函数readDate读取返回值res curl_easy_perform(curl); //类似于状态码printf(OK:%d\n,res);if(strstr(ocrRetBuf,是) ! NULL){ //判断翔云后台返回的字符串中有没有“是”printf(the same person\n);pinMode(SWITCH1,OUTPUT);digitalWrite(SWITCH1,LOW);}else{printf(different person\n);digitalWrite(SWITCH1,HIGH);}curl_easy_cleanup(curl);}}struct Devices camera {.name camera,.justDoOnce postUrl,.getFace getFace1,.getPicFromOCRBase64 getPicFromOCRBase641,//.readData readData1};struct Devices* addcameraToDeviceLink(struct Devices *head)
{if(head NULL){return camera;}else{camera.next head;head camera;}
} socket
#include wiringSerial.h
#include stdio.h
#include unistd.h
#include stdlib.h
#include wiringPi.h
#include string.h
#include sys/types.h
#include sys/socket.h
#include netinet/in.h
#include arpa/inet.h
#include inoutCommand.hint SocketInit(struct inputcommander* Socketlnits,char*ipAddress,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(Socketlnits-port));inet_aton(Socketlnits-ipAddress , s_addr.sin_addr);bind(s_fd , (struct sockaddr*)s_addr , sizeof(struct sockaddr_in));listen(s_fd,10);printf(socket server Listening \n);Socketlnits-sfds_fd;return s_fd;}struct inputcommander socketControl {.commandNamesocketserver,.command{\0},.port 8124,.ipAddress 192.168.43.165,.init SocketInit,.log {\0},.next NULL
};struct inputcommander* addsockControlLink(struct inputcommander* phead){if(pheadNULL){return socketControl;}else{socketControl.nextphead;pheadsocketControl;return phead;}}
voice control
#include inoutCommand.h
#include wiringSerial.h
#include stdio.h
#include unistd.h
#include stdlib.h
#include wiringPi.h
#include string.hint voicegetCommand(struct inputcommander* voicer){int nread 0;memset(voicer-command,\0,sizeof(voicer-command));nread read(voicer-fd, voicer-command, sizeof(voicer-command));if(nread0){printf( voice device read over times\n);}else{return nread;}
}int voiceInit(struct inputcommander* voicer,char*ipAddress,char*port){int fd;if((fd serialOpen(voicer-deviceName,9600))0){printf(voicelnit open error\n);return (-1);}printf(voicelnit...contun...\n);voicer-fdfd;return fd;}struct inputcommander voiceControl {.commandNamevoice,.deviceName/dev/ttyAMA0,.command{\0},.init voiceInit,.getCommand voicegetCommand,.log {\0},.next NULL
};struct inputcommander* addvoiceControlInputLink(struct inputcommander* phead){if(pheadNULL){return voiceControl;}else{voiceControl.nextphead;pheadvoiceControl;return phead;}
}main
#include stdio.h
#include string.h
#include stdlib.h
#include inoutCommand.h
#include controDevice.h
#includeunistd.h
#includepthread.h
#includewiringPi.h
#includewiringSerial.h
#includesys/types.h
#includesys/socket.h
#includearpa/inet.h
#includenetinet/in.h#define SWITCH1 21 //四盏灯对应的引脚
#define SWITCH2 22
#define SWITCH3 23
#define SWITCH4 24
#define SWITCH5 25struct Devices* temNULL;
struct inputcommander* commandheadNULL; struct inputcommander*socketHeadler NULL;int c_fd;struct Devices* findDeviceByName(char* name,struct Devices* phead){struct Devices*tmpphead;if(pheadNULL){return NULL;}else{while(tmp!NULL){if(strcmp(tmp-name,name)0){return tmp;} tmptmp-next;}return NULL;}
}struct inputcommander* findcommandByName(char* name,struct inputcommander* phead){struct inputcommander*tmpphead;if(pheadNULL){return NULL;}else{while(tmp!NULL){if(strcmp(tmp-commandName,name)0){return tmp;} tmptmp-next;}return NULL;}
}void *fireAlarmThread(void *data) //“火灾报警器线程”执行的函数
{int status;struct Devices *firetmp NULL;struct Devices *buztmp NULL;firetmp findDeviceByName(fire,tem); //寻找“火焰传感器”链表节点返回给firetmpbuztmp findDeviceByName(beep,tem); //寻找“蜂鸣器”链表节点返回给buztmpwhile(1){status firetmp-readStaus(firetmp-pinName); //读取“火焰传感器”状态buztmp-deviceInit(buztmp-pinName);if(status 0){ //检测到火焰或强光源printf(have fire\n);buztmp-open(buztmp-pinName); //打开蜂鸣器delay(1000); //延时1000毫秒1秒}if(status 1){ //未检测到火焰、强光源或解除警报buztmp-close(buztmp-pinName); //关闭蜂鸣器}}
}void *cameraThread_func(void* data)//起线程的函数有格式要求
{struct Devices *cameraTemp;cameraTemp findDeviceByName(camera, tem); //设备都要从工厂里面取出来if(cameraTemp NULL){ //防止段错误的必需判断当给指针赋值是一定要考虑NULL的情况否则后续操作都是空谈printf(find camera error\n);pthread_exit(NULL); //在线程中不用return}printf(222\n);cameraTemp-justDoOnce(); //调用postUrl函数
}void* read_Thread(void* datas){int n_read;memset(socketHeadler-command,\0,sizeof(socketHeadler-command));n_read read(c_fd,socketHeadler-command,sizeof(socketHeadler-command));if(n_read -1){perror(read);}else if(n_read0){printf(\n socker read number:%d , contixt:%s\n,n_read,socketHeadler-command);if(strstr(socketHeadler-command,KS) ! NULL){printf(open lock\n);pinMode(SWITCH1,OUTPUT);digitalWrite(SWITCH1,LOW);}if(strstr(socketHeadler-command,KYS) ! NULL){ pinMode(SWITCH2,OUTPUT);digitalWrite(SWITCH2,LOW);}if(strstr(socketHeadler-command,GYS) ! NULL){digitalWrite(SWITCH2,HIGH);}if(strstr(socketHeadler-command,KKT) ! NULL){ //对socket收到的指令进行分析并执行对应的操作pinMode(SWITCH4,OUTPUT);digitalWrite(SWITCH4,LOW);}if(strstr(socketHeadler-command,GKT) ! NULL){digitalWrite(SWITCH4,HIGH);}if(strstr(socketHeadler-command,KCT) ! NULL){ //对socket收到的指令进行分析并执行对应的操作pinMode(SWITCH3,OUTPUT);digitalWrite(SWITCH3,LOW);}if(strstr(socketHeadler-command,GCT) ! NULL){digitalWrite(SWITCH3,HIGH);}if(strstr(socketHeadler-command,GS) ! NULL){digitalWrite(SWITCH1,HIGH);}else{printf(Input error! \n);}}
}void* voiceThread(void* datas){int nread;struct inputcommander* voiceHeadfindcommandByName(voice,commandhead);if(voiceHeadNULL){printf( no voice \n);pthread_exit(NULL);}else{printf(%s find voice \n,voiceHead-commandName);if(voiceHead-init(voiceHead, NULL ,NULL)0){printf(voice init error!!!\n);pthread_exit(NULL);}else{printf( %s init successful!\n,voiceHead-commandName);}while(1){nread voiceHead-getCommand(voiceHead);if(nread 0){printf(waiting...\n);}else{printf(do divece control : %s\n,voiceHead-command);if(strstr(voiceHead-command,XJ) ! NULL){ //一级指令printf(收到:\n); }else if(strstr(voiceHead-command,KYSD) ! NULL){ pinMode(SWITCH2,OUTPUT);digitalWrite(SWITCH2,LOW);}else if(strstr(voiceHead-command,GYSD) ! NULL){digitalWrite(SWITCH2,HIGH);}else if(strstr(voiceHead-command,KCTD) ! NULL){pinMode(SWITCH3,OUTPUT);digitalWrite(SWITCH3,LOW);}else if(strstr(voiceHead-command,GCTD) ! NULL){digitalWrite(SWITCH3,HIGH);}else if(strstr(voiceHead-command,KKTD) ! NULL){pinMode(SWITCH4,OUTPUT);digitalWrite(SWITCH4,LOW);}else if(strstr(voiceHead-command,GKTD) ! NULL){digitalWrite(SWITCH4,HIGH);}else if(strstr(voiceHead-command,KS) ! NULL){pthread_t cameraThread;printf(1111\n);system(sudo killall -TERM motion);delay(3000); pthread_create(cameraThread,NULL,cameraThread_func,NULL);}}} }
}void* socketThread(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);socketHeadlerfindcommandByName(socketserver,commandhead);if(socketHeadler NULL){printf(NO find socketserver!\n);pthread_exit(NULL);}else{printf(find socketserver!\n); }socketHeadler-init(socketHeadler,NULL,NULL);while(1){c_fdaccept(socketHeadler-sfd,(struct sockaddr*)c_addr,clen);pthread_create(readThread,NULL,read_Thread,NULL); }
}void * video_thread(void *datas){system(sudo motion);
printf( chest ... \n);//pthread_exit(NULL); }int main(){if(wiringPiSetup() -1){ printf(wiringPiSetup failed!\n);return -1; }char name[128];pthread_t voice_thread;
pthread_t socket_thread;
pthread_t fireAlarm_thread;
pthread_t videoThread;//设备工厂初始化
temaddbathroomLink(tem);
temaddupstairLink(tem);
temaddrestaurantLink(tem);
temaddlivingroomLink(tem);tem addfiretoLink(tem);
temaddBeepToDeviceLink(tem); temaddcameraToDeviceLink(tem);commandheadaddvoiceControlInputLink(commandhead);
commandheadaddsockControlLink(commandhead);pthread_create(voice_thread , NULL , voiceThread , NULL);
pthread_create(socket_thread , NULL , socketThread , NULL);
pthread_create(fireAlarm_thread,NULL,fireAlarmThread,NULL);pthread_create(videoThread, NULL, video_thread, NULL);pthread_join(voice_thread,NULL);
pthread_join(socket_thread,NULL);
pthread_join(fireAlarm_thread,NULL);pthread_join(videoThread,NULL);return 0;
}