实验六 基于LiteOS实现端到端物联实验
一、实验目的
Huawei LiteOS基础上移植传感器代码、NB模组代码;上报光敏传感器数据;接收平台命令,并作出响应;对命令进行应答。
二、实验过程
1、在Keil环境中添加一下内容
2、在main.h文件下添加代码(开头有个井号),位置如图所示
include"stdint.h"
继续在此页面添加代码
typedef struct
{
uint8_t messageld;
char Data[5];
3、在main.c文件下添加代码(include前有个井号)
'''include "BH1750.h"
msg_for_BH1750 BH1750_send={02};
'''
在此代码之间继续添加代码
void task1(void)
{
int count=1;
while(1)
{
printf("This is task1, count is %d\r\n", count++);
LOS_TaskDelay(1000);
}
}
UINT32 creat_task1()
{
UINT32 uwRet = LOS_OK;
TSK_INIT_PARAM_S task_init_param;
task_init_param.usTaskPrio = 0;
task_init_param.pcName = "task1";
task_init_param.pfnTaskEntry = (TSK_ENTRY_FUNC)task1;
task_init_param.uwStackSize = 0x200;
uwRet = LOS_TaskCreate(&g_TskHandle, &task_init_param);
if (LOS_OK != uwRet)
{
return uwRet;
}
return uwRet;
}
void task2(void)
{
int count = 1;
while(1)
{
printf("This is task2, count is %d\r\n", count++);
LOS_TaskDelay(2000);
}
}
UINT32 creat_task2()
{
UINT32 uwRet = LOS_OK;
TSK_INIT_PARAM_S task_init_param;
task_init_param.usTaskPrio = 1;
task_init_param.pcName = "task2";
task_init_param.pfnTaskEntry = (TSK_ENTRY_FUNC)task2;
task_init_param.uwStackSize = 0x200;
uwRet = LOS_TaskCreate(&g_TskHandle, &task_init_param);
if (LOS_OK != uwRet)
{
return uwRet;
}
return uwRet;
}
VOID LightSensor_task(VOID)
{
UINT32 uwRet = LOS_OK;
short int Data;
Init_BH1750();//³õʼ»¯´«¸ÐÆ÷
while(1)//ËÀÑ»·
{
printf("This is LightSensor_task!\r\n");//´òÓ¡ÄÚÈÝ
Data = (int)Convert_BH1750();//ζȴ«¸ÐÆ÷µÄÖµ¶Áµ½DATAÖÐ
printf("\r\n***********************BH1750 Value is %d\r\n", Data);
sprintf(BH1750_send.Data, "%5d", Data);//½«DATAдµ½BH1750ÖÐ
uwRet = LOS_TaskDelay(500);
if (uwRet != LOS_OK)
return;
}
}
UINT32 creat_LightSensor_task()
{
UINT32 uwRet = LOS_OK;//É趨·ÃÎÊÖµ£¬Ä¬ÈÏ·ÃÎÊOK
TSK_INIT_PARAM_S task_init_param;
task_init_param.usTaskPrio = 0;//ËÀÑ»·º¯Êý£¬ÎÞÏÞ¶ÁÈ¡
task_init_param.pcName = "LightSensor_task";
task_init_param.pfnTaskEntry = (TSK_ENTRY_FUNC)LightSensor_task;
task_init_param.uwStackSize = 0x800;
uwRet = LOS_TaskCreate(&g_TskHandle, &task_init_param);
if (LOS_OK != uwRet)
{
return uwRet;
}
return uwRet;
}
VOID data_send_task(VOID)
{
UINT32 uwRet = LOS_OK;
los_nb_init((const int8_t)"49.4.85.232",(const int8_t)"5683",NULL);
los_nb_notify("+NNMI;",strlen("+NNMI;"),),NULL,nb_cmd_match);
LOS_TaskDelay(3000);
while(1)
{
if (los_nb_report((const char*)(&BH1750_send), sizeof(BH1750_send)) >= 0)
printf("Data send OK!\n");
else
printf("Data send Failed!\n");
uwRet=LOS_TaskDelay(2000);
if(uwRet!=LOS_OK)
return;
}
}
UINT32 creat_data_send_task()
{
UINT32 uwRet = LOS_OK;
TSK_INIT_PARAM_S task_init_param;
task_init_param.usTaskPrio = 1;
task_init_param.pcName = "data_send_task";
task_init_param.pfnTaskEntry = (TSK_ENTRY_FUNC)data_send_task;
task_init_param.uwStackSize = 0x400;
uwRet=LOS_TaskCreate(&g_TskHandle,&task_init_param);
if(uwRet!=LOS_OK)
{
return uwRet;
}
return uwRet;
}
在此代码中如下位置继续添加代码
UINT32 uwRet = LOS_OK;
uwRet = LOS_KernelInit();
if (uwRet != LOS_OK)
{
return LOS_NOK;
}
// uwRet = creat_task1();
// if (uwRet != LOS_OK)
// {
// return LOS_NOK;
// }
//
// uwRet = creat_task2();
// if (uwRet != LOS_OK)
// {
// return LOS_NOK;
// }
uwRet = creat_LightSensor_task();
if (uwRet != LOS_OK)
{
return LOS_NOK;
}
uwRet = creat_data_send_task();
if (uwRet != LOS_OK)
{
return LOS_NOK;
}
LOS_Start();
}