背景:
随着科技的发展,物联网设备的普及和应用越来越广泛,该系统基于ONENET的多区域温湿度监控系统是一套用于实时监测和管理不同地区温湿度的智能解决方案。该系统由一组分布在多个区域的传感器节点组成,采集环境数据并通过无线网络传输至云平台。借助云端分析和处理,用户可以远程监控各个区域的温湿度情况,并及时获取警报和报告。同时,该系统还具备数据存储和分析功能,为用户提供科学决策依据,助力业务优化和资源调配。
设计方案:
该项目的方案方案是通过RT-Thread操作系统和ONENET云平台实现物联网设备实时监控多个区域的温湿度,并显示在HMI_Board开发板中。为了实现一个设备同时监控多个设备发来的数据,一个HMI_Board开发板作为接收节点,剩余三个的作为发送节点,接收节点通过订阅ONENET上的多个产品,等待发送节点向ONENET上的每个特有的产品发送数据,接收节点通过数据解析是那个设备发过来的,然后显示在HMI_board开发板上,同时在ONENET云端这可以看到数据,实现多区域温湿度监控。
开发环境
1.开发板:HMI_Board
2.开发环境:RT-Thread studio V2.2.6
3.RT-Thread版本:4.1.0
4.I2C外设资源:P202—SCL P203—SDA
5.传感器:AHT10,RW007
6.云平台:ONENET
系统设计流程
重要代码设计
发送节点:(下面仅展示一个发送节点的代码,其他同理,只是发送的帧头不一样,方便接收节点解析)
hal_entry.c //包括系统的wifi的连接,mqtt初始化,aht10线程的初始化,mqtt发送线程的初始化
#include
#include "hal_data.h"
#include
#include
#include "my_aht10.h"
#include "my_onenet.h"
#define LED_PIN BSP_IO_PORT_02_PIN_09 /* Onboard LED pins */
void AHT10_read(void *parameter);
void onenet_onenet_mqtt_publish(void *parameter);
rt_mailbox_t temp_msg_mb;
rt_mp_t temp_msg_mp;
rt_sem_t my_sem1;
#define mb_len 32
#define mp_len 32
struct dht_htemp
{
int humi;
int temp;
};
void hal_entry(void)
{
temp_msg_mb = rt_mb_create("temp_mb", mb_len, RT_IPC_FLAG_FIFO);
temp_msg_mp = rt_mp_create("temp_mp", mp_len, sizeof(struct dht_htemp));
my_sem1 = rt_sem_create("my_sem1", 0, RT_IPC_FLAG_FIFO);
rt_wlan_connect("WWWW", "123456789zxc"); //连接wifi
onenet_mqtt_init(); //mqtt //mqtt初始化
my_aht10_init(); //aht10 //启动aht10线程
My_Onenet_Send(); //Onenet //启动讲数据上传ONENET上
}
my_aht10.c //读取温湿度线程,然后通过邮箱和内存池实现和mqtt上传数据线程进行交流
#include "my_aht10.h"
extern rt_mailbox_t temp_msg_mb;
extern rt_mp_t temp_msg_mp;
extern rt_sem_t my_sem1;
extern struct dht_htemp
{
int humi;
int temp;
};
static rt_thread_t aht10_tid = RT_NULL;
void AHT10_read(void *parameter)
{
struct dht_htemp *msg;
aht10_device_t dev;
const char *i2c_bus_name = "i2c1";
rt_thread_mdelay(500);
dev = aht10_init(i2c_bus_name);
if(dev == RT_NULL)
{
rt_kprintf("The sensor init failed.rn");
return ;
}
rt_sem_take(my_sem1, RT_WAITING_FOREVER);
while(1)
{
msg = rt_mp_alloc(temp_msg_mp, RT_WAITING_FOREVER);
msg->temp = (int)aht10_read_temperature(dev);
msg->humi = (int)aht10_read_humidity(dev);
rt_mb_send(temp_msg_mb, (rt_ubase_t)msg);
rt_kprintf("temp:[%d], humi:[%d],location:Nanyangn" ,msg->temp, msg->humi);
msg = RT_NULL;
rt_thread_mdelay(1000);
}
}
void my_aht10_init(void)
{
aht10_tid = rt_thread_create("aht10_thread", AHT10_read, RT_NULL, 2048, 17, 10);
if (aht10_tid)
{
rt_thread_startup(aht10_tid);
rt_kprintf("aht10_tid_init success!rn");
}
}
my_onenet.c //将数据上传到云端,并下发给接收节点
#include "my_onenet.h"
extern rt_mailbox_t temp_msg_mb;
extern rt_mp_t temp_msg_mp;
extern struct dht_htemp
{
int humi;
int temp;
};
static rt_thread_t onenet_tid = RT_NULL;
void onenet_onenet_mqtt_publish(void *parameter)
{
struct dht_htemp msg;
uint8_t tbuf[64] = {0};
size_t len = 0;
while(1)
{
if(rt_mb_recv(temp_msg_mb, (rt_ubase_t ) &msg, RT_WAITING_FOREVER) == RT_EOK)
{
rt_sprintf((char *) tbuf, "temp:[%d], humi:[%d],location:Nanyang", msg->temp, msg->humi);
len = strlen(tbuf);
// rt_kprintf("%dn",len);
if (onenet_mqtt_publish("classA", &tbuf, len) < 0)
{
rt_kprintf("upload string has an error!n");
}
else
{
onenet_mqtt_upload_digit("temperature", msg->temp);
onenet_mqtt_upload_digit("humidity", msg->humi);
rt_mp_free(msg);
msg = RT_NULL;
}
}
}
}
void My_Onenet_Send(void)
{
onenet_tid = rt_thread_create("Onenet_humi_temp",onenet_onenet_mqtt_publish,RT_NULL,2048,16,10);
if (onenet_tid)
{
rt_thread_startup(onenet_tid);
rt_kprintf("mqtt_thread_init success!rn");
}
}
接收节点
主要就是接收onenet每个产品上下发的数据进行解析并显示到显示屏上
对发来得数据进行解析:
int onenet_onenet_mqtt_subscribe(void)
{
if (onenet_mqtt_subscribe("classA") < 0)
{
LOG_E("upload string has an error!n");
}
if (onenet_mqtt_subscribe("classB") < 0)
{
LOG_E("upload string has an error!n");
}
if (onenet_mqtt_subscribe("classC") < 0)
{
LOG_E("upload string has an error!n");
}
return 0;
}
rt_err_t onenet_mqtt_subscribe(const char *topic)
{
RT_ASSERT(topic);
if ( MQTTSubscribe(&mq_client, topic, QOS0) < 0)
{
return -1;
}
return 0;
}
LCD显示:
static void lvgl_thread_entry(void parameter)
{
#if LV_USE_LOG
lv_log_register_print_cb(lv_rt_log);
#endif / LV_USE_LOG /
lv_init();
lv_port_disp_init();
lv_port_indev_init();
lv_obj_t label1 = lv_label_create(lv_scr_act());
lv_obj_t* label2 = lv_label_create(lv_scr_act());
lv_obj_t* label3 = lv_label_create(lv_scr_act());
lv_label_set_text(label1, "1");
lv_label_set_text(label2, "2");
lv_label_set_text(label3, "3");
lv_obj_set_style_text_align(label1, LV_TEXT_ALIGN_CENTER, 0); //设置对象字体样式居中
lv_obj_align(label1, LV_ALIGN_CENTER, 0, lv_pct(20)); //设置对象居中
lv_obj_set_style_text_align(label2, LV_TEXT_ALIGN_CENTER, 0); //设置对象字体样式居中
lv_obj_align(label2, LV_ALIGN_CENTER, 0,0); //设置对象居中
lv_obj_set_style_text_align(label3, LV_TEXT_ALIGN_CENTER, 0); //设置对象字体样式居中
lv_obj_align(label3, LV_ALIGN_CENTER, 0, lv_pct(-20)); //设置对象居中
/* handle the tasks of LVGL */
while(1)
{
lv_task_handler();
rt_thread_mdelay(500);
if(my_flag == 1)
{
lv_label_set_text(label1, str);
my_flag = 0;
}
if(my_flag == 2)
{
lv_label_set_text(label2, str1);
my_flag = 0;
}
if(my_flag == 3)
{
lv_label_set_text(label3, str2);
my_flag = 0;
}
}
}
效果展示
接收节点的显示屏显示:(显示三个不同组员采集到的数据,在不同地方,南阳,周口,长沙)
云平台设备在线情况以及传输数据显示
发送节点:
节点一:
节点二:
节点三:
评论
查看更多