项目视频演示:
项目硬件准备:
项目软件准备:
- 需要下载好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)
{
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;
}
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);
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(strstr(cmd,"kaideng")!=NULL)
{
printf("kaideng\n");
digitalWrite(SWITCHER3,LOW);
}
if(strstr(cmd,"guandeng")!=NULL)
{
printf("guandeng\n");
digitalWrite(SWITCHER3,HIGH);
}
if(strstr(cmd,"fangge")!=NULL)
{
printf("fangge\n");
system("killall mpg321 1>/dev/null 2>&1");
system("mpg321 /home/pi/Mylinux/BISHE/liwei/dream.mp3 &");
serial_cmd_type = 2;
time_cnt = 0;
} if(strstr(cmd,"baoshi")!=NULL)
{
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/BISHE/liwei/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;
}
语音模块的代码: 后续更新上
|