一、项目要求
检测农场环境指标:
1.温度
2.湿度
3.氧气浓度
4.本地实时显示
5.根据环境指标动态调控
6.远程调控--网络发送
二、图例
三、代码
.h
#ifndef __TYPE_H__
#define __TYPE_H__
#include <time.h>
#include <pthread.h>
typedef struct env
{
int devid;
float tmp;
float hum;
float oxy;
struct tm tim;
pthread_mutex_t mutex;
}Env_data_t;
#endif
#ifndef __TASK_H__
#define __TASK_H__
#include <pthread.h>
typedef void* (*Thread_fun_t)(void *);
typedef struct task
{
pthread_t tid;
Thread_fun_t pfun;
}Task_t;
extern int create_pthread_tasks(Task_t tasks[], int len);
extern void destroy_pthread_tasks(Task_t tasks[], int len);
#endif
.c
#include "tasks.h"
#include <stdio.h>
int create_pthread_tasks(Task_t tasks[], int len)
{
for (int i = 0; i < len; i++)
{
int ret = pthread_create(&(tasks[i].tid), NULL, tasks[i].pfun, NULL);
if (ret != 0)
{
printf("fail pthread_create\n");
return -1;
}
}
return 0;
}
void destroy_pthread_tasks(Task_t tasks[], int len)
{
for (int i = 0; i < len; i++)
{
pthread_join(tasks[i].tid, NULL);
}
}
main.c
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "tasks.h"
#include "type.h"
Env_data_t data_g;
void *get_data(void *arg)
{
time_t sec;
int cnt = 0;
while (1)
{
pthread_mutex_lock(&data_g.mutex);
sec = time(NULL);
struct tm *ptm = localtime(&sec);
data_g.tim = *ptm;
data_g.devid = cnt++;
data_g.tmp = 26.5;
data_g.hum = 20;
data_g.oxy = 25;
pthread_mutex_unlock(&data_g.mutex);
sleep(1);
}
return NULL;
}
void *show_data(void *arg)
{
while (1)
{
sleep(1);
pthread_mutex_lock(&data_g.mutex);
printf("[%4d-%2d-%2d %2d:%2d:%2d][%d][show] 温度: %f, 湿度: %f, 氧气浓度: %f\n",
data_g.tim.tm_year+1900, data_g.tim.tm_mon+1, data_g.tim.tm_mday,
data_g.tim.tm_hour, data_g.tim.tm_min, data_g.tim.tm_sec,
data_g.devid, data_g.tmp, data_g.hum, data_g.oxy);
pthread_mutex_unlock(&data_g.mutex);
}
return NULL;
}
void *storage_data(void *arg)
{
mkdir("../data", 0777);
FILE *fp = fopen("../data/data.txt", "a");
if (NULL == fp)
{
perror("fail fopen data.txt\n");
return NULL;
}
while (1)
{
sleep(1);
pthread_mutex_lock(&data_g.mutex);
fprintf(fp, "[%4d-%2d-%2d %2d:%2d:%2d][%d] 温度: %f, 湿度: %f, 氧气浓度: %f\n",
data_g.tim.tm_year+1900, data_g.tim.tm_mon+1, data_g.tim.tm_mday,
data_g.tim.tm_hour, data_g.tim.tm_min, data_g.tim.tm_sec,
data_g.devid, data_g.tmp, data_g.hum, data_g.oxy);
fflush(fp);
pthread_mutex_unlock(&data_g.mutex);
}
fclose(fp);
return NULL;
}
void *send_data(void *arg)
{
while (1)
{
sleep(1);
pthread_mutex_lock(&data_g.mutex);
printf("[%4d-%2d-%2d %2d:%2d:%2d][%d][send] 温度: %f, 湿度: %f, 氧气浓度: %f\n",
data_g.tim.tm_year+1900, data_g.tim.tm_mon+1, data_g.tim.tm_mday,
data_g.tim.tm_hour, data_g.tim.tm_min, data_g.tim.tm_sec,
data_g.devid, data_g.tmp, data_g.hum, data_g.oxy);
pthread_mutex_unlock(&data_g.mutex);
}
}
int main(int argc, const char *argv[])
{
Task_t tasks[] = {
{
.pfun = get_data, //对结构体中的部分成员进行初始化
},
{
.pfun = show_data,
},
{
.pfun = storage_data,
},
{
.pfun = send_data,
},
};
pthread_mutex_init(&(data_g.mutex), NULL);
create_pthread_tasks(tasks, sizeof(tasks) / sizeof(tasks[0]));
destroy_pthread_tasks(tasks, sizeof(tasks) / sizeof(tasks[0]));
pthread_mutex_destroy(&(data_g.mutex));
return 0;
}
四、测试效果