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