linux项目—基于树莓派的智能家居系统

infraredDeviceTmp = findDeviceByName("infrared", pdeviceHead); //在设备工厂找到火灾模块
buzzerDeviceTmp = findDeviceByName("buzzser", pdeviceHead);

infraredDeviceTmp->deviceInit(infraredDeviceTmp->pinNum); //红外模块初始化
buzzerDeviceTmp->deviceInit(buzzerDeviceTmp->pinNum);
printf("人体红外检测初始化\n");

while (1)
{
    delay(10);
    status = infraredDeviceTmp->readStatus(infraredDeviceTmp->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("livingroomLight", pdeviceHead); //查找卧室灯设备
            deviceTmp->deviceInit(deviceTmp->pinNum);          //卧室灯设备初始化
            deviceTmp->open(deviceTmp->pinNum);                //打开卧室灯
        }
        else if (strstr(voiceHandler->comand, "gwsd") != NULL)
        {
            printf("关闭卧室灯光\n");
            deviceTmp = findDeviceByName("livingroomLight", pdeviceHead);
            deviceTmp->deviceInit(deviceTmp->pinNum);
            deviceTmp->close(deviceTmp->pinNum);
        }
        else if (strstr(voiceHandler->comand, "kysd") != NULL)
        {
            printf("打开浴室灯光\n");
            deviceTmp = findDeviceByName("bathroomLight", pdeviceHead);
            deviceTmp->deviceInit(deviceTmp->pinNum);
            deviceTmp->open(deviceTmp->pinNum);
        }
        else if (strstr(voiceHandler->comand, "gysd") != NULL)
        {
            printf("关闭浴室灯光\n");
            deviceTmp = findDeviceByName("bathroomLight", pdeviceHead);
            deviceTmp->deviceInit(deviceTmp->pinNum);
            deviceTmp->close(deviceTmp->pinNum);
        }
        else if (strstr(voiceHandler->comand, "kktd") != NULL)
        {
            printf("打开客厅灯光\n");
            deviceTmp = findDeviceByName("upstairLight", pdeviceHead);
            deviceTmp->deviceInit(deviceTmp->pinNum);
            deviceTmp->open(deviceTmp->pinNum);
        }
        else if (strstr(voiceHandler->comand, "gktd") != NULL)
        {
            printf("关闭客厅灯光\n");
            deviceTmp = findDeviceByName("upstairLight", pdeviceHead);
            deviceTmp->deviceInit(deviceTmp->pinNum);
            deviceTmp->close(deviceTmp->pinNum);
        }
        else if (strstr(voiceHandler->comand, "kctd") != NULL)
        {
            printf("打开餐厅灯光\n");
            deviceTmp = findDeviceByName("restaurantLight", pdeviceHead);
            deviceTmp->deviceInit(deviceTmp->pinNum);
            deviceTmp->open(deviceTmp->pinNum);
        }
        else if (strstr(voiceHandler->comand, "gctd") != NULL)
        {
            printf("关闭餐厅灯光\n");
            deviceTmp = findDeviceByName("restaurantLight", 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("bathroomLight", pdeviceHead);
            deviceTmp->deviceInit(deviceTmp->pinNum);
            deviceTmp->close(deviceTmp->pinNum); //关闭浴室灯
            deviceTmp = findDeviceByName("upstairLight", pdeviceHead);
            deviceTmp->deviceInit(deviceTmp->pinNum);
            deviceTmp->close(deviceTmp->pinNum); //关闭客厅灯
            deviceTmp = findDeviceByName("livingroomLight", pdeviceHead);
            deviceTmp->deviceInit(deviceTmp->pinNum);
            deviceTmp->close(deviceTmp->pinNum); //关闭卧室灯
        }
        else if (strstr(voiceHandler->comand, "kqbd") != NULL)
        {
            printf("打开所有灯光\n");
            deviceTmp = findDeviceByName("restaurantLight", pdeviceHead);
            deviceTmp->deviceInit(deviceTmp->pinNum);
            deviceTmp->open(deviceTmp->pinNum);
            deviceTmp = findDeviceByName("bathroomLight", pdeviceHead);
            deviceTmp->deviceInit(deviceTmp->pinNum);
            deviceTmp->open(deviceTmp->pinNum);
            deviceTmp = findDeviceByName("upstairLight", pdeviceHead);
            deviceTmp->deviceInit(deviceTmp->pinNum);
            deviceTmp->open(deviceTmp->pinNum);
            deviceTmp = findDeviceByName("livingroomLight", 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);
        }

    }

    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("客户端指令:%s\n", socketHandler->comand);
        //处理客户端读到的命令
        if (strstr(socketHandler->comand, "kws") != NULL) //开卧室灯
        {
            printf("开卧室灯\n");
            deviceTmp = findDeviceByName("livingroomLight", pdeviceHead); //查找卧室灯设备
            deviceTmp->deviceInit(deviceTmp->pinNum);          //卧室灯设备初始化
            deviceTmp->open(deviceTmp->pinNum);                //打开卧室灯
        }
        else if (strstr(socketHandler->comand, "gws") != NULL) //关卧室灯
        {
            printf("关卧室灯\n");
            deviceTmp = findDeviceByName("livingroomLight", pdeviceHead);
            deviceTmp->deviceInit(deviceTmp->pinNum);
            deviceTmp->close(deviceTmp->pinNum);
        }
        else if (strstr(socketHandler->comand, "kys") != NULL) //开浴室灯
        {
            printf("开浴室灯\n");
            deviceTmp = findDeviceByName("bathroomLight", pdeviceHead);
            deviceTmp->deviceInit(deviceTmp->pinNum);
            deviceTmp->open(deviceTmp->pinNum);
        }
        else if (strstr(socketHandler->comand, "gys") != NULL) //关浴室灯
        {
            printf("关浴室灯\n");
            deviceTmp = findDeviceByName("bathroomLight", pdeviceHead);
            deviceTmp->deviceInit(deviceTmp->pinNum);
            deviceTmp->close(deviceTmp->pinNum);
        }
        else if (strstr(socketHandler->comand, "kkt") != NULL) //开客厅灯
        {
            printf("开客厅灯\n");
            deviceTmp = findDeviceByName("upstairLight", pdeviceHead);
            deviceTmp->deviceInit(deviceTmp->pinNum);
            deviceTmp->open(deviceTmp->pinNum);
        }
        else if (strstr(socketHandler->comand, "gkt") != NULL) //关客厅灯
        {
            printf("关客厅灯\n");
            deviceTmp = findDeviceByName("upstairLight", 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("restaurantLight", 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("restaurantLight", pdeviceHead);
            deviceTmp->deviceInit(deviceTmp->pinNum);
            deviceTmp->open(deviceTmp->pinNum);
            deviceTmp = findDeviceByName("bathroomLight", pdeviceHead);
            deviceTmp->deviceInit(deviceTmp->pinNum);
            deviceTmp->open(deviceTmp->pinNum);
            deviceTmp = findDeviceByName("upstairLight", pdeviceHead);
            deviceTmp->deviceInit(deviceTmp->pinNum);
            deviceTmp->open(deviceTmp->pinNum);
            deviceTmp = findDeviceByName("livingroomLight", pdeviceHead);
            deviceTmp->deviceInit(deviceTmp->pinNum);
            deviceTmp->open(deviceTmp->pinNum);
        }
        else if (strstr(socketHandler->comand, "gqb") != NULL) //关室内全部灯
        {
            printf("关室内全部灯\n");
            deviceTmp = findDeviceByName("restaurantLight", pdeviceHead);
            deviceTmp->deviceInit(deviceTmp->pinNum);
            deviceTmp->close(deviceTmp->pinNum);
            deviceTmp = findDeviceByName("bathroomLight", pdeviceHead);
            deviceTmp->deviceInit(deviceTmp->pinNum);
            deviceTmp->close(deviceTmp->pinNum);
            deviceTmp = findDeviceByName("upstairLight", pdeviceHead);
            deviceTmp->deviceInit(deviceTmp->pinNum);
            deviceTmp->close(deviceTmp->pinNum);
            deviceTmp = findDeviceByName("livingroomLight", 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 (infrared_flag == 0)
            {
                pthread_create(&infraredThread, NULL, infrared_thread, NULL);
                infrared_flag = 1;
            }
        }
        else if (strstr(socketHandler->comand, "ghw") != NULL) //关闭人体红外检测
        {
            printf("关闭人体红外检测\n");
            memset(Message[4], 0, sizeof Message[4]); //清空数组
            sprintf(Message[4], "未启用!");

            if (infrared_flag == 1)
            {
                pthread_cancel(infraredThread); //停止人体红外线程
                infrared_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("restaurantLight", pdeviceHead);
            deviceTmp->deviceInit(deviceTmp->pinNum);
            deviceTmp->open(deviceTmp->pinNum);
            deviceTmp = findDeviceByName("bathroomLight", pdeviceHead);
            deviceTmp->deviceInit(deviceTmp->pinNum);
            deviceTmp->open(deviceTmp->pinNum);
            deviceTmp = findDeviceByName("upstairLight", pdeviceHead);
            deviceTmp->deviceInit(deviceTmp->pinNum);
            deviceTmp->open(deviceTmp->pinNum);
            deviceTmp = findDeviceByName("livingroomLight", pdeviceHead);
            deviceTmp->deviceInit(deviceTmp->pinNum);
            deviceTmp->open(deviceTmp->pinNum);

            printf("关闭人体红外检测\n");
            memset(Message[4], 0, sizeof Message[4]); //清空数组
            sprintf(Message[4], "未启用!");

            if (infrared_flag == 1)
            {
                pthread_cancel(infraredThread); //停止人体红外线程
                infrared_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("restaurantLight", pdeviceHead);
            deviceTmp->deviceInit(deviceTmp->pinNum);
            deviceTmp->close(deviceTmp->pinNum);
            deviceTmp = findDeviceByName("bathroomLight", pdeviceHead);
            deviceTmp->deviceInit(deviceTmp->pinNum);
            deviceTmp->close(deviceTmp->pinNum);
            deviceTmp = findDeviceByName("upstairLight", pdeviceHead);
            deviceTmp->deviceInit(deviceTmp->pinNum);
            deviceTmp->close(deviceTmp->pinNum);
            deviceTmp = findDeviceByName("livingroomLight", 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 (infrared_flag == 0)
            {
                pthread_create(&infraredThread, NULL, infrared_thread, NULL);
                infrared_flag = 1;
            }
        }
        else if (strstr(socketHandler->comand, "face") != NULL)
        {
            //启动人脸识别线程
            printf("进行人脸识别开锁\n");
            pthread_create(&cameraThread, NULL, cameraThread_func, NULL);
        }
    }
    else
    {
        printf("客户端退出\n");
        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); //在控制工厂找到socket
if (socketHandler == NULL)
{
    printf("查找套接字处理程序错误\n");
    pthread_exit(NULL);
}
else
{
    printf("%s 初始化成功\n", socketHandler->commandName);
}

socketHandler->Init(socketHandler, NULL, NULL); //初始化socket

while (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 *key_thread()
{
int val;
while (1)
{
val = digitalRead(key);//获取key引脚状态给val
if (val == 0) //防止重复检测
{
delay(500);
val = digitalRead(key);
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 = addDoorLockToDeviceLink(pdeviceHead);        //门锁初始化
pdeviceHead = addFanToDeviceLink(pdeviceHead);             //电风扇初始化
pdeviceHead = addBuzzerToDeviceLink(pdeviceHead);          //蜂鸣器初始化
pdeviceHead = addFireToDeviceLink(pdeviceHead);            //火焰传感器初始化
pdeviceHead = addVibrateToDeviceLink(pdeviceHead);           //震动传感器初始化
pdeviceHead = addcameraToDeviceLink(pdeviceHead);          //摄像头模块初始化
pdeviceHead = addInfraredToDeviceLink(pdeviceHead);           //人体感应传感器初始化

pthread_create(&voiceThread, NULL, voice_thread, NULL);   // 语音线程启动
pthread_create(&socketThread, NULL, socket_thread, NULL); // socket服务器线程启动
pthread_create(&fireThread, NULL, fire_thread, NULL);     //火灾报警线程启动
pthread_create(&vibrateThread, NULL, vibrate_thread, NULL);   //震动报警线程启动
pthread_create(&keyThread, NULL, key_thread, NULL);       //按键检测线程启动
pthread_create(&dht11Thread, NULL, dht11_thread, NULL);   // 温湿度检测线程启动

pthread_join(voiceThread, NULL);
pthread_join(socketThread, NULL);
pthread_join(fireThread, NULL);
pthread_join(vibrateThread, NULL);
pthread_join(infraredThread, NULL);
pthread_join(keyThread, 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); //销毁互斥量

return 0;

}


#### bathroomLight.c(浴室灯)



#include “contrlDevices.h”

int bathroomLightOpen(int pinNum)
{
digitalWrite(pinNum, HIGH);
}

int bathroomLightClose(int pinNum)
{
digitalWrite(pinNum, LOW);
}

int bathroomLightCloseInit(int pinNum)
{
pinMode(pinNum, OUTPUT);
digitalWrite(pinNum, LOW);
// printf(“浴室灯初始化成功\n”);
}

int bathroomLightCloseStatus(int status)
{
}

struct Devices bathroomLight = {
.deviceName = “bathroomLight”,
.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;
}


#### buzzer.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);
}

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;
}


#### 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’}; //全局变量,用来接收对比两张照片结果的数据

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);//拼凑字符 执行base64+照片名 结果存放在tmpFile
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);              //读到bufpic中
system("rm -rf tmpFile"); 
close(fd);

return bufpic;                                //返回读的内容

}

void postUrl()
{
CURL *curl;
CURLcode res;

struct Devices *deviceTmp = NULL;

//分开定义,然后字符串拼接
char *key = "*******************";                  
char *secret = "********************"; 
int typeId = 21;
char *format = "xml";

char *base64BufPic1 = getFace1();                      //摄像头拍的  
char *base64BufPic2 = getPicFromOCRBase641("nb.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"); // 指定url
    curl_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);                   //等待3s
        digitalWrite(door_lock, HIGH); //关闭门锁
    }
    else
    {
        printf("不是同一个人\n");
    }
    curl_easy_cleanup(curl);
}

}

struct Devices camera = {

.deviceName = "camera",
.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;
}
}


#### doorLock.c(门锁)



#include “contrlDevices.h”

int doorLockOpen(int pinNum)
{
//digitalWrite(pinNum, LOW);
for(int i=0;i<20;i++)
{
digitalWrite(pinNum,HIGH);
delay(2);
digitalWrite(pinNum,LOW);
delay(18);
}
pinMode(pinNum,INPUT);
pinMode(pinNum,OUTPUT);
}

int doorLockClose(int pinNum)
{
digitalWrite(pinNum, HIGH);
}

int doorLockCloseInit(int pinNum)
{
pinMode(pinNum, OUTPUT);
digitalWrite(pinNum, HIGH);
}

int doorLockCloseStatus(int status)
{
}
//门锁
struct Devices doorLock = {
.deviceName = “lock”,
.pinNum = 28,
.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;
}


#### fan.c(风扇)



#include “contrlDevices.h”

int fanOpen(int pinNum)
{
pinMode(pinNum, OUTPUT);
}

int fanClose(int pinNum)
{
pinMode(pinNum, INPUT);
}

int fanCloseInit(int pinNum) //风扇有问题 设为输出就转 输入就停 高低电平影响不了
{
pinMode(pinNum, INPUT);
printf(“电风扇初始化成功\n”);
}

int fanCloseStatus(int status)
{
}

struct Devices 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;
}


#### 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;

}


#### infrared.c(人体红外)



#include “contrlDevices.h”

int infraredInit(int pinNum)
{
pinMode(pinNum, INPUT);
digitalWrite(pinNum, HIGH);
// printf(“人体感应传感器初始化成功\n”);
}

int readinfraredStatus(int pinNum)
{
return digitalRead(pinNum);
}

struct Devices infrared = {
.deviceName = “infrared”,
.pinNum = 5,
.deviceInit = infraredInit,
.readStatus = readinfraredStatus};

struct Devices *addInfraredToDeviceLink(struct Devices *phead)
{
if (phead == NULL)
{
return &infrared;
}
else
{
infrared.next = phead;
phead = &infrared;
}
return phead;
}


#### livingroomLight.c(卧室灯)



#include “contrlDevices.h”

int livingroomLightOpen(int pinNum)
{
digitalWrite(pinNum, HIGH);
}

int livingroomLightClose(int pinNum)
{
digitalWrite(pinNum, LOW);
}

int livingroomLightCloseInit(int pinNum)
{
pinMode(pinNum, OUTPUT);
digitalWrite(pinNum, LOW);
}

int livingroomLightCloseStatus(int status)
{
}

struct Devices livingroomLight = {
.deviceName = “livingroomLight”,
.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;
}


#### restaurantLight.c(餐厅灯)



#include “contrlDevices.h”

int restaurantLightOpen(int pinNum)
{
digitalWrite(pinNum, HIGH);
}

int restaurantLightClose(int pinNum)
{
digitalWrite(pinNum, LOW);
}

int restaurantLightCloseInit(int pinNum)
{
pinMode(pinNum, OUTPUT);
digitalWrite(pinNum, LOW);
}

int restaurantLightCloseStatus(int status)
{
}

struct Devices restaurantLight = {
.deviceName = “restaurantLight”,
.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;
}


#### 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.accept
c_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.socket
s_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.bind
bind(s_fd, (struct sockaddr *)&s_addr, sizeof(struct sockaddr_in));

// 3.listen
listen(s_fd, 10);
printf("套接字服务器侦听......\n");
socketMes->sfd = s_fd;

return s_fd;

}

struct InputCommander socketContrl = {
.commandName = “socketServer”,
.comand = {‘\0’},
.port = “8088”,
.ipAdress = “192.168.137.167”,
.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;

}


#### upstairLight.c(客厅灯)



#include <stdlib.h>
#include <fcntl.h>
#include “contrlDevices.h”

int upstairLightOpen(int pinNum) //打开客厅灯函数
{
digitalWrite(pinNum, HIGH);
}

int upstairLightClose(int pinNum) //关闭客厅灯函数
{
digitalWrite(pinNum, LOW);
}

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年嵌入式&物联网开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上嵌入式&物联网开发知识点,真正体系化!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新!!

ontrlToInputCommanderLink(struct InputCommander *phead)
{
if (phead == NULL)
{
return &socketContrl;
}
else
{
socketContrl.next = phead;
phead = &socketContrl;
}

return phead;

}


#### upstairLight.c(客厅灯)



#include <stdlib.h>
#include <fcntl.h>
#include “contrlDevices.h”

int upstairLightOpen(int pinNum) //打开客厅灯函数
{
digitalWrite(pinNum, HIGH);
}

int upstairLightClose(int pinNum) //关闭客厅灯函数
{
digitalWrite(pinNum, LOW);
}

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年嵌入式&物联网开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

[外链图片转存中…(img-hrQyRPDl-1715586186714)]

[外链图片转存中…(img-F7Tgm4Ib-1715586186715)]

[外链图片转存中…(img-MhOySQLJ-1715586186715)]

[外链图片转存中…(img-Oy9YLkKI-1715586186716)]

[外链图片转存中…(img-RUp2vfGU-1715586186716)]

[外链图片转存中…(img-TcMM0IVv-1715586186717)]

[外链图片转存中…(img-KPC3eGQM-1715586186718)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上嵌入式&物联网开发知识点,真正体系化!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新!!

  • 15
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值