电子说
概述
近年来,随着新一轮的科技发展以及工业产业升级,工业经济慢慢的由数字化向网络化和智能化发展。基于这一理念,本人利用rt-thread和HMI Board RA6M3开发了一套物联网工控机。
1、功能介绍
物联网工控机具有功能:
1、能够连接ntp服务器获取网络时间,并且能够将时间更新到rtc
2、具有WiFi连接功能
3、可以通过mqtt服务器控制外设
4、可以通过mqtt服务器获取外设状态
2、系统框架图
3、功能实现
1、配置系统包
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);
全部0条评论
快来发表一下你的评论吧 !