当前位置:   article > 正文

基于树莓派的智能家居项目及代码_树莓派项目

树莓派项目

一、功能介绍

基于树莓派的智能家居。智能家居用到的硬件有:树莓派3B+、SU-03语音识别模块、pi 摄像头、继电器组、小灯、火焰传感器、蜂鸣器、电磁锁、超声波测距模块、DHT11温湿度检测模块,433M射频编解码模块或者红外模块,面包板等。
采用了简单工厂模式的一个设计方式。稳定,拓展性更强。通过工厂创建一个通用的接口,集中管理设备和指令。

创建了两个工厂:设备工厂和指令工厂。

设备工厂:用于管理卧室灯,餐厅灯,楼梯灯,浴室灯,火焰传感器,蜂鸣器,电磁锁,超声波,摄像头等设备。初始化的时候,通过链表将各个模块连接起来(头插法)。在要使用某个模块时,只需要使用链表遍历,找到所需模块去调用功能。

指令工厂:管理串口输入输出和socket客户端的指令输入和输出。


具体功能如下:
1、可通过SU-03语音模块的口令模式,口令+具体控制,通过串口把控制指令传给树莓派,来控制客厅、餐厅、二楼、浴室的灯

2、可通过SU-03语音模块的口令模式,通过串口把控制指令传给树莓派,树莓派再传递给射频模块(或者红外模块)控制窗帘,泳池灯,空调等。
3、可以通过socket客户端来发指令来控制客厅、餐厅、二楼、浴室的灯,可以在socket客户端看到摄像头实时监控画面,并且可以在客户端实时看到温度,湿度,火灾检测等数据。
4、火灾报警,当火焰传感器检测到火焰的时候,蜂鸣器会报警。
5、视频监控采用开源mjpg-Streamer来实现的,设置摄像头开机自启动,监控画面可在http://192.168.1.3:8080/?action=stream去看到
6、人脸识别开锁和监控,人脸识别功能是使用的翔云平台的人脸识别解决方案,需要安装libcurl 和 openSSl库来支持https协议,通过系统调用wget   http://192.168.1.3:8080/?action=snapshot获取;

二、设计框图

三、程序实现

InputCommand.h:指令工厂

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <wiringPi.h>
  6. struct InputCommander
  7. {
  8. char commandName[128];
  9. char deviceName[128];//打开的设备名称
  10. char command[32];//获取指令
  11. char log[1024];
  12. int baud;//波特率
  13. int fd;
  14. int s_fd;
  15. char port[12];//端口号
  16. char ipAddress[32];//ip地址
  17. char temp[12];
  18. char hum[12];
  19. char fireIfOrNot[32];
  20. int (*Init)(struct InputCommander *pInputCommand);
  21. int (*getCommand)(struct InputCommander *pInputCommand);
  22. int (*sendCommand)(struct InputCommander *pInputCommand);
  23. struct InputCommander *next;
  24. };
  25. struct InputCommander * addSocketContrlToInputCommand(struct InputCommander * phead);
  26. struct InputCommander * addVoiceContrlToInputCommand(struct InputCommander * phead);

contrlDevices.h(设备工厂):

  1. #include <wiringPi.h>
  2. struct Devices
  3. {
  4. char deviceName[128];
  5. int status;
  6. int pinNum;
  7. int (*open)(int pinNum);
  8. int (*close)(int pinNum);
  9. int (*deviceInit)(int pinNum);
  10. int (*readStatus)(int pinNum);
  11. int (*changeStatus)(int pinNum);
  12. struct Devices *next;
  13. };
  14. struct Devices * addBathroomLightToDeviceLink(struct Devices *phead);
  15. struct Devices * addLivingroomLightToDeviceLink(struct Devices *phead);
  16. struct Devices * addUpstairLightToDeviceLink(struct Devices *phead);
  17. struct Devices * addRestaurantLightToDeviceLink(struct Devices *phead);
  18. struct Devices * addFireIfOrNotToDeviceLink(struct Devices *phead);
  19. struct Devices * addAlertToDeviceLink(struct Devices *phead);
  20. struct Devices * addLockToDeviceLink(struct Devices *phead);
  21. struct Devices * addCameraToDeviceLink(struct Devices *phead);
  22. struct Devices * addCsbToDeviceLink(struct Devices *phead);
  23. struct Devices * addDht11ToDeviceLink(struct Devices *phead);

socketContrl.c

  1. #include "InputCommand.h"
  2. #include <wiringSerial.h>
  3. #include <stdio.h>
  4. #include <sys/types.h>
  5. #include <sys/socket.h>
  6. #include <netinet/in.h>
  7. #include <arpa/inet.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. int socketInit(struct InputCommander *socketMes)
  11. {
  12. int s_fd;
  13. struct sockaddr_in s_addr;
  14. memset(&s_addr,0,sizeof(struct sockaddr_in));
  15. //1. socket
  16. s_fd = socket(AF_INET, SOCK_STREAM, 0);
  17. if(s_fd == -1){
  18. perror("socket");
  19. exit(-1);
  20. }
  21. s_addr.sin_family = AF_INET;
  22. s_addr.sin_port = htons(atoi(socketMes->port));
  23. inet_aton(socketMes->ipAddress,&s_addr.sin_addr);
  24. //2. bind
  25. bind(s_fd,(struct sockaddr *)&s_addr,sizeof(struct sockaddr_in));
  26. //3. listen
  27. listen(s_fd,20);
  28. printf("socket server listening...\n");
  29. socketMes->s_fd = s_fd;
  30. return s_fd;
  31. }
  32. struct InputCommander socketContrl={
  33. .commandName = "socketServer",
  34. .command={'\0'},
  35. .port="8888",
  36. .ipAddress="192.168.1.3",
  37. .log={'\0',},
  38. .Init =socketInit,
  39. .temp={'\0'},
  40. .hum={'\0'},
  41. .fireIfOrNot={'\0'}
  42. };
  43. struct InputCommander * addSocketContrlToInputCommand(struct InputCommander * phead)
  44. {
  45. if(phead ==NULL){
  46. return &socketContrl;
  47. }else{
  48. socketContrl.next = phead;
  49. return &socketContrl;
  50. }
  51. }

voiceContrl.c

  1. #include "InputCommand.h"
  2. #include <wiringSerial.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #define SIZE 7
  8. char Light_ON[7]={0xFD,0x03,0x55,0x55,0xC0,0x73,0xDF};
  9. char Light_OFF[7]={0xFD,0x03,0x55,0x55,0x30,0x73,0xDF};
  10. char CUR_ON[7]={0xFD,0x03,0x55,0x55,0x0C,0x73,0xDF};
  11. char CUR_OFF[7]={0xFD,0x03,0x55,0x55,0x03,0x73,0xDF};
  12. int voiceGetCommand(struct InputCommander *voicer)
  13. {
  14. int nread=0;
  15. memset(voicer->command,'\0',sizeof(voicer->command));
  16. nread=read(voicer->fd,voicer->command,sizeof(voicer->command));
  17. return nread;
  18. }
  19. int voiceInit(struct InputCommander *voicer)
  20. {
  21. int fd;
  22. if((fd =serialOpen(voicer->deviceName,voicer->baud))==-1){
  23. perror("serial open error\n");
  24. exit(-1);
  25. }
  26. voicer->fd = fd;
  27. return fd;
  28. }
  29. int voiceSendCommand(struct InputCommander *voicer)
  30. {
  31. int n_write=0;
  32. if(!strcmp(voicer->command,"s o")){
  33. n_write=write(voicer->fd,Light_ON,7);
  34. }else if(!strcmp(voicer->command,"s c")){
  35. n_write=write(voicer->fd,Light_OFF,7);
  36. }else if(!strcmp(voicer->command,"c o")){
  37. n_write=write(voicer->fd,CUR_ON,7);
  38. }else if(!strcmp(voicer->command,"c c")){
  39. n_write=write(voicer->fd,CUR_OFF,7);
  40. }
  41. return n_write;
  42. }
  43. struct InputCommander voiceContrl={
  44. .commandName = "voice",
  45. .deviceName="/dev/ttyAMA0",
  46. .baud=9600,
  47. .command={'\0'},//指令清零
  48. .log={'\0'},
  49. .Init =voiceInit,
  50. .getCommand = voiceGetCommand,
  51. .sendCommand= voiceSendCommand,
  52. .next =NULL
  53. };
  54. struct InputCommander * addVoiceContrlToInputCommand(struct InputCommander * phead)
  55. {
  56. if(phead ==NULL){
  57. return &voiceContrl;
  58. }else{
  59. voiceContrl.next = phead;
  60. return &voiceContrl;
  61. }
  62. }

bathroomLight.c: livingroomLight.c,restaurantLight.c,upstairLight.c同

  1. #include "contrlDevices.h"
  2. #include <stdlib.h>
  3. int bathroomLightOpen(int pinNum)
  4. {
  5. digitalWrite(pinNum,LOW);
  6. }
  7. int bathroomLightClose(int pinNum)
  8. {
  9. digitalWrite(pinNum,HIGH);
  10. }
  11. int bathroomLightInitCloseInit(int pinNum)
  12. {
  13. pinMode(pinNum,OUTPUT);
  14. digitalWrite(pinNum,HIGH);
  15. }
  16. struct Devices bathroomLight=
  17. {
  18. .deviceName="bathroomLight",
  19. .pinNum=21,
  20. .open = bathroomLightOpen,
  21. .close =bathroomLightClose,
  22. .deviceInit=bathroomLightInitCloseInit,
  23. };
  24. struct Devices * addBathroomLightToDeviceLink(struct Devices *phead)
  25. {
  26. if(phead == NULL){
  27. return &bathroomLight;
  28. }else{
  29. bathroomLight.next=phead;
  30. return &bathroomLight;
  31. }
  32. }

lock.c

  1. #include "contrlDevices.h"
  2. #include <stdlib.h>
  3. int lockOpen(int pinNum)
  4. {
  5. pinMode(pinNum,OUTPUT);
  6. //通电开锁,断电关锁;
  7. digitalWrite(pinNum,HIGH);
  8. digitalWrite(pinNum,LOW);
  9. }
  10. int lockClose(int pinNum)
  11. {
  12. pinMode(pinNum,OUTPUT);
  13. digitalWrite(pinNum,LOW);
  14. }
  15. int lockInit(int pinNum)
  16. {
  17. pinMode(pinNum,OUTPUT);
  18. digitalWrite(pinNum,LOW);
  19. }
  20. int lockReadStatus(int pinNum)
  21. {
  22. //高电平开,低电平为关
  23. return digitalRead(pinNum);
  24. }
  25. struct Devices lock=
  26. {
  27. .deviceName="lock",
  28. .pinNum=29,
  29. .open = lockOpen,
  30. .close =lockClose,
  31. .deviceInit=lockInit,
  32. .readStatus=lockReadStatus
  33. };
  34. struct Devices * addLockToDeviceLink(struct Devices *phead)
  35. {
  36. if(phead == NULL){
  37. return &lock;
  38. }else{
  39. lock.next=phead;
  40. return &lock;
  41. }
  42. }

fire.c

  1. #include "contrlDevices.h"
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. int fireIfOrNotInit(int pinNum)
  5. {
  6. pinMode(pinNum,INPUT);
  7. digitalWrite(pinNum,HIGH);
  8. }
  9. int fireStatusRead(int pinNum)
  10. {
  11. return digitalRead(pinNum);
  12. //低电平为有火灾,高电平为无火灾
  13. }
  14. struct Devices fireIfOrNot=
  15. {
  16. .deviceName="fireIfOrNot",
  17. .pinNum=25,
  18. .deviceInit=fireIfOrNotInit,
  19. .readStatus=fireStatusRead,
  20. };
  21. struct Devices * addFireIfOrNotToDeviceLink(struct Devices *phead)
  22. {
  23. if(phead == NULL){
  24. return &fireIfOrNot;
  25. }else{
  26. fireIfOrNot.next=phead;
  27. return &fireIfOrNot;
  28. }
  29. }

csb.c

  1. #include "contrlDevices.h"
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <sys/time.h>
  5. #define Trig 27
  6. #define Echo 28
  7. int csbInit(int pinNum)
  8. {
  9. pinMode(Trig,OUTPUT);
  10. pinMode(Echo,INPUT);
  11. }
  12. int getDistance(int pinNum)
  13. {
  14. //测量距离:2-400cm;
  15. struct timeval tv1; //timeval是time.h中的预定义结构体 其中包含两个一个是秒,一个是微秒
  16. struct timeval tv2;
  17. long start, stop;
  18. float dis;
  19. digitalWrite(Trig, LOW);
  20. delayMicroseconds(2);
  21. digitalWrite(Trig, HIGH);
  22. delayMicroseconds(10); //发出超声波脉冲
  23. digitalWrite(Trig, LOW);
  24. while(digitalRead(Echo) != 1);
  25. gettimeofday(&tv1, NULL); //获取当前时间 开始接收到返回信号的时候
  26. while(digitalRead(Echo) != 0);
  27. gettimeofday(&tv2, NULL); //获取当前时间 最后接收到返回信号的时候
  28. start = tv1.tv_sec * 1000000 + tv1.tv_usec; //微秒级的时间
  29. stop = tv2.tv_sec * 1000000 + tv2.tv_usec;
  30. dis = (float)(stop - start) / 1000000 * 34000 / 2; //计算时间差求出距离
  31. printf("dis=%.2f cm\n",dis);
  32. if(dis>100){
  33. return 1;
  34. }else{
  35. return 0;
  36. }
  37. }
  38. struct Devices csb=
  39. {
  40. .deviceName="csb",
  41. .deviceInit=csbInit,
  42. .readStatus=getDistance//大于1米,返回1,小于1米 返回0;
  43. };
  44. struct Devices * addCsbToDeviceLink(struct Devices *phead)
  45. {
  46. if(phead == NULL){
  47. return &csb;
  48. }else{
  49. csb.next=phead;
  50. return &csb;
  51. }
  52. }

dht11.c

  1. #include "contrlDevices.h"
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. unsigned long int databuf;
  6. int DHT11_start(int pinNum)
  7. {
  8. pinMode(pinNum, OUTPUT); // set mode to output
  9. digitalWrite(pinNum, HIGH); // output a high level
  10. sleep(1);
  11. pinMode(pinNum, OUTPUT);
  12. digitalWrite(pinNum, LOW);
  13. delay(25);
  14. digitalWrite(pinNum, HIGH);
  15. pinMode(pinNum, INPUT);
  16. pullUpDnControl(pinNum, PUD_UP); //当引脚被配置为输入(INPUT)模式,使用函数pullUpDnControl来激活其内部的上拉电阻或下拉电阻
  17. delayMicroseconds(27);
  18. return 1;
  19. }
  20. /*
  21. //主机接受数据
  22. 1.主机接受到从机回复的响应信号
  23. 2.格式0——54us的低电平+2327us的高电平
  24. 格式1——54us的低电平+6874us的高电平
  25. 3.思路:从识别到低电平开始,然后去除掉掉前面54秒的低电平还有
  26. */
  27. int DHT11_read(int pinNum)
  28. {
  29. int crc, i;
  30. if (0 == digitalRead(pinNum)) //主机接收到从机发送的响应信号(低电平)
  31. {
  32. while(!digitalRead(pinNum)); //主机接收到从机发送的响应信号(高电平)
  33. for (i = 0; i < 32; i++)
  34. {
  35. while(digitalRead(pinNum)); //数据位开始的54us低电平
  36. while(!digitalRead(pinNum)); //数据位开始的高电平就开始
  37. delayMicroseconds(32); //跳过位数据,32us已经是数据0和数据1的差距点
  38. databuf *= 2;
  39. if (digitalRead(pinNum) == 1)
  40. {
  41. databuf++;
  42. }
  43. }
  44. for (i = 0; i < 8; i++)
  45. {
  46. while (digitalRead(pinNum));
  47. while (!digitalRead(pinNum));
  48. delayMicroseconds(32);
  49. crc *= 2;
  50. if (digitalRead(pinNum) == 1)
  51. {
  52. crc++;
  53. }
  54. }
  55. return 1;
  56. }
  57. else
  58. {
  59. return 0;
  60. }
  61. }
  62. struct Devices dht11=
  63. {
  64. .deviceName="dht11",
  65. .pinNum=0,
  66. .deviceInit=DHT11_start,
  67. .readStatus=DHT11_read
  68. };
  69. struct Devices * addDht11ToDeviceLink(struct Devices *phead)
  70. {
  71. if(phead == NULL){
  72. return &dht11;
  73. }else{
  74. dht11.next=phead;
  75. return &dht11;
  76. }
  77. }

camera.c

  1. #include "contrlDevices.h"
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <stdio.h>
  5. #include <curl/curl.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <fcntl.h>
  12. char buf[10240]={'\0'};
  13. size_t readData( void *ptr, size_t size, size_t nmemb, void *stream)
  14. {
  15. strncpy(buf,ptr,1024);
  16. }
  17. char * base64Img(char *filename)
  18. {
  19. char cmd[128];
  20. char *bufPic;
  21. sprintf(cmd,"base64 %s >tmpFile",filename);
  22. system(cmd);
  23. int fd = open("./tmpFile",O_RDWR);
  24. int filesize = lseek(fd,0,SEEK_END);
  25. lseek(fd,0,SEEK_SET);
  26. bufPic=(char *)malloc(sizeof(char)*filesize+2);
  27. memset(bufPic,'\0',filesize+2);
  28. read(fd,bufPic,filesize);
  29. close(fd);
  30. system("rm -f tmpFile");
  31. return bufPic;
  32. }
  33. int postUrl(int pinNum)
  34. {
  35. CURL *curl;
  36. CURLcode res;
  37. int status;
  38. char *postString;
  39. char *key="PkRgwW5TLdU2kqLDpncZvz";
  40. char *secret="2977c55e431f487194e5eea44a804328";
  41. int typeId = 21;
  42. char *format="xml";
  43. char *img1=base64Img("./person1.jpg");
  44. char *img2 = base64Img("./person2.jpg");
  45. int len = strlen(key)+strlen(secret)+strlen(img1)+strlen(img2)+strlen(format);
  46. postString =(char *)malloc(sizeof(char)*(len+124));
  47. memset(postString,'\0',len+124);
  48. sprintf(postString,"&img1=%s&img2=%s&key=%s&secret=%s&typeId=%d&format=%s",img1,img2,key,secret,typeId,format);
  49. curl = curl_easy_init();
  50. if (curl)
  51. {
  52. curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "/tmp/cookie.txt"); //>定cookie文件
  53. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postString); // 指定post内容
  54. curl_easy_setopt(curl, CURLOPT_URL, "https://netocr.com/api/faceliu.do"); // 指定url
  55. curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,readData);
  56. res = curl_easy_perform(curl);
  57. if(strstr(buf,"是")!=NULL){
  58. printf("camera:same person\n");
  59. status=1;
  60. }else{
  61. printf("camera:diff person\n");
  62. status =0;
  63. }
  64. curl_easy_cleanup(curl);
  65. }
  66. return status;
  67. }
  68. int camera_readStatus(int pinNum)
  69. {
  70. int status;
  71. system(" wget -O ./person1.jpg http://192.168.1.3:8080/?action=snapshot");
  72. printf("take ptoto success\n");
  73. status = postUrl(pinNum);
  74. return status;
  75. }
  76. struct Devices camera=
  77. {
  78. .deviceName="camera",
  79. .readStatus=camera_readStatus//1是同一个人,开锁,0不是同一个人,关锁;
  80. };
  81. struct Devices * addCameraToDeviceLink(struct Devices *phead)
  82. {
  83. if(phead == NULL){
  84. return &camera;
  85. }else{
  86. camera.next=phead;
  87. return &camera;
  88. }
  89. }

主程序MainPro.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <pthread.h>
  5. #include <unistd.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <arpa/inet.h>
  9. #include "contrlDevices.h"
  10. #include "InputCommand.h"
  11. #include <wiringSerial.h>
  12. extern unsigned long int databuf;
  13. struct Devices *pdeviceHead = NULL;
  14. struct InputCommander *pCommandHead=NULL;
  15. struct InputCommander *socketHandler;
  16. struct InputCommander *voiceHandler;
  17. struct Devices *fireHandler;
  18. int c_fd;
  19. struct Devices * findDevicesByName(char *str,struct Devices * phead)
  20. {
  21. struct Devices *tmp = phead;
  22. if(tmp == NULL){
  23. printf("find devices error\n");
  24. return NULL;
  25. }else{
  26. while(tmp!=NULL){
  27. if(!strcmp(tmp->deviceName,str)){
  28. return tmp;
  29. }
  30. tmp=tmp->next;
  31. }
  32. }
  33. }
  34. struct InputCommander * findInputCommanderByName(char *str,struct InputCommander * phead)
  35. {
  36. struct InputCommander *tmp = phead;
  37. if(tmp == NULL){
  38. printf("find InputCommander error\n");
  39. return NULL;
  40. }else{
  41. while(tmp!=NULL){
  42. if(!strcmp(tmp->commandName,str)){
  43. return tmp;
  44. }
  45. tmp=tmp->next;
  46. }
  47. }
  48. }
  49. void openDevices(char *deviceName)
  50. {
  51. struct Devices *deviceHandler;
  52. deviceHandler=findDevicesByName(deviceName,pdeviceHead);
  53. deviceHandler->deviceInit(deviceHandler->pinNum);
  54. deviceHandler->open(deviceHandler->pinNum);
  55. }
  56. void closeDevices(char *deviceName)
  57. {
  58. struct Devices *deviceHandler;
  59. deviceHandler=findDevicesByName(deviceName,pdeviceHead);
  60. deviceHandler->deviceInit(deviceHandler->pinNum);
  61. deviceHandler->close(deviceHandler->pinNum);
  62. }
  63. void cmdContrlLight(char cmd[12])
  64. {
  65. if(!strcmp(cmd,"b o")){
  66. openDevices("bathroomLight");
  67. }else if(!strcmp(cmd,"b c")){
  68. closeDevices("bathroomLight");
  69. }else if(!strcmp(cmd,"l o")){
  70. openDevices("livingroomLight");
  71. }else if(!strcmp(cmd,"l c")){
  72. closeDevices("livingroomLight");
  73. }else if(!strcmp(cmd,"r o")){
  74. openDevices("restaurantLight");
  75. }else if(!strcmp(cmd,"r c")){
  76. closeDevices("restaurantLight");
  77. }else if(!strcmp(cmd,"u o")){
  78. openDevices("upstairLight");
  79. }else if(!strcmp(cmd,"u c")){
  80. closeDevices("upstairLight");
  81. }
  82. }
  83. void *read_thread(void *datas)
  84. {
  85. int n_read;
  86. while(1){
  87. memset(socketHandler->command,0,sizeof(socketHandler->command));
  88. n_read = read(c_fd,socketHandler->command, sizeof(socketHandler->command));
  89. if(n_read == -1){
  90. perror("read");
  91. }else if(n_read>0){
  92. printf("socket get cmd: %s\n",socketHandler->command);
  93. cmdContrlLight(socketHandler->command);
  94. }else{
  95. printf("client quit\n");
  96. break;
  97. }
  98. }
  99. }
  100. void *write_thread(void *datas)
  101. {
  102. struct Devices *dht11Handler;
  103. int n_write;
  104. char message[128]={'\0'};
  105. dht11Handler = findDevicesByName("dht11",pdeviceHead);
  106. while(1){
  107. dht11Handler->deviceInit(dht11Handler->pinNum);
  108. dht11Handler->readStatus(dht11Handler->pinNum);
  109. memset(socketHandler->hum,'\0',12);
  110. memset(socketHandler->temp,'\0',12);
  111. memset(socketHandler->fireIfOrNot,'\0',32);
  112. //低电平为有火灾,高电平为无火灾
  113. if(fireHandler->readStatus(fireHandler->pinNum)){
  114. strcpy(socketHandler->fireIfOrNot,"fire watching...");
  115. }else{
  116. strcpy(socketHandler->fireIfOrNot,"fire warning!!!Attention!");
  117. }
  118. sprintf(socketHandler->temp,"%d.%d°C", (databuf >> 8) & 0xff, databuf & 0xff);
  119. sprintf(socketHandler->hum,"%d.%d%rh",(databuf >> 24) & 0xff, (databuf >> 16) & 0xff);
  120. sprintf(message,"%s,%s,%s",socketHandler->temp,socketHandler->hum,socketHandler->fireIfOrNot);
  121. printf("message:%s\n",message);
  122. write(c_fd,message,strlen(message));
  123. sleep(10);
  124. }
  125. }
  126. void *socket_thread(void *datas)
  127. {
  128. pthread_t readThread;
  129. pthread_t writeThread;
  130. struct sockaddr_in c_addr;
  131. int n_read = 0;
  132. int clen = sizeof(struct sockaddr_in);
  133. memset(&c_addr,0,clen);
  134. socketHandler=findInputCommanderByName("socketServer",pCommandHead);
  135. if(socketHandler==NULL){
  136. printf("find socketHandler error\n");
  137. pthread_exit(NULL);
  138. }else{
  139. socketHandler->Init(socketHandler);
  140. printf("%s init success\n",socketHandler->commandName);
  141. while(1){
  142. //不断接收新的客户端的接入,接入新的客户端后创建新的读线程和写线程:
  143. c_fd = accept(socketHandler->s_fd,(struct sockaddr *)&c_addr,&clen);
  144. if(c_fd == -1){
  145. perror("accept");
  146. }
  147. pthread_create(&readThread,NULL,read_thread,NULL);
  148. pthread_create(&writeThread,NULL,write_thread,NULL);
  149. }
  150. }
  151. }
  152. void *voice_thread(void *datas)
  153. {
  154. int nread;
  155. voiceHandler=findInputCommanderByName("voice",pCommandHead);
  156. if(voiceHandler==NULL){
  157. printf("find voiceHandler error\n");
  158. pthread_exit(NULL);
  159. }else{
  160. if(voiceHandler->Init(voiceHandler)<0){
  161. printf("voice init error\n");
  162. pthread_exit(NULL);
  163. }else{
  164. printf("%s init success\n",voiceHandler->commandName);
  165. }
  166. while(1){
  167. nread=voiceHandler->getCommand(voiceHandler);
  168. if(nread==0){
  169. //printf("no data from voice\n");
  170. }else{
  171. printf("voice get cmd: %s\n",voiceHandler->command);
  172. voiceHandler->sendCommand(voiceHandler);
  173. cmdContrlLight(voiceHandler->command);
  174. }
  175. }
  176. }
  177. }
  178. void *fire_thread(void *datas)
  179. {
  180. struct Devices *alertHandler;
  181. fireHandler=findDevicesByName("fireIfOrNot",pdeviceHead);
  182. alertHandler=findDevicesByName("alert", pdeviceHead);
  183. fireHandler->deviceInit(fireHandler->pinNum);
  184. alertHandler->deviceInit(alertHandler->pinNum);
  185. while(1)
  186. {
  187. int status;
  188. status = fireHandler->readStatus(fireHandler->pinNum);
  189. if(!status){
  190. //有火灾,打开报警器:
  191. alertHandler->open(alertHandler->pinNum);
  192. }else{
  193. //无火灾:关闭报警器:
  194. alertHandler->close(alertHandler->pinNum);
  195. }
  196. }
  197. }
  198. void *camera_thread(void * datas)
  199. {
  200. struct Devices *cameraHandler;
  201. struct Devices *lockHandler;
  202. struct Devices *csbHandler;
  203. cameraHandler=findDevicesByName("camera",pdeviceHead );
  204. lockHandler=findDevicesByName("lock",pdeviceHead);
  205. csbHandler=findDevicesByName("csb",pdeviceHead);
  206. lockHandler->deviceInit(lockHandler->pinNum);
  207. csbHandler->deviceInit(csbHandler->pinNum);
  208. while(1){
  209. //距离大于1米,返回1
  210. while(! csbHandler->readStatus( csbHandler->pinNum)){
  211. printf("dis<1m,taking photo...\n");
  212. lockHandler->status=lockHandler->readStatus(lockHandler->pinNum);//检测锁的状态
  213. cameraHandler->status=cameraHandler->readStatus(cameraHandler->pinNum);//对比人脸;
  214. //1:是同一个人,开 锁,0:不是同一个人,关锁;
  215. //1:锁是开状态,0:锁是关闭状态;
  216. if( cameraHandler->status && !lockHandler->status){
  217. lockHandler->open(lockHandler->pinNum);
  218. printf("open lock\n");
  219. }
  220. }
  221. sleep(10);//10秒检测一次距离,距离小于1米就拍照对比
  222. }
  223. }
  224. int main()
  225. {
  226. pthread_t voiceThread;
  227. pthread_t socketThread;
  228. pthread_t fireIfOrNotThread;
  229. pthread_t cameraThread;
  230. if(wiringPiSetup()==-1){
  231. printf("wiringPi init error\n");
  232. exit(-1);
  233. }
  234. //1.指令工厂初始化
  235. pCommandHead= addSocketContrlToInputCommand(pCommandHead);
  236. pCommandHead=addVoiceContrlToInputCommand(pCommandHead);
  237. //2.设备控制工厂初始化
  238. pdeviceHead=addBathroomLightToDeviceLink(pdeviceHead);
  239. pdeviceHead=addLivingroomLightToDeviceLink(pdeviceHead);
  240. pdeviceHead=addUpstairLightToDeviceLink(pdeviceHead);
  241. pdeviceHead=addRestaurantLightToDeviceLink(pdeviceHead);
  242. pdeviceHead=addFireIfOrNotToDeviceLink(pdeviceHead);
  243. pdeviceHead=addAlertToDeviceLink(pdeviceHead);
  244. pdeviceHead=addLockToDeviceLink(pdeviceHead);
  245. pdeviceHead=addCameraToDeviceLink(pdeviceHead);
  246. pdeviceHead=addCsbToDeviceLink(pdeviceHead);
  247. pdeviceHead=addDht11ToDeviceLink(pdeviceHead);
  248. //3.线程池建立
  249. //3.1语音线程
  250. pthread_create(&voiceThread,NULL, voice_thread,NULL);
  251. //3.2socket 线程
  252. pthread_create(&socketThread,NULL, socket_thread,NULL);
  253. //3.3摄像头线程
  254. pthread_create(&cameraThread,NULL,camera_thread,NULL);
  255. //3.4火灾线程
  256. pthread_create(&fireIfOrNotThread,NULL,fire_thread,NULL);
  257. //等待线程退出:
  258. pthread_join(voiceThread,NULL);
  259. pthread_join(socketThread,NULL);
  260. pthread_join(fireIfOrNotThread,NULL);
  261. pthread_join(cameraThread,NULL);
  262. return 0;
  263. }

四、客户端

(37条消息) 智能家居Android设计_lelebanaba的博客-CSDN博客w

五、项目演示

智能家居

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/893484
推荐阅读
相关标签
  

闽ICP备14008679号