0
  • 聊天消息
  • 系統(tǒng)消息
  • 評(píng)論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會(huì)員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識(shí)你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

如何利用rt-thread和HMI Board RA6M3開(kāi)發(fā)一套物聯(lián)網(wǎng)工控機(jī)?

冬至子 ? 來(lái)源:MagicKingC ? 作者:MagicKingC ? 2023-09-15 15:27 ? 次閱讀

概述

近年來(lái),隨著新一輪的科技發(fā)展以及工業(yè)產(chǎn)業(yè)升級(jí),工業(yè)經(jīng)濟(jì)慢慢的由數(shù)字化向網(wǎng)絡(luò)化和智能化發(fā)展。基于這一理念,本人利用rt-thread和HMI Board RA6M3開(kāi)發(fā)了一套物聯(lián)網(wǎng)工控機(jī)。

1、功能介紹

物聯(lián)網(wǎng)工控機(jī)具有功能:

1、能夠連接ntp服務(wù)器獲取網(wǎng)絡(luò)時(shí)間,并且能夠?qū)r(shí)間更新到rtc

2、具有WiFi連接功能

3、可以通過(guò)mqtt服務(wù)器控制外設(shè)

4、可以通過(guò)mqtt服務(wù)器獲取外設(shè)狀態(tài)

2、系統(tǒng)框架圖

1.jpg

2.jpg

3、功能實(shí)現(xiàn)

1、配置系統(tǒng)包

1.jpg

2.jpg

2、部分代碼戰(zhàn)術(shù)

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) {//轉(zhuǎn)速
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);

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場(chǎng)。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問(wèn)題,請(qǐng)聯(lián)系本站處理。 舉報(bào)投訴
  • 物聯(lián)網(wǎng)
    +關(guān)注

    關(guān)注

    2900

    文章

    44062

    瀏覽量

    370244
  • 工控機(jī)
    +關(guān)注

    關(guān)注

    10

    文章

    1668

    瀏覽量

    50392
  • NTP
    NTP
    +關(guān)注

    關(guān)注

    1

    文章

    155

    瀏覽量

    13871
  • RT-Thread
    +關(guān)注

    關(guān)注

    31

    文章

    1261

    瀏覽量

    39839
  • MQTT協(xié)議
    +關(guān)注

    關(guān)注

    0

    文章

    96

    瀏覽量

    5335
  • RA6M3
    +關(guān)注

    關(guān)注

    0

    文章

    52

    瀏覽量

    92
收藏 人收藏

    評(píng)論

    相關(guān)推薦

    RA6M3 HMI Board評(píng)測(cè)挑戰(zhàn)】-瑞薩RA6M3上的CAN實(shí)踐

    RA6M3 HMI Board 評(píng)測(cè)大挑戰(zhàn)】-瑞薩RA6M3上的CAN實(shí)踐
    的頭像 發(fā)表于 10-17 14:55 ?1811次閱讀
    【<b class='flag-5'>RA6M3</b> <b class='flag-5'>HMI</b> <b class='flag-5'>Board</b>評(píng)測(cè)挑戰(zhàn)】-瑞薩<b class='flag-5'>RA6M3</b>上的CAN實(shí)踐

    RT-Thread聯(lián)合瑞薩即將發(fā)布高性價(jià)比HMI Board

    高性能芯片 RA6M3RT-Thread 軟件生態(tài),HMI Board 不僅硬件性能強(qiáng)勁,同時(shí)軟件生態(tài)豐富,助力開(kāi)發(fā)者快速
    發(fā)表于 05-08 08:22

    【預(yù)告】RT-Thread聯(lián)合瑞薩即將發(fā)布高性價(jià)比HMI Board

    薩高性能芯片 RA6M3RT-Thread 軟件生態(tài),HMI Board 不僅硬件性能強(qiáng)勁,同時(shí)軟件生態(tài)豐富,助力開(kāi)發(fā)者快速
    的頭像 發(fā)表于 05-11 20:16 ?852次閱讀
    【預(yù)告】<b class='flag-5'>RT-Thread</b>聯(lián)合瑞薩即將發(fā)布高性價(jià)比<b class='flag-5'>HMI</b> <b class='flag-5'>Board</b>

    限量預(yù)售|2D加速、JPEG硬解、超高性價(jià)比M4芯片,您的圖形評(píng)估利器——HMI-Board來(lái)啦!

    能力。依托于瑞薩高性能芯片 RA6M3RT-Thread 軟件生態(tài),HMI Board 不僅硬件性能強(qiáng)勁,同時(shí)軟件生態(tài)豐富,助力開(kāi)發(fā)
    的頭像 發(fā)表于 05-16 05:45 ?1253次閱讀
    限量預(yù)售|2D加速、JPEG硬解、超高性價(jià)比<b class='flag-5'>M</b>4芯片,您的圖形評(píng)估利器——<b class='flag-5'>HMI-Board</b>來(lái)啦!

    聯(lián)合發(fā)布 | 2D加速、JPEG硬解、超高性價(jià)比M4芯片,您的圖形評(píng)估利器——HMI-Board來(lái)啦!

    MCU RA6M3RT-Thread軟件生態(tài),HMI Board不僅硬件性能強(qiáng)勁,同時(shí)軟件生態(tài)豐富,助力開(kāi)發(fā)者快速
    的頭像 發(fā)表于 05-19 15:15 ?1441次閱讀
    聯(lián)合發(fā)布 | 2D加速、JPEG硬解、超高性價(jià)比<b class='flag-5'>M</b>4芯片,您的圖形評(píng)估利器——<b class='flag-5'>HMI-Board</b>來(lái)啦!

    vscode+pyocd+daplink調(diào)試瑞薩ra6m3

    拿到了瑞薩的ra6m3的板子,發(fā)現(xiàn)使用RT-Thread Stduio下載速度很慢,就想著搭建個(gè)vscode的環(huán)境試試。
    的頭像 發(fā)表于 06-07 11:51 ?1388次閱讀

    RA6M3 HMI Board 之SDHI實(shí)踐

    進(jìn)行說(shuō)明,然后使用RT-Studio新建工程進(jìn)行開(kāi)發(fā),通過(guò)RASC對(duì)RA6M3的底層硬件配置,然后使用SDHI接口實(shí)現(xiàn)SD的識(shí)別,搭配文件系統(tǒng)對(duì)SD卡進(jìn)行讀寫操作,同時(shí)記錄開(kāi)發(fā)遇到的問(wèn)
    的頭像 發(fā)表于 06-19 18:15 ?983次閱讀
    <b class='flag-5'>RA6M3</b> <b class='flag-5'>HMI</b> <b class='flag-5'>Board</b> 之SDHI實(shí)踐

    RA6M3 HMI Board 之RTC實(shí)踐

    開(kāi)發(fā)環(huán)境: RT-Thread Studio:v2.2.6 開(kāi)發(fā)板:RA6M3 HMI Board
    的頭像 發(fā)表于 06-21 21:31 ?2863次閱讀
    <b class='flag-5'>RA6M3</b> <b class='flag-5'>HMI</b> <b class='flag-5'>Board</b> 之RTC實(shí)踐

    【線下培訓(xùn)】上海臨港: RT-Thread × 瑞薩 工業(yè)監(jiān)視器 RA6M3 HMI Board解決方案

    RT-Thread 與瑞薩將于10月14日在上海臨港舉行場(chǎng)線下培訓(xùn)。 本次培訓(xùn)將為開(kāi)發(fā)者們帶來(lái)期待已久的熱門技術(shù)主題, RT-Thread × 瑞薩 工業(yè)監(jiān)視器
    的頭像 發(fā)表于 09-07 17:40 ?743次閱讀
    【線下培訓(xùn)】上海臨港: <b class='flag-5'>RT-Thread</b> × 瑞薩 工業(yè)監(jiān)視器 <b class='flag-5'>RA6M3</b> <b class='flag-5'>HMI</b> <b class='flag-5'>Board</b>解決方案

    vscode+pyocd+daplink調(diào)試瑞薩ra6m3步驟

    拿到了瑞薩的ra6m3的板子,發(fā)現(xiàn)使用RT-Thread Stduio下載速度很慢,就想著搭建個(gè)vscode的環(huán)境試試。
    的頭像 發(fā)表于 09-14 14:47 ?1557次閱讀

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

    高性能芯片 RA6M3RT-Thread 和 LVGL 軟件生態(tài),HMI Board 不僅硬件性能強(qiáng)勁,同時(shí)軟件生態(tài)豐富,助力開(kāi)發(fā)者快
    的頭像 發(fā)表于 09-15 18:25 ?1603次閱讀
    瑞薩<b class='flag-5'>RA6M3</b> <b class='flag-5'>HMI-Board</b> Video Demo 上手指南

    瑞薩 RA6M3 開(kāi)發(fā)實(shí)踐指南-UART實(shí)踐

    RT-Thread即將發(fā)布系列RA6M3外設(shè)驅(qū)動(dòng)的實(shí)踐指南,我們將使用瑞薩RA6M3 HMI-Board
    的頭像 發(fā)表于 09-23 12:15 ?1254次閱讀

    【議程發(fā)布】10月上海線下培訓(xùn):RT-Thread × 瑞薩 工業(yè)監(jiān)視器+HMI解決方案!

    。 本次培訓(xùn)RT-Thread將提供培訓(xùn)用開(kāi)發(fā)板: RA6M3 HMI Board 活動(dòng)議程: ? 報(bào)名方式: 下列任意方式都可以免費(fèi)報(bào)名參
    的頭像 發(fā)表于 09-28 18:45 ?508次閱讀
    【議程發(fā)布】10月上海線下培訓(xùn):<b class='flag-5'>RT-Thread</b> × 瑞薩 工業(yè)監(jiān)視器+<b class='flag-5'>HMI</b>解決方案!

    【直播預(yù)告】基于RT-Thread &amp; LVGL RA6M3 HMI-Board兒童益智學(xué)習(xí)機(jī)

    五晚,我們有幸邀請(qǐng)到了本次大賽的二等獎(jiǎng)得主,他將與我們分享他的作品——基于RT-Thread LVGL RA6M3 HMI-Board兒童益智學(xué)習(xí)機(jī)。 直播信息 直播主題 基于
    的頭像 發(fā)表于 11-30 19:05 ?520次閱讀
    【直播預(yù)告】基于<b class='flag-5'>RT-Thread</b> &amp; LVGL  <b class='flag-5'>RA6M3</b> <b class='flag-5'>HMI-Board</b>兒童益智學(xué)習(xí)<b class='flag-5'>機(jī)</b>

    RA6M3 RT-Thread* LVGL HMI Board開(kāi)發(fā)實(shí)踐指南-GPIO實(shí)踐

    RT-Thread即將發(fā)布系列RA6M3外設(shè)驅(qū)動(dòng)的實(shí)踐指南,我們將使用瑞薩RA6M3 HMI-Board
    的頭像 發(fā)表于 11-30 19:05 ?1116次閱讀
    <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'>開(kāi)發(fā)</b>實(shí)踐指南-GPIO實(shí)踐