0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

如何利用rt-thread和HMI Board RA6M3开发一套物联网工控机?

冬至子 来源:MagicKingC 作者:MagicKingC 2023-09-15 15:27 次阅读

概述

近年来,随着新一轮的科技发展以及工业产业升级,工业经济慢慢的由数字化向网络化和智能化发展。基于这一理念,本人利用rt-thread和HMI Board RA6M3开发了一套物联网工控机。

1、功能介绍

物联网工控机具有功能:

1、能够连接ntp服务器获取网络时间,并且能够将时间更新到rtc

2、具有WiFi连接功能

3、可以通过mqtt服务器控制外设

4、可以通过mqtt服务器获取外设状态

2、系统框架图

1.jpg

2.jpg

3、功能实现

1、配置系统包

1.jpg

2.jpg

2、部分代码战术

mqtt线程代码

#include "rtthread.h"
#include "wlan_mgnt.h"
#include "task_queue.h"
#include
#include "cjson.h"
#include "ui.h"
#include "mqttclient.h"
void LoadingBar_Animation(lv_obj_t TargetObject)
{
lv_anim_t PropertyAnimation_0;
lv_anim_init(&PropertyAnimation_0);
lv_anim_set_var(&PropertyAnimation_0, TargetObject);
lv_anim_set_exec_cb(&PropertyAnimation_0, (lv_anim_exec_xcb_t) lv_bar_set_value);
lv_anim_set_values(&PropertyAnimation_0, 0, 100);
lv_anim_set_time(&PropertyAnimation_0, 3000);
lv_anim_set_repeat_count(&PropertyAnimation_0, 0);
lv_anim_set_playback_time(&PropertyAnimation_0, 0);
lv_anim_start(&PropertyAnimation_0);
}
static char cid[64] = { 0 };
mqtt_client_t mqclient = NULL;
static void sub_topic_handle1(void
client, message_data_t
msg)
{
int result;
if (msg->message->payloadlen < 256)
{
result = rt_mq_send(&mqtt_mq,msg->message->payload,msg->message->payloadlen);
if (result != RT_EOK)
{
rt_kprintf("rt_mq_send ERRn");
}
}
}
static int mqtt_push(mqtt_client_t *client, const char *_data, size_t _data_len)
{
mqtt_message_t msg;
memset(&msg, 0, sizeof(msg));
msg.qos = QOS0;
msg.payload = (void *) _data;
msg.payloadlen = _data_len;
return mqtt_publish(client, "/ipc_iot/pub", &msg);
}
static char mqtt_send_buff[512] = { 0 };
static void sendCtrlMsg(mqtt_client_t *client, rt_uint32_t _data)
{
cJSON *root;
root = cJSON_CreateObject();
if (root != NULL)
{
cJSON_AddStringToObject(root, "type", "ctrl");
switch ((_data >> 28) & 0x0F)
{
case 0:
cJSON_AddStringToObject(root, "class", "key");
break;
case 1:
cJSON_AddStringToObject(root, "class", "elec1");
break;
case 2:
cJSON_AddStringToObject(root, "class", "elec2");
break;
case 3:
cJSON_AddStringToObject(root, "class", "elec3");
break;
}
cJSON data = cJSON_CreateObject();
cJSON_AddNumberToObject(data, "value", _data & 0x0fffffff);
cJSON_AddItemToObject(root, "data", data);
char json_str = cJSON_PrintUnformatted(root);
if (json_str != NULL)
{
int len = strlen(json_str);
if (len < sizeof(mqtt_send_buff))
{
memset(mqtt_send_buff, 0, sizeof(mqtt_send_buff));
memcpy(mqtt_send_buff, json_str, len);
mqtt_push(client, mqtt_send_buff, strlen(mqtt_send_buff));
}
free(json_str);
}
cJSON_Delete(root);
}
}
/

@param parameter
*/
static void mqtt_send_thread_entry(void *parameter)
{
rt_uint32_t mb_data;
rt_kprintf("mqtt_send_thread_entryrn");
mqtt_subscribe(mqclient, "/ipc_iot/sub", QOS0, sub_topic_handle1);
rt_kprintf("mqtt_publishrn");
rt_mb_send(&ctrl_mb, mb_data);
rt_thread_mdelay(500);
while (1)
{
if (rt_mb_recv(&ctrl_mb, &mb_data, RT_WAITING_FOREVER) == RT_EOK)
{
//0x0fffffff
rt_kprintf("mqtt_thread: get a mail from mailbox, the content:%08xn", mb_data);
sendCtrlMsg(mqclient, mb_data);
// rt_thread_mdelay(50);
}
}
}
void SetShowWinData(char *_data){
cJSON *root;
root = cJSON_Parse(_data);
if (root != NULL) {
cJSON *tmp;
tmp = cJSON_GetObjectItem(root, "type");
if (tmp != NULL) {
if (strcmp("show",tmp->valuestring) == 0) {
tmp = cJSON_GetObjectItem(root, "class");
if (tmp != NULL){
if (strcmp("temp",tmp->valuestring) == 0) {//温度
cJSON *data = cJSON_GetObjectItem(root, "data");
if (data != NULL) {
tmp = cJSON_GetObjectItem(data, "value");
rt_kprintf("value:%dn",tmp->valueint);
lv_chart_set_next_value(ui_TempChart, ui_TempChartSeries, tmp->valueint);
}
}else if (strcmp("hum",tmp->valuestring) == 0) {//湿度
cJSON *data = cJSON_GetObjectItem(root, "data");
if (data != NULL) {
tmp = cJSON_GetObjectItem(data, "value");
rt_kprintf("value:%dn",tmp->valueint);
lv_label_set_text_fmt(ui_HumShow, "%d%%",tmp->valueint);
}
}else if (strcmp("elect",tmp->valuestring) == 0) {//转速
cJSON *data = cJSON_GetObjectItem(root, "data");
if (data != NULL) {
tmp = cJSON_GetObjectItem(data, "value1");
rt_kprintf("value1:%dn",tmp->valueint);
lv_label_set_text_fmt(ui_Fan1Speed, "%d%%",tmp->valueint);
tmp = cJSON_GetObjectItem(data, "value2");
rt_kprintf("value2:%dn",tmp->valueint);
lv_label_set_text_fmt(ui_Fan2Speed, "%d%%",tmp->valueint);
tmp = cJSON_GetObjectItem(data, "value3");
rt_kprintf("value3:%dn",tmp->valueint);
lv_label_set_text_fmt(ui_Fan3Speed, "%d%%",tmp->valueint);
}
}else if (strcmp("all",tmp->valuestring) == 0) {//全部
cJSON *data = cJSON_GetObjectItem(root, "data");
if (data != NULL) {
tmp = cJSON_GetObjectItem(data, "value_temp");
rt_kprintf("value1:%dn",tmp->valueint);
lv_chart_set_next_value(ui_TempChart, ui_TempChartSeries, tmp->valueint);
tmp = cJSON_GetObjectItem(data, "value_hum");
rt_kprintf("value:%dn",tmp->valueint);
lv_label_set_text_fmt(ui_HumShow, "%d%%",tmp->valueint);
tmp = cJSON_GetObjectItem(data, "value_elect1");
rt_kprintf("value1:%dn",tmp->valueint);
lv_label_set_text_fmt(ui_Fan1Speed, "%d%%",tmp->valueint);
tmp = cJSON_GetObjectItem(data, "value_elect2");
rt_kprintf("value2:%dn",tmp->valueint);
lv_label_set_text_fmt(ui_Fan2Speed, "%d%%",tmp->valueint);
tmp = cJSON_GetObjectItem(data, "value_elect3");
rt_kprintf("value3:%dn",tmp->valueint);
lv_label_set_text_fmt(ui_Fan3Speed, "%d%%",tmp->valueint);
}
}
}
}
}
}
}
static void mqtt_recv_thread_entry(void *parameter)
{
rt_kprintf("mqtt_recv_thread_entryrn");
static char buf[128] = { 0 };
rt_thread_mdelay(500);
while (1)
{
memset(buf,0,sizeof(buf));
if (rt_mq_recv(&mqtt_mq, (void *)buf, sizeof(buf), RT_WAITING_FOREVER) > 0)
{
rt_kprintf("mqtt_recv_thread_entry:%sn", buf);
if (winClass == SHOW_WIN) {
SetShowWinData(buf);
}
}else {
rt_kprintf("recv_thread recv error!n");
}
}
}
rt_align(RT_ALIGN_SIZE)
static char mqtt_r_thread_stack[1024];
static struct rt_thread mqtt_r_thread;
static char mqtt_s_thread_stack[2048];
static struct rt_thread mqtt_s_thread;
int initMQTTTask(void)
{
rt_wlan_connect("test", "12345678");
lv_label_set_text(ui_LoadingText, "Loaded ");
LoadingImg_Animation(ui_LoadingImg, 0);
LoadingBar_Animation(ui_LoadingBar);
while (rt_wlan_is_connected() == RT_FALSE)
{
rt_thread_mdelay(50);
}
rt_thread_mdelay(500);
lv_label_set_text(ui_LoadingText, "Connect MQTT Server");
mqclient = mqtt_lease();
rt_snprintf(cid, sizeof(cid), "rtthread%d", rt_tick_get());
mqtt_set_host(mqclient, "192.168.8.178");
mqtt_set_port(mqclient, "1883");
mqtt_set_user_name(mqclient, "mqtt_client_iot");
mqtt_set_password(mqclient, "iot123456");
mqtt_set_client_id(mqclient, cid);
mqtt_set_clean_session(mqclient, 1);
KAWAII_MQTT_LOG_I("The ID of the Kawaii client is: %s rn", cid);
rt_thread_mdelay(500);
while (KAWAII_MQTT_CONNECT_FAILED_ERROR == mqtt_connect(mqclient))
{
rt_thread_mdelay(1000);
}
lv_label_set_text(ui_LoadingText, "MQTT Server OK");
rt_thread_mdelay(2500);
DisLoading_Animation(ui_LoadingPanel, 0);
rt_thread_mdelay(1000);
lv_obj_clear_flag(ui_MainPanel, LV_OBJ_FLAG_HIDDEN);
lv_obj_del(ui_LoadingPanel);
rt_thread_init(&mqtt_s_thread, "mqtt_s_t", mqtt_send_thread_entry, RT_NULL, &mqtt_s_thread_stack[0],
sizeof(mqtt_s_thread_stack), 18, 5);
rt_thread_startup(&mqtt_s_thread);
rt_thread_init(&mqtt_r_thread, "mqtt_r_t", mqtt_recv_thread_entry, RT_NULL, &mqtt_r_thread_stack[0],
sizeof(mqtt_r_thread_stack), 16, 5);
rt_thread_startup(&mqtt_r_thread);
return 0;
}
INIT_APP_EXPORT(initMQTTTask);
ntp线程代码


#include "ui.h"
#include "rtthread.h"
#include "ui_events.h"
#include "task_queue.h"
#include "ntp.h"
#include "wlan_mgnt.h"
/**

@param parameter
*/
static void ntp_thread_entry(void *parameter)
{
rt_uint32_t mb_data;
time_t now;
struct tm *p;
rt_bool_t isSyncRTC = RT_FALSE;
if (rt_mb_recv(&ctrl_mb, &mb_data, RT_WAITING_FOREVER) == RT_EOK);
while (1)
{
now = time(RT_NULL);
p = localtime(&now);
if (winClass == MIAN_WIN)
{
lv_label_set_text_fmt(ui_TimerLabel,
"%04d-%02d-%02d %02d:%02d:%02dn",
(1900 + p->tm_year), (1 + p->tm_mon), p->tm_mday,p->tm_hour,p->tm_min, p->tm_sec);
}
if (isSyncRTC == RT_FALSE && rt_wlan_is_connected() == RT_TRUE)
{
isSyncRTC = RT_TRUE;
ntp_sync_to_rtc("ntp.rt-thread.org");
}else if (rt_wlan_is_connected() == RT_TRUE && p->tm_hour == 0 &&
p->tm_min == 0 && p->tm_sec == 0) {
ntp_sync_to_rtc("ntp.rt-thread.org");
}
rt_thread_mdelay(1000);
}
}
static struct rt_thread ntp_thread;
static char ntp_stack[1280];
int initNTPTask(void)
{
rt_thread_init(&ntp_thread, "main_ui", ntp_thread_entry, RT_NULL, & ntp_stack[0],
sizeof(ntp_stack),16, 5);
rt_thread_startup(&ntp_thread);
return 0;
}
INIT_APP_EXPORT(initNTPTask);

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • 物联网
    +关注

    关注

    2873

    文章

    41851

    浏览量

    359367
  • 工控机
    +关注

    关注

    10

    文章

    1360

    浏览量

    48010
  • NTP
    NTP
    +关注

    关注

    1

    文章

    100

    浏览量

    13793
  • RT-Thread
    +关注

    关注

    31

    文章

    1164

    浏览量

    38967
  • MQTT协议
    +关注

    关注

    0

    文章

    90

    浏览量

    5244
  • RA6M3
    +关注

    关注

    0

    文章

    52

    浏览量

    21
收藏 人收藏

    评论

    相关推荐

    RA6M3 HMI Board评测挑战】-瑞萨RA6M3上的CAN实践

    RA6M3 HMI Board 评测大挑战】-瑞萨RA6M3上的CAN实践
    的头像 发表于 10-17 14:55 1140次阅读
    【<b class='flag-5'>RA6M3</b> <b class='flag-5'>HMI</b> <b class='flag-5'>Board</b>评测挑战】-瑞萨<b class='flag-5'>RA6M3</b>上的CAN实践

    基于 RT-Thread Studio的CPK-RA6M4 开发板环境搭建

    RT-Thread 项目 —> 基于开发板,创建CPK-RA6M4的工程2、打开创建的工程,双击 RA Smart Configurator 即可打开刚刚安装的 FSP 配置工
    发表于 01-10 14:41

    【新品发布】英飞凌PSoC 6 RTT联网开发板内容详解

    PSoC 6 RTT开发板在 RT-Thread 上外设支持情况:1、cypress_gateway_demoInfineon + RT-Thread
    发表于 04-13 13:46

    RT-Thread联合瑞萨即将发布高性价比HMI Board

    高性能芯片 RA6M3RT-Thread 软件生态,HMI Board 不仅硬件性能强劲,同时软件生态丰富,助力开发者快速
    发表于 05-08 08:22

    RT-Thread开发,如何有效学习RT-Thread的五个步骤

    RT-Thread推出RT-Thread Inside战略开放RT-Thread开发平台授权合作,与硬件十万个为什么合作首次推出第一款RT-
    的头像 发表于 09-25 09:55 3.4w次阅读
    <b class='flag-5'>RT-Thread</b><b class='flag-5'>开发</b>,如何有效学习<b class='flag-5'>RT-Thread</b>的五个步骤

    【预告】RT-Thread联合瑞萨即将发布高性价比HMI Board

    于瑞萨高性能芯片 RA6M3RT-Thread 软件生态,HMI Board 不仅硬件性能强劲,同时软件生态丰富,助力开发者快速
    的头像 发表于 05-11 20:16 515次阅读
    【预告】<b class='flag-5'>RT-Thread</b>联合瑞萨即将发布高性价比<b class='flag-5'>HMI</b> <b class='flag-5'>Board</b>

    限量预售|2D加速、JPEG硬解、超高性价比M4芯片,您的图形评估利器——HMI-Board来啦!

    能力。依托于瑞萨高性能芯片 RA6M3RT-Thread 软件生态,HMI Board 不仅硬件性能强劲,同时软件生态丰富,助力开发
    的头像 发表于 05-16 05:45 944次阅读
    限量预售|2D加速、JPEG硬解、超高性价比M4芯片,您的图形评估利器——<b class='flag-5'>HMI-Board</b>来啦!

    联合发布 | 2D加速、JPEG硬解、超高性价比M4芯片,您的图形评估利器——HMI-Board来啦!

    MCU RA6M3RT-Thread软件生态,HMI Board不仅硬件性能强劲,同时软件生态丰富,助力开发者快速
    的头像 发表于 05-19 15:15 1017次阅读
    联合发布 | 2D加速、JPEG硬解、超高性价比M4芯片,您的图形评估利器——<b class='flag-5'>HMI-Board</b>来啦!

    RA6M3 HMI Board 之RTC实践

    开发环境: RT-Thread Studio:v2.2.6 开发板:RA6M3 HMI Board
    的头像 发表于 06-21 21:31 2520次阅读
    <b class='flag-5'>RA6M3</b> <b class='flag-5'>HMI</b> <b class='flag-5'>Board</b> 之RTC实践

    【线下培训】上海临港: RT-Thread × 瑞萨 工业监视器 RA6M3 HMI Board解决方案

    RT-Thread 与瑞萨将于10月14日在上海临港举行一场线下培训。 本次培训将为开发者们带来期待已久的热门技术主题, RT-Thread × 瑞萨 工业监视器 RA6M3
    的头像 发表于 09-07 17:40 483次阅读
    【线下培训】上海临港: <b class='flag-5'>RT-Thread</b> × 瑞萨 工业监视器 <b class='flag-5'>RA6M3</b> <b class='flag-5'>HMI</b> <b class='flag-5'>Board</b>解决方案

    瑞萨RA6M3 HMI-Board Video Demo 上手指南

    于瑞萨高性能芯片 RA6M3RT-Thread 和 LVGL 软件生态,HMI Board 不仅硬件性能强劲,同时软件生态丰富,助力开发
    的头像 发表于 09-15 18:25 865次阅读
    瑞萨<b class='flag-5'>RA6M3</b> <b class='flag-5'>HMI-Board</b> Video Demo 上手指南

    瑞萨 RA6M3 开发实践指南-UART实践

    RT-Thread即将发布一系列RA6M3外设驱动的实践指南,我们将使用瑞萨RA6M3 HMI-Board 开发板,带领大家上手
    的头像 发表于 09-23 12:15 756次阅读

    【议程发布】10月上海线下培训:RT-Thread × 瑞萨 工业监视器+HMI解决方案!

    。 本次培训RT-Thread将提供培训用开发板: RA6M3 HMI Board 活动议程:   报名方式: 下列任意方式都可以免费报名参
    的头像 发表于 09-28 18:45 337次阅读
    【议程发布】10月上海线下培训:<b class='flag-5'>RT-Thread</b> × 瑞萨 工业监视器+<b class='flag-5'>HMI</b>解决方案!

    【直播预告】基于RT-Thread &amp; LVGL RA6M3 HMI-Board儿童益智学习机

    五晚,我们有幸邀请到了本次大赛的二等奖得主,他将与我们分享他的作品——基于RT-Thread LVGL RA6M3 HMI-Board儿童益智学习机。 直播信息 直播主题 基于HMI-Boar
    的头像 发表于 11-30 19:05 254次阅读
    【直播预告】基于<b class='flag-5'>RT-Thread</b> &amp; LVGL  <b class='flag-5'>RA6M3</b> <b class='flag-5'>HMI-Board</b>儿童益智学习机

    RA6M3 RT-Thread* LVGL HMI Board开发实践指南-GPIO实践

    RT-Thread即将发布一系列RA6M3外设驱动的实践指南,我们将使用瑞萨RA6M3 HMI-Board 开发板,带领大家上手
    的头像 发表于 11-30 19:05 575次阅读
    <b class='flag-5'>RA6M3</b> <b class='flag-5'>RT-Thread</b>* LVGL <b class='flag-5'>HMI</b> <b class='flag-5'>Board</b><b class='flag-5'>开发</b>实践指南-GPIO实践