Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
HMI-Board
【Renesas GUI挑战赛】物联网工控机
发布于 2023-09-15 00:07:27 浏览:715
订阅该版
[tocm] #### 概述 近年来,随着新一轮的科技发展以及工业产业升级,工业经济慢慢的由数字化向网络化和智能化发展。基于这一理念,本人利用rt-thread和HMI Board RA6M3开发了一套物联网工控机。 #### 1、功能介绍 物联网工控机具有功能: 1、能够连接ntp服务器获取网络时间,并且能够将时间更新到rtc 2、具有WiFi连接功能 3、可以通过mqtt服务器控制外设 4、可以通过mqtt服务器获取外设状态 #### 2、系统框架图 ##### 网络架构图 ![d.png](https://oss-club.rt-thread.org/uploads/20230914/ee746a3cf104deb1b7206505d09616c5.png) ##### 程序流程图 ![l1.png](https://oss-club.rt-thread.org/uploads/20230914/83b3f40c4f3950adc1e2bef0e7d62108.png) #### 3、功能实现 ###### 1、配置系统包 ![捕获.PNG](https://oss-club.rt-thread.org/uploads/20230914/57ff70db0d965f68b86941474e88b855.png.webp) ![捕获2.PNG](https://oss-club.rt-thread.org/uploads/20230914/698f42fb21fc6d7df6c2cb03f322bb37.png.webp) ![捕获3.PNG](https://oss-club.rt-thread.org/uploads/20230914/087c25ed7ccd8dfdb8313d3b4ca5338b.png.webp) 2、部分代码战术 mqtt线程代码 ```c #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 ERR\n"); } } } 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_entry\r\n"); mqtt_subscribe(mqclient, "/ipc_iot/sub", QOS0, sub_topic_handle1); rt_kprintf("mqtt_publish\r\n"); 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:%08x\n", 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:%d\n",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:%d\n",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:%d\n",tmp->valueint); lv_label_set_text_fmt(ui_Fan1Speed, "%d%%",tmp->valueint); tmp = cJSON_GetObjectItem(data, "value2"); rt_kprintf("value2:%d\n",tmp->valueint); lv_label_set_text_fmt(ui_Fan2Speed, "%d%%",tmp->valueint); tmp = cJSON_GetObjectItem(data, "value3"); rt_kprintf("value3:%d\n",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:%d\n",tmp->valueint); lv_chart_set_next_value(ui_TempChart, ui_TempChartSeries, tmp->valueint); tmp = cJSON_GetObjectItem(data, "value_hum"); rt_kprintf("value:%d\n",tmp->valueint); lv_label_set_text_fmt(ui_HumShow, "%d%%",tmp->valueint); tmp = cJSON_GetObjectItem(data, "value_elect1"); rt_kprintf("value1:%d\n",tmp->valueint); lv_label_set_text_fmt(ui_Fan1Speed, "%d%%",tmp->valueint); tmp = cJSON_GetObjectItem(data, "value_elect2"); rt_kprintf("value2:%d\n",tmp->valueint); lv_label_set_text_fmt(ui_Fan2Speed, "%d%%",tmp->valueint); tmp = cJSON_GetObjectItem(data, "value_elect3"); rt_kprintf("value3:%d\n",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_entry\r\n"); 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:%s\n", 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 \r\n", 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线程代码 ```c #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:%02d\n", (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); ``` #### 4、作品演示 [演示视频.mp4](https://club.rt-thread.org/file_download/b098409cbbafb64f) #### 5、总结 rt-thread具有大量的开发包,大大提高了开发者研发的速度。使用起来特别方便。 [工程代码](https://gitee.com/magickingc/ipc_iot.git "工程代码")
1
条评论
默认排序
按发布时间排序
登录
注册新账号
关于作者
MagicKingC
板凳打酱油
文章
3
回答
0
被采纳
0
关注TA
发私信
相关文章
1
HMI-Board创建RT-Thread Studio工程后添加LVGL后编译报错
2
使用瑞萨的 FSP里面不小心把配置属性的界面给删除掉了,然后就找不回来这个页面了,这啥软件啊叉掉了就找不回了,重装也不行
3
HMI Board DAP下载异常
4
HMI-Board拉取官方最新代码报错
5
HMI-Board FSP 關於Ethernet HAL 的設定疑問
6
如何在HMI-BOARD工程里面,开启HWTIMER外设
7
HMI_Board关于开启C++特性编译出错的问题
8
HMI_Board关于开启C++特性编译出错的问题续
9
HMI_Board配置Smart Configurator完外设后编译出现报错
10
关于HMI-Board ADC配置(RA6M3 HMI Board 之ADC实践)问题,没有“使能ADC选项”
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
5
五分钟玩转RT-Thread新社区
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
USB
DMA
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
FAL
rt-smart
ESP8266
I2C_IIC
UART
WIZnet_W5500
ota在线升级
cubemx
PWM
flash
freemodbus
BSP
packages_软件包
潘多拉开发板_Pandora
定时器
ADC
GD32
flashDB
socket
中断
编译报错
Debug
rt_mq_消息队列_msg_queue
SFUD
msh
keil_MDK
ulog
MicroPython
C++_cpp
本月问答贡献
出出啊
1517
个答案
342
次被采纳
小小李sunny
1444
个答案
290
次被采纳
张世争
812
个答案
177
次被采纳
crystal266
547
个答案
161
次被采纳
whj467467222
1222
个答案
148
次被采纳
本月文章贡献
出出啊
1
篇文章
2
次点赞
小小李sunny
1
篇文章
1
次点赞
张世争
1
篇文章
1
次点赞
crystal266
2
篇文章
2
次点赞
whj467467222
2
篇文章
2
次点赞
回到
顶部
发布
问题
投诉
建议
回到
底部