概述
近年來(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)框架圖
3、功能實(shí)現(xiàn)
1、配置系統(tǒng)包
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);
-
物聯(lián)網(wǎng)
+關(guān)注
關(guān)注
2900文章
44062瀏覽量
370244 -
工控機(jī)
+關(guān)注
關(guān)注
10文章
1668瀏覽量
50392 -
NTP
+關(guān)注
關(guān)注
1文章
155瀏覽量
13871 -
RT-Thread
關(guān)注
31文章
1261瀏覽量
39839 -
MQTT協(xié)議
+關(guān)注
關(guān)注
0文章
96瀏覽量
5335 -
RA6M3
+關(guān)注
關(guān)注
0文章
52瀏覽量
92
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論