赞
踩
目录
Orangepi Zero2 全志H616开发板,语音模块SU-03T,摄像头模组OV9726,蜂鸣器,火焰传感器,4个LED等,4路继电器组,6v电源,若干杜邦线。
MobaXterm、VS Code、FileZilla
在MobaXterm命令控制终端输入gpio readall可以查看开发板上的所有引脚。语音模块、蜂鸣器、火焰传感器和4路继电器组的引脚接线在下图框出。
由于加了温湿度传感器之后板子上引脚不够,所以在第二个视频演示时会把语言识别模块替换下来。温湿度传感器对应IO为,VCC--5V, GND--GND, DAT--3wPi。
工厂模式提供了一种将对象的实例化过程封装在工厂类中的方式。本文通过使用工厂模式,将对象的创建与使用代码分离,提供一种统一的接口来创建不同类型的对象。在工厂模式中,我们在创建对象时不会对客户端暴露创建逻辑,并且是通过使用一个共同的接口来指向新创建的对象(链表)。
- //1.指令工厂初始化
- pCommandHead = addVoiceContrlToInputCommandLink(pCommandHead);//串口
- pCommandHead = addSocketContrlToInputCommandLink(pCommandHead);
-
- //2.设备控制工厂初始化
- //四个LED+火灾+蜂鸣器+摄像头
- pdeviceHead = addBathroomLightToDeviceLink(pdeviceHead);
- pdeviceHead = addUpstairLightToDeviceLink(pdeviceHead);
- pdeviceHead = addRestaurantLightToDeviceLink(pdeviceHead);
- pdeviceHead = addLivingroomLightToDeviceLink(pdeviceHead);
- pdeviceHead = addFireToDeviceLink(pdeviceHead);
- pdeviceHead = addBeepToDeviceLink(pdeviceHead);
- pdeviceHead = addCameraToDeviceLink(pdeviceHead);
主函数中创建了三个线程:语音线程、火焰检测线程、网络线程。
语音线程(voiceThread):完成串口的配置和初始化,在while循环里每隔0.3s检查串口是否有语音命令词到来,有则执行对应操作。
火焰检查线程(socketThread):while循环里每隔0.5s检测是否有火焰,有则蜂鸣器发出警报,知道无火焰。
网络线程(socketThread):接收客户端指令,实现上位机远程发送指令完成 [ 功能要求 ] 所述功能;给客户端发送温湿度传感器所测数据,实时显示在QT界面上。
- //1、接收客户端的指令控制灯和摄像头 执行指令功能与语音控制复用
- //2、向客户端发送温湿度数据
- void *socket_thread(){
- 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);
- }
- printf("%s init success\n", socketHandler->commandName);
- socketHandler->Init(socketHandler, NULL, NULL);
-
- while(1){
- c_fd = accept(socketHandler->sfd, (struct sockaddr *)&c_addr, &clen);
- piThreadCreate(read_thread);
- pthread_tempAndHumi_create();
- }
- }
在网络线程中,首先对套接字进行初始化配置,包括IPV4因特网协议、TCP协议的配置(socket),绑定IP地址和端口号(bind),通过套接字标识符监听对应端口(listen)并等待客户端接入(accept)。
客户端接入后,即创建读数据线程,在while循环中进程阻塞在read函数,直到客户端发出指令,指令的执行函数与语音模块的语音指令执行函数复用(voiceContrlFunc函数在第4小点展示)。例如:在QT界面按下 [开客厅灯] 按钮,客户端通过网络发出字符串"OLL" ,在服务端read读到指令放入socketHandler->command,调用函数voiceContrlFunc(socketHandler->command)执行指令。
同时客户端在接入之后会调用函数pthread_tempAndHumi_create()创建发数据的线程,即实时发送温湿度的数据,QT中配合信号槽接收数据并在QT界面上显示,代码如下。
- void *read_thread(){
- while(1){
- int n_read = 0;
- memset(socketHandler->command, '\0', sizeof(socketHandler->command));
- n_read = read(c_fd, socketHandler->command, sizeof(socketHandler->command));//n_read是读到字节数
- voiceContrlFunc(socketHandler->command);
- if(n_read == -1){
- perror("read");
- }
- else if(n_read>0){
- printf("\nget: %d, %s\n",n_read, socketHandler->command);
- }
- else{
- printf("client quit\n");
- break;
- }
- }
- }
QT程序,控制开关客厅灯的函数:
- void Widget::on_livingRoomLight_clicked()
- {
- if(livingRoomLightFlag == 1){
- //客户端向服务端发送消息
- if(tcpSocket->state() == QAbstractSocket::ConnectedState){
- ui->livingRoomLight->setText("关客厅灯");
- tcpSocket->write("OLL");
- ui->textBrowser->append("> 客厅灯已打开\n");
- livingRoomLightFlag = 0;
- }
- else{
- ui->textBrowser->append("请先与服务端连接!");
- }
- }
- else{
- if(tcpSocket->state() == QAbstractSocket::ConnectedState){
- ui->livingRoomLight->setText("开客厅灯");
- tcpSocket->write("CLL");
- ui->textBrowser->append("> 客厅灯已关闭\n");
- livingRoomLightFlag = 1;
- }
- else{
- ui->textBrowser->append("请先与服务端连接!");
- }
- }
- }
QT程序中的信号槽连接和TCP网络读函数:
- connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(receiveMessages()));
-
- void Widget::receiveMessages()
- {
- QByteArray tmpByteArray = tcpSocket->readAll();
- char* tempHumi;
- tempHumi = tmpByteArray.data();
- ui->tempLabel->setText(QString::number(tempHumi[0]));
- ui->humiLabel->setText(QString::number(tempHumi[2]));
- }
PC端QT界面
APP界面
包括wiringPi库的初始化,蜂鸣器、火焰传感器、继电器组的输入输出引脚配置和高低电平设置。wiringP库下的串口配置和初始化。
本文用的是全志H616芯片中的串口设备/dev/ttyS5,波特率115200,实现与语音模块的串口通信。语音模块收到我们发出的命令词,将命令词转化为16进制数据通过串口发送到开发板,在程序中完成对串口数据接收、存储、判断是哪种命令、执行对应命令的操作,包括开关二楼灯、餐厅灯、客厅灯、卫生间灯、开启摄像头、拍一张照片和人脸识别。
语音模块SU-03T需要烧入对应命令的SDK,本文配置的SDK是在智能公元/AI产品零代码平台上完成,免费的。
- int get_voice_type(char *cmd)
- {
- if(!strcmp("OLL", cmd)) return OLL;
- if(!strcmp("ORL", cmd)) return ORL;
- if(!strcmp("OUL", cmd)) return OUL;
- if(!strcmp("OBL", cmd)) return OBL;
- if(!strcmp("CLL", cmd)) return CLL;
- if(!strcmp("CRL", cmd)) return CRL;
- if(!strcmp("CUL", cmd)) return CUL;
- if(!strcmp("CBL", cmd)) return CBL;
- if(!strcmp("OC" , cmd)) return OC ;
- if(!strcmp("TAP", cmd)) return TAP;
- if(!strcmp("OFR", cmd)) return OFR;
- perror("voice recognition failure");
- }
-
- void voiceContrlFunc(char *cmd){
- switch(get_voice_type(cmd)){
- case OLL://OLL ASCII对应的16进制4f 4c 4c
- printf("open livingroom light\n");
- struct Devices *tmpOpenLivingroomLight = findDeviceByName("livingroomLight", pdeviceHead);
- tmpOpenLivingroomLight->open(tmpOpenLivingroomLight->pinNum);
- break;
- case ORL://ORL 4f 52 4c
- printf("open restaurant light\n");
- struct Devices *tmpOpenRestaurantLight = findDeviceByName("restaurantLight", pdeviceHead);
- tmpOpenRestaurantLight->open(tmpOpenRestaurantLight->pinNum);
- break;
- case OUL://OUL 4f 55 4c
- printf("open upstair light\n");
- struct Devices *tmpOpenUpstairLight = findDeviceByName("upstairLight", pdeviceHead);
- tmpOpenUpstairLight->open(tmpOpenUpstairLight->pinNum);
- break;
- case OBL://OBL 4f 42 4c
- printf("open bathroom light\n");
- struct Devices *tmpOpenBathroomLight = findDeviceByName("bathroomLight", pdeviceHead);
- tmpOpenBathroomLight->open(tmpOpenBathroomLight->pinNum);
- break;
- case CLL://CLL 43 4c 4c
- printf("close livingroom light\n");
- struct Devices *tmpCloseLivingroomLight = findDeviceByName("livingroomLight", pdeviceHead);
- tmpCloseLivingroomLight->close(tmpCloseLivingroomLight->pinNum);
- break;
- case CRL://CRL 43 52 4c
- printf("close restaurant light\n");
- struct Devices *tmpCloseRestaurantLight = findDeviceByName("restaurantLight", pdeviceHead);
- tmpCloseRestaurantLight->close(tmpCloseRestaurantLight->pinNum);
- break;
- case CUL://CUL 43 55 4c
- printf("close upstair light\n");
- struct Devices *tmpCloseUpstairLight = findDeviceByName("upstairLight", pdeviceHead);
- tmpCloseUpstairLight->close(tmpCloseUpstairLight->pinNum);
- break;
- case CBL://CBL 43 42 4c
- printf("close bathroom light\n");
- struct Devices *tmpCloseBathroomLight = findDeviceByName("bathroomLight", pdeviceHead);
- tmpCloseBathroomLight->close(tmpCloseBathroomLight->pinNum);
- break;
- case OC://OC 4f 43
- printf("open camera\n");
- printf(" -------------------------------------------------------------------\n");
- printf(" --\033[1;32m 已开启摄像头,请到指定网页观看画面 https//192.168.43.206:8081 \033[0m--\n");//黄色字体
- printf(" -------------------------------------------------------------------\n");
- printf("\n");
- break;
- case TAP://TAP 54 41 50
- printf("take a picture\n");
- struct Devices *tmpTakeAPictureCamera = findDeviceByName("camera", pdeviceHead);
- tmpTakeAPictureCamera->takeAPicture();
- printf(" --------------------------------------\n");
- printf(" --\033[1;32m 已拍照,请在当前文件夹下查看照片 \033[0m--\n");//黄色字体
- printf(" --------------------------------------\n");
- printf("\n");
- break;
- case OFR://OFR 4f 46 52
- printf("open face recognition\n");
- struct Devices *tmpFaceRecCamera = findDeviceByName("camera", pdeviceHead);
- tmpFaceRecCamera->faceRecognition();
- break;
- }
- }
参考这篇文章:(1031条消息) 香橙派Orange Pi Zero 2开发板配置USB摄像头的方法_阿龙还在写代码的博客-CSDN博客
通过命令词【人脸识别】让摄像头拍摄一张照片并与本地的本人照片做对比,成功则蜂鸣器滴一声,失败则滴四声。人脸识别的处理程序调用的是翔云OCR人脸识别 (netocr.com)的人脸识别API,该接口地址https://netocr.com/api/faceliu.do是https协议的地址,https协议是在http和tcp之间多添加了一层,进行身份验证和数据加密。
若要访问https协议的地址,则需要用到Libcurl这个跨平台的网络协议库,配合OpenSSL库,就可以访问https协议的接口。(编译OpenSSL支持Libcurl的https访问,如果直接编译Libcurl,只能访问http不能访问https,需要OpenSSL库才能访问https)
camera.c
- #include "contrlDevices.h"
- #include <stdio.h>
- #include <curl/curl.h>
- #include <string.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
-
- extern struct Devices *pdeviceHead;//extern表面变量或函数是定义在其他文件中,声明 为全局变量在该源文件使用
- struct Devices* findDeviceByName(char *name, struct Devices *phead);
-
- #define true 1
- #define false 0
- struct Devices camera;
- char buf[1024] = {'\0'};//全局
-
- void cameraTakeAPicture(){
- system("(fswebcam -d /dev/video0 --no-banner -r 1280x720 -S 5 ./image.jpg) > tmpFile");//照片存放在当前目录下
- }
-
- size_t readData(void *ptr, size_t size, size_t nmemb, void *stream){
- strncpy(buf, ptr, 1024);
- }
-
-
- char *getPicBase64FromFile(char *filePath){
- char *bufPic = NULL;
- char cmd[128] = {'\0'};
- sprintf(cmd, "base64 %s > tmpFile", filePath);
- system(cmd);//图片的base64流数据存入tmpFile文件中
-
- int fd = open("./tmpFile", O_RDWR);
- int filelen = lseek(fd, 0, SEEK_END);
- lseek(fd, 0, SEEK_SET);//重新让文件的光标回到初始位置
- bufPic = (char *)malloc(filelen + 2);//+1也可以 多加点没毛病
- memset(bufPic, '\0', filelen + 2);
- read(fd, bufPic, filelen);
- close(fd);
- system("rm -f tmpFile");
- return bufPic;
- }
-
- void cameraFaceRecognition(){
- camera.takeAPicture();
- CURL *curl;
- CURLcode res;
- char *postString;
- char *img1;
- char *img2;
- char *key = "DYRrmZz2rTwYGywyWdhKzR";
- char *secret = "56bc8e083a9b4d9fbf590413ddcb3a61";
- int typeId = 21;
- char *format = "xml";
- char *bufPic1 = getPicBase64FromFile("./image.jpg");
- char *bufPic2 = getPicBase64FromFile("./zyl.jpg");
-
- int len = strlen(key) + strlen(secret) + strlen(bufPic1) + strlen(bufPic2) + 128;
- postString = (char *)malloc(len);
- memset(postString, '\0', len);//sizeof(postString)替换成len,因为postString是指针
- 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"); // 指定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);
-
- struct Devices *beepHandler = findDeviceByName("beep", pdeviceHead);
- if(strstr(buf, "是") != NULL){
- beepHandler->open(beepHandler->pinNum); usleep(300000);
- beepHandler->close(beepHandler->pinNum);
- printf("\n");
- printf(" -----------------------------------\n");
- printf(" --\033[1;32m 人脸识别成功: the same person \033[0m--\n");//绿色字体
- printf(" -----------------------------------\n");
- printf("\n");
- }
- else{
- int i = 4;
- while(i--){
- beepHandler->open(beepHandler->pinNum); usleep(200000);
- beepHandler->close(beepHandler->pinNum); usleep(100000);
- }
- printf("\n");
- printf(" ------------------------------------\n");
- printf(" --\033[1;31m 人脸识别失败: different person \033[0m--\n");//红色字体
- printf(" ------------------------------------\n");
- printf("\n");
- }
- curl_easy_cleanup(curl);
- }
- }
-
- //实例化对象
- struct Devices camera = {
- .deviceName = "camera",
-
- .takeAPicture = cameraTakeAPicture,
- .faceRecognition = cameraFaceRecognition
-
- };
-
- struct Devices* addCameraToDeviceLink(struct Devices *phead){
- if(phead == NULL){
- return &camera;
- }
- else{//头插
- camera.next = phead;
- phead = &camera;
- return phead;
- }
- }
搭建环境所需的安装包:
编译时需要用到一些库文件和该库文件里的头文件,用到温湿度传感器时加上tempAndHumi.c
gcc bathroomLight.c livingroomLight.c restaurantLight.c upstairLight.c socketContrl.c voiceContrl.c fireDetection.c beep.c camera.c usartContrl.c main.c -I ../httpHandler/curl-7.71.1/_install/include/ -L ../httpHandler/curl-7.71.1/_install/lib/ -lcurl -lwiringPi -lpthread
运行:
sudo ./a.out
智能家居功能展示
PC端QT界面功能展示
安卓app功能展示
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。