项目需求
需求1,2,3 --- 蜂鸣器,舵机,测距传感器的配合使用
实现思路:
代码展示:
v1.c:
需求4 --- socket服务器实现远程通讯控制的实现
代码展示:
v2_server.c:
v2_client.c:
实现效果:
需求5 --- 语音识别模块的配置,烧录和编程
1. 创建产品
2. 设置PIN脚为串口模式:
3. 设置唤醒词:
4. 设置指令语句:
5. 设置控制详情:
6. 其他配置,如声音,开机播报,主动退出等,都是按喜好设置
7. 下载SDK并烧写进入SU-03T
代码展示(client的代码不需要更新)
v3_server.c:
实现效果
需求6 --- 使用文件编程实现开关盖的历史记录
Linux显示当前的时间:date指令
代码展示(client的代码不需要更新)
v4_server:
实现效果
需求7 --- 使用嵌入式数据库实现开关盖的历史记录
代码展示(client的代码不需要更新)
v5_server.c:
实现效果
按照需求先逐个实现,然后最终整合:实际上除了需求8,其他知识点在之前都应该学习过了,这个项目的目的是对嵌入式Linux系统做一个知识点的阶段性整合复习,加深对于这些重要概念的理解。
创建一个“smart_bin”文件夹,将相关代码放在其中:
参考:
香橙派使用外设驱动库wiringOP来驱动蜂鸣器_mjmmm的博客-CSDN博客
香橙派使用外设驱动库wiringOP 配合时间函数来驱动测距模块-CSDN博客
香橙派使用外设驱动库wiringOP 配合定时器来驱动舵机_mjmmm的博客-CSDN博客
注意,在线程中如果想要使用printf打印调试信息可能会出现:无法与同步打印的问题;
解决方法是在printf之后紧跟着一句“ fflush(stdout)”
详见:
linux多线程拷贝信号量运用于线程之间通讯遇到的printf问题_linux printf卡住 导致所有进程printf卡住_爱出名的狗腿子的博客-CSDN博客
#include
#include
#include
#include
#include
#include
#include
#define PWM 5
#define BEEP 2
#define Trig 9
#define Echo 10
int angle;
double dist;
static int i;
void startHC() //先要给Trig一个至少10us/ms的高电平方波
{
digitalWrite (Trig, LOW) ;
delay(5); //5ms
digitalWrite (Trig, HIGH) ;
delay(5);
delay(5);
digitalWrite (Trig, LOW) ;
}
void signal_handler(int signum)
{
if(i <= angle){
digitalWrite(PWM, HIGH);
}else{
digitalWrite(PWM, LOW);
}
if(i == 40){ //40*500 = 20000us = 20ms
i = 0;
}
i++;
}
void *thread1(void *arg)
{
struct timeval startTime;
struct timeval stopTime;
double diffTime;
//double dist
while(1){
delay(300); //让HC-SR04稳定一下
startHC();
while(digitalRead(Echo) == 0); //程序会卡在这里直到Echo变高的一瞬间
gettimeofday(&startTime,NULL);
while(digitalRead(Echo) == 1); //程序会卡在这里直到Echo变低的一瞬间
gettimeofday(&stopTime,NULL);
diffTime = 1000000*(stopTime.tv_sec - startTime.tv_sec) + (stopTime.tv_usec - startTime.tv_usec);
dist = 0.034 * diffTime * 0.5;
//printf("dist = %f---",dist);
//fflush(stdout);
if(dist < 30 && angle == 1){//当距离小于30且盖子未开
angle = 3; //开盖
digitalWrite (BEEP, LOW) ; //蜂鸣器响
delay (100) ;
digitalWrite (BEEP, HIGH) ; //蜂鸣器不响
delay (100) ;
digitalWrite (BEEP, LOW) ; //蜂鸣器响
delay (100) ;
digitalWrite (BEEP, HIGH) ; //蜂鸣器不响
//sleep(2); //睡眠2秒,此处如果是sleep则运行到此处时其他线程会抢占,如果是delay则不会
delay(2000);
}else if(dist > 30 && angle == 3){//当距离大于30且盖子打开
angle = 1; //关盖
}
}
pthread_exit(NULL);
}
void *thread2(void *arg)
{
struct timeval startTime;
struct timeval stopTime;
double diffTime;
while(1){
while(angle == 1);//程序会卡在这里直到盖子打开
gettimeofday(&startTime,NULL);
while(angle == 3){
gettimeofday(&stopTime,NULL);
diffTime = (stopTime.tv_sec - startTime.tv_sec) + 1/1000000 *(stopTime.tv_usec - startTime.tv_usec);
if(diffTime > 10){ //盖子打开超过10秒
digitalWrite (BEEP, LOW) ; //蜂鸣器响
delay (1000) ; //一秒长鸣警报
digitalWrite (BEEP, HIGH) ; //蜂鸣器不响
break;
}
}
}
pthread_exit(NULL);
}
int main()
{
struct itimerval itv;
int ret;
//int param = 100;
pthread_t t1_id;
pthread_t t2_id;
wiringPiSetup () ;
pinMode (PWM, OUTPUT);
pinMode (Trig, OUTPUT);
pinMode (Echo, INPUT);
pinMode (BEEP, OUTPUT);
digitalWrite (Trig, LOW) ;
digitalWrite (Echo, LOW) ;
digitalWrite (BEEP, HIGH) ;
//设定定时时间
itv.it_interval.tv_sec = 0;
itv.it_interval.tv_usec = 500;
//设定开始生效,启动定时器的时间
itv.it_value.tv_sec = 1;
itv.it_value.tv_usec = 0;
//设定定时方式
if( -1 == setitimer(ITIMER_REAL, &itv, NULL)){
perror("error");
exit(-1);
}
//信号处理
signal(SIGALRM,signal_handler);
angle = 1;//初始化角度为关盖
ret = pthread_create(&t1_id,NULL,thread1,NULL);
if(ret != 0){
printf("thread1 create error\n");
}
ret = pthread_create(&t2_id,NULL,thread2,NULL);
if(ret != 0){
printf("thread2 create error\n");
}
pthread_join(t1_id,NULL);
pthread_join(t2_id,NULL);
return 0;
}
由于之前实现的代码都使用了线程;所以如果我现在和之前一样用fork来实现socket的accept和读写,那么fork之后线程数量会翻倍,同样的线程都会同时运行两个,而父子进程又可能对angle的值改变,引起混乱。
所以,我尝试将socket的编程也使用线程的方式来实现,由于在线程的实现中,会出现多个线程修改angle的情况,为了防止竞争,需要加互斥锁。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PWM 5
#define BEEP 2
#define Trig 9
#define Echo 10
int angle;
double dist;
static int i;
int sockfd;
int conn_sockfd;
int len = sizeof(struct sockaddr_in);
int ret;
char readbuf[128];
struct sockaddr_in my_addr;
struct sockaddr_in client_addr;
static int conn_flag;
pthread_mutex_t mutex;
int cmd_handler(int fd, char readbuf[128])
{
int ret;
int i = 0;
char str[128]; //将读到的数据备份在这里
strcpy(str,readbuf); //由于字符串的首地址是字符串的名字,所以此时相当于传入的地址,所有对字符串的操作都会影响它,所以需要进行备份,先备份再对备份的数据进行数据处理就不会影响原数据了
if(strcmp((char *)str,"open")==0 && angle == 1){ //收到open指令时垃圾桶盖子是关着的
ret = write(fd,"open success",20);
if(ret == -1){
perror("write");
pthread_exit(NULL);
}
angle = 3;
digitalWrite (BEEP, LOW) ; //蜂鸣器响
delay (100) ;
digitalWrite (BEEP, HIGH) ; //蜂鸣器不响
delay (100) ;
digitalWrite (BEEP, LOW) ; //蜂鸣器响
delay (100) ;
digitalWrite (BEEP, HIGH) ; //蜂鸣器不响
delay(2000);//延时2秒
}else if(strcmp((char *)str,"open")==0 && angle == 3){ //收到open指令时垃圾桶盖子是开着的
ret = write(fd,"already open!",20);
if(ret == -1){
perror("write");
pthread_exit(NULL);
}
}else if(strcmp((char *)str,"close")==0 && angle == 3){ //收到close指令时垃圾桶盖子是开着的
ret = write(fd,"close success",20);
if(ret == -1){
perror("write");
pthread_exit(NULL);
}
angle = 1;
delay(2000);//延时2秒
}else if(strcmp((char *)str,"close")==0 && angle == 1){ //收到close指令时垃圾桶盖子是关着的
ret = write(fd,"already close!",20);
if(ret == -1){
perror("write");
pthread_exit(NULL);
}
}else if(strcmp((char *)str,"quit")==0){ //如果客户端打出了quit
ret = write(fd,"Bye",4); //立刻回发一个Bye,目的是让客户端取消接收阻塞然后成功从FIFO读取到退出信息
if(ret == -1){
perror("write");
pthread_exit(NULL);
}
}
}
void startHC() //先要给Trig一个至少10us/ms的高电平方波
{
digitalWrite (Trig, LOW) ;
delay(5); //5ms
digitalWrite (Trig, HIGH) ;
delay(5);
delay(5);
digitalWrite (Trig, LOW) ;
}
void signal_handler(int signum)
{
if(i <= angle){
digitalWrite(PWM, HIGH);
}else{
digitalWrite(PWM, LOW);
}
if(i == 40){ //40*500 = 20000us = 20ms
i = 0;
}
i++;
}
void *thread1(void *arg)
{
struct timeval startTime;
struct timeval stopTime;
double diffTime;
//double dist
while(1){
delay(300); //让HC-SR04稳定一下
startHC();
while(digitalRead(Echo) == 0); //程序会卡在这里直到Echo变高的一瞬间
gettimeofday(&startTime,NULL);
while(digitalRead(Echo) == 1); //程序会卡在这里直到Echo变低的一瞬间
gettimeofday(&stopTime,NULL);
diffTime = 1000000*(stopTime.tv_sec - startTime.tv_sec) + (stopTime.tv_usec - startTime.tv_usec);
dist = 0.034 * diffTime * 0.5;
//printf("dist = %f---",dist);
//fflush(stdout);
pthread_mutex_lock(&mutex);//对angle值修改需要先上锁
if(dist < 30 && angle == 1){//当距离小于30且盖子未开
angle = 3; //开盖
digitalWrite (BEEP, LOW) ; //蜂鸣器响
delay (100) ;
digitalWrite (BEEP, HIGH) ; //蜂鸣器不响
delay (100) ;
digitalWrite (BEEP, LOW) ; //蜂鸣器响
delay (100) ;
digitalWrite (BEEP, HIGH) ; //蜂鸣器不响
delay(2000);
}else if(dist > 30 && angle == 3){//当距离大于30且盖子打开
angle = 1; //关盖
}
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void *thread2(void *arg)
{
struct timeval startTime;
struct timeval stopTime;
double diffTime;
while(1){
while(angle == 1);//程序会卡在这里直到盖子打开
gettimeofday(&startTime,NULL);
while(angle == 3){
gettimeofday(&stopTime,NULL);
diffTime = (stopTime.tv_sec - startTime.tv_sec) + 1/1000000 *(stopTime.tv_usec - startTime.tv_usec);
if(diffTime > 10){ //盖子打开超过10秒
digitalWrite (BEEP, LOW) ; //蜂鸣器响
delay (1000) ; //一秒长鸣警报
digitalWrite (BEEP, HIGH) ; //蜂鸣器不响
break;
}
}
}
pthread_exit(NULL);
}
void *thread3(void *arg)
{
while(1){
//accept
conn_sockfd = accept(sockfd,(struct sockaddr *)&client_addr,&len);
if(conn_sockfd == -1){
perror("accept");
pthread_exit(NULL);;
}else{
conn_flag = 1; //保证连接成功后才开始接收
printf("accept success, client IP = %s\n",inet_ntoa(client_addr.sin_addr));
fflush(stdout);
}
}
pthread_exit(NULL);
}
void *thread4(void *arg)
{
while(1){ //切不可直接“while(conn_flag == 1)”,这样在没有接入时会直接退出,要不停的循环查看flag的值
while(conn_flag == 1){
//read
memset(&readbuf,0,sizeof(readbuf));
ret = recv(conn_sockfd, &readbuf, sizeof(readbuf), 0);
if(ret == 0){ //如果recv函数返回0表示连接已经断开
printf("client has quit\n");
fflush(stdout);
close(conn_sockfd);
break;
}else if(ret == -1){
perror("recv");
conn_flag = 0; //此时打印一遍错误信息就会结束,如果不把flag置1,在一个客户端退出另一个客户端还未接入时就会不停的打印错误信息
//pthread_exit(NULL); //此处不能退出,因为因为这样如果有一个客户端接入并退出后这个线程就会退出,为了保证一个客户端退出后,另一个客户端还可以接入并正常工作,此处仅显示错误信息而不退出
}
pthread_mutex_lock(&mutex);//对angle值修改需要先上锁
cmd_handler(conn_sockfd, readbuf); //对客户端发来的消息进行判断的总函数
pthread_mutex_unlock(&mutex);
printf("\nclient: %s\n",readbuf);
fflush(stdout);
}
}
pthread_exit(NULL);
}
int main(int argc, char **argv)
{
struct itimerval itv;
//int param = 100;
pthread_t t1_id;
pthread_t t2_id;
pthread_t t3_id;
pthread_t t4_id;
memset(&my_addr,0,sizeof(struct sockaddr_in));
memset(&client_addr,0,sizeof(struct sockaddr_in));
if(argc != 3){
printf("param error!\n");
return 1;
}
wiringPiSetup () ;
pinMode (PWM, OUTPUT);
pinMode (Trig, OUTPUT);
pinMode (Echo, INPUT);
pinMode (BEEP, OUTPUT);
digitalWrite (Trig, LOW) ;
digitalWrite (Echo, LOW) ;
digitalWrite (BEEP, HIGH) ;
//设定定时时间
itv.it_interval.tv_sec = 0;
itv.it_interval.tv_usec = 500;
//设定开始生效,启动定时器的时间
itv.it_value.tv_sec = 1;
itv.it_value.tv_usec = 0;
//设定定时方式
if( -1 == setitimer(ITIMER_REAL, &itv, NULL)){
perror("error");
exit(-1);
}
//信号处理
signal(SIGALRM,signal_handler);
angle = 1;//初始化角度为关盖
//socket
sockfd = socket(AF_INET,SOCK_STREAM,0);
if(sockfd == -1){
perror("socket");
return 1;
}else{
printf("socket success, sockfd = %d\n",sockfd);
}
//bind
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(atoi(argv[2]));//host to net (2 bytes)
inet_aton(argv[1],&my_addr.sin_addr); //char* format -> net format
ret = bind(sockfd, (struct sockaddr *)&my_addr, len);
if(ret == -1){
perror("bind");
return 1;
}else{
printf("bind success\n");
}
//listen
ret = listen(sockfd,10);
if(ret == -1){
perror("listen");
return 1;
}else{
printf("listening...\n");
}
ret = pthread_mutex_init(&mutex, NULL);
if(ret != 0){
printf("mutex create error\n");
}
ret = pthread_create(&t1_id,NULL,thread1,NULL);
if(ret != 0){
printf("thread1 create error\n");
}
ret = pthread_create(&t2_id,NULL,thread2,NULL);
if(ret != 0){
printf("thread2 create error\n");
}
ret = pthread_create(&t3_id,NULL,thread3,NULL);
if(ret != 0){
printf("thread3 create error\n");
}
ret = pthread_create(&t4_id,NULL,thread4,NULL);
if(ret != 0){
printf("thread4 create error\n");
}
pthread_join(t1_id,NULL);
pthread_join(t2_id,NULL);
pthread_join(t3_id,NULL);
pthread_join(t4_id,NULL);
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char **argv)
{
int sockfd;
int ret;
int n_read;
int n_write;
char readbuf[128];
char msg[128];
int fd; //fifo
char fifo_readbuf[20] = {0};
char *fifo_msg = "quit";
pid_t fork_return;
if(argc != 3){
printf("param error!\n");
return 1;
}
struct sockaddr_in server_addr;
memset(&server_addr,0,sizeof(struct sockaddr_in));
//socket
sockfd = socket(AF_INET,SOCK_STREAM,0);
if(sockfd == -1){
perror("socket");
return 1;
}else{
printf("socket success, sockfd = %d\n",sockfd);
}
//connect
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(atoi(argv[2]));//host to net (2 bytes)
inet_aton(argv[1],&server_addr.sin_addr);
ret = connect(sockfd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr_in));
if(ret == -1){
perror("connect");
return 1;
}else{
printf("connect success!\n");
}
//fifo
if(mkfifo("./fifo",S_IRWXU) == -1 && errno != EEXIST)
{
perror("fifo");
}
//fork
fork_return = fork();
if(fork_return > 0){//father keeps writing msg
while(1){
//write
memset(&msg,0,sizeof(msg));
//printf("\ntype msg:");
scanf("%s",(char *)msg);
n_write = write(sockfd,&msg,strlen(msg));
if(msg[0]=='q' && msg[1]=='u' && msg[2]=='i' && msg[3]=='t'){
printf("quit detected!\n");
fd = open("./fifo",O_WRONLY);
write(fd,fifo_msg,strlen(fifo_msg));
close(fd);
close(sockfd);
wait(NULL);
break;
}
if(n_write == -1){
perror("write");
return 1;
}else{
printf("%d bytes msg sent\n",n_write);
}
}
}else if(fork_return < 0){
perror("fork");
return 1;
}else{//son keeps reading
while(1){
fd = open("./fifo",O_RDONLY|O_NONBLOCK);
lseek(fd, 0, SEEK_SET);
read(fd,&fifo_readbuf,20);
//printf("read from fifo:%s\n",fifo_readbuf);
if(fifo_readbuf[0]=='q' && fifo_readbuf[1]=='u' && fifo_readbuf[2]=='i' && fifo_readbuf[3]=='t'){
exit(1);
}
//read
memset(&readbuf,0,sizeof(readbuf));
n_read = read(sockfd,&readbuf,128);
if(n_read == -1){
perror("read");
return 1;
}else{
printf("\nserver: %s\n",readbuf);
}
}
}
return 0;
}
编译两个C文件:
然后先启动服务器端:
此时,如果距离检测到30cm以内就会开盖了,只是还没有客户端无法远程控制
再启动客户端:
反观服务端:
此时在客户端输入open或close即可实时遥控,并可以得到服务器的反馈:
如果客户端输入“quit”即可退出,并支持新的客户端的接入
(比如如果盖子已经打开,收到open会提示已经打开等..)
- 此处我先使用open指令打开盖子两秒
- 等盖子关闭后我靠近测距传感器让盖子打开两秒后再次输入open指令
- 然后在盖子打开时输入close指令,同时把手拿开(如果不把手拿开,盖子会因为close指令关闭两秒,然后因为距离小于30再次打开)
- 最后在盖子关上后输入close指令
- 由于盖子没开且收到了open指令,所以盖子打开2秒
- 由于盖子已经打开且收到了open指令,所以提示“盖子已经打开”
- 由于盖子打开且收到了close指令,所以盖子关闭2秒
- 由于盖子关闭且收到了close指令,所以提示“盖子已经关闭”
和之前学习的一样,使用SU-03T来实现语音识别,先进行语音识别的配置,以下只展示关键截图,具体的步骤和烧写流程参考我之前写的两篇:
基于香橙派和SU-03T 使用Linux实现语音控制刷抖音-CSDN博客
语音小车---6 + 最终整合-CSDN博客
对于SU-03T,串口的RX和TX分别对应B6和B7
并设置相应的波特率:
参数的设置就是行为的名字 -> 16进制ASCII码,已空格分开
open -> 6F 70 65 6E
close ->63 6C 6F 73 65
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "mjm_uart_tool.h"
#define PWM 5
#define BEEP 2
#define Trig 9
#define Echo 10
int angle;
double dist;
static int i;
int sockfd;
int conn_sockfd;
int len = sizeof(struct sockaddr_in);
int ret;
char readbuf[128];
struct sockaddr_in my_addr;
struct sockaddr_in client_addr;
static int conn_flag;
pthread_mutex_t mutex;
int cmd_handler(int fd, char readbuf[128])
{
int ret;
int i = 0;
char str[128]; //将读到的数据备份在这里
strcpy(str,readbuf); //由于字符串的首地址是字符串的名字,所以此时相当于传入的地址,所有对字符串的操作都会影响它,所以需要进行备份,先备份再对备份的数据进行数据处理就不会影响原数据了
if(strcmp((char *)str,"open")==0 && angle == 1){ //收到open指令时垃圾桶盖子是关着的
ret = write(fd,"open success",20);
if(ret == -1){
perror("write");
pthread_exit(NULL);
}
angle = 3;
digitalWrite (BEEP, LOW) ; //蜂鸣器响
delay (100) ;
digitalWrite (BEEP, HIGH) ; //蜂鸣器不响
delay (100) ;
digitalWrite (BEEP, LOW) ; //蜂鸣器响
delay (100) ;
digitalWrite (BEEP, HIGH) ; //蜂鸣器不响
delay(2000);//延时2秒
}else if(strcmp((char *)str,"open")==0 && angle == 3){ //收到open指令时垃圾桶盖子是开着的
ret = write(fd,"already open!",20);
if(ret == -1){
perror("write");
pthread_exit(NULL);
}
}else if(strcmp((char *)str,"close")==0 && angle == 3){ //收到close指令时垃圾桶盖子是开着的
ret = write(fd,"close success",20);
if(ret == -1){
perror("write");
pthread_exit(NULL);
}
angle = 1;
delay(2000);//延时2秒
}else if(strcmp((char *)str,"close")==0 && angle == 1){ //收到close指令时垃圾桶盖子是关着的
ret = write(fd,"already close!",20);
if(ret == -1){
perror("write");
pthread_exit(NULL);
}
}else if(strcmp((char *)str,"quit")==0){ //如果客户端打出了quit
ret = write(fd,"Bye",4); //立刻回发一个Bye,目的是让客户端取消接收阻塞然后成功从FIFO读取到退出信息
if(ret == -1){
perror("write");
pthread_exit(NULL);
}
}
}
void startHC() //先要给Trig一个至少10us/ms的高电平方波
{
digitalWrite (Trig, LOW) ;
delay(5); //5ms
digitalWrite (Trig, HIGH) ;
delay(5);
delay(5);
digitalWrite (Trig, LOW) ;
}
void signal_handler(int signum)
{
if(i <= angle){
digitalWrite(PWM, HIGH);
}else{
digitalWrite(PWM, LOW);
}
if(i == 40){ //40*500 = 20000us = 20ms
i = 0;
}
i++;
}
void *thread1(void *arg) //负责不断检测距离,小于30cm就滴滴开盖的线程
{
struct timeval startTime;
struct timeval stopTime;
double diffTime;
//double dist
while(1){
delay(300); //让HC-SR04稳定一下
startHC();
while(digitalRead(Echo) == 0); //程序会卡在这里直到Echo变高的一瞬间
gettimeofday(&startTime,NULL);
while(digitalRead(Echo) == 1); //程序会卡在这里直到Echo变低的一瞬间
gettimeofday(&stopTime,NULL);
diffTime = 1000000*(stopTime.tv_sec - startTime.tv_sec) + (stopTime.tv_usec - startTime.tv_usec);
dist = 0.034 * diffTime * 0.5;
//printf("dist = %f---",dist);
//fflush(stdout);
pthread_mutex_lock(&mutex);//对angle值修改需要先上锁
if(dist < 30 && angle == 1){//当距离小于30且盖子未开
angle = 3; //开盖
digitalWrite (BEEP, LOW) ; //蜂鸣器响
delay (100) ;
digitalWrite (BEEP, HIGH) ; //蜂鸣器不响
delay (100) ;
digitalWrite (BEEP, LOW) ; //蜂鸣器响
delay (100) ;
digitalWrite (BEEP, HIGH) ; //蜂鸣器不响
//sleep(2); //睡眠2秒,此处如果是sleep则运行到此处时其他线程会抢占,如果是delay则不会
delay(2000);
}else if(dist > 30 && angle == 3){//当距离大于30且盖子打开
angle = 1; //关盖
}
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void *thread2(void *arg) //负责检测开盖时间,超过10s就报警的线程
{
struct timeval startTime;
struct timeval stopTime;
double diffTime;
while(1){
while(angle == 1);//程序会卡在这里直到盖子打开
gettimeofday(&startTime,NULL);
while(angle == 3){
gettimeofday(&stopTime,NULL);
diffTime = (stopTime.tv_sec - startTime.tv_sec) + 1/1000000 *(stopTime.tv_usec - startTime.tv_usec);
if(diffTime > 10){ //盖子打开超过10秒
digitalWrite (BEEP, LOW) ; //蜂鸣器响
delay (1000) ; //一秒长鸣警报
digitalWrite (BEEP, HIGH) ; //蜂鸣器不响
break;
}
}
}
pthread_exit(NULL);
}
void *thread3(void *arg) //负责不断等待socket客户端接入的线程
{
while(1){
//accept
conn_sockfd = accept(sockfd,(struct sockaddr *)&client_addr,&len);
if(conn_sockfd == -1){
perror("accept");
pthread_exit(NULL);;
}else{
conn_flag = 1; //保证连接成功后才开始接收
printf("accept success, client IP = %s\n",inet_ntoa(client_addr.sin_addr));
fflush(stdout);
}
}
pthread_exit(NULL);
}
void *thread4(void *arg) //负责socket客户端接入后接收处理客户端指令的线程
{
while(1){ //切不可直接“while(conn_flag == 1)”,这样在没有接入时会直接退出,要不停的循环查看flag的值
while(conn_flag == 1){
//read
memset(&readbuf,0,sizeof(readbuf));
ret = recv(conn_sockfd, &readbuf, sizeof(readbuf), 0);
if(ret == 0){ //如果recv函数返回0表示连接已经断开
printf("client has quit\n");
fflush(stdout);
close(conn_sockfd);
break;
}else if(ret == -1){
perror("recv");
conn_flag = 0; //此时打印一遍错误信息就会结束,如果不把flag置1,在一个客户端退出另一个客户端还未接入时就会不停的打印错误信息
//pthread_exit(NULL); //此处不能退出,因为因为这样如果有一个客户端接入并退出后这个线程就会退出,为了保证一个客户端退出后,另一个客户端还可以接入并正常工作,此处仅显示错误信息而不退出
}
pthread_mutex_lock(&mutex);//对angle值修改需要先上锁
cmd_handler(conn_sockfd, readbuf); //对客户端发来的消息进行判断的总函数
pthread_mutex_unlock(&mutex);
printf("\nclient: %s\n",readbuf);
fflush(stdout);
}
}
pthread_exit(NULL);
}
void *thread5(void *arg) //负责通过串口接收语音模块指令的线程
{
char readbuf[32] = {'\0'};
while(1){
while(serialDataAvail (*((int *)arg))){
serialGetstring (*((int *)arg),readbuf) ;
//printf("-> %s\n",readbuf);
pthread_mutex_lock(&mutex);//对angle值修改需要先上锁
if(strcmp(readbuf,"open") == 0 && angle == 1){ //当收到open指令且盖子关闭时
angle = 3; //开盖
digitalWrite (BEEP, LOW) ; //蜂鸣器响
delay (100) ;
digitalWrite (BEEP, HIGH) ; //蜂鸣器不响
delay (100) ;
digitalWrite (BEEP, LOW) ; //蜂鸣器响
delay (100) ;
digitalWrite (BEEP, HIGH) ; //蜂鸣器不响
delay(2000);//延时2秒
}else if(strcmp(readbuf,"close") == 0 && angle == 3){//当收到close指令且盖子打开时
angle = 1; //关盖
delay(2000);//延时2秒
}else if(strcmp(readbuf,"open") == 0 && angle == 3){
printf("already open\n");
fflush(stdout);
}else if(strcmp(readbuf,"close") == 0 && angle == 1){
printf("already close\n");
fflush(stdout);
}else{
printf("unkown command\n");
fflush(stdout);
}
pthread_mutex_unlock(&mutex);
memset(readbuf,'\0',32);
}
}
pthread_exit(NULL);
}
int main(int argc, char **argv)
{
struct itimerval itv;
int io_fd;
pthread_t t1_id;
pthread_t t2_id;
pthread_t t3_id;
pthread_t t4_id;
pthread_t t5_id;
memset(&my_addr,0,sizeof(struct sockaddr_in));
memset(&client_addr,0,sizeof(struct sockaddr_in));
if(argc != 3){
printf("param error!\n");
return 1;
}
wiringPiSetup () ;
pinMode (PWM, OUTPUT);
pinMode (Trig, OUTPUT);
pinMode (Echo, INPUT);
pinMode (BEEP, OUTPUT);
digitalWrite (Trig, LOW) ;
digitalWrite (Echo, LOW) ;
digitalWrite (BEEP, HIGH) ;
//设定定时时间
itv.it_interval.tv_sec = 0;
itv.it_interval.tv_usec = 500;
//设定开始生效,启动定时器的时间
itv.it_value.tv_sec = 1;
itv.it_value.tv_usec = 0;
//设定定时方式
if( -1 == setitimer(ITIMER_REAL, &itv, NULL)){
perror("error");
exit(-1);
}
//信号处理
signal(SIGALRM,signal_handler);
angle = 1;//初始化角度为关盖
//打开串口驱动文件,配置波特率
if ((io_fd = myserialOpen ("/dev/ttyS5", 115200)) < 0) {
fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;
return 1 ;
}
//socket
sockfd = socket(AF_INET,SOCK_STREAM,0);
if(sockfd == -1){
perror("socket");
return 1;
}else{
printf("socket success, sockfd = %d\n",sockfd);
}
//bind
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(atoi(argv[2]));//host to net (2 bytes)
inet_aton(argv[1],&my_addr.sin_addr); //char* format -> net format
ret = bind(sockfd, (struct sockaddr *)&my_addr, len);
if(ret == -1){
perror("bind");
return 1;
}else{
printf("bind success\n");
}
//listen
ret = listen(sockfd,10);
if(ret == -1){
perror("listen");
return 1;
}else{
printf("listening...\n");
}
ret = pthread_mutex_init(&mutex, NULL);
if(ret != 0){
printf("mutex create error\n");
}
ret = pthread_create(&t1_id,NULL,thread1,NULL);
if(ret != 0){
printf("thread1 create error\n");
}
ret = pthread_create(&t2_id,NULL,thread2,NULL);
if(ret != 0){
printf("thread2 create error\n");
}
ret = pthread_create(&t3_id,NULL,thread3,NULL);
if(ret != 0){
printf("thread3 create error\n");
}
ret = pthread_create(&t4_id,NULL,thread4,NULL);
if(ret != 0){
printf("thread4 create error\n");
}
ret = pthread_create(&t5_id,NULL,thread5,(void *)&io_fd);
if(ret != 0){
printf("thread5 create error\n");
}
pthread_join(t1_id,NULL);
pthread_join(t2_id,NULL);
pthread_join(t3_id,NULL);
pthread_join(t4_id,NULL);
pthread_join(t5_id,NULL);
return 0;
}
由于此处使用了之前自己写的串口库,所以编译的时候需要连同那个串口库一起编译:
gcc mjm_uart_tool.c v3_server.c -lwiringPi -lwiringPiDev -lpthread -lm -lcrypt -lrt -o v3_server
1. 开机播报“小智随时准备着”
1. 当说出“你好小智"可以唤醒模块,模块回复“我是小智,你好”
2. 当超过10s没有指令或说出“退下”时,模块会进入休眠模式,并回复“有需要再叫我”
3. 当说出“打开盖子”时,模块回复“已为您打开盖子”,并打开盖子
4. 当说出“关闭盖子”时,模块回复“已为您关闭盖子”,并关闭盖子
回顾具体的需求:
记录一段时间内垃圾桶开关盖动作及开关盖指令来源并记录在文件中
回顾之前关于文件学习的博文:
文件的写入 和 读取_mjmmm的博客-CSDN博客
Linux 系统编程 开篇/ 文件的打开/创建-CSDN博客
详见:
Linux打印时间_笔记大全_设计学院
总结一下,就是使用以下指令即可打印当前的 “年-月-日-时-分-秒”
date +"%Y-%m-%d %H:%M:%S"
那么,相应在程序中调用popen函数执行这句话,就可以得到时间的字符串,将“时间的字符串”和“表示开关指令来源的字符串”以及“记录开关动作的字符串”进行拼接,就形成了一条历史记录;然后另开一个线程检测时间,当时间到达一天时,就将历史记录全部写入到指定的文件中。
关于字符串的拼接,使用的是strcat函数,在我之前的博文也使用过,注意strcat的对象必须是可变的字符串:
小插曲 -- 使用Linux编写 判断程序是否在运行的小程序-CSDN博客
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "mjm_uart_tool.h"
#define PWM 5
#define BEEP 2
#define Trig 9
#define Echo 10
int angle;
double dist;
static int i;
int sockfd;
int conn_sockfd;
int len = sizeof(struct sockaddr_in);
int ret;
char readbuf[128];
struct sockaddr_in my_addr;
struct sockaddr_in client_addr;
static int conn_flag;
char hist[128] = {0}; //用于存放一条历史记录,注意这个变量一定不能在create_hist里赋值,因为局部指针变量作为函数的返回值没有意义,会报错; 且作为strcat的对象必须是字符串变量
char hist_whole[10000] = {0}; //用于存放所有历史记录; 且作为strcat的对象必须是字符串变量i
pthread_mutex_t mutex;
char *create_hist(int type, int action)//用于生成一条历史记录的函数
//type的值1;2;3分别对应距离靠近开盖;客户端指令开盖;语音识别开盖
//action的值1;2分别对应开盖;关盖
{
FILE *fp;
char *dist = "||distance close||";
char *sock = "||client request||";
char *soun = "||voice command||";
char *open = "open action||";
char *close = "close action||";
char *c = "\n";
memset(&hist,'\0',sizeof(hist));
fp = popen("date +\"%Y-%m-%d %H:%M:%S\"","r");
fread(&hist, sizeof(char), 128, fp);
if(type == 1){ //如果时距离靠近导致的开关盖
strcat(hist,dist);
}else if(type == 2){ //如果是客户端指令导致的开关盖
strcat(hist,sock);
}else if(type == 3){ //如果是语音识别导致的开关盖
strcat(hist,soun);
}
if(action == 1){
strcat(hist,open);
}else if(action == 2){
strcat(hist,close);
}
strcat(hist,c);//每条历史记录结束加上一个换行键
return (char *)hist;
}
int cmd_handler(int fd, char readbuf[128])
{
int ret;
int i = 0;
char str[128]; //将读到的数据备份在这里
strcpy(str,readbuf); //由于字符串的首地址是字符串的名字,所以此时相当于传入的地址,所有对字符串的操作都会影响它,所以需要进行备份,先备份再对备份的数据进行数据处理就不会影响原数据了
if(strcmp((char *)str,"open")==0 && angle == 1){ //收到open指令时垃圾桶盖子是关着的
ret = write(fd,"open success",20);
if(ret == -1){
perror("write");
pthread_exit(NULL);
}
angle = 3;
create_hist(2,1);//构建一条历史记录
strcat(hist_whole,hist);//在总的历史记录中添加刚刚构建的历史记录
digitalWrite (BEEP, LOW) ; //蜂鸣器响
delay (100) ;
digitalWrite (BEEP, HIGH) ; //蜂鸣器不响
delay (100) ;
digitalWrite (BEEP, LOW) ; //蜂鸣器响
delay (100) ;
digitalWrite (BEEP, HIGH) ; //蜂鸣器不响
delay(2000);//延时2秒
}else if(strcmp((char *)str,"open")==0 && angle == 3){ //收到open指令时垃圾桶盖子是开着的
ret = write(fd,"already open!",20);
if(ret == -1){
perror("write");
pthread_exit(NULL);
}
}else if(strcmp((char *)str,"close")==0 && angle == 3){ //收到close指令时垃圾桶盖子是开着的
ret = write(fd,"close success",20);
if(ret == -1){
perror("write");
pthread_exit(NULL);
}
angle = 1;
create_hist(2,2);//构建一条历史记录
strcat(hist_whole,hist);//在总的历史记录中添加刚刚构建的历史记录
delay(2000);//延时2秒
}else if(strcmp((char *)str,"close")==0 && angle == 1){ //收到close指令时垃圾桶盖子是关着的
ret = write(fd,"already close!",20);
if(ret == -1){
perror("write");
pthread_exit(NULL);
}
}else if(strcmp((char *)str,"quit")==0){ //如果客户端打出了quit
ret = write(fd,"Bye",4); //立刻回发一个Bye,目的是让客户端取消接收阻塞然后成功从FIFO读取到退出信息
if(ret == -1){
perror("write");
pthread_exit(NULL);
}
}
}
void startHC() //先要给Trig一个至少10us/ms的高电平方波
{
digitalWrite (Trig, LOW) ;
delay(5); //5ms
digitalWrite (Trig, HIGH) ;
delay(5);
delay(5);
digitalWrite (Trig, LOW) ;
}
void signal_handler(int signum)
{
if(i <= angle){
digitalWrite(PWM, HIGH);
}else{
digitalWrite(PWM, LOW);
}
if(i == 40){ //40*500 = 20000us = 20ms
i = 0;
}
i++;
}
void *thread1(void *arg) //负责不断检测距离,小于30cm就滴滴开盖的线程
{
struct timeval startTime;
struct timeval stopTime;
double diffTime;
//double dist
while(1){
delay(300); //让HC-SR04稳定一下
startHC();
while(digitalRead(Echo) == 0); //程序会卡在这里直到Echo变高的一瞬间
gettimeofday(&startTime,NULL);
while(digitalRead(Echo) == 1); //程序会卡在这里直到Echo变低的一瞬间
gettimeofday(&stopTime,NULL);
diffTime = 1000000*(stopTime.tv_sec - startTime.tv_sec) + (stopTime.tv_usec - startTime.tv_usec);
dist = 0.034 * diffTime * 0.5;
//printf("dist = %f---",dist);
//fflush(stdout);
pthread_mutex_lock(&mutex);//对angle值修改需要先上锁
if(dist < 30 && angle == 1){//当距离小于30且盖子未开
angle = 3; //开盖
create_hist(1,1);//构建一条历史记录
strcat(hist_whole,hist);//在总的历史记录中添加刚刚构建的历史记录
digitalWrite (BEEP, LOW) ; //蜂鸣器响
delay (100) ;
digitalWrite (BEEP, HIGH) ; //蜂鸣器不响
delay (100) ;
digitalWrite (BEEP, LOW) ; //蜂鸣器响
delay (100) ;
digitalWrite (BEEP, HIGH) ; //蜂鸣器不响
delay(2000);
}else if(dist > 30 && angle == 3){//当距离大于30且盖子打开
angle = 1; //关盖
create_hist(1,2);//构建一条历史记录
strcat(hist_whole,hist);//在总的历史记录中添加刚刚构建的历史记录
}
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void *thread2(void *arg) //负责检测开盖时间,超过10s就报警的线程
{
struct timeval startTime;
struct timeval stopTime;
double diffTime;
while(1){
while(angle == 1);//程序会卡在这里直到盖子打开
gettimeofday(&startTime,NULL);
while(angle == 3){
gettimeofday(&stopTime,NULL);
diffTime = (stopTime.tv_sec - startTime.tv_sec) + 1/1000000 *(stopTime.tv_usec - startTime.tv_usec);
if(diffTime > 10){ //盖子打开超过10秒
digitalWrite (BEEP, LOW) ; //蜂鸣器响
delay (1000) ; //一秒长鸣警报
digitalWrite (BEEP, HIGH) ; //蜂鸣器不响
break;
}
}
}
pthread_exit(NULL);
}
void *thread3(void *arg) //负责不断等待socket客户端接入的线程
{
while(1){
//accept
conn_sockfd = accept(sockfd,(struct sockaddr *)&client_addr,&len);
if(conn_sockfd == -1){
perror("accept");
pthread_exit(NULL);;
}else{
conn_flag = 1; //保证连接成功后才开始接收
printf("accept success, client IP = %s\n",inet_ntoa(client_addr.sin_addr));
fflush(stdout);
}
}
pthread_exit(NULL);
}
void *thread4(void *arg) //负责socket客户端接入后接收处理客户端指令的线程
{
while(1){ //切不可直接“while(conn_flag == 1)”,这样在没有接入时会直接退出,要不停的循环查看flag的值
while(conn_flag == 1){
//read
memset(&readbuf,0,sizeof(readbuf));
ret = recv(conn_sockfd, &readbuf, sizeof(readbuf), 0);
if(ret == 0){ //如果recv函数返回0表示连接已经断开
printf("client has quit\n");
fflush(stdout);
close(conn_sockfd);
break;
}else if(ret == -1){
perror("recv");
conn_flag = 0; //此时打印一遍错误信息就会结束,如果不把flag置1,在一个客户端退出另一个客户端还未接入时就会不停的打印错误信息
//pthread_exit(NULL); //此处不能退出,因为因为这样如果有一个客户端接入并退出后这个线程就会退出,为了保证一个客户端退出后,另一个客户端还可以接入并正常工作,此处仅显示错误信息而不退出
}
pthread_mutex_lock(&mutex);//对angle值修改需要先上锁
cmd_handler(conn_sockfd, readbuf); //对客户端发来的消息进行判断的总函数
pthread_mutex_unlock(&mutex);
printf("\nclient: %s\n",readbuf);
fflush(stdout);
}
}
pthread_exit(NULL);
}
void *thread5(void *arg) //负责通过串口接收语音模块指令的线程
{
char io_readbuf[32] = {'\0'};
while(1){
while(serialDataAvail (*((int *)arg))){
serialGetstring (*((int *)arg),io_readbuf) ;
//printf("-> %s\n",io_readbuf);
pthread_mutex_lock(&mutex);//对angle值修改需要先上锁
if(strcmp(io_readbuf,"open") == 0 && angle == 1){ //当收到open指令且盖子关闭时
angle = 3; //开盖
create_hist(3,1);//构建一条历史记录
strcat(hist_whole,hist);//在总的历史记录中添加刚刚构建的历史记录
digitalWrite (BEEP, LOW) ; //蜂鸣器响
delay (100) ;
digitalWrite (BEEP, HIGH) ; //蜂鸣器不响
delay (100) ;
digitalWrite (BEEP, LOW) ; //蜂鸣器响
delay (100) ;
digitalWrite (BEEP, HIGH) ; //蜂鸣器不响
delay(2000);//延时2秒
}else if(strcmp(io_readbuf,"close") == 0 && angle == 3){//当收到close指令且盖子打开时
angle = 1; //关盖
create_hist(3,2);//构建一条历史记录
strcat(hist_whole,hist);//在总的历史记录中添加刚刚构建的历史记录
delay(2000);//延时2秒
}else if(strcmp(io_readbuf,"open") == 0 && angle == 3){
printf("already open\n");
fflush(stdout);
}else if(strcmp(io_readbuf,"close") == 0 && angle == 1){
printf("already close\n");
fflush(stdout);
}else{
printf("unkown command\n");
fflush(stdout);
}
pthread_mutex_unlock(&mutex);
memset(io_readbuf,'\0',32);
}
}
pthread_exit(NULL);
}
void *thread6(void *arg) //负责每隔一段时间写入历史记录的线程
{
struct timeval startTime;
struct timeval stopTime;
double diffTime;
int hist_fd; // file description
while(1){
gettimeofday(&startTime,NULL);
while(1){
gettimeofday(&stopTime,NULL);
diffTime = (stopTime.tv_sec - startTime.tv_sec) + 1/1000000 *(stopTime.tv_usec - startTime.tv_usec); //单位为秒
if(diffTime > 60){//如果时间是2分钟,由于线程的竞争机制,不一定会非常精确,所以使用>号
hist_fd = open("./history.txt",O_RDWR|O_CREAT|O_APPEND, 0666); //可读可写可打开的打开历史记录的文件,不存在就创建,且每次都追加写入
if(hist_fd < 0){
printf("fail to open history file!\n");
fflush(stdout);
}
ret = write(hist_fd, &hist_whole, strlen(hist_whole));
if(ret == -1){
printf("fail to write history write to file!\n");
fflush(stdout);
}else{
printf("write the following history write to file:\n");
printf("------------------------\n");
printf("%s",hist_whole);
printf("------------------------\n");
fflush(stdout);
}
close(hist_fd);
memset(hist_whole,'\0',sizeof(hist_whole)); //清空hist_whole!
break;
}
}
}
pthread_exit(NULL);
}
int main(int argc, char **argv)
{
struct itimerval itv;
int io_fd;
pthread_t t1_id;
pthread_t t2_id;
pthread_t t3_id;
pthread_t t4_id;
pthread_t t5_id;
pthread_t t6_id;
memset(&my_addr,0,sizeof(struct sockaddr_in));
memset(&client_addr,0,sizeof(struct sockaddr_in));
if(argc != 3){
printf("param error!\n");
return 1;
}
wiringPiSetup () ;
pinMode (PWM, OUTPUT);
pinMode (Trig, OUTPUT);
pinMode (Echo, INPUT);
pinMode (BEEP, OUTPUT);
digitalWrite (Trig, LOW) ;
digitalWrite (Echo, LOW) ;
digitalWrite (BEEP, HIGH) ;
//设定定时时间
itv.it_interval.tv_sec = 0;
itv.it_interval.tv_usec = 500;
//设定开始生效,启动定时器的时间
itv.it_value.tv_sec = 1;
itv.it_value.tv_usec = 0;
//设定定时方式
if( -1 == setitimer(ITIMER_REAL, &itv, NULL)){
perror("error");
exit(-1);
}
//信号处理
signal(SIGALRM,signal_handler);
angle = 1;//初始化角度为关盖
//打开串口驱动文件,配置波特率
if ((io_fd = myserialOpen ("/dev/ttyS5", 115200)) < 0) {
fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;
return 1 ;
}
//socket
sockfd = socket(AF_INET,SOCK_STREAM,0);
if(sockfd == -1){
perror("socket");
return 1;
}else{
printf("socket success, sockfd = %d\n",sockfd);
}
//bind
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(atoi(argv[2]));//host to net (2 bytes)
inet_aton(argv[1],&my_addr.sin_addr); //char* format -> net format
ret = bind(sockfd, (struct sockaddr *)&my_addr, len);
if(ret == -1){
perror("bind");
return 1;
}else{
printf("bind success\n");
}
//listen
ret = listen(sockfd,10);
if(ret == -1){
perror("listen");
return 1;
}else{
printf("listening...\n");
}
ret = pthread_mutex_init(&mutex, NULL);
if(ret != 0){
printf("mutex create error\n");
}
ret = pthread_create(&t1_id,NULL,thread1,NULL);
if(ret != 0){
printf("thread1 create error\n");
}
ret = pthread_create(&t2_id,NULL,thread2,NULL);
if(ret != 0){
printf("thread2 create error\n");
}
ret = pthread_create(&t3_id,NULL,thread3,NULL);
if(ret != 0){
printf("thread3 create error\n");
}
ret = pthread_create(&t4_id,NULL,thread4,NULL);
if(ret != 0){
printf("thread4 create error\n");
}
ret = pthread_create(&t5_id,NULL,thread5,(void *)&io_fd);
if(ret != 0){
printf("thread5 create error\n");
}
ret = pthread_create(&t6_id,NULL,thread6,NULL);
if(ret != 0){
printf("thread6 create error\n");
}
pthread_join(t1_id,NULL);
pthread_join(t2_id,NULL);
pthread_join(t3_id,NULL);
pthread_join(t4_id,NULL);
pthread_join(t5_id,NULL);
pthread_join(t6_id,NULL);
return 0;
}
由于测试需要,我将代码修改为每隔1分钟向文件写入并在屏幕上打印历史记录,如果需要实现每隔5分钟,1小时,1天,只需要修改thread6里difftime的判断条件即可!
编译并运行代码:
gcc mjm_uart_tool.c v4_server.c -lwiringPi -lwiringPiDev -lpthread -lm -lcrypt -lrt -o v4_server
然后接入v2_client,模拟各种操作,包括距离接近开关盖;客户端指令开关盖;语音控制开关盖....
可见,每隔一分钟,就会生成这一分钟内的所有历史记录并打印在屏幕上:
同时,打开该路径下新创建的“history.txt” ,可见历史记录已追加的方式不断写入:
回顾需求:
统计一段时间内垃圾桶开关盖次数及开关盖指令来源并记录在数据库中
回顾之前学习嵌入式数据库的博文:
使用香橙派学习 嵌入式数据库---SQLite-CSDN博客
注意,虽然同样是历史记录,但是写入文件的是详细的每一次的记录,而写入数据库的是开关盖次数和指令来源的统计!
思路是创建一个名为history的数据库,然后在其中创建一个名为history的表格,表格有三个字段,分别为char型的cause;Integer型的open;Integer型的close
然后分别就每一种指令形式创建开关盖的计数量,在刚刚的thread6计时时间到了只后统一通过sprintf构建sql指令字符串写入数据库
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "mjm_uart_tool.h"
#include
#define PWM 5
#define BEEP 2
#define Trig 9
#define Echo 10
int angle;
double dist;
static int i;
int sockfd;
int conn_sockfd;
int len = sizeof(struct sockaddr_in);
int ret;
char readbuf[128];
struct sockaddr_in my_addr;
struct sockaddr_in client_addr;
static int conn_flag;
char hist[128] = {0}; //用于存放一条历史记录,注意这个变量一定不能在create_hist里赋值,因为局部指针变量作为函数的返回值没有意义,会报错; 且作为strcat的对象必须是字符串变量
char hist_whole[10000] = {0}; //用于存放所有历史记录; 且作为strcat的对象必须是字符串变量
sqlite3 *db;
char *zErrMsg = 0;
int dist_open_count = 0;
int dist_close_count = 0;
int sock_open_count = 0;
int sock_close_count = 0;
int soun_open_count = 0;
int soun_close_count = 0;
pthread_mutex_t mutex;
int callback(void *arg, int column_size, char *column_value[], char *column_name[]) //数据库exec函数对应的callback函数
{
int j;
//printf("arg=%s\n",(char *)arg);
for(j=0;j 30 && angle == 3){//当距离大于30且盖子打开
angle = 1; //关盖
dist_close_count++;
create_hist(1,2);//构建一条历史记录
strcat(hist_whole,hist);//在总的历史记录中添加刚刚构建的历史记录
}
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void *thread2(void *arg) //负责检测开盖时间,超过10s就报警的线程
{
struct timeval startTime;
struct timeval stopTime;
double diffTime;
while(1){
while(angle == 1);//程序会卡在这里直到盖子打开
gettimeofday(&startTime,NULL);
while(angle == 3){
gettimeofday(&stopTime,NULL);
diffTime = (stopTime.tv_sec - startTime.tv_sec) + 1/1000000 *(stopTime.tv_usec - startTime.tv_usec);
if(diffTime > 10){ //盖子打开超过10秒
digitalWrite (BEEP, LOW) ; //蜂鸣器响
delay (1000) ; //一秒长鸣警报
digitalWrite (BEEP, HIGH) ; //蜂鸣器不响
break;
}
}
}
pthread_exit(NULL);
}
void *thread3(void *arg) //负责不断等待socket客户端接入的线程
{
while(1){
//accept
conn_sockfd = accept(sockfd,(struct sockaddr *)&client_addr,&len);
if(conn_sockfd == -1){
perror("accept");
pthread_exit(NULL);;
}else{
conn_flag = 1; //保证连接成功后才开始接收
printf("accept success, client IP = %s\n",inet_ntoa(client_addr.sin_addr));
fflush(stdout);
}
}
pthread_exit(NULL);
}
void *thread4(void *arg) //负责socket客户端接入后接收处理客户端指令的线程
{
while(1){ //切不可直接“while(conn_flag == 1)”,这样在没有接入时会直接退出,要不停的循环查看flag的值
while(conn_flag == 1){
//read
memset(&readbuf,0,sizeof(readbuf));
ret = recv(conn_sockfd, &readbuf, sizeof(readbuf), 0);
if(ret == 0){ //如果recv函数返回0表示连接已经断开
printf("client has quit\n");
fflush(stdout);
close(conn_sockfd);
break;
}else if(ret == -1){
perror("recv");
conn_flag = 0; //此时打印一遍错误信息就会结束,如果不把flag置1,在一个客户端退出另一个客户端还未接入时就会不停的打印错误信息
//pthread_exit(NULL); //此处不能退出,因为因为这样如果有一个客户端接入并退出后这个线程就会退出,为了保证一个客户端退出后,另一个客户端还可以接入并正常工作,此处仅显示错误信息而不退出
}
pthread_mutex_lock(&mutex);//对angle值修改需要先上锁
cmd_handler(conn_sockfd, readbuf); //对客户端发来的消息进行判断的总函数
pthread_mutex_unlock(&mutex);
printf("\nclient: %s\n",readbuf);
fflush(stdout);
}
}
pthread_exit(NULL);
}
void *thread5(void *arg) //负责通过串口接收语音模块指令的线程
{
char io_readbuf[32] = {'\0'};
while(1){
while(serialDataAvail (*((int *)arg))){
serialGetstring (*((int *)arg),io_readbuf) ;
//printf("-> %s\n",io_readbuf);
pthread_mutex_lock(&mutex);//对angle值修改需要先上锁
if(strcmp(io_readbuf,"open") == 0 && angle == 1){ //当收到open指令且盖子关闭时
angle = 3; //开盖
soun_open_count++;
create_hist(3,1);//构建一条历史记录
strcat(hist_whole,hist);//在总的历史记录中添加刚刚构建的历史记录
digitalWrite (BEEP, LOW) ; //蜂鸣器响
delay (100) ;
digitalWrite (BEEP, HIGH) ; //蜂鸣器不响
delay (100) ;
digitalWrite (BEEP, LOW) ; //蜂鸣器响
delay (100) ;
digitalWrite (BEEP, HIGH) ; //蜂鸣器不响
delay(2000);//延时2秒
}else if(strcmp(io_readbuf,"close") == 0 && angle == 3){//当收到close指令且盖子打开时
angle = 1; //关盖
soun_close_count++;
create_hist(3,2);//构建一条历史记录
strcat(hist_whole,hist);//在总的历史记录中添加刚刚构建的历史记录
delay(2000);//延时2秒
}else if(strcmp(io_readbuf,"open") == 0 && angle == 3){
printf("already open\n");
fflush(stdout);
}else if(strcmp(io_readbuf,"close") == 0 && angle == 1){
printf("already close\n");
fflush(stdout);
}else{
printf("unkown command\n");
fflush(stdout);
}
pthread_mutex_unlock(&mutex);
memset(io_readbuf,'\0',32);
}
}
pthread_exit(NULL);
}
void *thread6(void *arg) //负责每隔一段时间向文件和数据库写入历史记录的线程
{
struct timeval startTime;
struct timeval stopTime;
double diffTime;
while(1){
gettimeofday(&startTime,NULL);
while(1){
gettimeofday(&stopTime,NULL);
diffTime = (stopTime.tv_sec - startTime.tv_sec) + 1/1000000 *(stopTime.tv_usec - startTime.tv_usec); //单位为秒
if(diffTime > 60){//如果时间是2分钟,由于线程的竞争机制,不一定会非常精确,所以使用>号
write2file();//将历史记录写入文件
write2sql();//将历史记录写入数据库
break;
}
}
}
pthread_exit(NULL);
}
int main(int argc, char **argv)
{
struct itimerval itv;
int io_fd;
char *create_table_sql;
pthread_t t1_id;
pthread_t t2_id;
pthread_t t3_id;
pthread_t t4_id;
pthread_t t5_id;
pthread_t t6_id;
memset(&my_addr,0,sizeof(struct sockaddr_in));
memset(&client_addr,0,sizeof(struct sockaddr_in));
if(argc != 3){
printf("param error!\n");
return 1;
}
wiringPiSetup () ;
pinMode (PWM, OUTPUT);
pinMode (Trig, OUTPUT);
pinMode (Echo, INPUT);
pinMode (BEEP, OUTPUT);
digitalWrite (Trig, LOW) ;
digitalWrite (Echo, LOW) ;
digitalWrite (BEEP, HIGH) ;
//设定定时时间
itv.it_interval.tv_sec = 0;
itv.it_interval.tv_usec = 500;
//设定开始生效,启动定时器的时间
itv.it_value.tv_sec = 1;
itv.it_value.tv_usec = 0;
//设定定时方式
if( -1 == setitimer(ITIMER_REAL, &itv, NULL)){
perror("error");
exit(-1);
}
//信号处理
signal(SIGALRM,signal_handler);
angle = 1;//初始化角度为关盖
//打开串口驱动文件,配置波特率
if ((io_fd = myserialOpen ("/dev/ttyS5", 115200)) < 0) {
fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;
return 1 ;
}
//打开数据库
ret = sqlite3_open("history.db", &db);
if(ret != SQLITE_OK){
printf("Can't open database: %s\n", sqlite3_errmsg(db));
exit(0);
}else{
printf("Open database successfully\n");
}
//创建表格
//create_table_sql = "create table history(cause char,open Integer,close Integer);";
create_table_sql = "CREATE TABLE HISTORY(" \
"CAUSE CHAR(30) PRIMARY KEY NOT NULL," \
"OPEN INT NOT NULL," \
"CLOSE INT NOT NULL );" ;
ret = sqlite3_exec(db, create_table_sql, callback, 0, &zErrMsg);
if(ret != SQLITE_OK){
printf("Can't create table: %s\n", sqlite3_errmsg(db));
//exit(0);
}else{
printf("Table create successfully\n");
}
//初始化表格数据
init_table();
//socket
sockfd = socket(AF_INET,SOCK_STREAM,0);
if(sockfd == -1){
perror("socket");
return 1;
}else{
printf("socket success, sockfd = %d\n",sockfd);
}
//bind
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(atoi(argv[2]));//host to net (2 bytes)
inet_aton(argv[1],&my_addr.sin_addr); //char* format -> net format
ret = bind(sockfd, (struct sockaddr *)&my_addr, len);
if(ret == -1){
perror("bind");
return 1;
}else{
printf("bind success\n");
}
//listen
ret = listen(sockfd,10);
if(ret == -1){
perror("listen");
return 1;
}else{
printf("listening...\n");
}
ret = pthread_mutex_init(&mutex, NULL);
if(ret != 0){
printf("mutex create error\n");
}
ret = pthread_create(&t1_id,NULL,thread1,NULL);
if(ret != 0){
printf("thread1 create error\n");
}
ret = pthread_create(&t2_id,NULL,thread2,NULL);
if(ret != 0){
printf("thread2 create error\n");
}
ret = pthread_create(&t3_id,NULL,thread3,NULL);
if(ret != 0){
printf("thread3 create error\n");
}
ret = pthread_create(&t4_id,NULL,thread4,NULL);
if(ret != 0){
printf("thread4 create error\n");
}
ret = pthread_create(&t5_id,NULL,thread5,(void *)&io_fd);
if(ret != 0){
printf("thread5 create error\n");
}
ret = pthread_create(&t6_id,NULL,thread6,NULL);
if(ret != 0){
printf("thread6 create error\n");
}
pthread_join(t1_id,NULL);
pthread_join(t2_id,NULL);
pthread_join(t3_id,NULL);
pthread_join(t4_id,NULL);
pthread_join(t5_id,NULL);
pthread_join(t6_id,NULL);
return 0;
}
由于使用了SQL数据库,不要忘了链上“-lsqlite3”:
gcc mjm_uart_tool.c v5_server.c -lwiringPi -lwiringPiDev -lpthread -lm -lcrypt -lrt -lsqlite3 -o v5_server
且此处为了让屏幕显示不要太乱,且关于写入历史记录文件的代码已经证明了没问题,所以我将“把写入历史记录文件的历史记录打印到屏幕上” 的功能注释掉了
然后接入v2_client,模拟各种操作,包括距离接近开关盖;客户端指令开关盖;语音控制开关盖....
可见,每隔一分钟,就会统计这一分钟内的开关盖记录和来源并打印表格在屏幕上:
进入sql环境验证:
发现确实写入数据库成功!!