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

这个项目基于树莓派构建了一个智能家居系统,包括温湿度监测、火灾与震动报警、人体红外检测等功能。系统利用多线程处理各个传感器数据,如温湿度线程、火灾线程、震动线程和人体红外线程。此外,还有视频监控和语音线程。通过设备管理和控制,实现了设备的开关和状态检测,如灯光、门锁和风扇的控制。
摘要由CSDN通过智能技术生成

struct InputCommander *socketHandler = NULL;

pthread_mutex_t mutex; //定义互斥量(锁)

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

/* [0]:温度 [1]:湿度 [2]:火灾监控 [3]:震动监控 [4]:人体红外监控*/
char Message[5][100] = {“温度未启用!”, “湿度未启用!”, “火灾未启用!”, “震动未启用!”, “红外未启用!”};
int write_flag = 0; //标记位,标记通知线程是否打开 1:打开 0:关闭

int infrared_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)
{ 
    printf("find camera error\n");
    pthread_exit(NULL); 
}

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) 
{
    while (!digitalRead(6)); 

    for (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;

if (-1 == wiringPiSetup())
{
    printf("获取wiringpi库失败!");
}
printf("温湿度线程开启\n");
pinMode(6, OUTPUT);    
digitalWrite(6, HIGH); 

printf("温湿度开始读取-------\n");
while (1)
{
    pinMode(6, OUTPUT);    
    digitalWrite(6, HIGH); 
    delay(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]);
            sprintf(Message[0], "%d.%d C", W, w); //温度
            sprintf(Message[1], "%d.%d", S, s);   //湿度
        }

        databuf = 0;
    }
    else
    {
        printf("温湿度检测失败!\n");
        databuf = 0;
    }

}

}

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 *vibrate_thread(void *datas) //震动线程
{
char msg[100];
int status;
struct Devices *vibrateDeviceTmp = NULL;
struct Devices *buzzerDeviceTmp = NULL;

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

vibrateDeviceTmp->deviceInit(vibrateDeviceTmp->pinNum); //震动模块初始化
buzzerDeviceTmp->deviceInit(buzzerDeviceTmp->pinNum);
printf("震动线程初始化成功\n");
while (1)
{
    delay(5);
    status = vibrateDeviceTmp->readStatus(vibrateDeviceTmp->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 *infrared_thread(void *datas) // 人体红外检测线程
{
char msg[100];
int status;
struct Devices *infraredDeviceTmp = NULL;
struct Devices *buzzerDeviceTmp = NULL;

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(voiceHan
  • 17
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值