文章目录
MQTT协议介绍
MQTT(Message Queuing Telemetry Transport,消息队列遥测传输)是一个轻量级的消息传输协议,设计用于低带宽、不可靠网络环境下的设备通信,特别适合物联网,下面是一些关于MQTT的基本介绍:
基本概念
-
发布/订阅模型:
- 发布者(Publisher):发送消息的设备。
- 订阅者(Subscriber):接收消息的设备。
- 代理(Broker):中介服务器,负责接收发布者的消息并将其分发给相应的订阅者。
- 主题(Topic):消息分类的标签,发布者将消息发布到某个主题,订阅者订阅感兴趣的主题。
-
轻量级:由于其设计的简单性和有效性,MQTT协议在网络带宽占用和计算资源使用方面非常节省,适合在低功耗设备和不稳定网络环境下使用。
工作机制
-
连接建立:
- 设备与MQTT Broker建立连接时,需要提供客户端ID,这个ID是唯一的,用于标识每个设备。
- 连接还可以设置一些参数,如保持连接时间(Keep Alive)、清除会话(Clean Session)等。
-
消息发布:
- 发布者向Broker发送消息时,需要指定消息的主题,Broker会根据主题将消息转发给所有订阅了该主题的订阅者。
-
消息订阅:
- 订阅者向Broker发送订阅请求,指定感兴趣的主题,Broker会将与该主题相关的所有消息转发给订阅者。
-
消息传递质量:
- MQTT支持三种消息传递质量(QoS)级别:
- QoS 0:最多一次传递,消息可能丢失或重复("At most once")。
- QoS 1:至少一次传递,确保消息到达,但可能重复("At least once")。
- QoS 2:只有一次传递,确保消息到达且不重复("Exactly once")。
- MQTT支持三种消息传递质量(QoS)级别:
-
断开连接:
- 设备可以主动断开与Broker的连接,也可以设置遗嘱消息(Will Message),在连接异常断开时,Broker会向指定主题发布遗嘱消息。
安全性
MQTT本身并未内置强制的安全机制,但可以通过以下方式增强安全性:
- TLS/SSL:使用TLS/SSL加密通信,防止数据被窃听和篡改。
- 认证与授权:通过用户名和密码进行客户端身份验证,并使用ACL(访问控制列表)进行权限管理。
编写代码
首先,在./applications/BearPi/BearPi-HM_Nano/sample路径下新建文件夹,重命名为iot_mqtt,添加源码iot_mqtt.c和BUILD.gn,添加src和include文件夹,src中新建文件wifi_connect.c,include文件夹中存放wifi_connect.h;在BUILD.gn文件中添加以下内容:
static_library("iot_mqtt") {
sources = [
"iot_mqtt.c",
"src/wifi_connect.c",
]
cflags = [ "-Wno-unused-variable" ]
cflags += [ "-Wno-unused-but-set-variable" ]
include_dirs = [
"//utils/native/lite/include",
"//kernel/liteos_m/components/cmsis/2.0",
"//base/iot_hardware/interfaces/kits/wifiiot_lite",
"//foundation/communication/interfaces/kits/wifi_lite/wifiservice",
"//vendor/hisi/hi3861/hi3861/third_party/lwip_sack/include/",
"//third_party/cJSON",
"//third_party/paho_mqtt/MQTTPacket/src",
"//third_party/paho_mqtt/MQTTClient-C/src",
"include"
]
deps = [
"//third_party/paho_mqtt:pahomqtt_static",
]
}
在wifi_connect.c中添加以下内容:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "lwip/netif.h"
#include "lwip/netifapi.h"
#include "lwip/ip4_addr.h"
#include "lwip/api_shell.h"
#include "cmsis_os2.h"
#include "hos_types.h"
#include "wifi_device.h"
#include "wifiiot_errno.h"
#include "ohos_init.h"
#define DEF_TIMEOUT 15
#define ONE_SECOND 1
#define SELECT_WIFI_SECURITYTYPE WIFI_SEC_TYPE_PSK
static void WiFiInit(void);
static void WaitSacnResult(void);
static int WaitConnectResult(void);
static void OnWifiScanStateChangedHandler(int state, int size);
static void OnWifiConnectionChangedHandler(int state, WifiLinkedInfo *info);
static void OnHotspotStaJoinHandler(StationInfo *info);
static void OnHotspotStateChangedHandler(int state);
static void OnHotspotStaLeaveHandler(StationInfo *info);
static int g_staScanSuccess = 0;
static int g_ConnectSuccess = 0;
static int ssid_count = 0;
WifiEvent g_wifiEventHandler = {0};
WifiErrorCode error;
#define SELECT_WLAN_PORT "wlan0"
int WifiConnect(const char *ssid, const char *psk)
{
WifiScanInfo *info = NULL;
unsigned int size = WIFI_SCAN_HOTSPOT_LIMIT;
static struct netif *g_lwip_netif = NULL;
osDelay(200);
printf("<--System Init-->\r\n");
//初始化WIFI
WiFiInit();
//使能WIFI
if (EnableWifi() != WIFI_SUCCESS)
{
printf("EnableWifi failed, error = %d\r\n", error);
return -1;
}
//判断WIFI是否激活
if (IsWifiActive() == 0)
{
printf("Wifi station is not actived.\r\n");
return -1;
}
//分配空间,保存WiFi信息
info = malloc(sizeof(WifiScanInfo) * WIFI_SCAN_HOTSPOT_LIMIT);
if (info == NULL)
{
return -1;
}
//轮询查找WiFi列表
do{
//重置标志位
ssid_count = 0;
g_staScanSuccess = 0;
//开始扫描
Scan();
//等待扫描结果
WaitSacnResult();
//获取扫描列表
error = GetScanInfoList(info, &size);
}while(g_staScanSuccess != 1);
//打印WiFi列表
printf("********************\r\n");
for(uint8_t i = 0; i < ssid_count; i++)
{
printf("no:%03d, ssid:%-30s, rssi:%5d\r\n", i+1, info[i].ssid, info[i].rssi/100);
}
printf("********************\r\n");
//连接指定的WiFi热点
for(uint8_t i = 0; i < ssid_count; i++)
{
if (strcmp(ssid, info[i].ssid) == 0)
{
int result;
printf("Select:%3d wireless, Waiting...\r\n", i+1);
//拷贝要连接的热点信息
WifiDeviceConfig select_ap_config = {0};
strcpy(select_ap_config.ssid, info[i].ssid);
strcpy(select_ap_config.preSharedKey, psk);
select_ap_config.securityType = SELECT_WIFI_SECURITYTYPE;
if (AddDeviceConfig(&select_ap_config, &result) == WIFI_SUCCESS)
{
if (ConnectTo(result) == WIFI_SUCCESS && WaitConnectResult() == 1)
{
printf("WiFi connect succeed!\r\n");
g_lwip_netif = netifapi_netif_find(SELECT_WLAN_PORT);
break;
}
}
}
if(i == ssid_count-1)
{
printf("ERROR: No wifi as expected\r\n");
while(1) osDelay(100);
}
}
//启动DHCP
if (g_lwip_netif)
{
dhcp_start(g_lwip_netif);
printf("begain to dhcp\r\n");
}
//等待DHCP
for(;;)
{
if(dhcp_is_bound(g_lwip_netif) == ERR_OK)
{
printf("<-- DHCP state:OK -->\r\n");
//打印获取到的IP信息
netifapi_netif_common(g_lwip_netif, dhcp_clients_info_show, NULL);
break;
}
printf("<-- DHCP state:Inprogress -->\r\n");
osDelay(100);
}
osDelay(100);
return 0;
}
static void WiFiInit(void)
{
printf("<--Wifi Init-->\r\n");
g_wifiEventHandler.OnWifiScanStateChanged = OnWifiScanStateChangedHandler;
g_wifiEventHandler.OnWifiConnectionChanged = OnWifiConnectionChangedHandler;
g_wifiEventHandler.OnHotspotStaJoin = OnHotspotStaJoinHandler;
g_wifiEventHandler.OnHotspotStaLeave = OnHotspotStaLeaveHandler;
g_wifiEventHandler.OnHotspotStateChanged = OnHotspotStateChangedHandler;
error = RegisterWifiEvent(&g_wifiEventHandler);
if (error != WIFI_SUCCESS)
{
printf("register wifi event fail!\r\n");
}
else
{
printf("register wifi event succeed!\r\n");
}
}
static void OnWifiScanStateChangedHandler(int state, int size)
{
if (size > 0)
{
ssid_count = size;
g_staScanSuccess = 1;
}
printf("callback function for wifi scan:%d, %d\r\n", state, size);
return;
}
static void OnWifiConnectionChangedHandler(int state, WifiLinkedInfo *info)
{
if (info == NULL)
{
printf("WifiConnectionChanged:info is null, stat is %d.\n", state);
}
else
{
if (state == WIFI_STATE_AVALIABLE)
{
g_ConnectSuccess = 1;
}
else
{
g_ConnectSuccess = 0;
}
}
}
static void OnHotspotStaJoinHandler(StationInfo *info)
{
(void)info;
printf("STA join AP\n");
return;
}
static void OnHotspotStaLeaveHandler(StationInfo *info)
{
(void)info;
printf("HotspotStaLeave:info is null.\n");
return;
}
static void OnHotspotStateChangedHandler(int state)
{
printf("HotspotStateChanged:state is %d.\n", state);
return;
}
static void WaitSacnResult(void)
{
int scanTimeout = DEF_TIMEOUT;
while (scanTimeout > 0)
{
sleep(ONE_SECOND);
scanTimeout--;
if (g_staScanSuccess == 1)
{
printf("WaitSacnResult:wait success[%d]s\n", (DEF_TIMEOUT - scanTimeout));
break;
}
}
if (scanTimeout <= 0)
{
printf("WaitSacnResult:timeout!\n");
}
}
static int WaitConnectResult(void)
{
int ConnectTimeout = DEF_TIMEOUT;
while (ConnectTimeout > 0)
{
sleep(ONE_SECOND);
ConnectTimeout--;
if (g_ConnectSuccess == 1)
{
printf("WaitConnectResult:wait success[%d]s\n", (DEF_TIMEOUT - ConnectTimeout));
break;
}
}
if (ConnectTimeout <= 0)
{
printf("WaitConnectResult:timeout!\n");
return 0;
}
return 1;
}
在wifi_connect.h中添加以下内容:
#ifndef __WIFI_CONNECT_H__
#define __WIFI_CONNECT_H__
int WifiConnect(const char *ssid,const char *psk);
#endif /* __WIFI_CONNECT_H__ */
在iot_mqtt.c中添加以下内容:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "wifi_connect.h"
#include "MQTTClient.h"
static unsigned char sendBuf[1000];
static unsigned char readBuf[1000];
Network network;
void messageArrived(MessageData* data)
{
printf("Message arrived on topic %.*s: %.*s\n", data->topicName->lenstring.len, data->topicName->lenstring.data,
data->message->payloadlen, data->message->payload);
}
/* */
static void MQTT_DemoTask(void)
{
WifiConnect("wifi","123456");
printf("Starting ...\n");
int rc, count = 0;
MQTTClient client;
NetworkInit(&network);
printf("NetworkConnect ...\n");
begin:
NetworkConnect(&network, "81.70.36.7", 1883);
printf("MQTTClientInit ...\n");
MQTTClientInit(&client, &network, 2000, sendBuf, sizeof(sendBuf), readBuf, sizeof(readBuf));
MQTTString clientId = MQTTString_initializer;
clientId.cstring = "bearpi";
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
data.clientID = clientId;
data.willFlag = 0;
data.MQTTVersion = 3;
data.keepAliveInterval = 0;
data.cleansession = 1;
printf("MQTTConnect ...\n");
rc = MQTTConnect(&client, &data);
if (rc != 0) {
printf("MQTTConnect: %d\n", rc);
NetworkDisconnect(&network);
MQTTDisconnect(&client);
osDelay(200);
goto begin;
}
printf("MQTTSubscribe ...\n");
rc = MQTTSubscribe(&client, "hi3861", 2, messageArrived);
if (rc != 0) {
printf("MQTTSubscribe: %d\n", rc);
osDelay(200);
goto begin;
}
while (++count)
{
MQTTMessage message;
char payload[30];
message.qos = 2;
message.retained = 0;
message.payload = payload;
sprintf(payload, "hi3861 online");
message.payloadlen = strlen(payload);
if ((rc = MQTTPublish(&client, "hi3861", &message)) != 0){
printf("Return code from MQTT publish is %d\n", rc);
NetworkDisconnect(&network);
MQTTDisconnect(&client);
goto begin;
}
osDelay(50);
}
}
static void MQTT_Demo(void)
{
osThreadAttr_t attr;
attr.name = "MQTT_DemoTask";
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = 10240;
attr.priority = osPriorityNormal;
if (osThreadNew((osThreadFunc_t)MQTT_DemoTask, NULL, &attr) == NULL) {
printf("[MQTT_Demo] Falied to create MQTT_DemoTask!\n");
}
}
APP_FEATURE_INIT(MQTT_Demo);
其中,WifiConnect函数设置wifi的AP和PASSWD,NetworkInit和NetworkConnect函数初始化tcp网络连接,MQTTClientInit和MQTTConnect函数连接MQTT服务器
在./applications/BearPi/BearPi-HM_Nano/sample下的BUILD.gn文件中屏蔽其他应用,并添加如下代码:
"iot_mqtt:iot_mqtt"
编译代码并生成bin文件:
烧录测试
具体的烧录方式可参考官方文档,这里不多赘述。下面是结果:
串口打印
服务器显示节点在线
总结
hi3861通过编写build.gn来代替makefile,其他方面与带有rtos的开发方式基本一致