当前位置:   article > 正文

linux项目—基于树莓派的智能家居系统_智能家居全屋智能系统linux困难

智能家居全屋智能系统linux困难

1、项目简介

        项目采用工厂设计模式。所有控制以及外设的设备都做成一个个对象(java思想),分别将命令控制的连成一个控制链表,外设设备做成一个外设设备的链表,这样做是为了方便以后功能模块的添加。其中为了能分别做好控制,我们采用多线程来实现。

 工厂模式详解:

 最通俗易懂的讲解工厂模式_晴夏。的博客-CSDN博客https://blog.csdn.net/weixin_43757333/article/details/126294625

实现功能:

        树莓派通过摄像头将图像上传到翔云平台进行人脸识别开锁。使用串口连接语音模块,实现语音对浴室灯、卧室灯、餐厅灯、锁、风扇等的控制。通过socket连接全志,将全志作为客户端,通过接收全志发来的指令控制家居,同时将温度、湿度、报警器、红外等检测状态通过socket发送给全志,实时掌握家居情况。

智能家居功能拆分图:

2、代码示例

2.1树莓派代码:

contrlDevices.h(外设头文件)

  1. #include <wiringPi.h>
  2. #include <stdio.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 (*changStatus)(int status); //改变数据函数指针
  15. //摄像头相关的
  16. void (*justDoOnce)();
  17. char *(*getFace)();
  18. char *(*getPicFromOCRBase64)();
  19. size_t (*readData)();
  20. CURL *curl;
  21. char *key;
  22. char *secret;
  23. int typeId;
  24. char *format;
  25. bool (*cameraInit)(struct Devices *camera);
  26. int yesNum;
  27. int noNum;
  28. struct Devices *next;
  29. };

inputCommand.h(控制头文件)

  1. #include <wiringPi.h>
  2. #include <stdio.h>
  3. struct InputCommander
  4. {
  5. char commandName[128]; // socket名
  6. char deviceName[128]; //串口名
  7. char comand[32]; //控制命令
  8. int (*Init)(struct InputCommander *voicer, char *ipAdress, char *port); // socket初始化
  9. int (*getCommand)(struct InputCommander *voicer); //读取数据
  10. char log[1024];
  11. int fd;
  12. char port[12]; //端口号
  13. char ipAdress[32]; // IP地址
  14. int sfd;
  15. int cfd;
  16. struct InputCommander *next;
  17. };

start_web_video.sh(监控脚本)

  1. cd ../mjpg-streamer/mjpg-streamer-experimental/
  2. ./mjpg_streamer -i "./input_raspicam.so" -o "./output_http.so -w ./www"

mainPro(主函数)

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <pthread.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <arpa/inet.h>
  9. #include <netinet/in.h>
  10. #include <arpa/inet.h>
  11. #include <unistd.h>
  12. #include <sys/stat.h>
  13. #include <fcntl.h>
  14. #include "contrlDevices.h"
  15. #include "inputCommand.h"
  16. //控制添加到控制链表的函数声明
  17. struct InputCommander *addVoiceContrlToInputCommanderLink(struct InputCommander *phead);
  18. struct InputCommander *addsocketContrlToInputCommanderLink(struct InputCommander *phead);
  19. //设备加到设备链表函数的声明
  20. struct Devices *addBathroomLightToDeviceLink(struct Devices *phead);
  21. struct Devices *addupStairLightToDeviceLink(struct Devices *phead);
  22. struct Devices *addlivingroomLightToDeviceLink(struct Devices *phead);
  23. struct Devices *addrestaurantLightToDeviceLink(struct Devices *phead);
  24. struct Devices *addDoorLockToDeviceLink(struct Devices *phead);
  25. struct Devices *addFanToDeviceLink(struct Devices *phead);
  26. struct Devices *addFireToDeviceLink(struct Devices *phead);
  27. struct Devices *addInfraredToDeviceLink(struct Devices *phead);
  28. struct Devices *addBuzzerToDeviceLink(struct Devices *phead);
  29. struct Devices *addcameraToDeviceLink(struct Devices *phead);
  30. struct Devices *addVibrateToDeviceLink(struct Devices *phead);
  31. #define HIGH_TIME 32
  32. #define key 6 //拍照按键
  33. pthread_t voiceThread; //语音识别线程
  34. pthread_t socketThread; //服务器线程
  35. pthread_t writeThread; //通知线程:向客服端发送消息
  36. pthread_t fireThread; //火灾检测线程
  37. pthread_t vibrateThread; //震动检测线程
  38. pthread_t infraredThread; //人体红外检测线程
  39. pthread_t remoteThread; //遥控线程
  40. pthread_t keyThread; //按键检测线程
  41. pthread_t cameraThread; //人脸识别线程
  42. pthread_t monitoringThread; //视频监控线程
  43. pthread_t clientWemosThread; //客服端线程
  44. pthread_t dht11Thread; //温湿度线程
  45. struct InputCommander *pCommandHead = NULL;
  46. struct Devices *pdeviceHead = NULL;
  47. struct InputCommander *socketHandler = NULL;
  48. pthread_mutex_t mutex; //定义互斥量(锁)
  49. unsigned long databuf;
  50. int c_fd; //注意:涉及到多线程不要轻易的去传参
  51. int beep = 0; //蜂鸣器标志位 0:未被使用 1:被火灾传感器使用 2:被震动传感器使用
  52. //摄像头相关,改变返回值命名,因为C语言中没有这样的返回值
  53. #define true 1
  54. #define false 0
  55. typedef unsigned int bool;
  56. char buf[1024] = {'\0'};
  57. /* [0]:温度 [1]:湿度 [2]:火灾监控 [3]:震动监控 [4]:人体红外监控*/
  58. char Message[5][100] = {"温度未启用!", "湿度未启用!", "火灾未启用!", "震动未启用!", "红外未启用!"};
  59. int write_flag = 0; //标记位,标记通知线程是否打开 1:打开 0:关闭
  60. int infrared_flag = 0; //红外检测标志位 1:打开 0:关闭
  61. struct Devices *findDeviceByName(char *name, struct Devices *phead) //查询设备
  62. {
  63. if (phead == NULL)
  64. {
  65. return NULL;
  66. }
  67. while (phead != NULL)
  68. {
  69. // printf("phead:%s name:%s\n", phead->deviceName, name);
  70. if (strstr(phead->deviceName, name) != NULL)
  71. {
  72. return phead;
  73. }
  74. phead = phead->next;
  75. }
  76. return NULL;
  77. }
  78. struct InputCommander *findCommandByName(char *name, struct InputCommander *phead) //查询控制
  79. {
  80. if (phead == NULL)
  81. {
  82. return NULL;
  83. }
  84. while (phead != NULL)
  85. {
  86. if (strcmp(phead->commandName, name) == 0)
  87. {
  88. return phead;
  89. }
  90. phead = phead->next;
  91. }
  92. return NULL;
  93. }
  94. void *cameraThread_func(void *data) //起线程的函数有格式要求
  95. {
  96. struct Devices *cameraTemp;
  97. cameraTemp = findDeviceByName("camera", pdeviceHead);
  98. if (cameraTemp == NULL)
  99. {
  100. printf("find camera error\n");
  101. pthread_exit(NULL);
  102. }
  103. cameraTemp->justDoOnce(); //调用postUrl函数
  104. }
  105. unsigned char readSensorData(void) //温湿度初始化
  106. {
  107. char crc;
  108. char i;
  109. pinMode(6, OUTPUT); // 将模式设置为输出
  110. digitalWrite(6, LOW); // 输出高电平
  111. delay(25);
  112. digitalWrite(6, HIGH); // 输出低电平
  113. pinMode(6, INPUT); // 将模式设置为输入
  114. pullUpDnControl(6, PUD_UP);
  115. delayMicroseconds(27);
  116. if (digitalRead(6) == 0)
  117. {
  118. while (!digitalRead(6));
  119. for (i = 0; i < 32; i++)
  120. {
  121. while (digitalRead(6)); // 数据时钟启动
  122. while (!digitalRead(6)); //数据开始
  123. delayMicroseconds(HIGH_TIME);
  124. databuf *= 2;
  125. if (digitalRead(6) == 1) // 1
  126. {
  127. databuf++;
  128. }
  129. }
  130. for (i = 0; i < 8; i++)
  131. {
  132. while (digitalRead(6)); // 数据时钟启动
  133. while (!digitalRead(6)); // 数据开始
  134. delayMicroseconds(HIGH_TIME);
  135. crc *= 2;
  136. if (digitalRead(6) == 1) // 1
  137. {
  138. crc++;
  139. }
  140. }
  141. return 1;
  142. }
  143. else
  144. {
  145. return 0;
  146. }
  147. }
  148. void *dht11_thread(void *datas) //温湿度线程
  149. {
  150. int W = 0, w = 0;
  151. int S = 0, s = 0;
  152. // int count = 0;
  153. int temp = 0;
  154. if (-1 == wiringPiSetup())
  155. {
  156. printf("获取wiringpi库失败!");
  157. }
  158. printf("温湿度线程开启\n");
  159. pinMode(6, OUTPUT);
  160. digitalWrite(6, HIGH);
  161. printf("温湿度开始读取-------\n");
  162. while (1)
  163. {
  164. pinMode(6, OUTPUT);
  165. digitalWrite(6, HIGH);
  166. delay(3000);
  167. if (readSensorData())
  168. {
  169. W = (databuf >> 8) & 0xff;
  170. w = databuf & 0xff;
  171. S = (databuf >> 24) & 0xff;
  172. s = (databuf >> 16) & 0xff;
  173. printf("传感器数据读取正常!\n");
  174. if (temp == 0)
  175. {
  176. w++;
  177. s++;
  178. temp++;
  179. }
  180. else if (temp == 1)
  181. {
  182. if (w >= 1)
  183. {
  184. w--;
  185. }
  186. if (s >= 1)
  187. {
  188. w--;
  189. }
  190. temp--;
  191. }
  192. if ((W >= 15) && (W <= 35) && (S <= 95))
  193. {
  194. memset(Message[0], 0, sizeof Message[0]); //清空数组
  195. memset(Message[1], 0, sizeof Message[1]);
  196. sprintf(Message[0], "%d.%d C", W, w); //温度
  197. sprintf(Message[1], "%d.%d", S, s); //湿度
  198. }
  199. databuf = 0;
  200. }
  201. else
  202. {
  203. printf("温湿度检测失败!\n");
  204. databuf = 0;
  205. }
  206. }
  207. }
  208. void *fire_thread(void *datas) //火灾线程
  209. {
  210. char msg[100];
  211. int status;
  212. struct Devices *fireDeviceTmp = NULL;
  213. struct Devices *buzzerDeviceTmp = NULL;
  214. fireDeviceTmp = findDeviceByName("fire", pdeviceHead); //在设备工厂找到火灾模块
  215. buzzerDeviceTmp = findDeviceByName("buzzser", pdeviceHead);
  216. fireDeviceTmp->deviceInit(fireDeviceTmp->pinNum); //火灾模块初始化
  217. buzzerDeviceTmp->deviceInit(buzzerDeviceTmp->pinNum);
  218. printf("火灾线程初始化成功\n");
  219. while (1)
  220. {
  221. delay(10);
  222. status = fireDeviceTmp->readStatus(fireDeviceTmp->pinNum); //读取火灾模块实时数据
  223. if (status == 0)
  224. {
  225. beep = 1; //火灾传感器使用蜂鸣器
  226. buzzerDeviceTmp->open(buzzerDeviceTmp->pinNum); //蜂鸣器报警
  227. memset(Message[2], 0, sizeof Message[2]); //清空数组
  228. sprintf(Message[2], "警报:发生火灾!"); //更新火灾信息
  229. delay(200); //蜂鸣器报警延时
  230. }
  231. else if ((beep != 2) && (beep != 3)) //未被震动传感器和人体红外传感器使用
  232. {
  233. buzzerDeviceTmp->close(buzzerDeviceTmp->pinNum); //关闭蜂鸣器
  234. memset(Message[2], 0, sizeof Message[2]); //清空数组
  235. sprintf(Message[2], "正常"); //更新火灾信息
  236. beep = 0; //蜂鸣器未被使用
  237. }
  238. }
  239. }
  240. void *vibrate_thread(void *datas) //震动线程
  241. {
  242. char msg[100];
  243. int status;
  244. struct Devices *vibrateDeviceTmp = NULL;
  245. struct Devices *buzzerDeviceTmp = NULL;
  246. vibrateDeviceTmp = findDeviceByName("vibrate", pdeviceHead); //在设备工厂找到火灾模块
  247. buzzerDeviceTmp = findDeviceByName("buzzser", pdeviceHead);
  248. vibrateDeviceTmp->deviceInit(vibrateDeviceTmp->pinNum); //震动模块初始化
  249. buzzerDeviceTmp->deviceInit(buzzerDeviceTmp->pinNum);
  250. printf("震动线程初始化成功\n");
  251. while (1)
  252. {
  253. delay(5);
  254. status = vibrateDeviceTmp->readStatus(vibrateDeviceTmp->pinNum); //读取震动模块实时数据
  255. if (status == 0)
  256. {
  257. beep = 2; //震动传感器使用蜂鸣器
  258. buzzerDeviceTmp->open(buzzerDeviceTmp->pinNum);
  259. memset(Message[3], 0, sizeof Message[3]); //清空数组
  260. sprintf(Message[3], "警报:发生震动!");
  261. delay(300);
  262. }
  263. else if ((beep != 1) && (beep != 3)) //蜂鸣器未被火焰传感器和人体红外传感器使用
  264. {
  265. memset(Message[3], 0, sizeof Message[3]); //清空数组
  266. sprintf(Message[3], "正常");
  267. buzzerDeviceTmp->close(buzzerDeviceTmp->pinNum);
  268. beep = 0; //蜂鸣器未被使用
  269. }
  270. }
  271. }
  272. void *infrared_thread(void *datas) // 人体红外检测线程
  273. {
  274. char msg[100];
  275. int status;
  276. struct Devices *infraredDeviceTmp = NULL;
  277. struct Devices *buzzerDeviceTmp = NULL;
  278. infraredDeviceTmp = findDeviceByName("infrared", pdeviceHead); //在设备工厂找到火灾模块
  279. buzzerDeviceTmp = findDeviceByName("buzzser", pdeviceHead);
  280. infraredDeviceTmp->deviceInit(infraredDeviceTmp->pinNum); //红外模块初始化
  281. buzzerDeviceTmp->deviceInit(buzzerDeviceTmp->pinNum);
  282. printf("人体红外检测初始化\n");
  283. while (1)
  284. {
  285. delay(10);
  286. status = infraredDeviceTmp->readStatus(infraredDeviceTmp->pinNum); //读取人体感应模块实时数据
  287. if (status == 1)
  288. {
  289. beep = 3; //人体红外使用蜂鸣器
  290. memset(Message[4], 0, sizeof Message[4]); //清空数组
  291. sprintf(Message[4], "警报:有人进入!");
  292. buzzerDeviceTmp->open(buzzerDeviceTmp->pinNum);
  293. delay(200);
  294. buzzerDeviceTmp->close(buzzerDeviceTmp->pinNum);
  295. delay(100);
  296. }
  297. else if ((beep != 1) && (beep != 2)) //未被火焰传感器和震动传感器使用
  298. {
  299. memset(Message[4], 0, sizeof Message[4]); //清空数组
  300. sprintf(Message[4], "正常");
  301. buzzerDeviceTmp->close(buzzerDeviceTmp->pinNum);
  302. beep = 0; //蜂鸣器未被使用
  303. }
  304. }
  305. }
  306. void *monitoring_thread(void *datas) //视频监控线程
  307. {
  308. system("./start_web_video.sh"); //执行脚本,打开视频监控
  309. pthread_exit(NULL); //退出线程
  310. }
  311. void *voice_thread(void *arg) //语音线程
  312. {
  313. int i = 0;
  314. int nread;
  315. struct InputCommander *voiceHandler = NULL;
  316. struct Devices *deviceTmp = NULL;
  317. voiceHandler = findCommandByName("voice", pCommandHead); //在控制工厂找到语音模块
  318. if (voiceHandler == NULL)
  319. {
  320. printf("查找语音处理程序错误\n");
  321. pthread_exit(NULL);
  322. }
  323. else
  324. {
  325. if (voiceHandler->Init(voiceHandler, NULL, NULL) < 0)
  326. { //语音模块初始化
  327. printf("语音初始化错误\n");
  328. pthread_exit(NULL); //退出线程
  329. }
  330. else
  331. {
  332. printf("%s 初始化成功\n", voiceHandler->commandName);
  333. } //语音初始化完成
  334. pthread_mutex_lock(&mutex); //加锁
  335. //语音读取一级指令的时候,为了避免其它线程对于 紧接着读取二级指令的干扰
  336. while (1)
  337. {
  338. memset(voiceHandler->comand, '\0', sizeof(voiceHandler->comand));
  339. nread = voiceHandler->getCommand(voiceHandler); //读取来自语音模块的串口数据
  340. printf("get voice command:%s\n", voiceHandler->comand);
  341. if (nread == 0)
  342. {
  343. printf("没有来自语音的数据,请再说一遍\n");
  344. }
  345. else if (strstr(voiceHandler->comand, "kwsd") != NULL) //打开卧室灯命令
  346. {
  347. printf("打开卧室灯光\n");
  348. deviceTmp = findDeviceByName("livingroomLight", pdeviceHead); //查找卧室灯设备
  349. deviceTmp->deviceInit(deviceTmp->pinNum); //卧室灯设备初始化
  350. deviceTmp->open(deviceTmp->pinNum); //打开卧室灯
  351. }
  352. else if (strstr(voiceHandler->comand, "gwsd") != NULL)
  353. {
  354. printf("关闭卧室灯光\n");
  355. deviceTmp = findDeviceByName("livingroomLight", pdeviceHead);
  356. deviceTmp->deviceInit(deviceTmp->pinNum);
  357. deviceTmp->close(deviceTmp->pinNum);
  358. }
  359. else if (strstr(voiceHandler->comand, "kysd") != NULL)
  360. {
  361. printf("打开浴室灯光\n");
  362. deviceTmp = findDeviceByName("bathroomLight", pdeviceHead);
  363. deviceTmp->deviceInit(deviceTmp->pinNum);
  364. deviceTmp->open(deviceTmp->pinNum);
  365. }
  366. else if (strstr(voiceHandler->comand, "gysd") != NULL)
  367. {
  368. printf("关闭浴室灯光\n");
  369. deviceTmp = findDeviceByName("bathroomLight", pdeviceHead);
  370. deviceTmp->deviceInit(deviceTmp->pinNum);
  371. deviceTmp->close(deviceTmp->pinNum);
  372. }
  373. else if (strstr(voiceHandler->comand, "kktd") != NULL)
  374. {
  375. printf("打开客厅灯光\n");
  376. deviceTmp = findDeviceByName("upstairLight", pdeviceHead);
  377. deviceTmp->deviceInit(deviceTmp->pinNum);
  378. deviceTmp->open(deviceTmp->pinNum);
  379. }
  380. else if (strstr(voiceHandler->comand, "gktd") != NULL)
  381. {
  382. printf("关闭客厅灯光\n");
  383. deviceTmp = findDeviceByName("upstairLight", pdeviceHead);
  384. deviceTmp->deviceInit(deviceTmp->pinNum);
  385. deviceTmp->close(deviceTmp->pinNum);
  386. }
  387. else if (strstr(voiceHandler->comand, "kctd") != NULL)
  388. {
  389. printf("打开餐厅灯光\n");
  390. deviceTmp = findDeviceByName("restaurantLight", pdeviceHead);
  391. deviceTmp->deviceInit(deviceTmp->pinNum);
  392. deviceTmp->open(deviceTmp->pinNum);
  393. }
  394. else if (strstr(voiceHandler->comand, "gctd") != NULL)
  395. {
  396. printf("关闭餐厅灯光\n");
  397. deviceTmp = findDeviceByName("restaurantLight", pdeviceHead);
  398. deviceTmp->deviceInit(deviceTmp->pinNum);
  399. deviceTmp->close(deviceTmp->pinNum);
  400. }
  401. else if (strstr(voiceHandler->comand, "kycd") != NULL) //开泳灯指令
  402. {
  403. printf("打开泳池灯光\n");
  404. deviceTmp = findDeviceByName("yong", pdeviceHead);
  405. deviceTmp->deviceInit(deviceTmp->pinNum);
  406. deviceTmp->open(deviceTmp->pinNum);
  407. }
  408. else if (strstr(voiceHandler->comand, "gqbd") != NULL) //关闭全部灯指令
  409. {
  410. printf("关闭所有灯光\n");
  411. deviceTmp = findDeviceByName("chu", pdeviceHead);
  412. deviceTmp->deviceInit(deviceTmp->pinNum);
  413. deviceTmp->close(deviceTmp->pinNum); //关闭餐厅灯
  414. deviceTmp = findDeviceByName("bathroomLight", pdeviceHead);
  415. deviceTmp->deviceInit(deviceTmp->pinNum);
  416. deviceTmp->close(deviceTmp->pinNum); //关闭浴室灯
  417. deviceTmp = findDeviceByName("upstairLight", pdeviceHead);
  418. deviceTmp->deviceInit(deviceTmp->pinNum);
  419. deviceTmp->close(deviceTmp->pinNum); //关闭客厅灯
  420. deviceTmp = findDeviceByName("livingroomLight", pdeviceHead);
  421. deviceTmp->deviceInit(deviceTmp->pinNum);
  422. deviceTmp->close(deviceTmp->pinNum); //关闭卧室灯
  423. }
  424. else if (strstr(voiceHandler->comand, "kqbd") != NULL)
  425. {
  426. printf("打开所有灯光\n");
  427. deviceTmp = findDeviceByName("restaurantLight", pdeviceHead);
  428. deviceTmp->deviceInit(deviceTmp->pinNum);
  429. deviceTmp->open(deviceTmp->pinNum);
  430. deviceTmp = findDeviceByName("bathroomLight", pdeviceHead);
  431. deviceTmp->deviceInit(deviceTmp->pinNum);
  432. deviceTmp->open(deviceTmp->pinNum);
  433. deviceTmp = findDeviceByName("upstairLight", pdeviceHead);
  434. deviceTmp->deviceInit(deviceTmp->pinNum);
  435. deviceTmp->open(deviceTmp->pinNum);
  436. deviceTmp = findDeviceByName("livingroomLight", pdeviceHead);
  437. deviceTmp->deviceInit(deviceTmp->pinNum);
  438. deviceTmp->open(deviceTmp->pinNum);
  439. }
  440. else if (strstr(voiceHandler->comand, "gycd") != NULL) //关泳池灯指令
  441. {
  442. printf("关闭泳池灯光\n");
  443. deviceTmp = findDeviceByName("yong", pdeviceHead); //搜索泳池灯设备
  444. deviceTmp->deviceInit(deviceTmp->pinNum); //泳池灯设备初始化
  445. deviceTmp->close(deviceTmp->pinNum); //关泳池灯
  446. }
  447. else if (strstr(voiceHandler->comand, "kdfs") != NULL) //开电风扇指令
  448. {
  449. printf("打开电风扇\n");
  450. deviceTmp = findDeviceByName("fan", pdeviceHead);
  451. deviceTmp->deviceInit(deviceTmp->pinNum);
  452. deviceTmp->open(deviceTmp->pinNum);
  453. }
  454. else if (strstr(voiceHandler->comand, "gdfs") != NULL) //关电风扇指令
  455. {
  456. printf("关闭电风扇\n");
  457. deviceTmp = findDeviceByName("fan", pdeviceHead);
  458. deviceTmp->deviceInit(deviceTmp->pinNum);
  459. deviceTmp->close(deviceTmp->pinNum);
  460. }
  461. else if (strstr(voiceHandler->comand, "ks") != NULL) //开锁指令
  462. {
  463. printf("打开门锁\n");
  464. deviceTmp = findDeviceByName("lock", pdeviceHead);
  465. deviceTmp->deviceInit(deviceTmp->pinNum);
  466. deviceTmp->open(deviceTmp->pinNum);
  467. delay(3000);
  468. deviceTmp->close(deviceTmp->pinNum);
  469. }
  470. }
  471. pthread_mutex_unlock(&mutex); //解锁
  472. }
  473. }
  474. void *write_thread(void *datas) //通知线程,向客服端发送消息
  475. {
  476. while (1)
  477. {
  478. delay(500);
  479. write(c_fd, Message, 500);
  480. }
  481. }
  482. void *read_thread(void *datas) //通过socket读取客户端发来的数据
  483. {
  484. int n_read;
  485. struct Devices *deviceTmp = NULL;
  486. while (1)
  487. {
  488. memset(socketHandler->comand, '\0', sizeof(socketHandler->comand));
  489. n_read = read(c_fd, socketHandler->comand, sizeof(socketHandler->comand)); //读取客户端发来的数据
  490. if (n_read == -1)
  491. {
  492. perror("read_thread");
  493. }
  494. else if (n_read > 0)
  495. {
  496. printf("客户端指令:%s\n", socketHandler->comand);
  497. //处理客户端读到的命令
  498. if (strstr(socketHandler->comand, "kws") != NULL) //开卧室灯
  499. {
  500. printf("开卧室灯\n");
  501. deviceTmp = findDeviceByName("livingroomLight", pdeviceHead); //查找卧室灯设备
  502. deviceTmp->deviceInit(deviceTmp->pinNum); //卧室灯设备初始化
  503. deviceTmp->open(deviceTmp->pinNum); //打开卧室灯
  504. }
  505. else if (strstr(socketHandler->comand, "gws") != NULL) //关卧室灯
  506. {
  507. printf("关卧室灯\n");
  508. deviceTmp = findDeviceByName("livingroomLight", pdeviceHead);
  509. deviceTmp->deviceInit(deviceTmp->pinNum);
  510. deviceTmp->close(deviceTmp->pinNum);
  511. }
  512. else if (strstr(socketHandler->comand, "kys") != NULL) //开浴室灯
  513. {
  514. printf("开浴室灯\n");
  515. deviceTmp = findDeviceByName("bathroomLight", pdeviceHead);
  516. deviceTmp->deviceInit(deviceTmp->pinNum);
  517. deviceTmp->open(deviceTmp->pinNum);
  518. }
  519. else if (strstr(socketHandler->comand, "gys") != NULL) //关浴室灯
  520. {
  521. printf("关浴室灯\n");
  522. deviceTmp = findDeviceByName("bathroomLight", pdeviceHead);
  523. deviceTmp->deviceInit(deviceTmp->pinNum);
  524. deviceTmp->close(deviceTmp->pinNum);
  525. }
  526. else if (strstr(socketHandler->comand, "kkt") != NULL) //开客厅灯
  527. {
  528. printf("开客厅灯\n");
  529. deviceTmp = findDeviceByName("upstairLight", pdeviceHead);
  530. deviceTmp->deviceInit(deviceTmp->pinNum);
  531. deviceTmp->open(deviceTmp->pinNum);
  532. }
  533. else if (strstr(socketHandler->comand, "gkt") != NULL) //关客厅灯
  534. {
  535. printf("关客厅灯\n");
  536. deviceTmp = findDeviceByName("upstairLight", pdeviceHead);
  537. deviceTmp->deviceInit(deviceTmp->pinNum);
  538. deviceTmp->close(deviceTmp->pinNum);
  539. }
  540. else if (strstr(socketHandler->comand, "kct") != NULL) //开餐厅灯
  541. {
  542. printf("开餐厅灯\n");
  543. deviceTmp = findDeviceByName("chu", pdeviceHead);
  544. deviceTmp->deviceInit(deviceTmp->pinNum);
  545. deviceTmp->open(deviceTmp->pinNum);
  546. }
  547. else if (strstr(socketHandler->comand, "gct") != NULL) //关餐厅灯
  548. {
  549. printf("关餐厅灯\n");
  550. deviceTmp = findDeviceByName("restaurantLight", pdeviceHead);
  551. deviceTmp->deviceInit(deviceTmp->pinNum);
  552. deviceTmp->close(deviceTmp->pinNum);
  553. }
  554. else if (strstr(socketHandler->comand, "kfs") != NULL) //开风扇
  555. {
  556. printf("开电风扇\n");
  557. deviceTmp = findDeviceByName("fan", pdeviceHead);
  558. deviceTmp->deviceInit(deviceTmp->pinNum);
  559. deviceTmp->open(deviceTmp->pinNum);
  560. }
  561. else if (strstr(socketHandler->comand, "gfs") != NULL) //关风扇
  562. {
  563. printf("关电风扇\n");
  564. deviceTmp = findDeviceByName("fan", pdeviceHead);
  565. deviceTmp->deviceInit(deviceTmp->pinNum);
  566. deviceTmp->close(deviceTmp->pinNum);
  567. }
  568. else if (strstr(socketHandler->comand, "kqb") != NULL) //开室内全部灯
  569. {
  570. printf("开室内全部灯\n");
  571. deviceTmp = findDeviceByName("restaurantLight", pdeviceHead);
  572. deviceTmp->deviceInit(deviceTmp->pinNum);
  573. deviceTmp->open(deviceTmp->pinNum);
  574. deviceTmp = findDeviceByName("bathroomLight", pdeviceHead);
  575. deviceTmp->deviceInit(deviceTmp->pinNum);
  576. deviceTmp->open(deviceTmp->pinNum);
  577. deviceTmp = findDeviceByName("upstairLight", pdeviceHead);
  578. deviceTmp->deviceInit(deviceTmp->pinNum);
  579. deviceTmp->open(deviceTmp->pinNum);
  580. deviceTmp = findDeviceByName("livingroomLight", pdeviceHead);
  581. deviceTmp->deviceInit(deviceTmp->pinNum);
  582. deviceTmp->open(deviceTmp->pinNum);
  583. }
  584. else if (strstr(socketHandler->comand, "gqb") != NULL) //关室内全部灯
  585. {
  586. printf("关室内全部灯\n");
  587. deviceTmp = findDeviceByName("restaurantLight", pdeviceHead);
  588. deviceTmp->deviceInit(deviceTmp->pinNum);
  589. deviceTmp->close(deviceTmp->pinNum);
  590. deviceTmp = findDeviceByName("bathroomLight", pdeviceHead);
  591. deviceTmp->deviceInit(deviceTmp->pinNum);
  592. deviceTmp->close(deviceTmp->pinNum);
  593. deviceTmp = findDeviceByName("upstairLight", pdeviceHead);
  594. deviceTmp->deviceInit(deviceTmp->pinNum);
  595. deviceTmp->close(deviceTmp->pinNum);
  596. deviceTmp = findDeviceByName("livingroomLight", pdeviceHead);
  597. deviceTmp->deviceInit(deviceTmp->pinNum);
  598. deviceTmp->close(deviceTmp->pinNum);
  599. }
  600. else if (strstr(socketHandler->comand, "ks") != NULL) //开锁
  601. {
  602. printf("开锁\n");
  603. deviceTmp = findDeviceByName("lock", pdeviceHead);
  604. deviceTmp->deviceInit(deviceTmp->pinNum);
  605. deviceTmp->open(deviceTmp->pinNum);
  606. }
  607. else if (strstr(socketHandler->comand, "gs") != NULL) //关锁
  608. {
  609. deviceTmp = findDeviceByName("lock", pdeviceHead);
  610. deviceTmp->deviceInit(deviceTmp->pinNum);
  611. deviceTmp->close(deviceTmp->pinNum);
  612. }
  613. else if (strstr(socketHandler->comand, "kjk") != NULL) //开监控
  614. {
  615. printf("打开监控\n");
  616. pthread_create(&monitoringThread, NULL, monitoring_thread, NULL); //启动视频监控线程
  617. }
  618. else if (strstr(socketHandler->comand, "gjk") != NULL) //关监控
  619. {
  620. printf("关闭监控\n");
  621. // pthread_cancel(monitoringThread);
  622. system("killall -TERM mjpg_streamer"); //关闭摄像头监控功能
  623. }
  624. else if (strstr(socketHandler->comand, "khw") != NULL) //打开人体红外检测
  625. {
  626. printf("启动人体红外检测线程\n");
  627. beep = 3; //人体红外使用蜂鸣器
  628. deviceTmp = findDeviceByName("buzzser", pdeviceHead);
  629. deviceTmp->open(deviceTmp->pinNum);
  630. delay(700);
  631. deviceTmp->close(deviceTmp->pinNum);
  632. if (infrared_flag == 0)
  633. {
  634. pthread_create(&infraredThread, NULL, infrared_thread, NULL);
  635. infrared_flag = 1;
  636. }
  637. }
  638. else if (strstr(socketHandler->comand, "ghw") != NULL) //关闭人体红外检测
  639. {
  640. printf("关闭人体红外检测\n");
  641. memset(Message[4], 0, sizeof Message[4]); //清空数组
  642. sprintf(Message[4], "未启用!");
  643. if (infrared_flag == 1)
  644. {
  645. pthread_cancel(infraredThread); //停止人体红外线程
  646. infrared_flag = 0;
  647. }
  648. beep = 3; //人体红外使用蜂鸣器
  649. deviceTmp = findDeviceByName("buzzser", pdeviceHead);
  650. deviceTmp->open(deviceTmp->pinNum);
  651. delay(400);
  652. deviceTmp->close(deviceTmp->pinNum);
  653. delay(300);
  654. deviceTmp->open(deviceTmp->pinNum);
  655. delay(400);
  656. deviceTmp->close(deviceTmp->pinNum);
  657. }
  658. else if (strstr(socketHandler->comand, "gjms") != NULL) //归家模式
  659. {
  660. printf("开室内全部灯\n");
  661. deviceTmp = findDeviceByName("restaurantLight", pdeviceHead);
  662. deviceTmp->deviceInit(deviceTmp->pinNum);
  663. deviceTmp->open(deviceTmp->pinNum);
  664. deviceTmp = findDeviceByName("bathroomLight", pdeviceHead);
  665. deviceTmp->deviceInit(deviceTmp->pinNum);
  666. deviceTmp->open(deviceTmp->pinNum);
  667. deviceTmp = findDeviceByName("upstairLight", pdeviceHead);
  668. deviceTmp->deviceInit(deviceTmp->pinNum);
  669. deviceTmp->open(deviceTmp->pinNum);
  670. deviceTmp = findDeviceByName("livingroomLight", pdeviceHead);
  671. deviceTmp->deviceInit(deviceTmp->pinNum);
  672. deviceTmp->open(deviceTmp->pinNum);
  673. printf("关闭人体红外检测\n");
  674. memset(Message[4], 0, sizeof Message[4]); //清空数组
  675. sprintf(Message[4], "未启用!");
  676. if (infrared_flag == 1)
  677. {
  678. pthread_cancel(infraredThread); //停止人体红外线程
  679. infrared_flag = 0;
  680. }
  681. beep = 3; //人体红外使用蜂鸣器
  682. deviceTmp = findDeviceByName("buzzser", pdeviceHead);
  683. deviceTmp->open(deviceTmp->pinNum);
  684. delay(400);
  685. deviceTmp->close(deviceTmp->pinNum);
  686. delay(300);
  687. deviceTmp->open(deviceTmp->pinNum);
  688. delay(400);
  689. deviceTmp->close(deviceTmp->pinNum);
  690. beep = 0;
  691. }
  692. else if (strstr(socketHandler->comand, "ljms") != NULL) //离家模式
  693. {
  694. printf("关全部灯\n");
  695. deviceTmp = findDeviceByName("restaurantLight", pdeviceHead);
  696. deviceTmp->deviceInit(deviceTmp->pinNum);
  697. deviceTmp->close(deviceTmp->pinNum);
  698. deviceTmp = findDeviceByName("bathroomLight", pdeviceHead);
  699. deviceTmp->deviceInit(deviceTmp->pinNum);
  700. deviceTmp->close(deviceTmp->pinNum);
  701. deviceTmp = findDeviceByName("upstairLight", pdeviceHead);
  702. deviceTmp->deviceInit(deviceTmp->pinNum);
  703. deviceTmp->close(deviceTmp->pinNum);
  704. deviceTmp = findDeviceByName("livingroomLight", pdeviceHead);
  705. deviceTmp->deviceInit(deviceTmp->pinNum);
  706. deviceTmp->close(deviceTmp->pinNum);
  707. printf("关电风扇\n");
  708. deviceTmp = findDeviceByName("fan", pdeviceHead);
  709. deviceTmp->deviceInit(deviceTmp->pinNum);
  710. deviceTmp->close(deviceTmp->pinNum);
  711. printf("启动人体红外检测线程\n");
  712. beep = 3; //人体红外使用蜂鸣器
  713. deviceTmp = findDeviceByName("buzzser", pdeviceHead);
  714. deviceTmp->open(deviceTmp->pinNum);
  715. delay(700);
  716. deviceTmp->close(deviceTmp->pinNum);
  717. beep = 0;
  718. if (infrared_flag == 0)
  719. {
  720. pthread_create(&infraredThread, NULL, infrared_thread, NULL);
  721. infrared_flag = 1;
  722. }
  723. }
  724. else if (strstr(socketHandler->comand, "face") != NULL)
  725. {
  726. //启动人脸识别线程
  727. printf("进行人脸识别开锁\n");
  728. pthread_create(&cameraThread, NULL, cameraThread_func, NULL);
  729. }
  730. }
  731. else
  732. {
  733. printf("客户端退出\n");
  734. pthread_cancel(writeThread); //停止通知线程
  735. write_flag = 0; //通知线程标志位置0,通知线程关闭
  736. pthread_exit(NULL); //退出本线程
  737. }
  738. }
  739. }
  740. void *socket_thread(void *datas) //开启socket服务端,并将socket服务端初始化
  741. {
  742. int n_read = 0;
  743. pthread_t readPthread;
  744. struct sockaddr_in c_addr;
  745. memset(&c_addr, 0, sizeof(struct sockaddr_in));
  746. int clen = sizeof(struct sockaddr_in);
  747. socketHandler = findCommandByName("socketServer", pCommandHead); //在控制工厂找到socket
  748. if (socketHandler == NULL)
  749. {
  750. printf("查找套接字处理程序错误\n");
  751. pthread_exit(NULL);
  752. }
  753. else
  754. {
  755. printf("%s 初始化成功\n", socketHandler->commandName);
  756. }
  757. socketHandler->Init(socketHandler, NULL, NULL); //初始化socket
  758. while (1)
  759. {
  760. c_fd = accept(socketHandler->sfd, (struct sockaddr *)&c_addr, &clen);
  761. printf("c_fd = %d\n", c_fd);
  762. if (write_flag == 0) //通知线程处于关闭状态
  763. {
  764. write_flag = 1;
  765. pthread_create(&writeThread, NULL, write_thread, NULL); //打开通知线程
  766. }
  767. pthread_create(&readPthread, NULL, read_thread, NULL);
  768. }
  769. }
  770. //检测按键状态
  771. void *key_thread()
  772. {
  773. int val;
  774. while (1)
  775. {
  776. val = digitalRead(key);//获取key引脚状态给val
  777. if (val == 0) //防止重复检测
  778. {
  779. delay(500);
  780. val = digitalRead(key);
  781. if (val == 1) //按键按下,启动人脸识别线程
  782. {
  783. pthread_create(&cameraThread, NULL, cameraThread_func, NULL);
  784. }
  785. }
  786. }
  787. }
  788. int main()
  789. {
  790. char name[32] = {'\0'};
  791. //树莓派库初始化
  792. if (wiringPiSetup() == -1)
  793. {
  794. printf("硬件接口初始化失败\n");
  795. return -1;
  796. }
  797. pthread_mutex_init(&mutex, NULL); //初始化互斥量(锁)
  798. // 1、指令工厂初始化
  799. pCommandHead = addVoiceContrlToInputCommanderLink(pCommandHead); //语音识别初始化
  800. pCommandHead = addsocketContrlToInputCommanderLink(pCommandHead); //服务器初始化
  801. // 2、设备控制工程初始化
  802. pdeviceHead = addBathroomLightToDeviceLink(pdeviceHead); //浴室灯光初始化
  803. pdeviceHead = addupStairLightToDeviceLink(pdeviceHead); //卧室灯光初始化
  804. pdeviceHead = addlivingroomLightToDeviceLink(pdeviceHead); //客厅灯光初始化
  805. pdeviceHead = addrestaurantLightToDeviceLink(pdeviceHead); //餐厅灯光初始化
  806. pdeviceHead = addDoorLockToDeviceLink(pdeviceHead); //门锁初始化
  807. pdeviceHead = addFanToDeviceLink(pdeviceHead); //电风扇初始化
  808. pdeviceHead = addBuzzerToDeviceLink(pdeviceHead); //蜂鸣器初始化
  809. pdeviceHead = addFireToDeviceLink(pdeviceHead); //火焰传感器初始化
  810. pdeviceHead = addVibrateToDeviceLink(pdeviceHead); //震动传感器初始化
  811. pdeviceHead = addcameraToDeviceLink(pdeviceHead); //摄像头模块初始化
  812. pdeviceHead = addInfraredToDeviceLink(pdeviceHead); //人体感应传感器初始化
  813. pthread_create(&voiceThread, NULL, voice_thread, NULL); // 语音线程启动
  814. pthread_create(&socketThread, NULL, socket_thread, NULL); // socket服务器线程启动
  815. pthread_create(&fireThread, NULL, fire_thread, NULL); //火灾报警线程启动
  816. pthread_create(&vibrateThread, NULL, vibrate_thread, NULL); //震动报警线程启动
  817. pthread_create(&keyThread, NULL, key_thread, NULL); //按键检测线程启动
  818. pthread_create(&dht11Thread, NULL, dht11_thread, NULL); // 温湿度检测线程启动
  819. pthread_join(voiceThread, NULL);
  820. pthread_join(socketThread, NULL);
  821. pthread_join(fireThread, NULL);
  822. pthread_join(vibrateThread, NULL);
  823. pthread_join(infraredThread, NULL);
  824. pthread_join(keyThread, NULL);
  825. pthread_join(cameraThread, NULL);
  826. pthread_join(monitoringThread, NULL);
  827. pthread_join(clientWemosThread, NULL);
  828. pthread_join(dht11Thread, NULL);
  829. pthread_join(writeThread, NULL);
  830. pthread_mutex_destroy(&mutex); //销毁互斥量
  831. return 0;
  832. }

bathroomLight.c(浴室灯)

  1. #include "contrlDevices.h"
  2. int bathroomLightOpen(int pinNum)
  3. {
  4. digitalWrite(pinNum, HIGH);
  5. }
  6. int bathroomLightClose(int pinNum)
  7. {
  8. digitalWrite(pinNum, LOW);
  9. }
  10. int bathroomLightCloseInit(int pinNum)
  11. {
  12. pinMode(pinNum, OUTPUT);
  13. digitalWrite(pinNum, LOW);
  14. // printf("浴室灯初始化成功\n");
  15. }
  16. int bathroomLightCloseStatus(int status)
  17. {
  18. }
  19. struct Devices bathroomLight = {
  20. .deviceName = "bathroomLight",
  21. .pinNum = 24,
  22. .open = bathroomLightOpen,
  23. .close = bathroomLightClose,
  24. .deviceInit = bathroomLightCloseInit,
  25. .changStatus = bathroomLightCloseStatus};
  26. struct Devices *addBathroomLightToDeviceLink(struct Devices *phead)
  27. {
  28. if (phead == NULL)
  29. {
  30. return &bathroomLight;
  31. }
  32. else
  33. {
  34. bathroomLight.next = phead;
  35. phead = &bathroomLight;
  36. }
  37. return phead;
  38. }

buzzer.c(蜂鸣器)

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

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 29 //门锁
  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. char ocrRetBuf[1024] = {'\0'}; //全局变量,用来接收对比两张照片结果的数据
  19. size_t readData1(void *ptr, size_t size, size_t nmemb, void *stream)
  20. //回调函数,把从后台的数据拷贝给ocrRetBuf
  21. {
  22. strncpy(ocrRetBuf, ptr, 1024);
  23. }
  24. char *getFace1()
  25. {
  26. printf("拍照中...\n");
  27. system("raspistill -q 5 -t 1 -o image.jpg"); //-q 是图片质量,在0~100之间,我们调成5,压缩图片质量,生成的照片名字为imag.jpg
  28. //-t 是拍照延时,设定1s后拍照
  29. while (access("./image.jpg", F_OK) != 0); //判断是否拍照完毕
  30. printf("拍照完成\n");
  31. char *base64BufFaceRec = getPicFromOCRBase641("./image.jpg");
  32. // system("rm image.jpg");
  33. return base64BufFaceRec; //返回刚才拍照的base64
  34. }
  35. char *getPicFromOCRBase641(char *Filepath)
  36. {
  37. int fd;
  38. int filelen;
  39. char cmd[128] = {'\0'};
  40. sprintf(cmd, "base64 %s > tmpFile", Filepath);//拼凑字符 执行base64+照片名 结果存放在tmpFile
  41. system(cmd); //执行
  42. fd = open("./tmpFile", O_RDWR);
  43. filelen = lseek(fd, 0, SEEK_END); //计算指针偏移量 就是数据大小
  44. lseek(fd, 0, SEEK_SET); //移动光标
  45. char *bufpic = (char *)malloc(filelen + 2);
  46. memset(bufpic, '\0', filelen + 2);
  47. read(fd, bufpic, filelen + 128); //读到bufpic中
  48. system("rm -rf tmpFile");
  49. close(fd);
  50. return bufpic; //返回读的内容
  51. }
  52. void postUrl()
  53. {
  54. CURL *curl;
  55. CURLcode res;
  56. struct Devices *deviceTmp = NULL;
  57. //分开定义,然后字符串拼接
  58. char *key = "*******************";
  59. char *secret = "********************";
  60. int typeId = 21;
  61. char *format = "xml";
  62. char *base64BufPic1 = getFace1(); //摄像头拍的
  63. char *base64BufPic2 = getPicFromOCRBase641("nb.jpg");//房主照片
  64. int len = strlen(key) + strlen(secret) + strlen(base64BufPic1) + strlen(base64BufPic2) + 128; //分配空间不够会导致栈溢出
  65. char *postString = (char *)malloc(len);
  66. memset(postString, '\0', len); //因为postString是一个指针,不能用sizeof来计算其指向的大小
  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. {
  71. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postString); //指定post内容,传入参数
  72. curl_easy_setopt(curl, CURLOPT_URL, "https://netocr.com/api/faceliu.do"); // 指定url
  73. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, readData1); //回调函数readDate读取返回值
  74. res = curl_easy_perform(curl); //类似于状态码
  75. printf("OK:%d\n", res);
  76. if (strstr(ocrRetBuf, "是") != NULL)
  77. { //判断翔云后台返回的字符串中有没有“是”
  78. printf("是同一个人\n");
  79. digitalWrite(door_lock, LOW); //打开门锁
  80. delay(3000); //等待3s
  81. digitalWrite(door_lock, HIGH); //关闭门锁
  82. }
  83. else
  84. {
  85. printf("不是同一个人\n");
  86. }
  87. curl_easy_cleanup(curl);
  88. }
  89. }
  90. struct Devices camera = {
  91. .deviceName = "camera",
  92. .justDoOnce = postUrl,
  93. .getFace = getFace1,
  94. .getPicFromOCRBase64 = getPicFromOCRBase641,
  95. .readData = readData1
  96. };
  97. struct Devices *addcameraToDeviceLink(struct Devices *phead)
  98. {
  99. if (phead == NULL)
  100. {
  101. return &camera;
  102. }
  103. else
  104. {
  105. camera.next = phead;
  106. phead = &camera;
  107. }
  108. }

doorLock.c(门锁)

  1. #include "contrlDevices.h"
  2. int doorLockOpen(int pinNum)
  3. {
  4. //digitalWrite(pinNum, LOW);
  5. for(int i=0;i<20;i++)
  6. {
  7. digitalWrite(pinNum,HIGH);
  8. delay(2);
  9. digitalWrite(pinNum,LOW);
  10. delay(18);
  11. }
  12. pinMode(pinNum,INPUT);
  13. pinMode(pinNum,OUTPUT);
  14. }
  15. int doorLockClose(int pinNum)
  16. {
  17. digitalWrite(pinNum, HIGH);
  18. }
  19. int doorLockCloseInit(int pinNum)
  20. {
  21. pinMode(pinNum, OUTPUT);
  22. digitalWrite(pinNum, HIGH);
  23. }
  24. int doorLockCloseStatus(int status)
  25. {
  26. }
  27. //门锁
  28. struct Devices doorLock = {
  29. .deviceName = "lock",
  30. .pinNum = 28,
  31. .open = doorLockOpen,
  32. .close = doorLockClose,
  33. .deviceInit = doorLockCloseInit,
  34. .changStatus = doorLockCloseStatus};
  35. struct Devices *addDoorLockToDeviceLink(struct Devices *phead)
  36. {
  37. if (phead == NULL)
  38. {
  39. return &doorLock;
  40. }
  41. else
  42. {
  43. doorLock.next = phead;
  44. phead = &doorLock;
  45. }
  46. return phead;
  47. }

fan.c(风扇)

  1. #include "contrlDevices.h"
  2. int fanOpen(int pinNum)
  3. {
  4. pinMode(pinNum, OUTPUT);
  5. }
  6. int fanClose(int pinNum)
  7. {
  8. pinMode(pinNum, INPUT);
  9. }
  10. int fanCloseInit(int pinNum) //风扇有问题 设为输出就转 输入就停 高低电平影响不了
  11. {
  12. pinMode(pinNum, INPUT);
  13. printf("电风扇初始化成功\n");
  14. }
  15. int fanCloseStatus(int status)
  16. {
  17. }
  18. struct Devices fan = {
  19. .deviceName = "fan",
  20. .pinNum = 26,
  21. .open = fanOpen,
  22. .close = fanClose,
  23. .deviceInit = fanCloseInit,
  24. .changStatus = fanCloseStatus
  25. };
  26. struct Devices *addFanToDeviceLink(struct Devices *phead)
  27. {
  28. if (phead == NULL)
  29. {
  30. return &fan;
  31. }
  32. else
  33. {
  34. fan.next = phead;
  35. phead = &fan;
  36. }
  37. return phead;
  38. }

fire.c(火焰传感)

  1. #include "contrlDevices.h"
  2. int fireInit(int pinNum)
  3. {
  4. pinMode(pinNum, INPUT);
  5. digitalWrite(pinNum, HIGH);
  6. }
  7. int readFireStatus(int pinNum)
  8. {
  9. return digitalRead(pinNum);
  10. }
  11. struct Devices fire = {
  12. .deviceName = "fire",
  13. .pinNum = 25,
  14. .deviceInit = fireInit,
  15. .readStatus = readFireStatus
  16. };
  17. struct Devices *addFireToDeviceLink(struct Devices *phead)
  18. {
  19. if (phead == NULL)
  20. {
  21. return &fire;
  22. }
  23. else
  24. {
  25. fire.next = phead;
  26. phead = &fire;
  27. }
  28. return phead;
  29. }

infrared.c(人体红外)

  1. #include "contrlDevices.h"
  2. int infraredInit(int pinNum)
  3. {
  4. pinMode(pinNum, INPUT);
  5. digitalWrite(pinNum, HIGH);
  6. // printf("人体感应传感器初始化成功\n");
  7. }
  8. int readinfraredStatus(int pinNum)
  9. {
  10. return digitalRead(pinNum);
  11. }
  12. struct Devices infrared = {
  13. .deviceName = "infrared",
  14. .pinNum = 5,
  15. .deviceInit = infraredInit,
  16. .readStatus = readinfraredStatus};
  17. struct Devices *addInfraredToDeviceLink(struct Devices *phead)
  18. {
  19. if (phead == NULL)
  20. {
  21. return &infrared;
  22. }
  23. else
  24. {
  25. infrared.next = phead;
  26. phead = &infrared;
  27. }
  28. return phead;
  29. }

livingroomLight.c(卧室灯)

  1. #include "contrlDevices.h"
  2. int livingroomLightOpen(int pinNum)
  3. {
  4. digitalWrite(pinNum, HIGH);
  5. }
  6. int livingroomLightClose(int pinNum)
  7. {
  8. digitalWrite(pinNum, LOW);
  9. }
  10. int livingroomLightCloseInit(int pinNum)
  11. {
  12. pinMode(pinNum, OUTPUT);
  13. digitalWrite(pinNum, LOW);
  14. }
  15. int livingroomLightCloseStatus(int status)
  16. {
  17. }
  18. struct Devices livingroomLight = {
  19. .deviceName = "livingroomLight",
  20. .pinNum = 21,
  21. .open = livingroomLightOpen,
  22. .close = livingroomLightClose,
  23. .deviceInit = livingroomLightCloseInit,
  24. .changStatus = livingroomLightCloseStatus};
  25. struct Devices *addlivingroomLightToDeviceLink(struct Devices *phead)
  26. {
  27. if (phead == NULL)
  28. {
  29. return &livingroomLight;
  30. }
  31. else
  32. {
  33. livingroomLight.next = phead;
  34. phead = &livingroomLight;
  35. }
  36. return phead;
  37. }

restaurantLight.c(餐厅灯)

  1. #include "contrlDevices.h"
  2. int restaurantLightOpen(int pinNum)
  3. {
  4. digitalWrite(pinNum, HIGH);
  5. }
  6. int restaurantLightClose(int pinNum)
  7. {
  8. digitalWrite(pinNum, LOW);
  9. }
  10. int restaurantLightCloseInit(int pinNum)
  11. {
  12. pinMode(pinNum, OUTPUT);
  13. digitalWrite(pinNum, LOW);
  14. }
  15. int restaurantLightCloseStatus(int status)
  16. {
  17. }
  18. struct Devices restaurantLight = {
  19. .deviceName = "restaurantLight",
  20. .pinNum = 23,
  21. .open = restaurantLightOpen,
  22. .close = restaurantLightClose,
  23. .deviceInit = restaurantLightCloseInit,
  24. .changStatus = restaurantLightCloseStatus};
  25. struct Devices *addrestaurantLightToDeviceLink(struct Devices *phead)
  26. {
  27. if (phead == NULL)
  28. {
  29. return &restaurantLight;
  30. }
  31. else
  32. {
  33. restaurantLight.next = phead;
  34. phead = &restaurantLight;
  35. }
  36. return phead;
  37. }

socketContrl.c(socket控制)

  1. #include <wiringPi.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <wiringSerial.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 <arpa/inet.h>
  11. #include <string.h>
  12. #include <sys/types.h>
  13. #include <sys/stat.h>
  14. #include <fcntl.h>
  15. #include "inputCommand.h"
  16. int socketgetCommand(struct InputCommander *socketMes)
  17. {
  18. int c_fd;
  19. int n_read;
  20. struct sockaddr_in c_addr;
  21. memset(&c_addr, 0, sizeof(struct sockaddr_in));
  22. int clen = sizeof(struct sockaddr_in);
  23. // 4.accept
  24. c_fd = accept(socketMes->sfd, (struct sockaddr *)&c_addr, &clen);
  25. n_read = read(c_fd, socketMes->comand, sizeof(socketMes->comand));
  26. if (n_read == -1)
  27. {
  28. perror("读");
  29. }
  30. else if (n_read > 0)
  31. {
  32. printf("\n获取:%d\n", n_read);
  33. }
  34. else
  35. {
  36. printf("客户端退出\n");
  37. }
  38. return n_read;
  39. }
  40. int socketInit(struct InputCommander *socketMes, char *ipAdress, char *port)
  41. {
  42. int s_fd;
  43. struct sockaddr_in s_addr;
  44. memset(&s_addr, 0, sizeof(struct sockaddr_in));
  45. // 1.socket
  46. s_fd = socket(AF_INET, SOCK_STREAM, 0);
  47. if (s_fd == -1)
  48. {
  49. perror("socket");
  50. exit(-1);
  51. }
  52. s_addr.sin_family = AF_INET;
  53. s_addr.sin_port = htons(atoi(socketMes->port));
  54. inet_aton(socketMes->ipAdress, &s_addr.sin_addr);
  55. //解决服务器程序结束后端口被占用的情况
  56. int opt = 1;
  57. setsockopt(s_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
  58. // 2.bind
  59. bind(s_fd, (struct sockaddr *)&s_addr, sizeof(struct sockaddr_in));
  60. // 3.listen
  61. listen(s_fd, 10);
  62. printf("套接字服务器侦听......\n");
  63. socketMes->sfd = s_fd;
  64. return s_fd;
  65. }
  66. struct InputCommander socketContrl = {
  67. .commandName = "socketServer",
  68. .comand = {'\0'},
  69. .port = "8088",
  70. .ipAdress = "192.168.137.167",
  71. .Init = socketInit,
  72. .getCommand = socketgetCommand,
  73. .log = {'\0'},
  74. .next = NULL};
  75. struct InputCommander *addsocketContrlToInputCommanderLink(struct InputCommander *phead)
  76. {
  77. if (phead == NULL)
  78. {
  79. return &socketContrl;
  80. }
  81. else
  82. {
  83. socketContrl.next = phead;
  84. phead = &socketContrl;
  85. }
  86. return phead;
  87. }

upstairLight.c(客厅灯)

  1. #include <stdlib.h>
  2. #include <fcntl.h>
  3. #include "contrlDevices.h"
  4. int upstairLightOpen(int pinNum) //打开客厅灯函数
  5. {
  6. digitalWrite(pinNum, HIGH);
  7. }
  8. int upstairLightClose(int pinNum) //关闭客厅灯函数
  9. {
  10. digitalWrite(pinNum, LOW);
  11. }
  12. int upstairLightCloseInit(int pinNum) //初始化函数
  13. {
  14. pinMode(pinNum, OUTPUT); //配置引脚为输出引脚
  15. digitalWrite(pinNum, LOW); //引脚输出高电平,即默认为关闭状态
  16. }
  17. int upstairLightCloseStatus(int status)
  18. {
  19. }
  20. struct Devices upstairLight = { //客厅灯设备链表节点
  21. .deviceName = "upstairLight",
  22. .pinNum = 22,
  23. .open = upstairLightOpen,
  24. .close = upstairLightClose,
  25. .deviceInit = upstairLightCloseInit,
  26. .changStatus = upstairLightCloseStatus
  27. };
  28. struct Devices *addupStairLightToDeviceLink(struct Devices *phead) //头插法将设备节点加入设备工厂链表函数
  29. {
  30. if (phead == NULL)
  31. {
  32. return &upstairLight;
  33. }
  34. else
  35. {
  36. upstairLight.next = phead;
  37. phead = &upstairLight;
  38. }
  39. }

vibrate.c(震动传感器)

  1. #include "contrlDevices.h"
  2. int vibrateInit(int pinNum)
  3. {
  4. pinMode(pinNum, INPUT);
  5. digitalWrite(pinNum, HIGH);
  6. }
  7. int readvibrateStatus(int pinNum)
  8. {
  9. return digitalRead(pinNum);
  10. }
  11. struct Devices vibrate = {
  12. .deviceName = "vibrate",
  13. .pinNum = 1,
  14. .deviceInit = vibrateInit,
  15. .readStatus = readvibrateStatus};
  16. struct Devices *addVibrateToDeviceLink(struct Devices *phead)
  17. {
  18. if (phead == NULL)
  19. {
  20. return &vibrate;
  21. }
  22. else
  23. {
  24. vibrate.next = phead;
  25. phead = &vibrate;
  26. }
  27. return phead;
  28. }

voiceContrl.c(语音模块)

  1. #include <wiringPi.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <wiringSerial.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include "inputCommand.h"
  8. int getCommand(struct InputCommander *voicer) //获取语音指令
  9. {
  10. int nread = 0;
  11. memset(voicer->comand, '\0', sizeof(voicer->comand));
  12. nread = read(voicer->fd, voicer->comand, sizeof(voicer->comand));//将语音模块返回的数据存放在缓冲区comand
  13. return nread;
  14. }
  15. //语音模块初始化
  16. int voiceInit(struct InputCommander *voicer, char *ipAdress, char *port)
  17. {
  18. int fd;
  19. if ((fd = serialOpen(voicer->deviceName, 9600)) == -1)
  20. {
  21. printf("语音初始化失败\n");
  22. exit(-1);
  23. }
  24. voicer->fd = fd;
  25. printf("语音初始化结束\n");
  26. return fd;
  27. }
  28. //语音模块结构体
  29. struct InputCommander voiceContrl = {
  30. .commandName = "voice",
  31. .deviceName = "/dev/ttyAMA0",//此文件用于存放语音模块返回的数据
  32. .comand = {'\0'},
  33. .Init = voiceInit,
  34. .getCommand = getCommand,
  35. .log = {'\0'},
  36. .next = NULL
  37. };
  38. struct InputCommander *addVoiceContrlToInputCommanderLink(struct InputCommander *phead)
  39. {
  40. if (phead == NULL)
  41. {
  42. return &voiceContrl;
  43. }
  44. else
  45. {
  46. voiceContrl.next = phead;
  47. phead = &voiceContrl;
  48. }
  49. return phead;
  50. }

2.2全志代码

  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <unistd.h>
  5. #include <netinet/in.h>
  6. #include <arpa/inet.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <wiringPi.h>
  10. int main(int argc, char **argv)
  11. {
  12. int c_fd;
  13. int n_read;
  14. int a = 0;
  15. char msg[24] = {};
  16. char readBuf[128];
  17. struct sockaddr_in c_addr;
  18. memset(&c_addr,0,sizeof(struct sockaddr_in));
  19. c_fd = socket(AF_INET, SOCK_STREAM, 0);
  20. if(c_fd == -1){
  21. perror("socket");
  22. exit(-1);
  23. }
  24. c_addr.sin_family = AF_INET;
  25. c_addr.sin_port = htons(atoi(argv[2]));
  26. inet_aton(argv[1],&c_addr.sin_addr);
  27. if(connect(c_fd, (struct sockaddr *)&c_addr,sizeof(struct sockaddr)) == -1){
  28. perror("connect");
  29. exit(-1);
  30. }
  31. while(1)
  32. {
  33. if(fork() == 0)
  34. {
  35. memset(msg,0,sizeof(msg));
  36. scanf("%s",msg);
  37. write(c_fd,msg,strlen(msg));
  38. }
  39. n_read = read(c_fd, readBuf, 128);
  40. if(n_read == -1){
  41. perror("read");
  42. }else{
  43. printf("服务端消息%d,%s\n",n_read,readBuf);
  44. }
  45. delay(4000);
  46. }
  47. return 0;
  48. }

3、编译运行效果

树莓派端编译

gcc camera.c doorLock.c vibrate.c livingroomLight.c upstairLight.c bathroomLight.c fan.c lock.c restaurantLight.c socketContrl.c voiceContrl.c buzzer.c fire.c mainPro.c infrared.c -lwiringPi -lpthread -lcurl

全志端编译:

  1. gcc client.c -lwiringPi
  2. ./a.out 192.168.137.167 8088

运行效果:

 

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

闽ICP备14008679号