基于树莓派的智能家居控制系统设计

完整文件下载

基于树莓派的智能家居控制系统设计
https://download.csdn.net/download/G1842965496/85802405

一、功能介绍

1.通过手机APP、语音识别等控制家电,对门锁,灯光、电风扇等设备进行控制,开发回家模式,睡觉模式等应用场景。
2.通过视频监控、火灾报警、震动报警和人体感应报警保证家居的安全。
3.将温湿度检测数据和家居安防状况实时同步到用户的手机APP进行显示。
4.遥控控制可以保证在没有网络的情况下也可以对家居进行控制。
5.通过翔云平台提供的人脸对比服务完成人脸识别开锁

二、实物展示

1.家居模型实物



2.部分功能展示
手机APP

火灾测试
火灾报警
视频监控
温湿度检测
人脸识别按钮
人脸识别开锁

三、程序(手机APP程序过多不展示)

1.主程序mainPro.c

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>#include "contrlDevices.h"
#include "inputCommand.h"
#define HIGH_TIME 32// 433m端口
#define D0 4
#define D1 3
#define D2 2
#define D3 0#define key 28 //拍照按键
// #define door_Lock 29 //门锁//注意:定义线程不使用指针以免空指针异常
//注意:不建议线程传参(链表头)所以定为全局变量
pthread_t voiceThread;       //语音识别线程
pthread_t socketThread;      //服务器线程
pthread_t writeThread;       //通知线程:向客服端发送消息
pthread_t fireThread;        //火灾检测线程
pthread_t shakeThread;       //震动检测线程
pthread_t humanThread;       //人体红外检测线程
pthread_t remoteThread;      //遥控线程
pthread_t keyThread;         //按键检测线程
pthread_t cameraThread;      //人脸识别线程
pthread_t monitoringThread;  //视频监控线程
pthread_t clientWemosThread; //客服端线程
pthread_t dht11Thread;       //温湿度线程struct InputCommander *pCommandHead = NULL;
struct Devices *pdeviceHead = NULL;
struct InputCommander *socketHandler = NULL;
// struct InputCommander *clientHandler = NULL;pthread_mutex_t mutex; //定义互斥量(锁)
// pthread_cond_t  cond;   //条件unsigned long databuf;int c_fd; //注意:涉及到多线程不要轻易的去传参int beep = 0; //蜂鸣器标志位   0:未被使用   1:被火灾传感器使用    2:被震动传感器使用
//摄像头相关,改变返回值命名,因为C语言中没有这样的返回值
#define true 1
#define false 0
typedef unsigned int bool;
char buf[1024] = {'\0'};/*通知app [0]:温度   [1]:湿度   [2]:火灾监控   [3]:震动监控   [4]:人体红外监控*/
char Message[5][100] = {"未启用!", "未启用!", "未启用!", "未启用!", "未启用!"};
int write_flag = 0; //标记位,标记通知线程是否打开     1:打开   0:关闭int human_flag = 0; //红外检测标志位     1:打开   0:关闭struct Devices *findDeviceByName(char *name, struct Devices *phead) //查询设备
{if (phead == NULL){return NULL;}while (phead != NULL){// printf("phead:%s  name:%s\n", phead->deviceName, name);if (strstr(phead->deviceName, name) != NULL){return phead;}phead = phead->next;}return NULL;
}struct InputCommander *findCommandByName(char *name, struct InputCommander *phead) //查询控制
{if (phead == NULL){return NULL;}while (phead != NULL){if (strcmp(phead->commandName, name) == 0){return phead;}phead = phead->next;}return NULL;
}//查找设备
void *cameraThread_func(void *data) //起线程的函数有格式要求
{struct Devices *cameraTemp;cameraTemp = findDeviceByName("camera", pdeviceHead); //设备都要从工厂里面取出来if (cameraTemp == NULL){ //防止段错误的必需判断,当给指针赋值是,一定要考虑NULL的情况,否则后续操作都是空谈printf("find camera error\n");pthread_exit(NULL); //在线程中不用return}cameraTemp->justDoOnce(); //调用postUrl函数
}unsigned char readSensorData(void) //温湿度初始化
{char crc;char i;pinMode(6, OUTPUT);   // 将模式设置为输出digitalWrite(6, LOW); // 输出高电平delay(25);digitalWrite(6, HIGH); // 输出低电平pinMode(6, INPUT);     // 将模式设置为输入pullUpDnControl(6, PUD_UP);delayMicroseconds(27);if (digitalRead(6) == 0) // SENSOR ANS{while (!digitalRead(6)); // wait to highfor (i = 0; i < 32; i++){while (digitalRead(6)); // 数据时钟启动while (!digitalRead(6)); //数据开始delayMicroseconds(HIGH_TIME);databuf *= 2;if (digitalRead(6) == 1) // 1{databuf++;}}for (i = 0; i < 8; i++){while (digitalRead(6)); // 数据时钟启动while (!digitalRead(6)); // 数据开始delayMicroseconds(HIGH_TIME);crc *= 2;if (digitalRead(6) == 1) // 1{crc++;}}return 1;}else{return 0;}
}void *dht11_thread(void *datas) //温湿度线程
{int W = 0, w = 0;int S = 0, s = 0;// int count = 0;int temp = 0;printf("Use GPIO4 to read data!\n");if (-1 == wiringPiSetup()){printf("Setup wiringPi failed!");// return 1;}pinMode(6, OUTPUT);    // set mode to outputdigitalWrite(6, HIGH); // output a high levelprintf("Enter OS-------\n");while (1){pinMode(6, OUTPUT);    // set mode to outputdigitalWrite(6, HIGH); // output a high leveldelay(3000);if (readSensorData()){W = (databuf >> 8) & 0xff;w = databuf & 0xff;S = (databuf >> 24) & 0xff;s = (databuf >> 16) & 0xff;// printf("传感器数据读取正常!\n");if (temp == 0){w++;s++;temp++;}else if (temp == 1){if (w >= 1){w--;}if (s >= 1){w--;}temp--;}if ((W >= 15) && (W <= 35) && (S <= 95)){memset(Message[0], 0, sizeof Message[0]); //清空数组memset(Message[1], 0, sizeof Message[1]);// printf("温度:%d.%d    湿度:%d.%d\n", W, w, S, s); //清空数组sprintf(Message[0], "%d.%d C", W, w); //温度sprintf(Message[1], "%d.%d", S, s);   //湿度//  printf("温度:%d.%d C", (databuf >> 8) & 0xff, databuf & 0xff);               //温度}                                         //   printf("   湿度:%d.%d %\n", (databuf >> 24) & 0xff, (databuf >> 16) & 0xff); //湿度databuf = 0;}else{//  count++;//  memset(Message[0], 0, sizeof Message[0]); //清空数组//  memset(Message[1], 0, sizeof Message[1]); //清空数组// sprintf(Message[0], "监测错误!");        //温度//  sprintf(Message[1], "监测错误!");        //湿度printf("Sorry! Sensor dosent ans!\n");databuf = 0;}// if (count >= 3)// {//     pthread_create(&dht11Thread, NULL, dht11_thread, NULL);// }}
}void *fire_thread(void *datas) //火灾线程
{char msg[100];int status;struct Devices *fireDeviceTmp = NULL;struct Devices *buzzerDeviceTmp = NULL;fireDeviceTmp = findDeviceByName("fire", pdeviceHead); //在设备工厂找到火灾模块buzzerDeviceTmp = findDeviceByName("buzzser", pdeviceHead);fireDeviceTmp->deviceInit(fireDeviceTmp->pinNum); //火灾模块初始化buzzerDeviceTmp->deviceInit(buzzerDeviceTmp->pinNum);printf("火灾线程初始化成功\n");while (1){delay(10);status = fireDeviceTmp->readStatus(fireDeviceTmp->pinNum); //读取火灾模块实时数据if (status == 0){beep = 1;                                       //火灾传感器使用蜂鸣器buzzerDeviceTmp->open(buzzerDeviceTmp->pinNum); //蜂鸣器报警memset(Message[2], 0, sizeof Message[2]);       //清空数组sprintf(Message[2], "警报:发生火灾 !");       //更新火灾信息delay(200);                                     //蜂鸣器报警延时}else if ((beep != 2) && (beep != 3)) //未被震动传感器和人体红外传感器使用{buzzerDeviceTmp->close(buzzerDeviceTmp->pinNum); //关闭蜂鸣器memset(Message[2], 0, sizeof Message[2]);        //清空数组sprintf(Message[2], "正常");                     //更新火灾信息beep = 0;                                        //蜂鸣器未被使用}}
}void *shake_thread(void *datas) //震动线程
{char msg[100];int status;struct Devices *shakeDeviceTmp = NULL;struct Devices *buzzerDeviceTmp = NULL;shakeDeviceTmp = findDeviceByName("shake", pdeviceHead); //在设备工厂找到火灾模块buzzerDeviceTmp = findDeviceByName("buzzser", pdeviceHead);shakeDeviceTmp->deviceInit(shakeDeviceTmp->pinNum); //震动模块初始化buzzerDeviceTmp->deviceInit(buzzerDeviceTmp->pinNum);printf("震动线程初始化成功\n");while (1){delay(5);status = shakeDeviceTmp->readStatus(shakeDeviceTmp->pinNum); //读取震动模块实时数据if (status == 0){beep = 2; //震动传感器使用蜂鸣器buzzerDeviceTmp->open(buzzerDeviceTmp->pinNum);memset(Message[3], 0, sizeof Message[3]); //清空数组sprintf(Message[3], "警报:发生震动 !");delay(300);}else if ((beep != 1) && (beep != 3)) //蜂鸣器未被火焰传感器和人体红外传感器使用{memset(Message[3], 0, sizeof Message[3]); //清空数组sprintf(Message[3], "正常");buzzerDeviceTmp->close(buzzerDeviceTmp->pinNum);beep = 0; //蜂鸣器未被使用}}
}void *human_thread(void *datas) // 人体红外检测线程
{char msg[100];int status;struct Devices *humanDeviceTmp = NULL;struct Devices *buzzerDeviceTmp = NULL;humanDeviceTmp = findDeviceByName("human", pdeviceHead); //在设备工厂找到火灾模块buzzerDeviceTmp = findDeviceByName("buzzser", pdeviceHead);humanDeviceTmp->deviceInit(humanDeviceTmp->pinNum); //红外模块初始化buzzerDeviceTmp->deviceInit(buzzerDeviceTmp->pinNum);printf("人体红外检测初始化\n");while (1){delay(10);status = humanDeviceTmp->readStatus(humanDeviceTmp->pinNum); //读取人体感应模块实时数据if (status == 1){beep = 3;                                 //人体红外使用蜂鸣器memset(Message[4], 0, sizeof Message[4]); //清空数组sprintf(Message[4], "警报:有人进入 !");buzzerDeviceTmp->open(buzzerDeviceTmp->pinNum);delay(200);buzzerDeviceTmp->close(buzzerDeviceTmp->pinNum);delay(100);}else if ((beep != 1) && (beep != 2)) //未被火焰传感器和震动传感器使用{memset(Message[4], 0, sizeof Message[4]); //清空数组sprintf(Message[4], "正常");buzzerDeviceTmp->close(buzzerDeviceTmp->pinNum);beep = 0; //蜂鸣器未被使用}}
}void *monitoring_thread(void *datas) //视频监控线程
{system("./start_web_video.sh"); //执行脚本,打开视频监控pthread_exit(NULL);             //退出线程
}void *voice_thread(void *arg) //语音线程
{int i = 0;int nread;struct InputCommander *voiceHandler = NULL;struct Devices *deviceTmp = NULL;voiceHandler = findCommandByName("voice", pCommandHead); //在控制工厂找到语音模块if (voiceHandler == NULL){printf("查找语音处理程序错误\n");pthread_exit(NULL);}else{if (voiceHandler->Init(voiceHandler, NULL, NULL) < 0){ //语音模块初始化printf("语音初始化错误\n");pthread_exit(NULL); //退出线程}else{printf("%s 初始化成功\n", voiceHandler->commandName);} //语音初始化完成pthread_mutex_lock(&mutex); //加锁//语音读取一级指令的时候,为了避免其它线程对于 紧接着读取二级指令的干扰while (1){memset(voiceHandler->comand, '\0', sizeof(voiceHandler->comand));nread = voiceHandler->getCommand(voiceHandler); //读取来自语音模块的串口数据printf("get voice command:%s\n", voiceHandler->comand);if (nread == 0){printf("没有来自语音的数据,请再说一遍\n");}else if (strstr(voiceHandler->comand, "kwsd") != NULL) //打开卧室灯命令{printf("打开卧室灯光\n");deviceTmp = findDeviceByName("shui", pdeviceHead); //查找卧室灯设备deviceTmp->deviceInit(deviceTmp->pinNum);          //卧室灯设备初始化deviceTmp->open(deviceTmp->pinNum);                //打开卧室灯}else if (strstr(voiceHandler->comand, "gwsd") != NULL){printf("关闭卧室灯光\n");deviceTmp = findDeviceByName("shui", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->close(deviceTmp->pinNum);}else if (strstr(voiceHandler->comand, "kysd") != NULL){printf("打开浴室灯光\n");deviceTmp = findDeviceByName("yu", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->open(deviceTmp->pinNum);}else if (strstr(voiceHandler->comand, "gysd") != NULL){printf("关闭浴室灯光\n");deviceTmp = findDeviceByName("yu", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->close(deviceTmp->pinNum);}else if (strstr(voiceHandler->comand, "kktd") != NULL){printf("打开客厅灯光\n");deviceTmp = findDeviceByName("kt", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->open(deviceTmp->pinNum);}else if (strstr(voiceHandler->comand, "gktd") != NULL){printf("关闭客厅灯光\n");deviceTmp = findDeviceByName("kt", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->close(deviceTmp->pinNum);}else if (strstr(voiceHandler->comand, "kctd") != NULL){printf("打开餐厅灯光\n");deviceTmp = findDeviceByName("chu", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->open(deviceTmp->pinNum);}else if (strstr(voiceHandler->comand, "gctd") != NULL){printf("关闭餐厅灯光\n");deviceTmp = findDeviceByName("chu", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->close(deviceTmp->pinNum);}else if (strstr(voiceHandler->comand, "kycd") != NULL) //开泳灯指令{printf("打开泳池灯光\n");deviceTmp = findDeviceByName("yong", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->open(deviceTmp->pinNum);}else if (strstr(voiceHandler->comand, "gqbd") != NULL) //关闭全部灯指令{printf("关闭所有灯光\n");deviceTmp = findDeviceByName("chu", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->close(deviceTmp->pinNum); //关闭餐厅灯deviceTmp = findDeviceByName("yu", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->close(deviceTmp->pinNum); //关闭浴室灯deviceTmp = findDeviceByName("kt", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->close(deviceTmp->pinNum); //关闭客厅灯deviceTmp = findDeviceByName("shui", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->close(deviceTmp->pinNum); //关闭卧室灯}else if (strstr(voiceHandler->comand, "kqbd") != NULL){printf("打开所有灯光\n");deviceTmp = findDeviceByName("chu", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->open(deviceTmp->pinNum);deviceTmp = findDeviceByName("yu", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->open(deviceTmp->pinNum);deviceTmp = findDeviceByName("kt", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->open(deviceTmp->pinNum);deviceTmp = findDeviceByName("shui", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->open(deviceTmp->pinNum);}else if (strstr(voiceHandler->comand, "gycd") != NULL) //关泳池灯指令{printf("关闭泳池灯光\n");deviceTmp = findDeviceByName("yong", pdeviceHead); //搜索泳池灯设备deviceTmp->deviceInit(deviceTmp->pinNum);          //泳池灯设备初始化deviceTmp->close(deviceTmp->pinNum);               //关泳池灯}else if (strstr(voiceHandler->comand, "kdfs") != NULL) //开电风扇指令{printf("打开电风扇\n");deviceTmp = findDeviceByName("fan", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->open(deviceTmp->pinNum);}else if (strstr(voiceHandler->comand, "gdfs") != NULL) //关电风扇指令{printf("关闭电风扇\n");deviceTmp = findDeviceByName("fan", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->close(deviceTmp->pinNum);}else if (strstr(voiceHandler->comand, "ks") != NULL) //开锁指令{printf("打开门锁\n");deviceTmp = findDeviceByName("lock", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->open(deviceTmp->pinNum);delay(3000);deviceTmp->close(deviceTmp->pinNum);}/*else{deviceTmp = findDeviceByName(voiceHandler->comand, pdeviceHead);if (deviceTmp == NULL){printf("查找设备按名称错误\n");}else{printf("查找设备 = %s\n", deviceTmp->deviceName);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->open(deviceTmp->pinNum);}}*/}pthread_mutex_unlock(&mutex); //解锁}
}void *write_thread(void *datas) //通知线程,向客服端发送消息
{while (1){delay(500);write(c_fd, Message, 500);}
}void *read_thread(void *datas) //通过socket读取客户端发来的数据
{int n_read;struct Devices *deviceTmp = NULL;while (1){memset(socketHandler->comand, '\0', sizeof(socketHandler->comand));n_read = read(c_fd, socketHandler->comand, sizeof(socketHandler->comand)); //读取客户端发来的数据if (n_read == -1){perror("read_thread");}else if (n_read > 0){printf("APP客户端指令:%s\n", socketHandler->comand);//处理客户端读到的命令if (strstr(socketHandler->comand, "kws") != NULL) //开卧室灯{printf("开卧室灯\n");deviceTmp = findDeviceByName("shui", pdeviceHead); //查找卧室灯设备deviceTmp->deviceInit(deviceTmp->pinNum);          //卧室灯设备初始化deviceTmp->open(deviceTmp->pinNum);                //打开卧室灯}else if (strstr(socketHandler->comand, "gws") != NULL) //关卧室灯{printf("关卧室灯\n");deviceTmp = findDeviceByName("shui", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->close(deviceTmp->pinNum);}else if (strstr(socketHandler->comand, "kys") != NULL) //开浴室灯{printf("开浴室灯\n");deviceTmp = findDeviceByName("yu", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->open(deviceTmp->pinNum);}else if (strstr(socketHandler->comand, "gys") != NULL) //关浴室灯{printf("关浴室灯\n");deviceTmp = findDeviceByName("yu", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->close(deviceTmp->pinNum);}else if (strstr(socketHandler->comand, "kkt") != NULL) //开客厅灯{printf("开客厅灯\n");deviceTmp = findDeviceByName("kt", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->open(deviceTmp->pinNum);}else if (strstr(socketHandler->comand, "gkt") != NULL) //关客厅灯{printf("关客厅灯\n");deviceTmp = findDeviceByName("kt", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->close(deviceTmp->pinNum);}else if (strstr(socketHandler->comand, "kct") != NULL) //开餐厅灯{printf("开餐厅灯\n");deviceTmp = findDeviceByName("chu", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->open(deviceTmp->pinNum);}else if (strstr(socketHandler->comand, "gct") != NULL) //关餐厅灯{printf("关餐厅灯\n");deviceTmp = findDeviceByName("chu", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->close(deviceTmp->pinNum);}else if (strstr(socketHandler->comand, "kyc") != NULL) //开泳池灯{printf("开泳池灯\n");deviceTmp = findDeviceByName("yong", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->open(deviceTmp->pinNum);}else if (strstr(socketHandler->comand, "gyc") != NULL) //关泳池灯{printf("关泳池灯\n");deviceTmp = findDeviceByName("yong", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->close(deviceTmp->pinNum);}else if (strstr(socketHandler->comand, "kfs") != NULL) //开风扇{printf("开电风扇\n");deviceTmp = findDeviceByName("fan", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->open(deviceTmp->pinNum);}else if (strstr(socketHandler->comand, "gfs") != NULL) //关风扇{printf("关电风扇\n");deviceTmp = findDeviceByName("fan", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->close(deviceTmp->pinNum);}else if (strstr(socketHandler->comand, "kqb") != NULL) //开室内全部灯{printf("开室内全部灯\n");deviceTmp = findDeviceByName("chu", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->open(deviceTmp->pinNum);deviceTmp = findDeviceByName("yu", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->open(deviceTmp->pinNum);deviceTmp = findDeviceByName("kt", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->open(deviceTmp->pinNum);deviceTmp = findDeviceByName("shui", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->open(deviceTmp->pinNum);}else if (strstr(socketHandler->comand, "gqb") != NULL) //关室内全部灯{printf("关室内全部灯\n");deviceTmp = findDeviceByName("chu", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->close(deviceTmp->pinNum);deviceTmp = findDeviceByName("yu", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->close(deviceTmp->pinNum);deviceTmp = findDeviceByName("kt", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->close(deviceTmp->pinNum);deviceTmp = findDeviceByName("shui", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->close(deviceTmp->pinNum);}else if (strstr(socketHandler->comand, "ks") != NULL) //开锁{printf("开锁\n");deviceTmp = findDeviceByName("lock", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->open(deviceTmp->pinNum);}else if (strstr(socketHandler->comand, "gs") != NULL) //关锁{deviceTmp = findDeviceByName("lock", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->close(deviceTmp->pinNum);}else if (strstr(socketHandler->comand, "kjk") != NULL) //开监控{printf("打开监控\n");pthread_create(&monitoringThread, NULL, monitoring_thread, NULL); //启动视频监控线程}else if (strstr(socketHandler->comand, "gjk") != NULL) //关监控{printf("关闭监控\n");// pthread_cancel(monitoringThread);system("killall -TERM mjpg_streamer"); //关闭摄像头监控功能}else if (strstr(socketHandler->comand, "khw") != NULL) //打开人体红外检测{printf("启动人体红外检测线程\n");beep = 3; //人体红外使用蜂鸣器deviceTmp = findDeviceByName("buzzser", pdeviceHead);deviceTmp->open(deviceTmp->pinNum);delay(700);deviceTmp->close(deviceTmp->pinNum);if (human_flag == 0){pthread_create(&humanThread, NULL, human_thread, NULL);human_flag = 1;}}else if (strstr(socketHandler->comand, "ghw") != NULL) //关闭人体红外检测{printf("关闭人体红外检测\n");memset(Message[4], 0, sizeof Message[4]); //清空数组sprintf(Message[4], "未启用!");if (human_flag == 1){pthread_cancel(humanThread); //停止人体红外线程human_flag = 0;}beep = 3; //人体红外使用蜂鸣器deviceTmp = findDeviceByName("buzzser", pdeviceHead);deviceTmp->open(deviceTmp->pinNum);delay(400);deviceTmp->close(deviceTmp->pinNum);delay(300);deviceTmp->open(deviceTmp->pinNum);delay(400);deviceTmp->close(deviceTmp->pinNum);}else if (strstr(socketHandler->comand, "gjms") != NULL) //归家模式{printf("开室内全部灯\n");deviceTmp = findDeviceByName("chu", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->open(deviceTmp->pinNum);deviceTmp = findDeviceByName("yu", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->open(deviceTmp->pinNum);deviceTmp = findDeviceByName("kt", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->open(deviceTmp->pinNum);deviceTmp = findDeviceByName("shui", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->open(deviceTmp->pinNum);printf("关闭人体红外检测\n");memset(Message[4], 0, sizeof Message[4]); //清空数组sprintf(Message[4], "未启用!");if (human_flag == 1){pthread_cancel(humanThread); //停止人体红外线程human_flag = 0;}beep = 3; //人体红外使用蜂鸣器deviceTmp = findDeviceByName("buzzser", pdeviceHead);deviceTmp->open(deviceTmp->pinNum);delay(400);deviceTmp->close(deviceTmp->pinNum);delay(300);deviceTmp->open(deviceTmp->pinNum);delay(400);deviceTmp->close(deviceTmp->pinNum);beep = 0;}else if (strstr(socketHandler->comand, "ljms") != NULL) //离家模式{printf("关全部灯\n");deviceTmp = findDeviceByName("chu", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->close(deviceTmp->pinNum);deviceTmp = findDeviceByName("yu", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->close(deviceTmp->pinNum);deviceTmp = findDeviceByName("kt", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->close(deviceTmp->pinNum);deviceTmp = findDeviceByName("shui", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->close(deviceTmp->pinNum);deviceTmp = findDeviceByName("yong", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->close(deviceTmp->pinNum);printf("关电风扇\n");deviceTmp = findDeviceByName("fan", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->close(deviceTmp->pinNum);printf("启动人体红外检测线程\n");beep = 3; //人体红外使用蜂鸣器deviceTmp = findDeviceByName("buzzser", pdeviceHead);deviceTmp->open(deviceTmp->pinNum);delay(700);deviceTmp->close(deviceTmp->pinNum);beep = 0;if (human_flag == 0){pthread_create(&humanThread, NULL, human_thread, NULL);human_flag = 1;}}else if (strstr(socketHandler->comand, "face") != NULL){//启动人脸识别线程printf("进行人脸识别开锁\n");pthread_create(&cameraThread, NULL, cameraThread_func, NULL);}}else{printf("客户端退出\n");// exit(-1); //客户端退出,服务器程序退出pthread_cancel(writeThread); //停止通知线程write_flag = 0;              //通知线程标志位置0,通知线程关闭pthread_exit(NULL);          //退出本线程}}
}void *socket_thread(void *datas) //开启socket服务端,并将socket服务端初始化
{int n_read = 0;pthread_t readPthread;struct sockaddr_in c_addr;memset(&c_addr, 0, sizeof(struct sockaddr_in));int clen = sizeof(struct sockaddr_in);socketHandler = findCommandByName("socketServer", pCommandHead); //在控制工厂找到socketif (socketHandler == NULL){printf("查找套接字处理程序错误\n");pthread_exit(NULL);}else{printf("%s 初始化成功\n", socketHandler->commandName);}socketHandler->Init(socketHandler, NULL, NULL); //初始化socketwhile (1){c_fd = accept(socketHandler->sfd, (struct sockaddr *)&c_addr, &clen);printf("c_fd = %d\n", c_fd);if (write_flag == 0) //通知线程处于关闭状态{write_flag = 1;pthread_create(&writeThread, NULL, write_thread, NULL); //打开通知线程}pthread_create(&readPthread, NULL, read_thread, NULL);}
}void *remote_thread(void *datas) // 433m遥控线程
{int val = 0, val1 = 0, val2 = 0, val3 = 0;int count = 0, count1 = 0, count2 = 0, count3 = 0;int temp = 0, temp1 = 0, temp2 = 0, temp3 = 0;struct Devices *deviceTmp = NULL;wiringPiSetup();pinMode(D0, INPUT);pinMode(D1, INPUT);pinMode(D2, INPUT);pinMode(D3, INPUT);digitalWrite(D0, LOW);digitalWrite(D1, LOW);digitalWrite(D2, LOW);digitalWrite(D3, LOW);printf("遥控模块初始化成功\n");while (1){delay(10);val = digitalRead(D0);val1 = digitalRead(D1);val2 = digitalRead(D2);val3 = digitalRead(D3);if (val == 1) //电风扇遥控信号{if (count == 0) //电风扇遥控按键被按下{if (temp == 0) //打开电风扇{printf("433M:打开电风扇\n");temp = 1;deviceTmp = findDeviceByName("fan", pdeviceHead); //查找电风扇设备deviceTmp->deviceInit(deviceTmp->pinNum);         //电风扇设备初始化deviceTmp->open(deviceTmp->pinNum);               //打开电风扇}else if (temp == 1) //关闭电风扇{printf("433M:关闭电风扇\n");temp = 0;deviceTmp = findDeviceByName("fan", pdeviceHead); //查找电风扇设备deviceTmp->deviceInit(deviceTmp->pinNum);         //电风扇设备初始化deviceTmp->close(deviceTmp->pinNum);              //关闭电风扇}count = 1;}}else if (val == 0){count = 0;}if (val1 == 1) //门锁遥控{if (count1 == 0){if (temp1 == 0){printf("433M:打开门锁\n");temp1 = 1;deviceTmp = findDeviceByName("lock", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->open(deviceTmp->pinNum);delay(3000);deviceTmp->close(deviceTmp->pinNum);}else if (temp1 == 1){printf("433M:关闭门锁\n");temp1 = 0;deviceTmp = findDeviceByName("lock", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->close(deviceTmp->pinNum);}count1 = 1;}}else if (val1 == 0){count1 = 0;}if (val2 == 1) //泳池灯遥控{if (count2 == 0){if (temp2 == 0){printf("433M:开泳池灯\n");temp2 = 1;deviceTmp = findDeviceByName("yong", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->open(deviceTmp->pinNum);}else if (temp2 == 1){printf("433M:关泳池灯\n");temp2 = 0;deviceTmp = findDeviceByName("yong", pdeviceHead);deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->close(deviceTmp->pinNum);}count2 = 1;}}else if (val2 == 0){count2 = 0;}if (val3 == 1) //人体红外线程控制{if (count3 == 0){if (temp3 == 0) // 启动人体红外检测线程{printf("433M:启动人体红外\n");temp3 = 1;printf("启动人体红外检测线程\n");beep = 3; //人体红外使用蜂鸣器deviceTmp = findDeviceByName("buzzser", pdeviceHead);// deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->open(deviceTmp->pinNum);delay(700);deviceTmp->close(deviceTmp->pinNum);beep = 0;if (human_flag == 0){pthread_create(&humanThread, NULL, human_thread, NULL);human_flag = 1;}}else if (temp3 == 1){printf("433M:关闭人体红外\n");memset(Message[4], 0, sizeof Message[4]); //清空数组sprintf(Message[4], "未启用!");temp3 = 0;printf("关闭人体红外检测线程\n");beep = 3; //人体红外使用蜂鸣器// TerminateThread(human_thread, -1 );  // 中止人体红外检测线程// CloseHandle(human_thread); //清楚人体红外线程堆栈// _beginthread(human_thread,0, NULL);if (human_flag == 1){pthread_cancel(humanThread); //停止人体红外线程human_flag = 0;}deviceTmp = findDeviceByName("buzzser", pdeviceHead);// deviceTmp->deviceInit(deviceTmp->pinNum);deviceTmp->open(deviceTmp->pinNum);delay(400);deviceTmp->close(deviceTmp->pinNum);delay(300);deviceTmp->open(deviceTmp->pinNum);delay(400);deviceTmp->close(deviceTmp->pinNum);beep = 0;}count3 = 1;}}else if (val3 == 0){count3 = 0;}}
}//检测按键状态
void *key_thread()
{// pinMode(door_Lock, OUTPUT); //将门锁端口置为输出int val;while (1){val = digitalRead(key);// printf("key = %d\n", val);if (val == 0) //防止重复检测{delay(500);val = digitalRead(key);// printf("key = %d\n", val);if (val == 1) //按键按下,启动人脸识别线程{pthread_create(&cameraThread, NULL, cameraThread_func, NULL);}}}
}int main()
{char name[32] = {'\0'};//树莓派库初始化if (wiringPiSetup() == -1){printf("硬件接口初始化失败\n");return -1;}pthread_mutex_init(&mutex, NULL); //初始化互斥量(锁)// 1、指令工厂初始化pCommandHead = addVoiceContrlToInputCommanderLink(pCommandHead);  //语音识别初始化pCommandHead = addsocketContrlToInputCommanderLink(pCommandHead); //服务器初始化// 2、设备控制工程初始化pdeviceHead = addBathroomLightToDeviceLink(pdeviceHead);   //浴室灯光初始化pdeviceHead = addupStairLightToDeviceLink(pdeviceHead);    //卧室灯光初始化pdeviceHead = addlivingroomLightToDeviceLink(pdeviceHead); //客厅灯光初始化pdeviceHead = addrestaurantLightToDeviceLink(pdeviceHead); //餐厅灯光初始化pdeviceHead = addpoolLightToDeviceLink(pdeviceHead);       //泳池灯光初始化pdeviceHead = addDoorLockToDeviceLink(pdeviceHead);        //门锁初始化pdeviceHead = addFanToDeviceLink(pdeviceHead);             //电风扇初始化pdeviceHead = addBuzzerToDeviceLink(pdeviceHead);          //蜂鸣器初始化pdeviceHead = addFireToDeviceLink(pdeviceHead);            //火焰传感器初始化pdeviceHead = addShakeToDeviceLink(pdeviceHead);           //震动传感器初始化pdeviceHead = addcameraToDeviceLink(pdeviceHead);          //摄像头模块初始化pdeviceHead = addHumanToDeviceLink(pdeviceHead);           //人体感应传感器初始化//参数2:线程属性,一般设置为NULL,参数3:线程干活的函数,参数4:往voice_thread线程里面传送数据。pthread_create(&voiceThread, NULL, voice_thread, NULL);   // 语音线程启动pthread_create(&socketThread, NULL, socket_thread, NULL); // socket服务器线程启动pthread_create(&fireThread, NULL, fire_thread, NULL);     //火灾报警线程启动pthread_create(&shakeThread, NULL, shake_thread, NULL);   //震动报警线程启动pthread_create(&keyThread, NULL, key_thread, NULL);       //按键检测线程启动pthread_create(&remoteThread, NULL, remote_thread, NULL); // 遥控线程启动pthread_create(&dht11Thread, NULL, dht11_thread, NULL);   // 温湿度检测线程启动//等待线程pthread_join(voiceThread, NULL);pthread_join(socketThread, NULL);pthread_join(fireThread, NULL);pthread_join(shakeThread, NULL);pthread_join(humanThread, NULL);pthread_join(keyThread, NULL);pthread_join(remoteThread, NULL);pthread_join(cameraThread, NULL);pthread_join(monitoringThread, NULL);pthread_join(clientWemosThread, NULL);pthread_join(dht11Thread, NULL);pthread_join(writeThread, NULL);pthread_mutex_destroy(&mutex); //销毁互斥量// pthread_cond_destroy(&cond);        //条件的销毁return 0;
}

2.摄像头camera.c

//摄像头
#include "contrlDevices.h"
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <curl/curl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>
#define door_lock 29 //门锁
void postUrl();
size_t readData1(void *ptr, size_t size, size_t nmemb, void *stream);
char *getFace1();
char *getPicFromOCRBase641(char *Filepath);
struct Devices *addcameraToDeviceLink(struct Devices *phead);char ocrRetBuf[1024] = {'\0'}; //全局变量,用来接收从OCR后台返回的数据size_t readData1(void *ptr, size_t size, size_t nmemb, void *stream)
//回调函数,把从后台的数据拷贝给ocrRetBuf
{strncpy(ocrRetBuf, ptr, 1024);
}char *getFace1()
{printf("拍照中...\n");system("raspistill -q 5 -t 1 -o image.jpg"); //-q 是图片质量,在0~100之间,我们调成5,压缩图片质量,生成的照片名字为imag.jpg//-t 是拍照延时,设定1s后拍照while (access("./image.jpg", F_OK) != 0); //判断是否拍照完毕printf("拍照完成\n");char *base64BufFaceRec = getPicFromOCRBase641("./image.jpg");// system("rm image.jpg");return base64BufFaceRec; //返回刚才拍照的base64
}char *getPicFromOCRBase641(char *Filepath)
{int fd;int filelen;char cmd[128] = {'\0'};sprintf(cmd, "base64 %s > tmpFile", Filepath);system(cmd);fd = open("./tmpFile", O_RDWR);filelen = lseek(fd, 0, SEEK_END);lseek(fd, 0, SEEK_SET);char *bufpic = (char *)malloc(filelen + 2);memset(bufpic, '\0', filelen + 2);read(fd, bufpic, filelen + 128);system("rm -rf tmpFile");close(fd);return bufpic;
}void postUrl()
{CURL *curl;CURLcode res;struct Devices *deviceTmp = NULL;//分开定义,然后字符串拼接char *key = "XXXXejLCjAKZUN2D63XXXX";                  //用自己的char *secret = "XXXXXXXXadca46859adXXXX69092XXXXXXXX"; //用自己的int typeId = 21;char *format = "xml";char *base64BufPic1 = getFace1();char *base64BufPic2 = getPicFromOCRBase641("gyf1.jpg");int len = strlen(key) + strlen(secret) + strlen(base64BufPic1) + strlen(base64BufPic2) + 128; //分配空间不够会导致栈溢出char *postString = (char *)malloc(len);memset(postString, '\0', len); //因为postString是一个指针,不能用sizeof来计算其指向的大小sprintf(postString, "img1=%s&img2=%s&key=%s&secret=%s&typeId=%d&format=%s", base64BufPic1, base64BufPic2, key, secret, typeId, format); //根据平台的传参格式编写curl = curl_easy_init();if (curl){curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postString);                   //指定post内容,传入参数curl_easy_setopt(curl, CURLOPT_URL, "https://netocr.com/api/faceliu.do"); // 指定urlcurl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, readData1);                 //回调函数readDate读取返回值res = curl_easy_perform(curl);                                            //类似于状态码printf("OK:%d\n", res);if (strstr(ocrRetBuf, "是") != NULL){ //判断翔云后台返回的字符串中有没有“是”printf("是同一个人\n");digitalWrite(door_lock, LOW);  //打开门锁delay(3000);                   //等待3sdigitalWrite(door_lock, HIGH); //关闭门锁}else{printf("不是同一个人\n");}curl_easy_cleanup(curl);}
}int cameraInit(int pinNum) //该函数无用
{pinMode(pinNum, OUTPUT);digitalWrite(pinNum, HIGH);//  printf("摄像头模块初始化成功\n");
}struct Devices camera = {.deviceName = "camera",.deviceInit = cameraInit,.pinNum = 6, //此引脚号无用.justDoOnce = postUrl,.getFace = getFace1,.getPicFromOCRBase64 = getPicFromOCRBase641,.readData = readData1};struct Devices *addcameraToDeviceLink(struct Devices *phead)
{if (phead == NULL){return &camera;}else{camera.next = phead;phead = &camera;}
}

3.外设设备的头文件contrlDevices.h

//外设设备的头文件
#include <wiringPi.h>
#include <stdio.h>
#include <curl/curl.h>typedef unsigned int bool;struct Devices
{char deviceName[128]; //设备名int status;           //读取到的数据int pinNum;           //引脚号int (*open)(int pinNum);       //打开设备函数指针int (*close)(int pinNum);      //关闭设备函数指针int (*deviceInit)(int pinNum); //设备初始化函数指针int (*readStatus)(int pinNum);  //读取数据函数指针int (*changStatus)(int status); //改变数据函数指针//摄像头相关的void (*justDoOnce)();           //用于摄像头char *(*getFace)();             //用于摄像头char *(*getPicFromOCRBase64)(); //用于摄像头size_t (*readData)();           //用于摄像头CURL *curl;char *key;char *secret;int typeId;char *format;bool (*cameraInit)(struct Devices *camera);int yesNum;int noNum;struct Devices *next;
};//每个设备加到链表函数的声明
struct Devices *addBathroomLightToDeviceLink(struct Devices *phead);
struct Devices *addupStairLightToDeviceLink(struct Devices *phead);
struct Devices *addlivingroomLightToDeviceLink(struct Devices *phead);
struct Devices *addrestaurantLightToDeviceLink(struct Devices *phead);
struct Devices *addpoolLightToDeviceLink(struct Devices *phead);
struct Devices *addDoorLockToDeviceLink(struct Devices *phead);
struct Devices *addFanToDeviceLink(struct Devices *phead);
struct Devices *addFireToDeviceLink(struct Devices *phead);
struct Devices *addShakeToDeviceLink(struct Devices *phead);
struct Devices *addHumanToDeviceLink(struct Devices *phead) ;
struct Devices *addBuzzerToDeviceLink(struct Devices *phead);// struct Devices *addcameraContrlToDeviceLink(struct Devices *phead);
struct Devices *addcameraToDeviceLink(struct Devices *phead);

4.控制的头文件inputCommand.h

//控制的头文件
#include <wiringPi.h>
#include <stdio.h>struct InputCommander
{char commandName[128]; // socket名char deviceName[128];  //串口名char comand[32];       //控制命令int (*Init)(struct InputCommander *voicer, char *ipAdress, char *port); // socket初始化int (*getCommand)(struct InputCommander *voicer);                       //读取数据char log[1024];int fd;char port[12];     //端口号char ipAdress[32]; // IP地址int sfd;int cfd;struct InputCommander *next;
};//每个控制添加到控制链表的函数声明
struct InputCommander *addVoiceContrlToInputCommanderLink(struct InputCommander *phead);
struct InputCommander *addsocketContrlToInputCommanderLink(struct InputCommander *phead);
struct InputCommander *addclientContrlToInputCommanderLink(struct InputCommander *phead);

5.语音模块voiceContrl.c

//语音控制设备
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <wiringSerial.h>
#include <unistd.h>
#include <string.h>#include "inputCommand.h"int getCommand(struct InputCommander *voicer) //获取语音指令
{int nread = 0;memset(voicer->comand, '\0', sizeof(voicer->comand));nread = read(voicer->fd, voicer->comand, sizeof(voicer->comand));return nread;
}
//语音模块初始化
int voiceInit(struct InputCommander *voicer, char *ipAdress, char *port)
{int fd;if ((fd = serialOpen(voicer->deviceName, 9600)) == -1){printf("语音初始化失败\n");exit(-1);}voicer->fd = fd;printf("语音初始化结束\n");return fd;
}
//语音模块结构体
struct InputCommander voiceContrl = {.commandName = "voice",.deviceName = "/dev/ttyAMA0",.comand = {'\0'},.Init = voiceInit,.getCommand = getCommand,.log = {'\0'},.next = NULL};struct InputCommander *addVoiceContrlToInputCommanderLink(struct InputCommander *phead)
{if (phead == NULL){return &voiceContrl;}else{voiceContrl.next = phead;phead = &voiceContrl;}return phead;
}

6.蜂鸣器buzzser.c

//蜂鸣器
#include "contrlDevices.h"int buzzerOpen(int pinNum)
{digitalWrite(pinNum, LOW);
}int buzzerClose(int pinNum)
{digitalWrite(pinNum, HIGH);
}int buzzerInit(int pinNum)
{pinMode(pinNum, OUTPUT);digitalWrite(pinNum, HIGH);printf("蜂鸣器初始化成功\n");
}struct Devices buzzer = {.deviceName = "buzzser",.pinNum = 7,.open = buzzerOpen,.close = buzzerClose,.deviceInit = buzzerInit};struct Devices *addBuzzerToDeviceLink(struct Devices *phead)
{if (phead == NULL){return &buzzer;}else{buzzer.next = phead;phead = &buzzer;}return phead;
}

7.电风扇fan.c

//电风扇
#include "contrlDevices.h"int fanOpen(int pinNum)
{// pinMode(pinNum, OUTPUT);digitalWrite(pinNum, LOW);
}int fanClose(int pinNum)
{// pinMode(pinNum, INPUT);digitalWrite(pinNum, HIGH);
}int fanCloseInit(int pinNum)
{//  pinMode(pinNum, INPUT);pinMode(pinNum, OUTPUT);digitalWrite(pinNum, HIGH);//printf("电风扇初始化成功\n");
}int fanCloseStatus(int status)
{}struct Devices fan = {//.deviceName = "fan",.deviceName = "fan",.pinNum = 26,.open = fanOpen,.close = fanClose,.deviceInit = fanCloseInit,.changStatus = fanCloseStatus};struct Devices *addFanToDeviceLink(struct Devices *phead)
{if (phead == NULL){return &fan;}else{fan.next = phead;phead = &fan;}return phead;
}

8.人体红外检测human.c

//人体红外检测
#include "contrlDevices.h"int humanInit(int pinNum)
{pinMode(pinNum, INPUT);digitalWrite(pinNum, HIGH);// printf("人体感应传感器初始化成功\n");
}int readhumanStatus(int pinNum)
{return digitalRead(pinNum);
}struct Devices human = {.deviceName = "human",.pinNum = 5,.deviceInit = humanInit,.readStatus = readhumanStatus};struct Devices *addHumanToDeviceLink(struct Devices *phead)
{if (phead == NULL){return &human;}else{human.next = phead;phead = &human;}return phead;
}

9.火焰传感器fire.c

//火焰传感器
#include "contrlDevices.h"int fireInit(int pinNum)
{pinMode(pinNum, INPUT);digitalWrite(pinNum, HIGH);
}int readFireStatus(int pinNum)
{return digitalRead(pinNum);
}struct Devices fire = {.deviceName = "fire",.pinNum = 25,.deviceInit = fireInit,.readStatus = readFireStatus};struct Devices *addFireToDeviceLink(struct Devices *phead)
{if (phead == NULL){return &fire;}else{fire.next = phead;phead = &fire;}return phead;
}

10.震动传感器shake.c

//震动传感器
#include "contrlDevices.h"int shakeInit(int pinNum)
{pinMode(pinNum, INPUT);digitalWrite(pinNum, HIGH);
}int readShakeStatus(int pinNum)
{return digitalRead(pinNum);
}struct Devices shake = {.deviceName = "shake",.pinNum = 1,.deviceInit = shakeInit,.readStatus = readShakeStatus};struct Devices *addShakeToDeviceLink(struct Devices *phead)
{if (phead == NULL){return &shake;}else{shake.next = phead;phead = &shake;}return phead;
}

11.服务器socketContrl.c

// socket控制
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <wiringSerial.h>
#include <unistd.h>#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>#include "inputCommand.h"int socketgetCommand(struct InputCommander *socketMes)
{int c_fd;int n_read;struct sockaddr_in c_addr;memset(&c_addr, 0, sizeof(struct sockaddr_in));int clen = sizeof(struct sockaddr_in);// 4.acceptc_fd = accept(socketMes->sfd, (struct sockaddr *)&c_addr, &clen);n_read = read(c_fd, socketMes->comand, sizeof(socketMes->comand));if (n_read == -1){perror("读");}else if (n_read > 0){printf("\n获取:%d\n", n_read);}else{printf("客户端退出\n");}return n_read;
}int socketInit(struct InputCommander *socketMes, char *ipAdress, char *port)
{/*形参虽然定多了,用不上,咱不管*/int s_fd;struct sockaddr_in s_addr;memset(&s_addr, 0, sizeof(struct sockaddr_in));// 1.sockets_fd = socket(AF_INET, SOCK_STREAM, 0);if (s_fd == -1){perror("socket");exit(-1);}s_addr.sin_family = AF_INET;s_addr.sin_port = htons(atoi(socketMes->port));inet_aton(socketMes->ipAdress, &s_addr.sin_addr);//解决服务器程序结束后端口被占用的情况int opt = 1;setsockopt(s_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));// 2.bindbind(s_fd, (struct sockaddr *)&s_addr, sizeof(struct sockaddr_in));// 3.listenlisten(s_fd, 10);printf("套接字服务器侦听......\n");socketMes->sfd = s_fd;return s_fd;
}struct InputCommander socketContrl = {.commandName = "socketServer",.comand = {'\0'},.port = "8083",.ipAdress = "172.26.183.9",//  .ipAdress = "192.168.245.9",.Init = socketInit,.getCommand = socketgetCommand,.log = {'\0'},.next = NULL};struct InputCommander *addsocketContrlToInputCommanderLink(struct InputCommander *phead)
{if (phead == NULL){return &socketContrl;}else{socketContrl.next = phead;phead = &socketContrl;}return phead;
}

12.客服端(wemos做服务器)client_wemos.c

//树莓派作为客服端  连接wemos D1服务器
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <wiringSerial.h>
#include <unistd.h>#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>#include "inputCommand.h"int clientInit(struct InputCommander *client, char *ipAdress, char *port)
{struct sockaddr_in addr;memset(&addr, 0, sizeof(struct sockaddr_in));// 1.socket 创建套接字int s_fd = socket(AF_INET, SOCK_STREAM, 0);if (s_fd == -1){perror("socket");exit(-1);}addr.sin_family = AF_INET;addr.sin_port = htons(atoi(client->port));inet_aton(client->ipAdress, &addr.sin_addr);// 2.connect 连接服务器if (connect(s_fd, (struct sockaddr *)&addr, sizeof(struct sockaddr)) == -1) //这里是连接wemosD1{perror("connect");exit(-1);}printf("客户端 wemosD1 连接....\n");client->sfd = s_fd;return s_fd;
}struct InputCommander clientContrl = {.commandName = "client",     //加入到控制链表的名字.comand = {'\0'},            //命令.port = "8888",              // wemosD1 端口号.ipAdress = "192.168.1.103", // wemosD1 IP地址.Init = clientInit,          //树莓派客户端初始化.next = NULL};struct InputCommander *addclientContrlToInputCommanderLink(struct InputCommander *phead)
{if (phead == NULL){return &clientContrl;}else{clientContrl.next = phead;phead = &clientContrl;}return phead;
}

13.浴室灯bathroomLight.c

//浴室灯
#include "contrlDevices.h"int bathroomLightOpen(int pinNum)
{digitalWrite(pinNum, LOW);
}int bathroomLightClose(int pinNum)
{digitalWrite(pinNum, HIGH);
}int bathroomLightCloseInit(int pinNum)
{pinMode(pinNum, OUTPUT);digitalWrite(pinNum, HIGH);//  printf("浴室灯初始化成功\n");
}int bathroomLightCloseStatus(int status)
{}struct Devices bathroomLight = {//.deviceName = "bathroomLight",.deviceName = "yu",.pinNum = 24,.open = bathroomLightOpen,.close = bathroomLightClose,.deviceInit = bathroomLightCloseInit,.changStatus = bathroomLightCloseStatus};struct Devices *addBathroomLightToDeviceLink(struct Devices *phead)
{if (phead == NULL){return &bathroomLight;}else{bathroomLight.next = phead;phead = &bathroomLight;}return phead;
}

14.门锁doorLock.c

//门锁
#include "contrlDevices.h"int doorLockOpen(int pinNum)
{digitalWrite(pinNum, LOW);
}int doorLockClose(int pinNum)
{digitalWrite(pinNum, HIGH);
}int doorLockCloseInit(int pinNum)
{pinMode(pinNum, OUTPUT);digitalWrite(pinNum, HIGH);// printf("门锁初始化成功\n");
}int doorLockCloseStatus(int status)
{}struct Devices doorLock = {//.deviceName = "doorLock",.deviceName = "lock",.pinNum = 29,.open = doorLockOpen,.close = doorLockClose,.deviceInit = doorLockCloseInit,.changStatus = doorLockCloseStatus};struct Devices *addDoorLockToDeviceLink(struct Devices *phead)
{if (phead == NULL){return &doorLock;}else{doorLock.next = phead;phead = &doorLock;}return phead;
}

15.卧室灯livingroomLigth.c

//卧室灯
#include "contrlDevices.h"int livingroomLightOpen(int pinNum)
{digitalWrite(pinNum, LOW);
}int livingroomLightClose(int pinNum)
{digitalWrite(pinNum, HIGH);
}int livingroomLightCloseInit(int pinNum)
{pinMode(pinNum, OUTPUT);digitalWrite(pinNum, HIGH);//  printf("卧室灯初始化成功\n");
}int livingroomLightCloseStatus(int status)
{}struct Devices livingroomLight = {//.deviceName = "livingroomLight",.deviceName = "shui",.pinNum = 21,.open = livingroomLightOpen,.close = livingroomLightClose,.deviceInit = livingroomLightCloseInit,.changStatus = livingroomLightCloseStatus};struct Devices *addlivingroomLightToDeviceLink(struct Devices *phead)
{if (phead == NULL){return &livingroomLight;}else{livingroomLight.next = phead;phead = &livingroomLight;}return phead;
}

16.泳池灯poolLight.c

//泳池灯
#include "contrlDevices.h"int poolLightOpen(int pinNum)
{digitalWrite(pinNum, LOW);
}int poolLightClose(int pinNum)
{digitalWrite(pinNum, HIGH);
}int poolLightCloseInit(int pinNum)
{pinMode(pinNum, OUTPUT);digitalWrite(pinNum, HIGH);// printf("泳池灯初始化成功\n");
}int poolLightCloseStatus(int status)
{}struct Devices poolLight = {//.deviceName = "poolLight",.deviceName = "yong",.pinNum = 27,.open = poolLightOpen,.close = poolLightClose,.deviceInit = poolLightCloseInit,.changStatus = poolLightCloseStatus};struct Devices *addpoolLightToDeviceLink(struct Devices *phead)
{if (phead == NULL){return &poolLight;}else{poolLight.next = phead;phead = &poolLight;}return phead;
}

17.餐厅灯restaurantLight.c

//餐厅灯
#include "contrlDevices.h"int restaurantLightOpen(int pinNum)
{digitalWrite(pinNum, LOW);
}int restaurantLightClose(int pinNum)
{digitalWrite(pinNum, HIGH);
}int restaurantLightCloseInit(int pinNum)
{pinMode(pinNum, OUTPUT);digitalWrite(pinNum, HIGH);//  printf("餐厅灯初始化成功\n");
}int restaurantLightCloseStatus(int status)
{}struct Devices restaurantLight = {//.deviceName = "restaurantLight",.deviceName = "chu",.pinNum = 23,.open = restaurantLightOpen,.close = restaurantLightClose,.deviceInit = restaurantLightCloseInit,.changStatus = restaurantLightCloseStatus};struct Devices *addrestaurantLightToDeviceLink(struct Devices *phead)
{if (phead == NULL){return &restaurantLight;}else{restaurantLight.next = phead;phead = &restaurantLight;}return phead;
}

18.客厅灯upstairLight.c

//客厅灯
#include <stdlib.h>
#include <fcntl.h>
#include "contrlDevices.h"int upstairLightOpen(int pinNum) //打开客厅灯函数
{digitalWrite(pinNum, LOW);
}int upstairLightClose(int pinNum) //关闭客厅灯函数
{digitalWrite(pinNum, HIGH);
}int upstairLightCloseInit(int pinNum) //初始化函数
{pinMode(pinNum, OUTPUT);    //配置引脚为输出引脚digitalWrite(pinNum, HIGH); //引脚输出高电平,即默认为关闭状态// printf("客厅灯初始化成功\n");
}int upstairLightCloseStatus(int status)
{}struct Devices upstairLight = { //客厅灯设备链表节点.deviceName = "kt",.pinNum = 22,.open = upstairLightOpen,.close = upstairLightClose,.deviceInit = upstairLightCloseInit,.changStatus = upstairLightCloseStatus};struct Devices *addupStairLightToDeviceLink(struct Devices *phead) //头插法将设备节点加入设备工厂链表函数
{if (phead == NULL){return &upstairLight;}else{upstairLight.next = phead;phead = &upstairLight;}
}

19.视频监控脚本start_web_video.sh

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

基于树莓派的智能家居控制系统设计相关推荐

  1. 基于树莓派的智能家居控制系统设计论文参考

    完整论文咨询可WX联系:gyf1842965496 智能家居控制系统功能实现详细介绍:基于树莓派的智能家居控制系统设计https://blog.csdn.net/G1842965496/article ...

  2. 基于ANDROID的智能家居控制系统设计

    pe基于Android的智能家居控制系统设计 配套论文

  3. android+智能家居控制系统,基于Android的智能家居控制系统设计与实现

    摘要: 随着科技的发展和人民生活水平的提高,智能家居系统逐渐走入大众视野,越来越受到人们的青睐.目前市面上智能家居产品也越来越多样化,基于Android的智能家居控制系统是目前的研究热点之一,本论文以 ...

  4. 基于STM32的智能家居控制系统设计与实现(带红外遥控控制空调)

    1. 前言 智能家居作为家庭信息化的实现方式,已经成为社会信息化发展的重要组成部分,物联网因其巨大的应用前景,将是智能家居产业发展过程中一个比较现实的突破口,对智能家居的产业发展具有重大意义. 本文基 ...

  5. 写基于stm32智能家居控制系统设计论文

    写作一篇基于STM32的智能家居控制系统设计论文需要涵盖以下内容: 简要介绍STM32微控制器的硬件特性和软件开发环境: 分析当前市场上关于智能家居控制系统的发展现状,并阐述为什么选择STM32作为系 ...

  6. 基于STM32设计的智能家居控制系统设计_语音+环境检测(OneNet)_2022

    1. 前言 如今的智能家居越来越深入人心,越来越受人们的欢迎.智能家居作为与人们日常生活息息相关的科技智能产物,可以给我们的生活带来舒适便利.随着物联网.云计算.无线通信等新技术的发展,智能家居得到了 ...

  7. 基于STM32物联网WiFi智能家居控制系统设计(原理图+源代码+系统资料)

    基于STM32物联网WiFi智能家居控制系统设计(原理图+源代码+系统资料) 原理图:Altium Designer 程序编译器:keil 5 编程语言:C语言 设计编号:C0053 主要功能: 1. ...

  8. ChatGPT直出1.5w字论文查重率才30% - 基于物联网技术的智能家居控制系统设计与实现

    文章目录 ChatGPT直出1.5w字论文查重率才30% - 基于物联网技术的智能家居控制系统设计与实现 一.绪论 1.1 研究背景与意义 1.2 国内外研究现状分析 1.3 研究内容与目标 1.4 ...

  9. python语音控制智能家电_基于树莓派的智能家居语音控制系统

    2018-10 基于树莓派的智能家居语音控制系统 Intelligent home voice control system based on raspberry Pi 刘 华 , 田占生 , 冯宇飞 ...

最新文章

  1. pytorch中load和load_state_dict区别
  2. 移植uboot第十步:制作uboot补丁
  3. 嵌入式linux 分区挂载,嵌入式linux系统的开发——文件系统的分区和挂载
  4. 红帽启动apache服务器_redhat 启动ssh服务器
  5. alert回调_你知道javascript函数的回调怎么用吗?
  6. CTFHUB 《请求方式》 http请求,curl命令总结
  7. 蚂蚁金服 Service Mesh 落地实践与挑战|成都Service Mesh沙龙预告
  8. html中只能上传文件word,HTML文件表单,接受Word文档(HTML file form, accept Word documents)...
  9. Java从入门到精通07-排序
  10. 什么是计算机嵌套分类汇总,excel嵌套分类汇总 Excel表格中创建嵌套分类汇总和查看嵌套分类汇总明细的方法...
  11. Unity3D之Json序列化
  12. Markdown stackoverflow 增加中划线
  13. java swing开发打飞机的小游戏源代码下载
  14. 高德地图语音助手实测:驾车导航基本实现动口不动手
  15. android应用上架到各大应用市场总结
  16. 计算机一级怎么发送邮件到别人邮箱,电脑怎样发邮件到指定的邮箱地址(小白必须电脑操作邮件收发)...
  17. 微信小程序 模拟支付宝账单的个人记账小程序
  18. 关于地理数据收集与处理的基本工具推荐(2)---10m精度的全球土地覆盖数据下载
  19. ZBrush教程(一):常用笔刷安装及运用
  20. 【转】Chrome浏览器截全屏

热门文章

  1. matlab 自由度,matlab二自由度系统振动
  2. springboot启动报错org.yaml.snakeyaml.error.YAMLException...
  3. The Genome Reference Consortium Human Genome Build 37 now Available(GRCh37)
  4. 了解python语言的开发者_Python开发者
  5. 基于ant-design-vue的简易农历日历
  6. YOLOv2论文翻译详解
  7. Access denied for user ‘‘@‘localhost‘ (using password: NO)
  8. 教资面试计算机硬件真题,初中信息技术教资面试真题:保存图文-复制并保存文字...
  9. 国家级专新特精“小巨人”「皖仪科技」携手企企通,打造采购数字化平台成功上线
  10. android+6.0中兴v5s,中兴V5S的手机系统是什么?中兴V5S能升级安卓4.3吗?