当前位置:   article > 正文

基于树莓派开发的智能家居控制系统_树莓派家居智能控制系统

树莓派家居智能控制系统

 一.功能介绍

1.通过手机端网络调试助手,语音识别,对灯光,风扇,等电器进行远程控制。

 2.通过火焰报警,震动报警,视频监控等功能保证家庭的安全。

3.实时将环境温湿度数据,各种安防信息发送到客户端进行显示。

4.通过红外遥控也能对部分家电进行远程控制

5.通过翔云平台提供的人脸对比识别服务,实现人脸识别开锁功能。

二.实物展示

   

                 人脸识别按钮和门锁                                                433m红外接收模块

                         摄像头                                                        火焰报警和雨滴传感器

    手机端实时显示环境状态            手机端通过指令控制家电                  网页实时视频监控

 三.程序

 编译运行:

 1.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 <netinet/in.h>
  10. #include <stdint.h>
  11. #include <curl/curl.h>
  12. #include"InputCommand.h"
  13. #include"contrlDevices.h"
  14. //为了线程里能调到链表头,要把这两个表头设置为全局变量
  15. struct Devices *pdeviceHead = NULL;
  16. struct InputCommander *pCommandHead = NULL;
  17. struct InputCommander *socketHandler = NULL;
  18. int c_fd;
  19. //定义互斥量(锁)
  20. pthread_mutex_t mutex;
  21. typedef unsigned char uint8;
  22. typedef unsigned int uint16;
  23. typedef unsigned long uint32;
  24. #define HIGH_TIME 32
  25. pthread_t writeThread;
  26. pthread_t voiceThread;
  27. pthread_t socketThread;
  28. pthread_t fireThread;
  29. pthread_t DHT11Thread;
  30. pthread_t readThread;
  31. pthread_t shakeThread;
  32. pthread_t windowThread;
  33. pthread_t keyThread;
  34. pthread_t cameraThread;
  35. pthread_t remoteThread;
  36. int tmp = 0;//设置火灾和振动的标志位
  37. int wintmp1 = 0;
  38. int wintmp2 = 0;
  39. #define key 6 //拍照按键
  40. char Message[4][100] = {"未启用!", "未启用!", "未启用!", "未启用!"};
  41. int write_flag = 0; //标记位,标记通知线程是否打开 1:打开 0:关闭
  42. int a = 0;
  43. int b = 0;
  44. extern uint32 databuf;
  45. struct Devices* findDeviceByName(char *name, struct Devices *phead)
  46. {
  47. struct Devices *tmp = phead;
  48. if(phead == NULL){
  49. return NULL;
  50. }else{
  51. while(tmp != NULL){
  52. if(strcmp(tmp->deviceName, name) == 0){
  53. return tmp;
  54. }
  55. tmp = tmp->next;
  56. }
  57. return NULL;
  58. }
  59. }
  60. struct InputCommander* findCommandByName(char *name, struct InputCommander *phead)
  61. {
  62. struct InputCommander *tmp = phead;
  63. if(phead == NULL){
  64. return NULL;
  65. }else{
  66. while(tmp != NULL){
  67. if(strcmp(tmp->commandName, name) == 0){
  68. return tmp;
  69. }
  70. tmp = tmp->next;
  71. }
  72. return NULL;
  73. }
  74. }
  75. void *window_thread(void *datas)
  76. {
  77. struct Devices *windowDeviceTmp = NULL;
  78. windowDeviceTmp = findDeviceByName("window", pdeviceHead);
  79. if(windowDeviceTmp == NULL)
  80. {
  81. printf("find window error\n");
  82. pthread_exit(NULL);
  83. }
  84. windowDeviceTmp->deviceInit(windowDeviceTmp->pinNum);
  85. while(1){
  86. if(digitalRead(5) == 0){ //没雨 开窗 5引脚就是检测是否有雨水
  87. sleep(1);
  88. b = 1;
  89. if(a != 0){
  90. windowDeviceTmp->open(windowDeviceTmp->pinNum);//正转 开窗
  91. sleep(5);
  92. windowDeviceTmp->deviceInit(windowDeviceTmp->pinNum);//
  93. while(digitalRead(5) == 0);
  94. }
  95. }
  96. else if(digitalRead(5) == 1){
  97. sleep(1);
  98. a = 1;
  99. if(b != 0){
  100. windowDeviceTmp->close(windowDeviceTmp->pinNum);//反转 关窗
  101. sleep(5);
  102. windowDeviceTmp->deviceInit(windowDeviceTmp->pinNum);//
  103. while(digitalRead(5) == 1);
  104. }
  105. }
  106. }
  107. }
  108. void *shake_thread(void *datas) //震动线程
  109. {
  110. int tmp1 = 0;
  111. struct Devices *shakeDeviceTmp = NULL;
  112. struct Devices *shakebuzzerDeviceTmp = NULL;
  113. shakeDeviceTmp = findDeviceByName("shake", pdeviceHead); //在设备工厂找到震动模块和震动蜂鸣器
  114. shakebuzzerDeviceTmp = findDeviceByName("shakebuzzer", pdeviceHead);
  115. if((shakeDeviceTmp == NULL)&&(shakebuzzerDeviceTmp == NULL))
  116. {
  117. printf("find shake or shakebuzzer error\n");
  118. pthread_exit(NULL);
  119. }
  120. shakeDeviceTmp->deviceInit(shakeDeviceTmp->pinNum); //震动初始化
  121. shakebuzzerDeviceTmp->deviceInit(shakebuzzerDeviceTmp->pinNum);//蜂鸣器初始化
  122. while(1)
  123. {
  124. //tmp = 1;
  125. if(digitalRead(26) == 1)
  126. {
  127. shakebuzzerDeviceTmp->open(shakebuzzerDeviceTmp->pinNum); //开震动蜂鸣器
  128. delay(3);
  129. shakebuzzerDeviceTmp->close(shakebuzzerDeviceTmp->pinNum);
  130. memset(Message[3], 0, sizeof Message[3]); //清空数组
  131. sprintf(Message[3], "无震动 "); //更新震动信息
  132. }
  133. else
  134. {
  135. memset(Message[3], 0, sizeof Message[3]); //清空数组
  136. sprintf(Message[3], "有震动! ");
  137. shakebuzzerDeviceTmp->close(shakebuzzerDeviceTmp->pinNum); //关震动蜂鸣器
  138. }
  139. }
  140. }
  141. void *fire_thread(void *datas) //火灾线程
  142. {
  143. struct Devices *fireDeviceTmp = NULL;
  144. struct Devices *buzzerDeviceTmp = NULL;
  145. fireDeviceTmp = findDeviceByName("fireIfOrNot", pdeviceHead); //在设备工厂找到火焰模块和火焰蜂鸣器
  146. buzzerDeviceTmp = findDeviceByName("buzzer", pdeviceHead);
  147. if((fireDeviceTmp == NULL)&&(buzzerDeviceTmp == NULL))
  148. {
  149. printf("find fire or buzzer error\n");
  150. pthread_exit(NULL);
  151. }
  152. fireDeviceTmp->deviceInit(fireDeviceTmp->pinNum); //火灾模块初始化
  153. buzzerDeviceTmp->deviceInit(buzzerDeviceTmp->pinNum);//蜂鸣器初始化
  154. while(1)
  155. {
  156. if(digitalRead(25) == 1)
  157. {
  158. buzzerDeviceTmp->open(buzzerDeviceTmp->pinNum); //开火灾蜂鸣器
  159. memset(Message[2], 0, sizeof Message[2]); //清空数组
  160. sprintf(Message[2], "无火灾"); //更新火灾信息
  161. }
  162. else
  163. {
  164. buzzerDeviceTmp->close(buzzerDeviceTmp->pinNum); //关火灾蜂鸣器
  165. memset(Message[2], 0, sizeof Message[2]); //清空数组
  166. sprintf(Message[2], "有火灾! ");
  167. tmp = 0;
  168. }
  169. }
  170. }
  171. void *voice_thread(void* datas)//语音线程
  172. {
  173. struct InputCommander *voiceHandler;
  174. struct Devices *deviceTmp = NULL;
  175. int nread;
  176. voiceHandler = findCommandByName("voice", pCommandHead);//找语音节点
  177. if(voiceHandler == NULL)
  178. {
  179. printf("find voice error\n");
  180. pthread_exit(NULL);
  181. }
  182. else
  183. { //找到了
  184. if(voiceHandler->Init(voiceHandler, NULL, NULL) < 0)
  185. { //先初始化
  186. printf("voice init error\n");
  187. pthread_exit(NULL);//退出线程
  188. }
  189. else
  190. {
  191. printf("%s init success\n",voiceHandler->commandName);
  192. }
  193. pthread_mutex_lock(&mutex); //加锁
  194. //语音读取一级指令的时候,为了避免其它线程对于 紧接着读取二级指令的干扰
  195. while(1)
  196. { //读数据
  197. memset(voiceHandler->command, '\0', sizeof(voiceHandler->command));
  198. nread = voiceHandler->getCommand(voiceHandler);//获取语音模块发来的数据
  199. printf("get voice command:%s\n", voiceHandler->command);
  200. if(nread == 0)
  201. {
  202. printf("nodata from voice\n");
  203. }
  204. else if(strstr(voiceHandler->command, "kycd") != NULL) //1如果收到数据
  205. {
  206. deviceTmp = findDeviceByName("livingRoomLight",pdeviceHead);//通过名子找到节点
  207. deviceTmp->deviceInit(deviceTmp->pinNum);//初始化
  208. deviceTmp->open(deviceTmp->pinNum);//开泳池灯
  209. }
  210. else if(strstr(voiceHandler->command, "gycd") != NULL)
  211. {
  212. deviceTmp = findDeviceByName("livingRoomLight",pdeviceHead);//通过名子找到节点
  213. deviceTmp->deviceInit(deviceTmp->pinNum);//初始化
  214. deviceTmp->close(deviceTmp->pinNum);//关泳池灯
  215. }
  216. else if(strstr(voiceHandler->command, "kwsd") != NULL) //
  217. {
  218. deviceTmp = findDeviceByName("upstairLight",pdeviceHead);//通过名子找到节点
  219. deviceTmp->deviceInit(deviceTmp->pinNum);//初始化
  220. deviceTmp->open(deviceTmp->pinNum);//开卧室灯
  221. }
  222. else if(strstr(voiceHandler->command, "gwsd") != NULL)
  223. {
  224. deviceTmp = findDeviceByName("upstairLight",pdeviceHead);//通过名子找到节点
  225. deviceTmp->deviceInit(deviceTmp->pinNum);//初始化
  226. deviceTmp->close(deviceTmp->pinNum);//关卧室灯
  227. }
  228. else if(strstr(voiceHandler->command, "kysd") != NULL)
  229. {
  230. deviceTmp = findDeviceByName("bathroomLight",pdeviceHead);
  231. deviceTmp->deviceInit(deviceTmp->pinNum);//初始化
  232. deviceTmp->open(deviceTmp->pinNum);//开浴室灯
  233. }
  234. else if(strstr(voiceHandler->command, "gysd") != NULL)
  235. {
  236. deviceTmp = findDeviceByName("bathroomLight",pdeviceHead);
  237. deviceTmp->deviceInit(deviceTmp->pinNum);//初始化
  238. deviceTmp->close(deviceTmp->pinNum);//关浴室灯
  239. }
  240. else if(strstr(voiceHandler->command, "kcfd") != NULL)
  241. {
  242. deviceTmp = findDeviceByName("restauranLight",pdeviceHead);
  243. deviceTmp->deviceInit(deviceTmp->pinNum);//初始化
  244. deviceTmp->open(deviceTmp->pinNum);//开厨房灯
  245. }
  246. else if(strstr(voiceHandler->command, "gcfd") != NULL)
  247. {
  248. deviceTmp = findDeviceByName("restauranLight",pdeviceHead);
  249. deviceTmp->deviceInit(deviceTmp->pinNum);//初始化
  250. deviceTmp->close(deviceTmp->pinNum);//关厨房灯
  251. }
  252. else if(strstr(voiceHandler->command, "kfs") != NULL) //开风扇
  253. {
  254. deviceTmp = findDeviceByName("fan",pdeviceHead);
  255. deviceTmp->deviceInit(deviceTmp->pinNum);//初始化
  256. deviceTmp->open(deviceTmp->pinNum);
  257. }
  258. else if(strstr(voiceHandler->command, "gfs") != NULL) //关风扇
  259. {
  260. deviceTmp = findDeviceByName("fan",pdeviceHead);
  261. deviceTmp->deviceInit(deviceTmp->pinNum);//初始化
  262. deviceTmp->close(deviceTmp->pinNum);
  263. }
  264. else if(strstr(voiceHandler->command, "quankai") != NULL)
  265. {
  266. deviceTmp = findDeviceByName("livingRoomLight",pdeviceHead);//通过名子找到节点
  267. deviceTmp->deviceInit(deviceTmp->pinNum);//初始化
  268. deviceTmp->open(deviceTmp->pinNum);//开泳池灯
  269. deviceTmp = findDeviceByName("upstairLight",pdeviceHead);//通过名子找到节点
  270. deviceTmp->deviceInit(deviceTmp->pinNum);//初始化
  271. deviceTmp->open(deviceTmp->pinNum);//开卧室灯
  272. deviceTmp = findDeviceByName("bathroomLight",pdeviceHead);
  273. deviceTmp->deviceInit(deviceTmp->pinNum);//初始化
  274. deviceTmp->open(deviceTmp->pinNum);//开浴室灯
  275. deviceTmp = findDeviceByName("restauranLight",pdeviceHead);
  276. deviceTmp->deviceInit(deviceTmp->pinNum);//初始化
  277. deviceTmp->open(deviceTmp->pinNum);//开厨房灯
  278. deviceTmp = findDeviceByName("fan",pdeviceHead);
  279. deviceTmp->deviceInit(deviceTmp->pinNum);//初始化
  280. deviceTmp->open(deviceTmp->pinNum);//开风扇
  281. }
  282. else if(strstr(voiceHandler->command, "quanguan") != NULL) //如果收到数据
  283. {
  284. deviceTmp = findDeviceByName("livingRoomLight",pdeviceHead);//通过名子找到节点
  285. deviceTmp->deviceInit(deviceTmp->pinNum);//初始化
  286. deviceTmp->close(deviceTmp->pinNum);//关泳池灯
  287. deviceTmp = findDeviceByName("upstairLight",pdeviceHead);//通过名子找到节点
  288. deviceTmp->deviceInit(deviceTmp->pinNum);//初始化
  289. deviceTmp->close(deviceTmp->pinNum);//关卧室灯
  290. deviceTmp = findDeviceByName("bathroomLight",pdeviceHead);
  291. deviceTmp->deviceInit(deviceTmp->pinNum);//初始化
  292. deviceTmp->close(deviceTmp->pinNum);//关浴室灯
  293. deviceTmp = findDeviceByName("restauranLight",pdeviceHead);
  294. deviceTmp->deviceInit(deviceTmp->pinNum);//初始化
  295. deviceTmp->close(deviceTmp->pinNum);//关厨房灯
  296. deviceTmp = findDeviceByName("fan",pdeviceHead);
  297. deviceTmp->deviceInit(deviceTmp->pinNum);//初始化
  298. deviceTmp->close(deviceTmp->pinNum);//关风扇
  299. }
  300. }
  301. }
  302. }
  303. void *read_thread(void *datas)
  304. {
  305. int n_read;
  306. struct Devices *deviceTmp = NULL;
  307. while(1)
  308. {
  309. memset(socketHandler->command, '\0', sizeof(socketHandler->command));
  310. n_read = read(c_fd, socketHandler->command, sizeof(socketHandler->command));//客户端发来的数据存到 socketHandler->command
  311. if(n_read == -1){
  312. perror("read");
  313. }
  314. else if(n_read > 0){
  315. printf("\nget: %d,%s\n",n_read,socketHandler->command);//打印客户端发来的数据
  316. //if (strstr(socketHandler->command, "kwsd") != NULL)
  317. if(strstr(socketHandler->command, "kycd") != NULL) //1如果收到数据
  318. {
  319. deviceTmp = findDeviceByName("livingRoomLight",pdeviceHead);//通过名子找到节点
  320. deviceTmp->deviceInit(deviceTmp->pinNum);//初始化
  321. deviceTmp->open(deviceTmp->pinNum);//开泳池灯
  322. }
  323. else if(strstr(socketHandler->command, "gycd") != NULL)
  324. {
  325. deviceTmp = findDeviceByName("livingRoomLight",pdeviceHead);//通过名子找到节点
  326. deviceTmp->deviceInit(deviceTmp->pinNum);//初始化
  327. deviceTmp->close(deviceTmp->pinNum);//关泳池灯
  328. }
  329. else if(strstr(socketHandler->command, "kwsd") != NULL) //
  330. {
  331. deviceTmp = findDeviceByName("upstairLight",pdeviceHead);//通过名子找到节点
  332. deviceTmp->deviceInit(deviceTmp->pinNum);//初始化
  333. deviceTmp->open(deviceTmp->pinNum);//开卧室灯
  334. }
  335. else if(strstr(socketHandler->command, "gwsd") != NULL)
  336. {
  337. deviceTmp = findDeviceByName("upstairLight",pdeviceHead);//通过名子找到节点
  338. deviceTmp->deviceInit(deviceTmp->pinNum);//初始化
  339. deviceTmp->close(deviceTmp->pinNum);//关卧室灯
  340. }
  341. else if(strstr(socketHandler->command, "kysd") != NULL)
  342. {
  343. deviceTmp = findDeviceByName("bathroomLight",pdeviceHead);
  344. deviceTmp->deviceInit(deviceTmp->pinNum);//初始化
  345. deviceTmp->open(deviceTmp->pinNum);//开浴室灯
  346. }
  347. else if(strstr(socketHandler->command, "gysd") != NULL)
  348. {
  349. deviceTmp = findDeviceByName("bathroomLight",pdeviceHead);
  350. deviceTmp->deviceInit(deviceTmp->pinNum);//初始化
  351. deviceTmp->close(deviceTmp->pinNum);//关浴室灯
  352. }
  353. else if(strstr(socketHandler->command, "kcfd") != NULL)
  354. {
  355. deviceTmp = findDeviceByName("restauranLight",pdeviceHead);
  356. deviceTmp->deviceInit(deviceTmp->pinNum);//初始化
  357. deviceTmp->open(deviceTmp->pinNum);//开厨房灯
  358. }
  359. else if(strstr(socketHandler->command, "gcfd") != NULL)
  360. {
  361. deviceTmp = findDeviceByName("restauranLight",pdeviceHead);
  362. deviceTmp->deviceInit(deviceTmp->pinNum);//初始化
  363. deviceTmp->close(deviceTmp->pinNum);//关厨房灯
  364. }
  365. else if(strstr(socketHandler->command, "kfs") != NULL) //开风扇
  366. {
  367. deviceTmp = findDeviceByName("fan",pdeviceHead);
  368. deviceTmp->deviceInit(deviceTmp->pinNum);//初始化
  369. deviceTmp->open(deviceTmp->pinNum);
  370. }
  371. else if(strstr(socketHandler->command, "gfs") != NULL) //关风扇
  372. {
  373. deviceTmp = findDeviceByName("fan",pdeviceHead);
  374. deviceTmp->deviceInit(deviceTmp->pinNum);//初始化
  375. deviceTmp->close(deviceTmp->pinNum);
  376. }
  377. else if(strstr(socketHandler->command, "quankai") != NULL)
  378. {
  379. deviceTmp = findDeviceByName("livingRoomLight",pdeviceHead);//通过名子找到节点
  380. deviceTmp->deviceInit(deviceTmp->pinNum);//初始化
  381. deviceTmp->open(deviceTmp->pinNum);//开泳池灯
  382. deviceTmp = findDeviceByName("upstairLight",pdeviceHead);//通过名子找到节点
  383. deviceTmp->deviceInit(deviceTmp->pinNum);//初始化
  384. deviceTmp->open(deviceTmp->pinNum);//开卧室灯
  385. deviceTmp = findDeviceByName("bathroomLight",pdeviceHead);
  386. deviceTmp->deviceInit(deviceTmp->pinNum);//初始化
  387. deviceTmp->open(deviceTmp->pinNum);//开浴室灯
  388. deviceTmp = findDeviceByName("restauranLight",pdeviceHead);
  389. deviceTmp->deviceInit(deviceTmp->pinNum);//初始化
  390. deviceTmp->open(deviceTmp->pinNum);//开厨房灯
  391. deviceTmp = findDeviceByName("fan",pdeviceHead);
  392. deviceTmp->deviceInit(deviceTmp->pinNum);//初始化
  393. deviceTmp->open(deviceTmp->pinNum);//开风扇
  394. }
  395. else if(strstr(socketHandler->command, "quanguan") != NULL) //如果收到数据
  396. {
  397. deviceTmp = findDeviceByName("livingRoomLight",pdeviceHead);//通过名子找到节点
  398. deviceTmp->deviceInit(deviceTmp->pinNum);//初始化
  399. deviceTmp->close(deviceTmp->pinNum);//关泳池灯
  400. deviceTmp = findDeviceByName("upstairLight",pdeviceHead);//通过名子找到节点
  401. deviceTmp->deviceInit(deviceTmp->pinNum);//初始化
  402. deviceTmp->close(deviceTmp->pinNum);//关卧室灯
  403. deviceTmp = findDeviceByName("bathroomLight",pdeviceHead);
  404. deviceTmp->deviceInit(deviceTmp->pinNum);//初始化
  405. deviceTmp->close(deviceTmp->pinNum);//关浴室灯
  406. deviceTmp = findDeviceByName("restauranLight",pdeviceHead);
  407. deviceTmp->deviceInit(deviceTmp->pinNum);//初始化
  408. deviceTmp->close(deviceTmp->pinNum);//关厨房灯
  409. deviceTmp = findDeviceByName("fan",pdeviceHead);
  410. deviceTmp->deviceInit(deviceTmp->pinNum);//初始化
  411. deviceTmp->close(deviceTmp->pinNum);//关风扇
  412. }
  413. }
  414. else{
  415. printf("client quit\n");
  416. write_flag = 0;
  417. pthread_cancel(writeThread);
  418. pthread_exit(NULL);
  419. }
  420. }
  421. }
  422. void *write_thread(void *datas) //实时向客户端发送消息
  423. {
  424. while (1)
  425. {
  426. delay(1000);
  427. write(c_fd, Message, 400);
  428. }
  429. }
  430. void *socket_thread(void* datas)
  431. {
  432. int n_read = 0;
  433. struct sockaddr_in c_addr;
  434. memset(&c_addr, 0, sizeof(struct sockaddr_in));
  435. int clen = sizeof(struct sockaddr_in);
  436. socketHandler = findCommandByName("socketServer", pCommandHead);//找socket节点
  437. if(socketHandler == NULL){
  438. printf("find socketHandler error\n");
  439. pthread_exit(NULL);
  440. }else{
  441. printf("%s init success\n",socketHandler->commandName);
  442. }
  443. socketHandler->Init(socketHandler, NULL, NULL);//找到就初始化
  444. while(1){ //只要有新的客户端接入 就创建新线程去对接
  445. c_fd = accept(socketHandler->sfd, (struct sockaddr *)&c_addr, &clen);
  446. pthread_create(&readThread, NULL, read_thread, NULL);//读客户端发来的数据
  447. if (write_flag == 0){
  448. write_flag = 1;
  449. pthread_create(&writeThread, NULL, write_thread, NULL);//往客户端发数据 比如温湿度
  450. }
  451. }
  452. }
  453. void *DHT11_thread(void* datas)
  454. {
  455. struct Devices *DHT11DeviceTmp = NULL;
  456. DHT11DeviceTmp = findDeviceByName("DHT11", pdeviceHead); //在设备工厂找到温湿度模块
  457. DHT11DeviceTmp->deviceInit(DHT11DeviceTmp->pinNum); //温湿度模块初始化
  458. while (1)
  459. {
  460. //pinMode(7, OUTPUT); // set mode to output
  461. // digitalWrite(7, 1); // output a high level
  462. delay(3000);
  463. if (DHT11DeviceTmp->deviceInit(DHT11DeviceTmp->pinNum))
  464. {
  465. // printf("Sensor data read ok!\n");
  466. memset(Message[0], 0, sizeof Message[0]); //清空数组
  467. memset(Message[1], 0, sizeof Message[1]);
  468. sprintf(Message[0], "湿度:%d.%d ", (databuf >> 24) & 0xff, (databuf >> 16) & 0xff);
  469. sprintf(Message[1], "温度:%d.%d ", (databuf >> 8) & 0xff, databuf & 0xff);
  470. //printf("shidu = %d.%d\n", (databuf >> 24) & 0xff, (databuf >> 16) & 0xff);
  471. //printf("wendu = %d.%d\n", (databuf >> 8) & 0xff, databuf & 0xff);
  472. databuf = 0;
  473. }
  474. else
  475. {
  476. printf("Sensor dosent ans!\n");
  477. databuf = 0;
  478. }
  479. }
  480. }
  481. void *remote_thread(void *datas)//433m线程
  482. {
  483. int val0 = 0, val1 = 0, val2 = 0, val3 = 0;
  484. int temp0 = 0, temp1 = 0,temp2 = 0,temp3 = 0;
  485. int count0 = 0, count1 = 0, count2 = 0, count3 = 0;
  486. struct Devices *deviceTmp = NULL;
  487. wiringPiSetup();
  488. pinMode(12, INPUT);
  489. pinMode(13, INPUT);
  490. pinMode(14, INPUT);
  491. pinMode(30, INPUT);
  492. digitalWrite(12, LOW);
  493. digitalWrite(13, LOW);
  494. digitalWrite(14, LOW);
  495. digitalWrite(30, LOW);
  496. while(1)
  497. {
  498. val0 = digitalRead(12);
  499. val1 = digitalRead(13);
  500. val2 = digitalRead(14);
  501. val3 = digitalRead(30);
  502. if (val0 == 1) //电风扇遥控信号
  503. {
  504. if(count0 == 0)
  505. {
  506. delay(500);
  507. if (temp0 == 0) //打开厨房灯
  508. {
  509. printf("433M:open restauranLight\n");
  510. temp0 = 1;
  511. deviceTmp = findDeviceByName("restauranLight", pdeviceHead);
  512. deviceTmp->deviceInit(deviceTmp->pinNum);
  513. deviceTmp->open(deviceTmp->pinNum);
  514. while(val0 == 0);
  515. }
  516. else if (temp0 == 1) //关闭厨房灯
  517. {
  518. printf("433M:close restauranLight\n");
  519. temp0 = 0;
  520. deviceTmp = findDeviceByName("restauranLight", pdeviceHead);
  521. deviceTmp->deviceInit(deviceTmp->pinNum);
  522. deviceTmp->close(deviceTmp->pinNum);
  523. }
  524. count0 = 1;
  525. }
  526. }
  527. else if (val0 == 0)
  528. {
  529. count0 = 0;
  530. }
  531. if (val1 == 1) //泳池灯遥控信号
  532. {
  533. if(count1 == 0)
  534. {
  535. delay(500);
  536. if (temp1 == 0) //打开泳池灯
  537. {
  538. printf("433M:open livingRoomLight\n");
  539. temp1 = 1;
  540. deviceTmp = findDeviceByName("livingRoomLight", pdeviceHead);
  541. deviceTmp->deviceInit(deviceTmp->pinNum);
  542. deviceTmp->open(deviceTmp->pinNum);
  543. while(val1 == 0);
  544. }
  545. else if (temp1 == 1) //关闭泳池灯
  546. {
  547. printf("433M:close livingRoomLight\n");
  548. temp1 = 0;
  549. deviceTmp = findDeviceByName("livingRoomLight", pdeviceHead);
  550. deviceTmp->deviceInit(deviceTmp->pinNum);
  551. deviceTmp->close(deviceTmp->pinNum);
  552. }
  553. count1 = 1;
  554. }
  555. }
  556. else if (val1 == 0)
  557. {
  558. count1 = 0;
  559. }
  560. if (val2 == 1) //卧室灯遥控信号
  561. {
  562. if(count2 == 0)
  563. {
  564. delay(500);
  565. if (temp2 == 0) //打开卧室灯
  566. {
  567. printf("433M:open upstairLight\n");
  568. temp2 = 1;
  569. deviceTmp = findDeviceByName("upstairLight", pdeviceHead);
  570. deviceTmp->deviceInit(deviceTmp->pinNum);
  571. deviceTmp->open(deviceTmp->pinNum);
  572. while(val2 == 0);
  573. }
  574. else if (temp2 == 1) //关闭卧室灯
  575. {
  576. printf("433M:close upstairLight\n");
  577. temp2 = 0;
  578. deviceTmp = findDeviceByName("upstairLight", pdeviceHead);
  579. deviceTmp->deviceInit(deviceTmp->pinNum);
  580. deviceTmp->close(deviceTmp->pinNum);
  581. }
  582. count2 = 1;
  583. }
  584. }
  585. else if (val2 == 0)
  586. {
  587. count2 = 0;
  588. }
  589. if (val3 == 1) //浴室灯遥控信号
  590. {
  591. if(count3 == 0)
  592. {
  593. delay(500);
  594. if (temp3 == 0) //打开浴室灯
  595. {
  596. printf("433M:open bathroomLight\n");
  597. temp3 = 1;
  598. deviceTmp = findDeviceByName("bathroomLight", pdeviceHead);
  599. deviceTmp->deviceInit(deviceTmp->pinNum);
  600. deviceTmp->open(deviceTmp->pinNum);
  601. while(val3 == 0);
  602. }
  603. else if (temp3 == 1) //关闭浴室灯
  604. {
  605. printf("433M:close bathroomLight\n");
  606. temp3 = 0;
  607. deviceTmp = findDeviceByName("bathroomLight", pdeviceHead);
  608. deviceTmp->deviceInit(deviceTmp->pinNum);
  609. deviceTmp->close(deviceTmp->pinNum);
  610. }
  611. count3 = 1;
  612. }
  613. }
  614. else if (val3 == 0)
  615. {
  616. count3 = 0;
  617. }
  618. }
  619. }
  620. void *cameraThread_func(void *data) //起线程的函数有格式要求
  621. {
  622. struct Devices *cameraTemp;
  623. cameraTemp = findDeviceByName("camera", pdeviceHead); //找到摄像头节点
  624. if (cameraTemp == NULL)
  625. {
  626. printf("find camera error\n");
  627. pthread_exit(NULL); //在线程中不用return
  628. }
  629. cameraTemp->justDoOnce(); //调用postUrl函数
  630. }
  631. void *key_thread()
  632. {
  633. // pinMode(door_Lock, OUTPUT); //将门锁端口置为输出
  634. int val;
  635. while (1)
  636. {
  637. val = digitalRead(key);
  638. // printf("key = %d\n", val);
  639. if (val == 0) //防止重复检测
  640. {
  641. delay(500);
  642. val = digitalRead(key);
  643. // printf("key = %d\n", val);
  644. if (val == 1) //按键按下,启动人脸识别线程
  645. {
  646. pthread_create(&cameraThread, NULL, cameraThread_func, NULL);
  647. }
  648. }
  649. }
  650. }
  651. int main()
  652. {
  653. char name[128];
  654. struct Devices *tmp = NULL;
  655. if(-1 == wiringPiSetup()){
  656. return -1;
  657. }
  658. //1.指令工厂初始化
  659. pCommandHead = addvoiceContrlToInputCommandLink(pCommandHead);//语音
  660. pCommandHead = addSocketContrlToInputCommandLink(pCommandHead);//socket
  661. //2.设备控制工厂初始化
  662. pdeviceHead = addBathroomLightToDeviceLink(pdeviceHead);
  663. pdeviceHead = addUpstairLightToDeviceLink(pdeviceHead);
  664. pdeviceHead = addRestauranLightToDeviceLink(pdeviceHead);
  665. pdeviceHead = addLivingRoomLightToDeviceLink(pdeviceHead);
  666. pdeviceHead = addfireToDeviceLink(pdeviceHead);//火灾模块
  667. pdeviceHead = addbuzzerToDeviceLink(pdeviceHead);//火灾蜂鸣器
  668. pdeviceHead = addshakebuzzerToDeviceLink(pdeviceHead);//震动蜂鸣器
  669. pdeviceHead = addfanToDeviceLink(pdeviceHead);
  670. pdeviceHead = addDHT11ToDeviceLink(pdeviceHead);
  671. pdeviceHead = addcameraToDeviceLink(pdeviceHead);//摄像头
  672. pdeviceHead = addShakeToDeviceLink(pdeviceHead);//震动模块
  673. pdeviceHead = addwindowToDeviceLink(pdeviceHead);
  674. //3.线程池建立
  675. //int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg)
  676. //3.1 语音线程
  677. pthread_create(&voiceThread, NULL, voice_thread, NULL);
  678. //3.2 socket线程
  679. pthread_create(&socketThread, NULL, socket_thread, NULL);
  680. //3.3火灾线程
  681. pthread_create(&fireThread, NULL, fire_thread, NULL);
  682. //3.4 摄像头线程 这里把摄像头设为了开机自启
  683. //3.5 温湿度线程
  684. pthread_create(&DHT11Thread, NULL, DHT11_thread, NULL);
  685. //3.6 震动线程
  686. pthread_create(&shakeThread, NULL, shake_thread, NULL);
  687. //3.7 窗户线程
  688. pthread_create(&windowThread, NULL, window_thread, NULL);
  689. //3.8 按钮线程
  690. pthread_create(&keyThread, NULL, key_thread, NULL);
  691. //3.9 433m线程
  692. pthread_create(&remoteThread, NULL, remote_thread, NULL);
  693. pthread_join(voiceThread, NULL);
  694. pthread_join(socketThread, NULL);
  695. pthread_join(fireThread, NULL);
  696. pthread_join(DHT11Thread, NULL);
  697. pthread_join(writeThread, NULL);
  698. pthread_join(shakeThread, NULL);
  699. pthread_join(windowThread, NULL);
  700. pthread_join(keyThread, NULL);
  701. pthread_join(cameraThread, NULL);
  702. pthread_join(remoteThread, NULL);
  703. return 0;
  704. }

contrlDevices.h

  1. #include<stdlib.h>
  2. #include<wiringPi.h>
  3. #include <curl/curl.h>
  4. typedef unsigned int bool;
  5. struct Devices
  6. {
  7. char deviceName[128]; //
  8. int status; //状态 : 开/
  9. int pinNum;
  10. int (*open)(int pinNum);
  11. int (*close)(int pinNum);
  12. int (*deviceInit)(int pinNum);
  13. int (*readStatus)(int pinNum);
  14. int (*changeStatus)(int status);
  15. //摄像头相关的
  16. void (*justDoOnce)();
  17. char* (*getFace)();
  18. char* (*getPicFromOCRBase64)();
  19. size_t (*readData)();
  20. struct Devices *next;
  21. };
  22. struct Devices* addBathroomLightToDeviceLink(struct Devices *phead);
  23. struct Devices* addUpstairLightToDeviceLink(struct Devices *phead);
  24. struct Devices* addRestauranLightToDeviceLink(struct Devices *phead);
  25. struct Devices* addLivingRoomLightToDeviceLink(struct Devices *phead);
  26. struct Devices* addfireToDeviceLink(struct Devices *phead);
  27. struct Devices* addfanToDeviceLink(struct Devices *phead);
  28. struct Devices* addDHT11ToDeviceLink(struct Devices *phead);
  29. struct Devices *addShakeToDeviceLink(struct Devices *phead);
  30. struct Devices* addbuzzerToDeviceLink(struct Devices *phead);//火灾蜂鸣器
  31. struct Devices* addshakebuzzerToDeviceLink(struct Devices *phead);//震动蜂鸣器
  32. struct Devices* addwindowToDeviceLink(struct Devices *phead);
  33. struct Devices *addcameraToDeviceLink(struct Devices *phead);//摄像头结点

InputCommand.h

  1. #include<stdlib.h>
  2. #include<wiringPi.h>
  3. struct InputCommander //
  4. {
  5. char commandName[128];//名字
  6. char deviceName[128];//设备名
  7. char command[32]; //指令
  8. int (*Init)(struct InputCommander *voicer, char *ipAdress, char *port);//操作函数 : 串口号 ip 端口号
  9. int (*getCommand)(struct InputCommander *voicer);//
  10. char log[1024]; //日志
  11. int fd;
  12. char port[12];//端口号
  13. char ipAddress[32];//ip地址
  14. int sfd;
  15. struct InputCommander *next;
  16. };
  17. struct InputCommander* addvoiceContrlToInputCommandLink(struct InputCommander *phead);
  18. struct InputCommander* addSocketContrlToInputCommandLink(struct InputCommander *phead);

bathroomLight.c(四个房间小灯,风扇代码基本相似,这里只展示其中一个)

  1. #include"contrlDevices.h"
  2. #include<stdlib.h>
  3. #include<fcntl.h>
  4. int bathroomLightOpen(int pinNum)
  5. {
  6. digitalWrite(pinNum,LOW);
  7. }
  8. int bathroomLightClose(int pinNum)
  9. {
  10. digitalWrite(pinNum,HIGH);
  11. }
  12. int bathroomLightCloseInit(int pinNum)
  13. {
  14. pinMode(pinNum,OUTPUT);
  15. digitalWrite(pinNum,HIGH);
  16. }
  17. int bathroomLightCloseStatus(int status)
  18. {
  19. }
  20. struct Devices bathroomLight = { //这里是在定义的同时赋值
  21. .deviceName = "bathroomLight",
  22. .pinNum = 22,
  23. .open = bathroomLightOpen,
  24. .close = bathroomLightClose,
  25. .deviceInit = bathroomLightCloseInit,
  26. .changeStatus = bathroomLightCloseStatus
  27. };
  28. struct Devices* addBathroomLightToDeviceLink(struct Devices *phead)
  29. {
  30. if(phead == NULL){
  31. return &bathroomLight;
  32. }
  33. else{
  34. bathroomLight.next = phead;
  35. phead = &bathroomLight;
  36. return phead;
  37. }
  38. }

 window.c

  1. #include"contrlDevices.h"
  2. #include<stdlib.h>
  3. #include<fcntl.h>
  4. int windowOpen(int pinNum)
  5. {
  6. digitalWrite(2,LOW);
  7. digitalWrite(3,HIGH);
  8. }
  9. int windowClose(int pinNum)
  10. {
  11. digitalWrite(2,HIGH);
  12. digitalWrite(3,LOW);
  13. }
  14. int windowCloseInit(int pinNum)//用的L9110电机实现正反转,所以要初始化两个引脚
  15. {
  16. pinMode(2,OUTPUT);
  17. pinMode(3,OUTPUT);
  18. digitalWrite(2,LOW);
  19. digitalWrite(3,LOW);//23都配置为低电平 默认不转
  20. pinMode(4,OUTPUT);
  21. digitalWrite(4,HIGH);
  22. pinMode(0,OUTPUT); //门锁初始化
  23. digitalWrite(0,HIGH);
  24. }
  25. int windowCloseStatus(int status)
  26. {
  27. }
  28. struct Devices window = {
  29. .deviceName = "window",
  30. .pinNum = 2,
  31. .open = windowOpen,
  32. .close = windowClose,
  33. .deviceInit = windowCloseInit,
  34. .changeStatus = windowCloseStatus
  35. };
  36. struct Devices* addwindowToDeviceLink(struct Devices *phead)
  37. {
  38. if(phead == NULL){
  39. return &window;
  40. }
  41. else{
  42. window.next = phead;
  43. phead = &window;
  44. return phead;
  45. }
  46. }

DHT11.c

  1. #include"contrlDevices.h"
  2. #include<stdlib.h>
  3. #include<fcntl.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <stdint.h>
  8. typedef unsigned char uint8;
  9. typedef unsigned int uint16;
  10. typedef unsigned long uint32;
  11. #define HIGH_TIME 32
  12. int pinNumber = 7;
  13. uint32 databuf;
  14. int DHT11CloseInit(int pinNum)
  15. {
  16. uint8 crc;
  17. uint8 i;
  18. pinMode(pinNumber, OUTPUT); // set mode to output
  19. digitalWrite(pinNumber, 0); // output a high level
  20. delay(25);
  21. digitalWrite(pinNumber, 1); // output a low level
  22. pinMode(pinNumber, INPUT); // set mode to input
  23. pullUpDnControl(pinNumber, PUD_UP);
  24. delayMicroseconds(27);
  25. if (digitalRead(pinNumber) == 0) //SENSOR ANS
  26. {
  27. while (!digitalRead(pinNumber))
  28. ; //wait to high
  29. for (i = 0; i < 32; i++)
  30. {
  31. while (digitalRead(pinNumber))
  32. ; //data clock start
  33. while (!digitalRead(pinNumber))
  34. ; //data start
  35. delayMicroseconds(HIGH_TIME);
  36. databuf *= 2;
  37. if (digitalRead(pinNumber) == 1) //1
  38. {
  39. databuf++;
  40. }
  41. }
  42. for (i = 0; i < 8; i++)
  43. {
  44. while (digitalRead(pinNumber))
  45. ; //data clock start
  46. while (!digitalRead(pinNumber))
  47. ; //data start
  48. delayMicroseconds(HIGH_TIME);
  49. crc *= 2;
  50. if (digitalRead(pinNumber) == 1) //1
  51. {
  52. crc++;
  53. }
  54. }
  55. return 1;
  56. }
  57. else
  58. {
  59. return 0;
  60. }
  61. }
  62. int DHT11Open(int pinNum)
  63. {
  64. }
  65. int DHT11Close(int pinNum)
  66. {
  67. }
  68. int DHT11CloseStatus(int status)
  69. {
  70. }
  71. struct Devices DHT11 = { //这里是在定义的同时赋值
  72. .deviceName = "DHT11",
  73. .pinNum = 7,
  74. .open = DHT11Open,
  75. .close = DHT11Close,
  76. .deviceInit = DHT11CloseInit,
  77. .changeStatus = DHT11CloseStatus
  78. };
  79. struct Devices* addDHT11ToDeviceLink(struct Devices *phead)
  80. {
  81. if(phead == NULL){
  82. return &DHT11;
  83. }
  84. else{
  85. DHT11.next = phead;
  86. phead = &DHT11;
  87. return phead;
  88. }
  89. }

voiceContrl.c

  1. #include<unistd.h>
  2. #include<wiringPi.h>
  3. #include<stdlib.h>
  4. #include<stdio.h>
  5. #include<string.h>
  6. #include<wiringSerial.h>
  7. #include"InputCommand.h"
  8. int voiceGetCommand(struct InputCommander *voicer)
  9. {
  10. int nread = 0;
  11. memset(voicer->command, '\0', sizeof(voicer->command));
  12. nread = read(voicer->fd, voicer->command, sizeof(voicer->command));
  13. if(nread == 0){
  14. printf("voice no datas\n");
  15. }else{
  16. return nread;
  17. }
  18. }
  19. int voiceInit(struct InputCommander *voicer, char *ipAdress, char *port)
  20. {
  21. int fd;
  22. if((fd = serialOpen("/dev/ttyAMA0",9600))==-1)//初始化串口,波特率9600
  23. {
  24. printf("voice init error\n");
  25. exit(-1);
  26. }
  27. voicer->fd = fd;
  28. return fd;
  29. }
  30. struct InputCommander voiceContrl = {
  31. .commandName = "voice",
  32. .deviceName = "/dev/ttyAMA0",//设备名
  33. .command = {'\0'}, //指令
  34. .Init = voiceInit,//串口初始化
  35. .getCommand = voiceGetCommand,
  36. .log = {'\0'},
  37. .next = NULL
  38. };
  39. struct InputCommander* addvoiceContrlToInputCommandLink(struct InputCommander *phead)
  40. {
  41. if(phead == NULL){
  42. return &voiceContrl;
  43. }
  44. else{
  45. voiceContrl.next = phead;
  46. phead = &voiceContrl;
  47. return phead;
  48. }
  49. }

socketContrl.c

  1. #include<unistd.h>
  2. #include<wiringPi.h>
  3. #include<stdlib.h>
  4. #include<stdio.h>
  5. #include<wiringSerial.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <arpa/inet.h>
  9. #include <netinet/in.h>
  10. #include <string.h>
  11. #include"InputCommand.h"
  12. int socketGetCommand(struct InputCommander *socketMes)
  13. {
  14. int c_fd;
  15. int n_read = 0;
  16. struct sockaddr_in c_addr;
  17. memset(&c_addr, 0, sizeof(struct sockaddr_in));
  18. int clen = sizeof(struct sockaddr_in);
  19. //4. accept
  20. c_fd = accept(socketMes->sfd, (struct sockaddr *)&c_addr, &clen);
  21. n_read = read(c_fd, socketMes->command, sizeof(socketMes->command));
  22. if(n_read == -1){
  23. perror("read");
  24. }else if(n_read > 0){
  25. printf("\nget: %d\n",n_read);
  26. }else{
  27. printf("client quit\n");
  28. }
  29. return n_read;
  30. }
  31. int socketInit(struct InputCommander *socketMes, char *ipAdress, char *port)
  32. {
  33. int s_fd;
  34. struct sockaddr_in s_addr;
  35. memset(&s_addr, 0, sizeof(struct sockaddr_in));
  36. //1.socket
  37. s_fd = socket(AF_INET, SOCK_STREAM, 0);
  38. if (s_fd == -1)
  39. {
  40. perror("socket");
  41. exit(-1);
  42. }
  43. s_addr.sin_family = AF_INET;
  44. s_addr.sin_port = htons(atoi(socketMes->port)); //atoi()把ascii转成整形数
  45. //htons()返回网络字节序的值(大段字节序)
  46. inet_aton(socketMes->ipAddress, &(s_addr.sin_addr)); //aton()把字符串形式的IP转换成网络能识别的格式
  47. //解决服务器程序结束后端口被占用的情况
  48. int opt = 1;
  49. setsockopt(s_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
  50. //2.bind
  51. bind(s_fd, (struct sockaddr *)&s_addr, sizeof(struct sockaddr_in));
  52. //3.listen
  53. listen(s_fd, 10);
  54. printf("socket server listening...\n");
  55. socketMes->sfd = s_fd;
  56. return s_fd;
  57. }
  58. struct InputCommander socketContrl = {
  59. .commandName = "socketServer",
  60. .command = {'\0'}, //指令
  61. .port = "8083",
  62. .ipAddress = "192.168.101.223",
  63. .Init = socketInit,//初始化
  64. .getCommand = socketGetCommand,
  65. .log = {'\0'},
  66. .next = NULL
  67. };
  68. struct InputCommander* addSocketContrlToInputCommandLink(struct InputCommander *phead)
  69. {
  70. if(phead == NULL){
  71. return &socketContrl;
  72. }
  73. else{
  74. socketContrl.next = phead;
  75. phead = &socketContrl;
  76. return phead;
  77. }
  78. }

Camera.c

  1. #include "contrlDevices.h"
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <curl/curl.h>
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <fcntl.h>
  10. #include <sys/types.h>
  11. #include <unistd.h>
  12. #define door_lock 0 //门锁
  13. void postUrl();
  14. size_t readData1(void *ptr, size_t size, size_t nmemb, void *stream);
  15. char* getFace1();
  16. char* getPicFromOCRBase641(char *Filepath);
  17. struct Devices* addcameraToDeviceLink(struct Devices *phead);
  18. int cameraInit(int pinNum);
  19. char ocrRetBuf[1024] = {'\0'};
  20. size_t readData1(void *ptr, size_t size, size_t nmemb, void *stream)
  21. //回调函数,把从后台的数据拷贝给ocrRetBuf
  22. {
  23. strncpy(ocrRetBuf,ptr,1024);//ocrRetBuf接收从OCR后台返回的数据,并打印
  24. printf("%s\n",ocrRetBuf);
  25. }
  26. char* getFace1()
  27. {
  28. printf("pai zhao zhong\n");
  29. //system("raspistill -q 5 -t 1 -o image.jpg");
  30. system("wget http://192.168.101.223:8080/?action=snapshot -O ./image.jpg ");
  31. while(access("./image.jpg",F_OK) != 0); //判断是否拍照完毕
  32. printf("paizhao wanbi\n");
  33. char* base64BufFaceRec = getPicFromOCRBase641("./image.jpg");
  34. //system("rm image.jpg");
  35. return base64BufFaceRec; //返回刚才拍照的base64
  36. }
  37. char *getPicFromOCRBase641(char *Filepath)
  38. {
  39. int fd;
  40. int filelen;
  41. char cmd[128]={'\0'};
  42. sprintf(cmd,"base64 %s > tmpFile",Filepath);
  43. system(cmd);
  44. fd=open("./tmpFile",O_RDWR);
  45. filelen=lseek(fd,0,SEEK_END);//计算文件大小
  46. lseek(fd,0,SEEK_SET); //移动光标到头
  47. char *bufpic=(char *)malloc(filelen+2);
  48. memset(bufpic,'\0',filelen+2);
  49. read(fd,bufpic,filelen+128); //把内容读到bufpic内
  50. system("rm -rf tmpFile");//删除这个临时文件
  51. close(fd);
  52. return bufpic; //返回bufpic这个地址
  53. }
  54. void postUrl()
  55. {
  56. CURL *curl;
  57. CURLcode res;
  58. char* key = "5xxxsWxxxGfP2pmxxxchSJ"; //翔云平台购买人脸识别后的key
  59. char* secret = "396xxxd33xxx487exxxf139axxx6d789"; //翔云平台购买人脸识别后的secret
  60. int typeId = 21;
  61. char* format = "xml";
  62. char* base64BufPic1 = getFace1();
  63. char* base64BufPic2 = getPicFromOCRBase641("lit1.jpg");
  64. int len = strlen(key)+strlen(secret)+strlen(base64BufPic1)+strlen(base64BufPic2)+128;
  65. char* postString = (char* )malloc(len);
  66. memset(postString,'\0',len);
  67. sprintf(postString,"img1=%s&img2=%s&key=%s&secret=%s&typeId=%d&format=%s",base64BufPic1,base64BufPic2,key,secret,typeId,format);//根据平台的传参格式编写
  68. curl = curl_easy_init();
  69. if(curl){
  70. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postString); //指定post内容,传入参数
  71. curl_easy_setopt(curl, CURLOPT_URL, "https://netocr.com/api/faceliu.do");// 指定url
  72. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,readData1); //回调函数readDate读取返回值
  73. res = curl_easy_perform(curl);
  74. printf("OK:%d\n",res);
  75. if(strstr(ocrRetBuf,"是") != NULL){ //判断翔云后台返回的字符串中有没有“是”
  76. printf("the same person\n");
  77. digitalWrite(door_lock, LOW); //打开门锁
  78. delay(3000); //等待3s
  79. digitalWrite(door_lock, HIGH); //关闭门锁
  80. }
  81. else{
  82. printf("different person\n");
  83. }
  84. curl_easy_cleanup(curl);
  85. }
  86. }
  87. int cameraInit(int pinNum)//该函数无用
  88. {
  89. pinMode(0,OUTPUT);
  90. digitalWrite(0,HIGH);
  91. }
  92. struct Devices camera = {
  93. .deviceName = "camera",
  94. .deviceInit = cameraInit,
  95. .pinNum = 0, //此引脚号无用
  96. .justDoOnce = postUrl,
  97. .getFace = getFace1,
  98. .getPicFromOCRBase64 = getPicFromOCRBase641,
  99. .readData = readData1
  100. };
  101. struct Devices* addcameraToDeviceLink(struct Devices *phead)
  102. {
  103. if(phead == NULL){
  104. return &camera;
  105. }
  106. else{
  107. camera.next = phead;
  108. phead = &camera;
  109. }
  110. }

其余代码和上面展示的有些很类似,就不放出了。

四.项目总结

        1.做完后感觉不是很难,主要是要明白这个项目代码的设计结构和里面的一些逻辑。

        2.人脸识别功能的整合是这个项目相对复杂的地方,这里我也参考了其他博客。

        3.前面学到的编写树莓派驱动代码,各种库的使用是重点,以后也会复习。

        4.火焰和震动传感器可以同时只使用一个蜂鸣器,风扇有时会出现一些小bug,代码还有优化的地方。

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
  

闽ICP备14008679号