1引言
在万物互联的时代,如何选择一款性能优异、开发便捷、生态成熟的MCU来作为物联网设备的“大脑”,是整个项目成功的关键。近期,笔者有幸体验了兆易创新(GigaDevice)推出的GD32VW553系列双频Wi-Fi 6微控制器,并以其为核心,构建了一套完整的MQTT物联网通信系统,最终通过微信小程序进行远程监控。本文将从一个开发者的角度,对这套技术方案进行全面的深度测评。
一、 主角登场:GD32VW553芯片
硬件优势:
RISC-V内核高能效加持:GD32VW553基于旗下基于RISC-V架构的Bumblebee内核,主频高达160MHz。在实际测试中,其计算性能足以轻松应对TCP/IP协议栈、MQTT协议解析、传感器数据采集和加密算法等多项任务并行运行,功耗控制却相当出色。
双频Wi-Fi 6集成:这是其最大亮点之一。芯片集成了2.4GHz & 5GHz双频段802.11ax Wi-Fi 6射频模块。实测在复杂网络环境下,5GHz频段带来了更高速率和更低延迟的数据传输,有效减少了同频干扰,设备连接路由器更加稳定快速。
丰富的外设与安全特性:芯片提供了充足的GPIO、UART、SPI、I2C等接口,便于连接各类传感器。内置的硬件加密引擎(AES, SHA, PKC, TRNG)为MQTT的TLS加密通信提供了硬件级支持,大幅提升安全性并减轻CPU负担。
开发环境:
官方提供的GD32VW55x评估板和配套的GD32IDE开发环境(基于GD32EmbeddedBuilder_v1.5.1.30366)开箱即用。SDK包结构清晰,提供了Wi-Fi连接、Socket编程、AT指令等丰富的示例代码,大大降低了上手门槛。
二、 核心实战:MQTT通信实现
由于现在阿里云物联网平台已经不再允许开放,我本来想用阿里云的ECS免费版临时搭建个MQTT使用,结果在开发微信小程序时发现要想使用MQTT必须使用域名,但是域名还要备案,时间较长就果断放弃了这个方案,不急的小伙伴可以尝试一下。我选择的是EMQX平台免费的MQTT服务器,百度就可以搜到,急用的小伙伴可以采用一下。
设置连接要连接使用的WiFi名称密码以及要连接的MQTT服务器的IP以及端口。
#define SSID "captain"
#define PASSWORD "926521944" // NULL // ""
char alarm_state;
#define SERVER_PORT 1883
ip_addr_t sever_ip_addr = IPADDR4_INIT_BYTES(44,232,241,40);
static char client_id[] = {'c', 'a', 'p', 'a', 't', 'a', 'i', 'n'};
struct mqtt_connect_client_info_t client_info = {
//client_id
client_id,
//client_user
NULL,//NULL,
//client_password
NULL,//NULL,
//keep_alive
120,
//topic
NULL,
//msg
NULL,
//qos
0,
//retain
0
};以下是GD32VW553连接MQTT服务器的过程;
static int client_connect(void)
{
err_t ret = ERR_OK;
uint32_t connect_time = 0;
mqtt_client = mqtt_client_new();
if (mqtt_client == NULL) {
printf("Can't get mqtt client.\r\n");
return -1;
}
printf("MQTT: start link server...\r\n");
mqtt_ssl_cfg(mqtt_client, tls_auth_mode);
mqtt_set_inpub_callback(mqtt_client, mqtt_receive_pub_topic_print, mqtt_receive_msg_print, &client_info);
if (current_mqtt_mode == MODE_TYPE_MQTT5) {
if (mqtt5_param_cfg(mqtt_client)) {
printf("MQTT: Configuration MQTT parameters failed, stop connection.\r\n");
return -2;
}
connect_time = sys_current_time_get();
ret = mqtt5_client_connect(mqtt_client, &sever_ip_addr, SERVER_PORT, mqtt_connect_callback, NULL, &client_info,
&(mqtt_client->mqtt5_config->connect_property_info),
&(mqtt_client->mqtt5_config->will_property_info));
if (ret != ERR_OK) {
printf("MQTT mqtt_client: connect to server failed.\r\n");
return ret;
}
while (mqtt_client_is_connected(mqtt_client) == false) {
if ((sys_current_time_get() - connect_time) > 5000) {
printf("MQTT mqtt_client: connect to server timeout.\r\n");
return -3;
}
if (connect_fail_reason == MQTT_CONNECTION_REFUSE_PROTOCOL) {
mqtt5_disconnect(mqtt_client);
mqtt5_param_delete(mqtt_client);
printf("MQTT: The server does not support version 5.0, now switch to version 3.1.1.\r\n");
current_mqtt_mode = MODE_TYPE_MQTT;
connect_fail_reason = -1;
break;
} else if (connect_fail_reason > 0) {
mqtt5_fail_reason_display((mqtt5_connect_return_res_t)connect_fail_reason);
return connect_fail_reason;
}
sys_yield();
}
}
if (current_mqtt_mode == MODE_TYPE_MQTT) {
connect_time = sys_current_time_get();
ret = mqtt_client_connect(mqtt_client, &sever_ip_addr, SERVER_PORT, mqtt_connect_callback, NULL, &client_info);
if (ret != ERR_OK) {
printf("MQTT mqtt_client: connect to server failed.\r\n");
return ret;
}
while(mqtt_client_is_connected(mqtt_client) == false) {
if ((sys_current_time_get() - connect_time) > 5000) {
printf("MQTT mqtt_client: connect to server timeout.\r\n");
return -3;
}
if (connect_fail_reason > 0) {
mqtt_fail_reason_display((mqtt_connect_return_res_t)connect_fail_reason);
return connect_fail_reason;
}
sys_yield();
}
}
printf("MQTT: Successfully connected to server.\r\n");
return 0;
}GD32VW553通过扫描wifi、连接WiFi、连接到MQTT服务器以及订阅主题的过程
static void wifi_client_connect(void)
{
int status = 0;
char *ssid = SSID;
char *password = PASSWORD;
struct mac_scan_result candidate;
if (ssid == NULL) {
printf("ssid can not be NULL!\r\n");
}
/*
* 1. Start Wi-Fi scan
*/
printf("Start Wi-Fi scan.\r\n");
status = wifi_management_scan(1, ssid);
if (status != 0) {
printf("Wi-Fi scan failed.\r\n");
}
sys_memset(&candidate, 0, sizeof(struct mac_scan_result));
status = wifi_netlink_candidate_ap_find(WIFI_VIF_INDEX_DEFAULT, NULL, ssid, &candidate);
if (status != 0) {
}
/*
* 2. Start Wi-Fi connection
*/
printf("Start Wi-Fi connection.\r\n");
if (wifi_management_connect(ssid, password, 1) != 0) {
printf("Wi-Fi connection failed\r\n");
}
/*
* 3. Start MQTT client
*/
printf("Start MQTT client.\r\n");
if (client_connect() != 0) {
printf("MQTT connect server failed.\r\n");
}
///************新加服务器订阅****************////
if (client_subscribe() != 0) {
printf("MQTT subscribe failed.\r\n");
}
}建立freertos任务,不断往MQTT服务器发数据,同时检测mqtt服务器以及wifi是否断开
static void mqtt_client_task(void *param)
{
uint16_t i;
while(1){
////************检测WiFi以及mqtt是否断开************/////
if (mqtt_client == NULL || !mqtt_client_is_connected(mqtt_client)) {
printf("Connection lost, reconnecting...\r\n");
wifi_client_connect(); // 重新连接
}
/**************发送数据到服务器**************/
if(MPC_ALARM == 0) {
alarm_state = alarm(ADC_CH0,ADC_CH1,ADC_CH2);
sprintf(adc_msg,"{\"ADC0\":%1.f,\"ADC1\":%1.f,\"ADC2\":%1.f,\"alarm\":\"%s\"}",ADC_CH0, ADC_CH1, ADC_CH2,(alarm_state?"on":"off"));
}
if (client_publish(topic_pub, adc_msg, strlen(adc_msg)) != 0) {
printf("MQTT publish failed.\r\n");
}
sys_ms_sleep(200);
}
}微信小程序开发是使用微信开发者工具实现的,具体的开发过程建议B站找寻专业博主学习,小白就不在这里解释了,我这里只简单的叙述一下微信小程序连接MQTT以及从服务器获取数据的过程,
// index.js
// 获取应用实例
var mqtt = require('../../utils/mqtt.min')
var cilent = null
Page({
data: {
temp0:'0',
temp1:'0',
temp2:'0',
temp3:'0',
temp4:'0',
temp5:'0'
},
onLoad(){
this.connectMqtt()
},
connectMqtt:function(){
var that = this
const options = {
connectTimeout:4000,
clientId:'WX-captain',
port:8084,
username:'WX-captain',
password:'926521944'
}
client = mqtt.connect('wxs://broker.emqx.io:8084/mqtt',options)
client.on('connect',(e)=>
{
console.log('服务器连接成功')
client.subscribe('topic_pub_test',{qos:0},function(err){
if(!err)
{
console.log('订阅成功')
}
})
})
client.on('message',function(topic,message){
let tem = {}
tem=JSON.parse(message)
that.setData({
temp0:tem.ADC0,
temp1:tem.ADC1,
temp2:tem.ADC2,
temp3:tem.alarm
})
console.log(tem)
})
},
})
以下是最终实现GD32VW553采集数据通过MATT协议一微信小程序实现通信的演示图:

OK,文章到此技术,纯属文章小白,不喜勿喷,有技术问题欢迎一起讨论!

