0


基于树莓派的智能家居项目整理

文章目录

一、功能介绍

二、设计框图

三、实物展示

四、程序

一、功能介绍
基于树莓派的智能家居。智能家居用到的硬件有:树莓派4B、LD3320语音识别模块、pi 摄像头、继电器组、小灯、火焰传感器、蜂鸣器、电磁锁
采用了简单工厂模式的一个设计方式。稳定,拓展性更强,在C语言中,因为没有接口、类这一说法,所以这里采用了结构体来“等效替换”。有四个灯,所以我创建了四个灯控制.c程序。每一个程序文件中,都有一个设备结构体,每个程序文件的函数实现方法不同,当有新设备进入只需要在创建一个.c文件,改变函数实现方法即可。初始化的时候,通过链表将各个模块连接起来(头插法)。在要使用某个模块时,只需要使用链表遍历,找到所需模块去调用功能
具体功能是:
1、可通过ld3320语音模块的口令模式,口令+具体控制,通过串口把控制指令传给树莓派,来控制客厅、餐厅、二楼、浴室的灯
2、也可以通过socket客户端来发指令来控制灯的开关
3、火灾报警,当火焰传感器检测到火焰的时候,蜂鸣器会报警。
4、视频监控采用开源mjpg-Streamer来实现的,程序执行时创建一个视频监控的线程,用system函数调用启动脚本运行,监控画面可在http://172.20.10.8:8080去看到
5、人脸识别开锁,人脸识别功能是使用的翔云平台的人脸识别解决方案,需要安装libcurl 和 openSSl库来支持https协议,通过系统调用wget +http://172.20.10.8:8080/?action=snapshot -O ./huyu1.jpg 指令到树莓派的监控页面"去截取一帧保存到本地,获取图片的base64编码,工程文件夹下也有一张照片,huyu.jpg格式,相当于采集的人脸。也是获取图片的base64编码,通过sprintf函数将访问翔云需要的两张图片的base64编码与Key、secret、typeId、format拼接在一起,通过https协议去访问翔云平台, 识别成功后会将识别结果返回,通过回调函数readData将返回的字符串读到readBuff里,通过strstr去readbuff里找有没有字符’是’,如果识别成功就去控制电磁锁打开。

二、设计框图
在这里插入图片描述
效果展示:https://www.bilibili.com/video/BV1uD4y1C7PH?share_source=copy_web&vd_source=5e457ac82f63b0e36b8b9e2708807f31

三、实物展示
请添加图片描述
请添加图片描述
请添加图片描述
四、程序

1、InputCommand.h

  1. #include<wiringPi.h>#include<stddef.h>structInputCommander{char commandName[128];char deviceName[128];char command[32];int(*Init)(structInputCommander*voicer,char*ipAdress,char*port);int(*getCommand)(structInputCommander*voicer);char log[1024];int fd;char port[12];char ipAddress[32];int sfd;structInputCommander*next;};structInputCommander*addVoiceControlToInputCommandLink(structInputCommander*phead);structInputCommander*addSocketControlToInputCommandLink(structInputCommander*phead);

2、controlDevices .h

  1. #include<wiringPi.h>#include<stdio.h>structDevices{char deviceName[128];int status;int pinNum;int(*open)(int pinNum);int(*close)(int pinNum);int(*deviceInit)(int pinNum);int(*readStatus)(int pinNum);int(*changeStatus)(int status);void(*faceRecognition)();//人脸识别char*(*takePictureInit)();//用于摄像头char*(*getPicBase64)();//用于摄像头size_t(*readData)();//用于摄像头structDevices*next;};structDevices*addBathroomLightToDeviceLink(structDevices*phead);structDevices*addUpstairLightToDeviceLink(structDevices*phead);structDevices*addDiningroomLightToDeviceLink(structDevices*phead);structDevices*addLivingroomLightToDeviceLink(structDevices*phead);structDevices*addFireAlarmToDeviceLink(structDevices*phead);structDevices*addBeepToDeviceLink(structDevices*phead);structDevices*addLookToDeviceLink(structDevices*phead);structDevices*addCameraToDeviceLink(structDevices*phead);

3、mainPro.c

  1. #include<stdio.h>#include<string.h>#include<stdlib.h>#include<unistd.h>#include<pthread.h>#include<sys/types.h>#include<sys/socket.h>#include<netinet/in.h>#include<arpa/inet.h>#include"controlDevices.h"#include"InputCommand.h"structInputCommander*pCommandHead =NULL;structDevices*pdeviceHead =NULL;structInputCommander*socketHandler =NULL;int c_fd;char*name =NULL;pthread_t cameraThread;structDevices*findDeviceByName(char*name,structDevices*phead){structDevices*tmp = phead;if(phead ==NULL){returnNULL;}else{while(tmp !=NULL){if(strcmp(tmp->deviceName,name)==0){return tmp;}
  2. tmp = tmp->next;}returnNULL;}}structInputCommander*findCommandByName(char*name,structInputCommander*phead){structInputCommander*tmp = phead;if(phead ==NULL){returnNULL;}else{while(tmp !=NULL){if(strcmp(tmp->commandName,name)==0){return tmp;}
  3. tmp = tmp->next;}returnNULL;}}void*camera_thread(void*datas)//摄像头线程{structDevices*cameraTemp;
  4. cameraTemp =findDeviceByName("camera", pdeviceHead);//设备都要从工厂里面取出来if(cameraTemp ==NULL){//防止段错误的必需判断,当给指针赋值是,一定要考虑NULL的情况printf("find camera error\n");pthread_exit(NULL);//在线程中不用return}
  5. cameraTemp->faceRecognition();//调用人脸识别函数}void*voice_thread(void*datas)//语音线程{int nread;structInputCommander*voiceHandler =NULL;structDevices*linkHandler =NULL;
  6. voiceHandler =findCommandByName("voice",pCommandHead);if(voiceHandler ==NULL){printf("find voicehandler error\n");pthread_exit(NULL);}else{if(voiceHandler->Init(voiceHandler,NULL,NULL)<0){printf("voice init error\n");pthread_exit(NULL);}else{printf("%s init success!\n",voiceHandler->commandName);}while(1){memset(voiceHandler->command,'\0',sizeof(voiceHandler->command));
  7. nread = voiceHandler->getCommand(voiceHandler);//读取来自语音模块的消息printf(" %s\n",voiceHandler->command);if(nread >1){if(strstr(voiceHandler->command,"KEL")!=NULL){//开二楼灯
  8. linkHandler =findDeviceByName("upstairLight",pdeviceHead);
  9. linkHandler->deviceInit(linkHandler->pinNum);
  10. linkHandler->open(linkHandler->pinNum);printf("open upstairLight\n");}elseif(strstr(voiceHandler->command,"GEL")!=NULL){//关二楼灯
  11. linkHandler =findDeviceByName("upstairLight",pdeviceHead);
  12. linkHandler->close(linkHandler->pinNum);printf("close upstairLight\n");}elseif(strstr(voiceHandler->command,"KYS")!=NULL){//开浴室灯
  13. linkHandler =findDeviceByName("bathroomLight",pdeviceHead);
  14. linkHandler->deviceInit(linkHandler->pinNum);
  15. linkHandler->open(linkHandler->pinNum);printf("open bathroomlight\n");}elseif(strstr(voiceHandler->command,"GYS")!=NULL){//关浴室灯
  16. linkHandler =findDeviceByName("bathroomLight",pdeviceHead);
  17. linkHandler->close(linkHandler->pinNum);printf("open bathroomlight\n");}elseif(strstr(voiceHandler->command,"KKT")!=NULL){//开客厅灯
  18. linkHandler =findDeviceByName("livingroomLight",pdeviceHead);
  19. linkHandler->deviceInit(linkHandler->pinNum);
  20. linkHandler->open(linkHandler->pinNum);printf("open livingroomLight\n");}elseif(strstr(voiceHandler->command,"GKT")!=NULL){//关客厅灯
  21. linkHandler =findDeviceByName("livingroomLight",pdeviceHead);
  22. linkHandler->close(linkHandler->pinNum);printf("open livingroomLight\n");}elseif(strstr(voiceHandler->command,"KCT")!=NULL){//开餐厅灯
  23. linkHandler =findDeviceByName("diningroomLight",pdeviceHead);
  24. linkHandler->deviceInit(linkHandler->pinNum);
  25. linkHandler->open(linkHandler->pinNum);printf("open diningroomLight\n");}elseif(strstr(voiceHandler->command,"GCT")!=NULL){//关餐厅灯
  26. linkHandler =findDeviceByName("diningroomLight",pdeviceHead);
  27. linkHandler->close(linkHandler->pinNum);printf("open diningroomLight\n");}elseif(strstr(voiceHandler->command,"KM")!=NULL){//开门pthread_create(&cameraThread,NULL,camera_thread,NULL);}elseif(strstr(voiceHandler->command,"DQK")!=NULL){//打开全部灯
  28. linkHandler =findDeviceByName("upstairLight",pdeviceHead);
  29. linkHandler->deviceInit(linkHandler->pinNum);
  30. linkHandler->open(linkHandler->pinNum);
  31. linkHandler =findDeviceByName("bathroomLight",pdeviceHead);
  32. linkHandler->deviceInit(linkHandler->pinNum);
  33. linkHandler->open(linkHandler->pinNum);
  34. linkHandler =findDeviceByName("livingroomLight",pdeviceHead);
  35. linkHandler->deviceInit(linkHandler->pinNum);
  36. linkHandler->open(linkHandler->pinNum);
  37. linkHandler =findDeviceByName("diningroomLight",pdeviceHead);
  38. linkHandler->deviceInit(linkHandler->pinNum);
  39. linkHandler->open(linkHandler->pinNum);printf("All closed\n");}elseif(strstr(voiceHandler->command,"DQG")!=NULL){//关闭全部灯
  40. linkHandler =findDeviceByName("upstairLight",pdeviceHead);
  41. linkHandler->close(linkHandler->pinNum);
  42. linkHandler =findDeviceByName("bathroomLight",pdeviceHead);
  43. linkHandler->close(linkHandler->pinNum);
  44. linkHandler =findDeviceByName("livingroomLight",pdeviceHead);
  45. linkHandler->close(linkHandler->pinNum);
  46. linkHandler =findDeviceByName("diningroomLight",pdeviceHead);
  47. linkHandler->close(linkHandler->pinNum);printf("All open\n");}elseif(nread ==0){printf("nodata from voice......\n");}//printf("%d\n",strlen(voiceHandler->command));}}}}void*read_thread(void*datas){int n_read;structDevices*linkHandler1 =NULL;memset(socketHandler->command,'\0',sizeof(socketHandler->command));
  48. n_read =read(c_fd,socketHandler->command,sizeof(socketHandler->command));if(n_read>0){printf("get message:%d,%s\n",n_read,socketHandler->command);if(strstr(socketHandler->command,"KEL")!=NULL){//开二楼灯
  49. linkHandler1 =findDeviceByName("upstairLight",pdeviceHead);
  50. linkHandler1->deviceInit(linkHandler1->pinNum);
  51. linkHandler1->open(linkHandler1->pinNum);printf("open upstairLight\n");}elseif(strstr(socketHandler->command,"GEL")!=NULL){//关二楼灯
  52. linkHandler1 =findDeviceByName("upstairLight",pdeviceHead);
  53. linkHandler1->close(linkHandler1->pinNum);printf("close upstairLight\n");}elseif(strstr(socketHandler->command,"KYS")!=NULL){//开浴室灯
  54. linkHandler1 =findDeviceByName("bathroomLight",pdeviceHead);
  55. linkHandler1->deviceInit(linkHandler1->pinNum);
  56. linkHandler1->open(linkHandler1->pinNum);printf("open bathroomlight\n");}elseif(strstr(socketHandler->command,"GYS")!=NULL){//关浴室灯
  57. linkHandler1 =findDeviceByName("bathroomLight",pdeviceHead);
  58. linkHandler1->close(linkHandler1->pinNum);printf("close bathroomlight\n");}elseif(strstr(socketHandler->command,"KKT")!=NULL){//开客厅灯
  59. linkHandler1 =findDeviceByName("livingroomLight",pdeviceHead);
  60. linkHandler1->deviceInit(linkHandler1->pinNum);
  61. linkHandler1->open(linkHandler1->pinNum);printf("open livingroomLight\n");}elseif(strstr(socketHandler->command,"GKT")!=NULL){//关客厅灯
  62. linkHandler1 =findDeviceByName("livingroomLight",pdeviceHead);
  63. linkHandler1->close(linkHandler1->pinNum);printf("close livingroomLight\n");}elseif(strstr(socketHandler->command,"KCT")!=NULL){//开餐厅灯
  64. linkHandler1 =findDeviceByName("diningroomLight",pdeviceHead);
  65. linkHandler1->deviceInit(linkHandler1->pinNum);
  66. linkHandler1->open(linkHandler1->pinNum);printf("open diningroomLight\n");}elseif(strstr(socketHandler->command,"GCT")!=NULL){//关餐厅灯
  67. linkHandler1 =findDeviceByName("diningroomLight",pdeviceHead);
  68. linkHandler1->close(linkHandler1->pinNum);printf("close diningroomLight\n");}elseif(strstr(socketHandler->command,"KM")!=NULL){//开门//linkHandler1 = findDeviceByName("camera",pdeviceHead);//linkHandler1->takePictureInit();//linkHandler1->faceRecognition();pthread_create(&cameraThread,NULL,camera_thread,NULL);}elseif(strstr(socketHandler->command,"GM")!=NULL){//关门
  69. linkHandler1 =findDeviceByName("lock",pdeviceHead);
  70. linkHandler1->close(linkHandler1->pinNum);}elseif(strstr(socketHandler->command,"DQK")!=NULL){//打开全部灯
  71. linkHandler1 =findDeviceByName("upstairLight",pdeviceHead);
  72. linkHandler1->deviceInit(linkHandler1->pinNum);
  73. linkHandler1->open(linkHandler1->pinNum);
  74. linkHandler1 =findDeviceByName("bathroomLight",pdeviceHead);
  75. linkHandler1->deviceInit(linkHandler1->pinNum);
  76. linkHandler1->open(linkHandler1->pinNum);
  77. linkHandler1 =findDeviceByName("livingroomLight",pdeviceHead);
  78. linkHandler1->deviceInit(linkHandler1->pinNum);
  79. linkHandler1->open(linkHandler1->pinNum);
  80. linkHandler1 =findDeviceByName("diningroomLight",pdeviceHead);
  81. linkHandler1->deviceInit(linkHandler1->pinNum);
  82. linkHandler1->open(linkHandler1->pinNum);printf("All open\n");}elseif(strstr(socketHandler->command,"DQG")!=NULL){//关闭全部灯
  83. linkHandler1 =findDeviceByName("upstairLight",pdeviceHead);
  84. linkHandler1->close(linkHandler1->pinNum);
  85. linkHandler1 =findDeviceByName("bathroomLight",pdeviceHead);
  86. linkHandler1->close(linkHandler1->pinNum);
  87. linkHandler1 =findDeviceByName("livingroomLight",pdeviceHead);
  88. linkHandler1->close(linkHandler1->pinNum);
  89. linkHandler1 =findDeviceByName("diningroomLight",pdeviceHead);
  90. linkHandler1->close(linkHandler1->pinNum);printf("All closed\n");}elseif(n_read ==-1){perror("read");}}}void*socket_thread(void*datas)//socket线程{int n_read =0;pthread_t readThread;structsockaddr_in c_addr;memset(&c_addr,0,sizeof(structsockaddr_in));int clen =sizeof(structsockaddr_in);
  91. socketHandler =findCommandByName("socketServer",pCommandHead);if(socketHandler ==NULL){printf("find socketHandler error\n");pthread_exit(NULL);}else{printf("%s init success!\n",socketHandler->commandName);}
  92. socketHandler->Init(socketHandler,NULL,NULL);while(1){
  93. c_fd =accept(socketHandler->sfd,(structsockaddr*)&c_addr,&clen);pthread_create(&readThread,NULL,read_thread,NULL);}}void*video_thread(void*datas)//视频监控线程{system("/home/pi/mjpg-streamer/mjpg-streamer-experimental/start.sh");pthread_exit(NULL);}void*fireAlarm_thread(void*datas)//火灾报警线程{int status;structDevices*firetmp =NULL;structDevices*beeptmp =NULL;
  94. firetmp =findDeviceByName("fireAlarm",pdeviceHead);//寻找“火焰传感器”链表节点,返回给firetmp
  95. beeptmp =findDeviceByName("beep",pdeviceHead);//寻找“蜂鸣器”链表节点,返回给buztmpwhile(1){
  96. status = firetmp->readStatus(firetmp->pinNum);//读取“火焰传感器”状态if(status ==0){
  97. beeptmp->deviceInit(beeptmp->pinNum);//检测到火焰或强光源
  98. beeptmp->open(beeptmp->pinNum);//打开蜂鸣器printf("Warning of fire!!!\n");delay(1000);//延时1000毫秒=1秒}if(status ==1){//未检测到火焰、强光源或解除警报
  99. beeptmp->close(beeptmp->pinNum);//关闭蜂鸣器}}}intmain(){//char name[128] = {'\0'};pthread_t voiceThread;pthread_t socketThread;pthread_t fireAlarmThread;pthread_t videoThread;pthread_t cameraThread;structDevices*tmp =NULL;if(wiringPiSetup()==-1){return-1;}//1. 指令工厂初始化
  100. pCommandHead =addVoiceControlToInputCommandLink(pCommandHead);
  101. pCommandHead =addSocketControlToInputCommandLink(pCommandHead);//2. 设备控制工厂初始化
  102. pdeviceHead =addBathroomLightToDeviceLink(pdeviceHead);
  103. pdeviceHead =addUpstairLightToDeviceLink(pdeviceHead);
  104. pdeviceHead =addLivingroomLightToDeviceLink(pdeviceHead);
  105. pdeviceHead =addDiningroomLightToDeviceLink(pdeviceHead);
  106. pdeviceHead =addFireAlarmToDeviceLink(pdeviceHead);
  107. pdeviceHead =addBeepToDeviceLink(pdeviceHead);
  108. pdeviceHead =addLookToDeviceLink(pdeviceHead);
  109. pdeviceHead =addCameraToDeviceLink(pdeviceHead);structDevices*tmpequiphead = pdeviceHead;if(tmpequiphead !=NULL){//设备工厂所有设备初始化
  110. tmpequiphead->deviceInit(tmpequiphead->pinNum);
  111. tmpequiphead = tmpequiphead->next;}//3.池建立//3.1 语音线程//int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void *), void *restrict arg);pthread_create(&voiceThread,NULL,voice_thread,NULL);//3.2 socket线程pthread_create(&socketThread,NULL,socket_thread,NULL);//3.3 摄像头线程//printf("camera_thread1===============\n");pthread_create(&cameraThread,NULL,camera_thread,NULL);//3.4 火灾线程pthread_create(&fireAlarmThread,NULL,fireAlarm_thread,NULL);//3.视频监控 pthread_create(&videoThread,NULL, video_thread,NULL);pthread_join(voiceThread,NULL);pthread_join(socketThread,NULL);pthread_join(cameraThread,NULL);pthread_join(fireAlarmThread,NULL);pthread_join(videoThread,NULL);return0;}

4、upstairLight.c

  1. #include"controlDevices.h"#include<stddef.h>/*struct Devices
  2. {
  3. char deviceName[128];
  4. int status;
  5. int (*open)();
  6. int (*close)();
  7. int (*deviceInit)();
  8. int (*readStatus)()
  9. int (*changeStatus)(int status);
  10. struct Devices *next;
  11. };
  12. */intupstairLightOpen(int pinNum){digitalWrite(pinNum,HIGH);}intupstairLightClose(int pinNum){digitalWrite(pinNum,LOW);}intupstairLightInit(int pinNum){pinMode(pinNum,OUTPUT);digitalWrite(pinNum,LOW);}intupstairLightStatus(int status){}structDevices upstairLight ={.deviceName ="upstairLight",.pinNum =21,.open = upstairLightOpen,.close = upstairLightClose,.deviceInit = upstairLightInit,.changeStatus = upstairLightStatus,.next =NULL};structDevices*addUpstairLightToDeviceLink(structDevices*phead){if(phead ==NULL){return&upstairLight;}else{
  13. upstairLight.next = phead;
  14. phead =&upstairLight;return phead;}}

5、bathroomLight.c

  1. #include"controlDevices.h"#include<stddef.h>/*struct Devices
  2. {
  3. char deviceName[128];
  4. int status;
  5. int (*open)();
  6. int (*close)();
  7. int (*deviceInit)();
  8. int (*readStatus)()
  9. int (*changeStatus)(int status);
  10. struct Devices *next;
  11. };
  12. */intbathroomLightOpen(int pinNum){digitalWrite(pinNum,HIGH);}intbathroomLightClose(int pinNum){digitalWrite(pinNum,LOW);}intbathroomLightInit(int pinNum){pinMode(pinNum,OUTPUT);digitalWrite(pinNum,LOW);}intbathroomLightStatus(int status){}structDevices bathroomLight ={.deviceName ="bathroomLight",.pinNum =22,.open = bathroomLightOpen,.close = bathroomLightClose,.deviceInit = bathroomLightInit,.changeStatus = bathroomLightStatus,.next =NULL};structDevices*addBathroomLightToDeviceLink(structDevices*phead){if(phead ==NULL){return&bathroomLight;}else{
  13. bathroomLight.next = phead;
  14. phead =&bathroomLight;return phead;}}

6、livingroomLight.c

  1. #include"controlDevices.h"#include<stddef.h>/*struct Devices
  2. {
  3. char deviceName[128];
  4. int status;
  5. int (*open)();
  6. int (*close)();
  7. int (*deviceInit)();
  8. int (*readStatus)()
  9. int (*changeStatus)(int status);
  10. struct Devices *next;
  11. };
  12. */intlivingroomLightOpen(int pinNum){digitalWrite(pinNum,HIGH);}intlivingroomLightClose(int pinNum){digitalWrite(pinNum,LOW);}intlivingroomLightInit(int pinNum){pinMode(pinNum,OUTPUT);digitalWrite(pinNum,LOW);}intlivingroomLightStatus(int status){}structDevices livingroomLight ={.deviceName ="livingroomLight",.pinNum =23,.open = livingroomLightOpen,.close = livingroomLightClose,.deviceInit = livingroomLightInit,.changeStatus = livingroomLightStatus,.next =NULL};structDevices*addLivingroomLightToDeviceLink(structDevices*phead){if(phead ==NULL){return&livingroomLight;}else{
  13. livingroomLight.next = phead;
  14. phead =&livingroomLight;return phead;}}

7、diningroomLight.c

  1. #include"controlDevices.h"#include<stddef.h>/*struct Devices
  2. {
  3. char deviceName[128];
  4. int status;
  5. int (*open)();
  6. int (*close)();
  7. int (*deviceInit)();
  8. int (*readStatus)()
  9. int (*changeStatus)(int status);
  10. struct Devices *next;
  11. };
  12. */intdiningroomLightOpen(int pinNum){digitalWrite(pinNum,HIGH);}intdiningroomLightClose(int pinNum){digitalWrite(pinNum,LOW);}intdiningroomLightInit(int pinNum){pinMode(pinNum,OUTPUT);digitalWrite(pinNum,LOW);}intdiningroomLightStatus(int status){}structDevices diningroomLight ={.deviceName ="diningroomLight",.pinNum =24,.open = diningroomLightOpen,.close = diningroomLightClose,.deviceInit = diningroomLightInit,.changeStatus = diningroomLightStatus,.next =NULL};structDevices*addDiningroomLightToDeviceLink(structDevices*phead){if(phead ==NULL){return&diningroomLight;}else{
  13. diningroomLight.next = phead;
  14. phead =&diningroomLight;return phead;}}

8、fire.c

  1. #include"controlDevices.h"#include<stddef.h>intfireAlarmInit(int pinNum){pinMode(pinNum,INPUT);digitalWrite(pinNum,HIGH);}intfireStatusRead(int pinNum){returndigitalRead(pinNum);}structDevices fireAlarm ={.deviceName ="fireAlarm",.pinNum =25,.deviceInit = fireAlarmInit,.readStatus = fireStatusRead,.next =NULL};structDevices*addFireAlarmToDeviceLink(structDevices*phead){if(phead ==NULL){return& fireAlarm;}else{
  2. fireAlarm.next = phead;
  3. phead =& fireAlarm;}}

9、beep.c

  1. #include"controlDevices.h"structDevices*addBeepToDeviceLink(structDevices*phead);intbeepInit(int pinNum)//初始化函数{pinMode(pinNum,OUTPUT);//配置引脚为输出引脚digitalWrite(pinNum,LOW);//引脚输出低电平,即默认为关闭状态}intbeepOpen(int pinNum)//打开蜂鸣器函数{digitalWrite(pinNum,HIGH);}intbeepClose(int pinNum)//关闭蜂鸣器函数{digitalWrite(pinNum,LOW);}structDevices beep ={//蜂鸣器设备链表节点.deviceName ="beep",.pinNum =27,//树莓派gpio引脚27.deviceInit = beepInit,.open = beepOpen,.close = beepClose,.next =NULL};structDevices*addBeepToDeviceLink(structDevices*phead)//头插法将设备节点加入设备工厂链表函数{if(phead ==NULL){return&beep;}else{
  2. beep.next = phead;
  3. phead =&beep;return phead;}}

10、voiceControl.c

  1. #include"InputCommand.h"#include<wiringPi.h>#include<wiringSerial.h>#include<stdio.h>#include<unistd.h>#include<stdlib.h>#include<string.h>intvoiceGetCommand(structInputCommander*voicer){int nread =0;//memset(voicer->command,'\0',sizeof(voicer->command));
  2. nread =read(voicer->fd,voicer->command,sizeof(voicer->command));if(nread ==0){printf("usart for voice read over time!\n");}else{return nread;//读取到的指令是存放到voicer.command的}}intvoiceInit(structInputCommander*voicer,char*ipAdress,char*port){int fd;//printf("test init1\n");if((fd =serialOpen(voicer->deviceName,9600))==-1)//初始化串口,波特率9600{printf("open serial error\n");exit(-1);}
  3. voicer->fd = fd;return fd;}structInputCommander voiceControl ={.commandName ="voice",.deviceName ="/dev/ttyAMA0",.command ={'\0'},.Init = voiceInit,.getCommand = voiceGetCommand,.log ={'\0'},.next =NULL};structInputCommander*addVoiceControlToInputCommandLink(structInputCommander*phead){if(phead ==NULL){return&voiceControl;}else{
  4. voiceControl.next = phead;
  5. phead =&voiceControl;}}

11、socketControl.c


本文转载自: https://blog.csdn.net/m0_61511416/article/details/127330096
版权归原作者 sunshime. 所有, 如有侵权,请联系我们删除。

“基于树莓派的智能家居项目整理”的评论:

还没有评论