目录
一、串口通信线程控制代码
mianPro.c
inputCommand.h
voiceControl.c
测试结果
二、语音控制部分
一、串口通信线程控制代码
mianPro.c
#include <pthread.h>
#include "controlDevice.h"
#include "inputCommand.h"struct InputCommand *pcommandHead = NULL; //定义指令工厂初始链表头struct InputCommand* findCommandByName(char *name, struct InputCommand *phead)
{struct InputCommand *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 *voiceControlThread(void *data) //“语音控制线程”执行函数
{ int nread;struct InputCommand *voiceHandler = NULL;voiceHandler = findCommandByName("voice", pcommandHead);if(voiceHandler == NULL){printf("find voiceHandler error\n");pthread_exit(NULL);}else{if(voiceHandler->Init(voiceHandler) < 0){ //“语音控制”功能初始化printf("voiceControl init error\n");pthread_exit(NULL);}else{printf("voiceControl init success\n");}while(1){memset(voiceHandler->command,'\0',sizeof(voiceHandler->command));nread = voiceHandler->getCommand(voiceHandler);if(nread == 0){ //串口没有获取到指令printf("No voiceCommand received\n");}else{ //获取到指令printf("Get VoiceCommand -->%s\n",voiceHandler->command); }} }
}int main()
{if (wiringPiSetup () == -1) { fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ; return 1 ; }pthread_t voiceControl_thread;//指令工厂初始化pcommandHead = addVoiceControlToInputCommandLink(pcommandHead);//语音线程pthread_create(&voiceControl_thread,NULL,voiceControlThread,NULL);pthread_join(voiceControl_thread, NULL); //主函数等待线程退出return 0;
}
inputCommand.h
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <wiringPi.h>
#include <wiringSerial.h>struct InputCommand
{char commandName[128]; //“控制方式”名char deviceName[128]; char command[32]; //存放指令int fd; //存放文件描述符(串口/网络)int s_fd; //存放服务器套接字描述符char port[12]; //存放端口号char ipAdress[32]; //存放 IP地址char log[1024]; //日志int (*Init)(struct InputCommand *voice); //“初始化”函数指针int (*getCommand)(struct InputCommand *voice); //“获取指令”函数指针struct InputCommand *next;
};struct InputCommand* addVoiceControlToInputCommandLink(struct InputCommand *phead); //“语音控制”加入指令链表函数声明
voiceControl.c
#include "inputCommand.h"int voiceInit(struct InputCommand *voice)
{int fd;if ((fd = serialOpen (voice->deviceName, 115200)) < 0) { fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ; return 1 ; }voice->fd = fd;return fd;
}int voiceGetCommand(struct InputCommand *voice)
{int nread = 0;nread = read(voice->fd, voice->command, sizeof(voice->command)); //读取串口return nread; //返回读取到数据的字节数,实际读取的指令放到了command里
}struct InputCommand voiceControl = {.commandName = "voice",.deviceName = "/dev/ttyS5",.command = '\0',.Init = voiceInit,.getCommand = voiceGetCommand,.log = {'\0'},.next = NULL
};struct InputCommand* addVoiceControlToInputCommandLink(struct InputCommand *phead) //“语音控制”(对象)加入指令方式链表函数
{if(phead == NULL){return &voiceControl;}else{voiceControl.next = phead;phead = &voiceControl;return phead;}
}
测试结果
二、串口语音控制部分
智能公元/AIOT快速产品化平台 (smartpi.cn)
void Command(struct InputCommand* CmdHandler)
{struct Devices *tmp =NULL;if(strcmp("OL1",CmdHandler->command) == 0){tmp = findDeviceByName("livingroomLight",pdeviceHead);if(tmp != NULL){tmp->open(tmp->pinNum);printf("已打开客厅灯\n");}}if(strcmp("CL1",CmdHandler->command) == 0){tmp = findDeviceByName("livingroomLight",pdeviceHead);if(tmp != NULL){tmp->close(tmp->pinNum);printf("已关闭客厅灯\n");}}if(strcmp("OL2",CmdHandler->command) == 0){tmp = findDeviceByName("restaurantLight",pdeviceHead);if(tmp != NULL){tmp->open(tmp->pinNum);printf("已打开餐厅灯\n");}}if(strcmp("CL2",CmdHandler->command) == 0){tmp = findDeviceByName("restaurantLight",pdeviceHead);if(tmp != NULL){tmp->close(tmp->pinNum);printf("已关闭餐厅灯\n");}}if(strcmp("OL3",CmdHandler->command) == 0){tmp = findDeviceByName("bedroomLight",pdeviceHead);if(tmp != NULL){tmp->open(tmp->pinNum);printf("已打开卧室灯\n");}}if(strcmp("CL3",CmdHandler->command) == 0){tmp = findDeviceByName("bedroomLight",pdeviceHead);if(tmp != NULL){tmp->close(tmp->pinNum);printf("已关闭卧室灯\n");}}if(strcmp("OL4",CmdHandler->command) == 0){tmp = findDeviceByName("bathroomLight",pdeviceHead);if(tmp != NULL){tmp->open(tmp->pinNum);printf("已打开浴室灯\n");}}if(strcmp("CL4",CmdHandler->command) == 0){tmp = findDeviceByName("bathroomLight",pdeviceHead);if(tmp != NULL){tmp->close(tmp->pinNum);printf("已关闭浴室灯\n");}}if(strcmp("OLALL",CmdHandler->command) == 0){tmp = findDeviceByName("livingroomLight",pdeviceHead);if(tmp != NULL) tmp->open(tmp->pinNum);tmp = findDeviceByName("restaurantLight",pdeviceHead);if(tmp != NULL) tmp->open(tmp->pinNum);tmp = findDeviceByName("bedroomLight",pdeviceHead);if(tmp != NULL) tmp->open(tmp->pinNum);tmp = findDeviceByName("bathroomLight",pdeviceHead);if(tmp != NULL) tmp->open(tmp->pinNum);printf("已打开所有灯\n");}if(strcmp("CLALL",CmdHandler->command) == 0){tmp = findDeviceByName("livingroomLight",pdeviceHead);if(tmp != NULL) tmp->close(tmp->pinNum);tmp = findDeviceByName("restaurantLight",pdeviceHead);if(tmp != NULL) tmp->close(tmp->pinNum);tmp = findDeviceByName("bedroomLight",pdeviceHead);if(tmp != NULL) tmp->close(tmp->pinNum);tmp = findDeviceByName("bathroomLight",pdeviceHead);if(tmp != NULL) tmp->close(tmp->pinNum);printf("已关闭所有灯\n");}
}
网络服务器线程控制也可以调用
智能家居项目目录
智能家居(1) —— 工厂模式引入&工厂模式实现继电器控制
智能家居(2) —— 工厂模式实现烟雾报警
智能家居(3) —— 串口通信(语音识别)线程控制
智能家居(4) —— 网络服务器线程控制
智能家居(5) —— 智能家居项目整合(语音控制线程,网络控制线程、烟雾报警线程)
网络编程知识预备(1) —— 7层OSI网络模型
网络编程知识预备(2) —— 三次握手与四次挥手、半连接状态、2MSL
网络编程知识预备(3) —— TCP流量控制(滑动窗口)、拥塞控制
网络编程知识预备(4) —— SOCKET、TCP、HTTP之间的区别与联系
网络编程知识预备(5) —— 了解应用层的HTTP协议与HTTPS协议
网络编程知识预备(6) —— libcurl库简介及其编程访问百度首页
智能家居(6) —— 香橙派摄像头安装实现监控功能
智能家居(7) —— 人脸识别 & 翔云平台编程使用(编译openSSL支持libcurl的https访问、安装SSL依赖库openSSL)
智能家居(8) —— 香橙派摄像头加入设备工厂