#include <wiringSerial.h> #include <wiringPi.h> #include <stdio.h> #include <string.h> #include <unistd.h> #include <sys/time.h> #define SWITCHER 25//定义25口为继电器口 int main () { int fd; char cmd[128]={'\0'}; int nread; wiringPiSetup();//初始化 pinMode(SWITCHER,OUTPUT);//初始化端口 if(wiringPiSetup() == -1){ //如果初始化失败,就输出错误信息 程序初始化时务必进行 printf("setup wiringPi failed !"); return 1; } fd = serialOpen("/dev/ttyAMA0",9600);//语音模块打开 digitalWrite(SWITCHER,HIGH);//高电平关断 while(1){ nread = read (fd, cmd, sizeof(cmd));//读取数据 if(strlen(cmd)==0){//如果没有数据就显示超时 printf("chaoShi\n"); continue; } if (strstr(cmd,"open")!=NULL){//如果收到打开字样 printf("open light\n");//树莓派界面输出开灯 digitalWrite(SWITCHER,LOW);//低电平开灯 } if(strstr(cmd,"close")!=NULL){//如果收到关断字样 printf("close light\n");//打印关灯 digitalWrite(SWITCHER,HIGH);//高点平关灯 } memset(cmd,'\0',sizeof(cmd)/sizeof(char));//回收,关断 } return 0; }
https://player.youku.com/embed/XNDkxMTU4MDU2NA==
树莓派语音实现继电器开关