https://player.youku.com/embed/XNTg2MDI4NjQ5Mg==
基于树莓派的智能音响
项目硬件准备:
树莓派3B
语音识别模块
耳机一个
灯泡两个
项目软件准备:
需要下载好wiringPi这个库,可以留言问我要,下载好mpg321和espeak很好下载。
使用VI编辑,然后GCC编译一下运行就好啦,对了加上这个wiringPi这个库,也就是,gcc xxx.c -lwiringPi这样进行编译。
项目在树莓派中的代码:
#include <wiringSerial.h> #include <wiringPi.h> #include <stdio.h> #include <error.h> #include <string.h> #include <unistd.h> #include <sys/time.h> #include <stdlib.h> #include <sys/types.h> #include <fcntl.h> #define SWITCHER7 7 #define SWITCHER3 3 #define MUSICSTOP1 11 #define MUSICSTOP2 13 int system(const char *string); char* cmd_system(const char* command); char* cmd_system(const char* command)//用于在代码中执行SHELL命令 { char* result =""; FILE *fpRead; fpRead = popen(command,"r"); char buf[1024]; memset(buf,'\0',sizeof(buf)); while(fgets(buf,1024-1,fpRead)!=NULL) { result = buf; } if(fpRead!=NULL) pclose(fpRead); return result; } int main() { char cmd[128]={'\0'}; int fd; char buf[128]; int nread; int flags; int serial_cmd_type = 0; int time_cnt = 0; char report[256] = {0}; char hour_eg[2]; char min_eg[2]; char* result; wiringPiSetup(); pinMode(SWITCHER3,OUTPUT); pinMode(SWITCHER7,OUTPUT); digitalWrite(SWITCHER3,HIGH); digitalWrite(SWITCHER7,HIGH); memset(buf,'\0',sizeof(buf)); if(wiringPiSetup() == -1) { } fd = serialOpen("/dev/ttyAMA0",9600); if(flags = fcntl(fd, F_GETFL, 0) < 0)//用于串口一直在执行接收数据,而不是阻塞中 { perror("fcntl"); return -1; } flags |= O_NONBLOCK; if(fcntl(fd, F_SETFL, flags) < 0) { perror("fcntl"); return -1; } while(1) { nread = read (fd, cmd, sizeof(cmd));//不断的去读取串口接收到的数据 //if(strlen(cmd)==0) //{ // printf("chaoShi\n"); //} //printf("nread:%d\n",nread); if(strstr(cmd,"kaideng")!=NULL)//当串口接收到到kaideng字符串,就开灯了 { printf("kaideng\n"); digitalWrite(SWITCHER3,LOW); //serial_cmd_type = 1; //time_cnt = 0; } if(strstr(cmd,"guandeng")!=NULL)//当串口接收到到guandeng字符串,就开灯了 { printf("guandeng\n"); digitalWrite(SWITCHER3,HIGH); //serial_cmd_type = 1; //time_cnt = 0; } if(strstr(cmd,"fangge")!=NULL)//当串口接收到到fangge字符串,就开灯了 { printf("fangge\n"); system("killall mpg321 1>/dev/null 2>&1");//首先杀死音乐播放当前播放的歌曲的进程 system("mpg321 /home/pi/Mylinux/dream.mp3 &");//在后台进行播放歌曲 serial_cmd_type = 2; time_cnt = 0; } if(strstr(cmd,"baoshi")!=NULL)//串口收到baoshi字符串,进行报时 { system("killall mpg321 1>/dev/null 2>&1");//杀死放歌的进程 system("killall espeak 1>/dev/null 2>&1");//杀死报时的进程 printf("baoshi\n"); result = cmd_system("date +'%H%M'");//进行读取时间 strncpy(hour_eg,result,2); strncpy(min_eg,result+2,2); hour_eg[2]='\0'; min_eg[2]='\0'; sprintf(report,"espeak -v zh \"现在时间%s点%s分\"",hour_eg,min_eg); printf("time:s%",report); digitalWrite(SWITCHER7,LOW);//报时的时候打开指示灯 system(report);//进行报时 digitalWrite(SWITCHER7,HIGH);//报完关闭指示灯 serial_cmd_type = 3; time_cnt = 0; } if(strstr(cmd,"huange")!=NULL)//换一首歌曲播放 { printf("huange\n"); system("killall mpg321 1>/dev/null 2>&1"); system("mpg321 /home/pi/Mylinux/xiao.mp3 &"); serial_cmd_type = 4; time_cnt = 0; } memset(cmd,'\0',sizeof(cmd)/sizeof(char)); } switch (serial_cmd_type) { case 1: printf("case1\n"); break; case 2://放歌的时候进行指示灯的闪烁 printf("case2\n"); time_cnt++; printf("time_cnt2:%d\n",time_cnt); if(time_cnt < MUSICSTOP1) { printf("LOW\n"); digitalWrite(SWITCHER7,LOW); delay(1000); } break; case 3: printf("case3\n"); digitalWrite(SWITCHER7,HIGH); break; case 4://放歌的时候进行指示灯的闪烁 printf("case4\n"); time_cnt++; printf("time_cnt4:%d\n",time_cnt); if(time_cnt < MUSICSTOP2) { printf("LOW\n"); digitalWrite(SWITCHER7,LOW); delay(1000); } break; default: printf("case5\n"); break; } if(serial_cmd_type == 2||serial_cmd_type == 4)//放歌的时候进行指示灯的闪烁 { printf("HIGH\n"); digitalWrite(SWITCHER7,HIGH); } delay(1000); close(fd); return 0; }
语音模块的代码:
后续更新上