验证中...
开源中国 2018 年度最后一场技术盛会邀你来约~错过就要等明年啦!点此立即预约
语言: C/C++
分类: 嵌入式开发
最后更新于 2018-12-09 10:04
片段 1 片段 2 片段 3 片段 4 片段 5 片段 6
quaaquarius.c
原始数据 复制代码
/*--------------------------------------------------------------------------*/
/* Project : QuaAquarius */
/* File : quaaquarius.c */
/* Summary : The implementation of main function's file for this project */
/* Declare : Copyright(C) 2016-2017 QuaOrgin */
/* Remark : none */
/* History : v1.00 Jackie Jiang 2017/10/31 Create */
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/* */
/* Include Files */
/* */
/*--------------------------------------------------------------------------*/
#include "quaaquarius.h"
#include "libemqtt.h"
#include "cJSON.h"
#include "mqttclient.h"
#include "readcfg.h"
#include "uart.h"
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
//#include<stdio.h>
#include <sys/time.h>
#include <math.h>
#include <stdlib.h>
//#include<unistd.h>
/*--------------------------------------------------------------------------*/
/* */
/* Macros Define */
/* */
/*--------------------------------------------------------------------------*/
#define cMinPeriod 5000 //10ms周期
/*--------------------------------------------------------------------------*/
/* */
/* Local Types */
/* */
/*--------------------------------------------------------------------------*/
struct timeval tv;
UINT periodCnt;
UINT flag5;
UINT flag30;
UINT flag60;
UINT flag10ms;
/*--------------------------------------------------------------------------*/
/* */
/* Global Types */
/* */
/*--------------------------------------------------------------------------*/
QUAAQUARIUSDATA g_sQuaAquariusData = {0};
/*--------------------------------------------------------------------------*/
/* */
/* Extern Global Types */
/* */
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/* */
/* Extern Functions */
/* */
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/* */
/* Functions Declare */
/* */
/*--------------------------------------------------------------------------*/
void sys_init(void);
void sys_sigroutine(int signal);
void sys_sigterm(int signal);
void sys_timer(void);
int sys_ontask_10ms(void);
int sys_ontask_1s(void);
int sys_ontask_5s(void);
int sys_ontask_30s(void);
int sys_ontask_1min(void);
void sys_final(void);
/*--------------------------------------------------------------------------*/
/* */
/* Functions Define */
/* */
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/* @(p) */
/* Name : sys_init */
/* Function : 系统初始化函数 */
/* Return : 无 */
/* Argument : 无 */
/* Create : v1.00 Jackie Jiang 2017/10/31 */
/* Update : */
/* Remark : */
/*--------------------------------------------------------------------------*/
void sys_init(void)
{
periodCnt = 0;
flag5 = 0;
flag30 = 0;
flag60 = 0;
flag10ms = 0;
read_config();//读取配置文件信息
mqttclient_init();//MQTT客户端初始化
sys_timer();//打开定时器与终端
mqttclient_subscribe();//MQTT客户端订阅TOPIC
g_sQuaAquariusData.sServer.uFlag = 0x00;
uart_open();//打开串口1、2、3
}
/*--------------------------------------------------------------------------*/
/* @(p) */
/* Name : main */
/* Function : 主函数入口 */
/* Return : 无 */
/* Argument : 无 */
/* Create : v1.00 Jackie Jiang 2017/10/31 */
/* Update : */
/* Remark : */
/*--------------------------------------------------------------------------*/
int main(int argc, char* argv[])
{
int nRetVal = 0x00;
int nDoRun = 0x01;
//1.system init
sys_init();
//2.main running
while(nDoRun != 0x00 && g_sQuaAquariusData.sServer.gpa >= 0)
{
nRetVal = mqttclient_recv();
}
//3.system final
sys_final();
return nRetVal;
}
/*--------------------------------------------------------------------------*/
/* @(p) */
/* Name : sys_sigroutine */
/* Function : 系统分时任务入口 */
/* Return : 无 */
/* Argument : 无 */
/* Create : v1.00 Jackie Jiang 2017/10/31 */
/* Update : */
/* Remark : */
/*--------------------------------------------------------------------------*/
void sys_sigroutine(int signal)
{
int nRetVal = 0x00;
switch(signal)
{
case SIGALRM:
{
/*periodCnt++;
if(periodCnt >= 500)
{
mqttclient_ping();
gettimeofday(&tv,NULL);
printf("5s task %ld %ld\n",tv.tv_sec,tv.tv_usec);
periodCnt = 0;
}*/
nRetVal = sys_ontask_10ms();
if(periodCnt % 100 == 0)
{
//nRetVal = sys_ontask_1s();
}
if(periodCnt % 500 == 0)
{
nRetVal = sys_ontask_5s();
}
if(periodCnt % 3000 == 0)
{
//nRetVal = sys_ontask_30s();
}
if(periodCnt % 6000 == 0)
{
nRetVal = sys_ontask_1min();
}
periodCnt = (periodCnt + 1 < 6000 ? periodCnt + 1 : 0);
break;
}
case SIGVTALRM:
{
/*gettimeofday(&tv,NULL);
printf("05ms vt %ld %ld\n",tv.tv_sec,tv.tv_usec);*/
break;
}
default:
{
break;
}
}
return;
}
/*--------------------------------------------------------------------------*/
/* @(p) */
/* Name : sys_sigterm */
/* Function : On 10ms'task,then do */
/* Return : int , 1 - OK */
/* Argument : none */
/* Create : v1.00 Jackie Jiang 2017/10/31 */
/* Update : */
/* Remark : */
/*--------------------------------------------------------------------------*/
void sys_sigterm(int signal)
{
printf("Goodbye!\n");
// >>>>> DISCONNECT
//关闭串口
uart_close();
//断开MQTT客户端连接
mqttclient_final();
//mqtt_disconnect(&broker);
//close_socket(&broker);
exit(0);
}
/*--------------------------------------------------------------------------*/
/* @(p) */
/* Name : sys_timer */
/* Function : On 10ms'task,then do */
/* Return : int , 1 - OK */
/* Argument : none */
/* Create : v1.00 Jackie Jiang 2017/10/31 */
/* Update : */
/* Remark : */
/*--------------------------------------------------------------------------*/
void sys_timer(void)
{
struct itimerval itv_real;
//struct itimerval itv_virtual;
struct itimerval itv_old;
signal(SIGALRM,sys_sigroutine);
//signal(SIGVTALRM,sys_sigroutine);
signal(SIGINT,sys_sigterm);
itv_real.it_value.tv_sec = 0;//当前时间计数s
itv_real.it_value.tv_usec = cMinPeriod;//当前时间计数us
itv_real.it_interval.tv_sec = 0;//时间间隔s
itv_real.it_interval.tv_usec = cMinPeriod;//时间间隔us
setitimer(ITIMER_REAL,&itv_real,&itv_old);
//itv_virtual.it_value.tv_sec = 0;/*当前时间计数*/
//itv_virtual.it_value.tv_usec = 50000;/*当前时间计数*/
//itv_virtual.it_interval.tv_sec = 0;/*时间间隔*/
//itv_virtual.it_interval.tv_usec = 50000;/*时间间隔*/
//setitimer(ITIMER_VIRTUAL,&itv_virtual,&itv_old);
}
/*--------------------------------------------------------------------------*/
/* @(p) */
/* Name : sys_ontask_10ms */
/* Function : On 10ms'task,then do */
/* Return : int , 1 - OK */
/* Argument : none */
/* Create : v1.00 Jackie Jiang 2017/10/31 */
/* Update : */
/* Remark : */
/*--------------------------------------------------------------------------*/
int sys_ontask_10ms(void)
{
int nRetVal = 0x01;
if(flag10ms >= 300)
{
//printf("10ms task------->\n");
uart1_recv();//入水水质
uart2_recv();//出水水质
uart3_recv();//放水板通讯
flag10ms = 300;
}
flag10ms++;
return nRetVal;
}
/*--------------------------------------------------------------------------*/
/* @(p) */
/* Name : sys_ontask_1s */
/* Function : On 1s'task,then do */
/* Return : int , 1 - OK */
/* Argument : none */
/* Create : v1.00 Jackie Jiang 2017/10/31 */
/* Update : */
/* Remark : */
/*--------------------------------------------------------------------------*/
int sys_ontask_1s(void)
{
int nRetVal = 0x01;
//printf("1s task------->\n");
/*if(g_sQuaAquariusData.sServer.uFlag == 0x01)
{
mqttclient_publish(cOrderType,0x02);
g_sQuaAquariusData.sServer.uFlag = 0x00;
printf("mqtt send\n");
}*/
return nRetVal;
}
/*--------------------------------------------------------------------------*/
/* @(p) */
/* Name : sys_ontask_5s */
/* Function : On 5s'task,then do */
/* Return : int , 1 - OK */
/* Argument : none */
/* Create : v1.00 Jackie Jiang 2017/10/31 */
/* Update : */
/* Remark : */
/*--------------------------------------------------------------------------*/
int sys_ontask_5s(void)
{
int nRetVal = 0x01;
if(flag5 != 0)
{
printf("5s task------->\n");
time_t timep;
time(&timep);
printf("%s",ctime(&timep));
uart1_send();//入水水质检测命令
uart2_send();//出水水质检测命令
mqttclient_ping();
}
flag5 = 1;
return nRetVal;
}
/*--------------------------------------------------------------------------*/
/* @(p) */
/* Name : sys_ontask_30s */
/* Function : On 30s'task,then do */
/* Return : int , 1 - OK */
/* Argument : none */
/* Create : v1.00 Jackie Jiang 2017/10/31 */
/* Update : */
/* Remark : */
/*--------------------------------------------------------------------------*/
int sys_ontask_30s(void)
{
int nRetVal = 0x01;
return nRetVal;
}
/*--------------------------------------------------------------------------*/
/* @(p) */
/* Name : sys_ontask_1min */
/* Function : On 1min'task,then do */
/* Return : int , 1 - OK */
/* Argument : none */
/* Create : v1.00 Jackie Jiang 2017/10/31 */
/* Update : */
/* Remark : */
/*--------------------------------------------------------------------------*/
int sys_ontask_1min(void)
{
int nRetVal = 0x01;
if(flag60 != 0)
{
printf("1min task------->\n");
time_t timep;
time(&timep);
printf("%s",ctime(&timep));
mqttclient_publish(cMessageType);//每一分钟发送一次水质信息
}
flag60 = 1;
return nRetVal;
}
/*--------------------------------------------------------------------------*/
/* @(p) */
/* NAME : sys_final */
/* FUNCTION : 系统反初始化 */
/* RETURN : none */
/* ARGUMENT : none */
/* CREATE : v0.01 ZH 2012/04/02 Create */
/* UPDATE : */
/* REMARK : */
/*--------------------------------------------------------------------------*/
void sys_final(void)
{
uart_close();//关闭串口
mqttclient_final();//断开MQTT客户端连接
}
cJSON.c
原始数据