在阿里云IoT平臺中,MQTT協(xié)議是一種重要的連接方式,可以用于設備與平臺之間的通信。通過配置設備的Topic和訂閱規(guī)則,設備可以在平臺上發(fā)布/訂閱消息,實現(xiàn)設備狀態(tài)的監(jiān)控、控制和數(shù)據(jù)的傳輸。同時,阿里云IoT平臺還提供了MQTT SDK和API,方便開發(fā)者快速接入平臺,實現(xiàn)物聯(lián)網(wǎng)應用的開發(fā)和部署。我們可以基于Mqtt接口,將自己的設備接入阿里云的IOT平臺,這里主要記錄實現(xiàn)方法,具體阿里云上的產品、設備創(chuàng)建不再記錄,阿里云文檔上比較詳細了。
下載阿里云的LinkSdk
地址:https://help.aliyun.com/zh/iot/user-guide/download-device-sdks選擇C LinkSdk:
選擇C LinkSdk
我這里沒有使用LinkSdk里的mqtt,而是使用上次移植的mosquito,這里下載這個LinkSdk主要是用里面的認證部分(aiot_mqtt_sign.c)。
示例代碼
下面代碼中,利用aiotMqttSign函數(shù)生成clientId,用戶名和密碼。
aliy.cpp
#include?"mosquittopp.h"
#include?<stdio.h>
#include?<string.h>
#include?<unistd.h>
#include?<stdlib.h>
#include?<iostream>
extern?int?aiotMqttSign(const?char?*productKey,?const?char?*deviceName,?const?char?*deviceSecret,?
?????????????????????????char?clientId[150],?char?username[65],?char?password[65]);
using?namespace?std;
#define?EXAMPLE_PRODUCT_KEY???"hj1skja****"
#define?EXAMPLE_DEVICE_NAME???"FZ00**"
#define?EXAMPLE_DEVICE_SECRET???????"7bb8c2cfb69***************************"
class?MyMqtt?:?public?mosqpp::mosquittopp
{
public:
????MyMqtt(const?char?*id,?const?char?*host,?int?port,?const?char?*username,?const?char?*password)?:?mosquittopp(id)
????{
????????mosqpp::lib_init();?//?初始化mosquitto庫
????????username_pw_set(username,?password);?//?設置用戶名和密碼
????????connect(host,?port,?60);?//?連接到MQTT服務器
????}
????~MyMqtt()
????{
????????disconnect();?//?斷開連接
????????mosqpp::lib_cleanup();?//?清理mosquitto庫
????}
????void?on_connect(int?rc)
????{
????????if?(rc?==?0)
????????{
????????????std::cout?<<?"連接成功"?<<?std::endl;
????????????subscribe(NULL,?"/sys/hj1skj****/FZ00**/thing/event/property/post_reply",?0);?//?訂閱主題
????????????subscribe(NULL,?"/sys/hj1skj****/FZ00**/thing/event/property/set",?0);?//?訂閱主題
????????}
????????else
????????{
????????????std::cout?<<?"連接失敗"?<<?std::endl;
????????}
????}
????void?on_message(const?struct?mosquitto_message?*message)
????{
????????std::cout?<<?"收到消息:"?<<?(char?*)message->payload?<<?std::endl;
????}
};
int?main(int?argc,?char?*argv[])
{
????const?char?*mqtt_host?=?"hj1skja****.iot-as-mqtt.cn-shanghai.aliyuncs.com";
????int?mqtt_port?=?1883;
????char?clientId[256]?=?{0};
????char?username[65]?=?{0};
????char?password[65]?=?{0};
????if?(aiotMqttSign(EXAMPLE_PRODUCT_KEY,?EXAMPLE_DEVICE_NAME,?EXAMPLE_DEVICE_SECRET,?clientId,?username,?password)?<?0)?{
????????printf("aiotMqttSign?errorn");
????????return?-1;
????}
????printf("clientId:?%sn",?clientId);
????printf("username:?%sn",?username);
????printf("password:?%sn",?password);
????MyMqtt?mqtt(clientId,?mqtt_host,?mqtt_port,?username,?password);
????mqtt.loop_start();?//?開始循環(huán)
????string?msg="{"params":{"CurrentTemperature":27.37,"CurrentHumidity":56.8,"version":"ver1.0.1","GeoLocation":{"Longitude":113.987817,"Latitude":34.987895,"Altitude":123.1,"CoordinateSystem":1}}}";
????while?(1)
????{
????????//?發(fā)布消息
????????mqtt.publish(NULL,?"/sys/hj1skj****/FZ00**/thing/event/property/post",?msg.size(),?msg.c_str());
????????sleep(5);
????}
????mqtt.loop_stop();?//?停止循環(huán)
????return?0;
}
這個代碼中是定時上傳一串模擬的溫濕度數(shù)據(jù),這個Json的字段名稱和阿里云IOT平臺上的設備模型字段保持一致,這樣平臺才能夠正常解析。
{
????"params":{
????????"CurrentTemperature":27.37,
????????"CurrentHumidity":56.8,
????????"version":"ver1.0.1",
????????"GeoLocation":{
????????????"Longitude":113.987817,
????????????"Latitude":34.987895,
????????????"Altitude":123.1,
????????????"CoordinateSystem":1
????????}
????},
????"time":"2022-03-05_00:45:26"
}
測試效果
將代碼編譯測試后,放在板子上運行,需要注意的時候,板子上的系統(tǒng)要支持域名解析,配置好DNS。訂閱與發(fā)布的消息主題與平臺上也要保持一致。運行:
./aliy_mqtt?
macSrc:?clientIdFZ0***&hj1skja****deviceNameFZ0001productKeyhj1s****timestamp2524608000000
clientId:?FZ00***&hj1skja****|timestamp=2524608000000,_v=paho-c-1.0.0,securemode=3,signmethod=hmacsha256,lan=C|
username:?FZ0***&hj1skjaDSpk
password:?6CC4F399F59A1CDB2D355DB449BB741AF5C3713C9F9B**************
連接成功
收到消息:{"params":{"CurrentTemperature":27.37,"CurrentHumidity":56.8,"version":"ver1.0.1","GeoLocation":{"Longitude":113.987817,"Latitude":34.987895,"Altitude":123.1,"CoordinateSystem":1}},"time":"2022-03-05_00:45:26"}
收到消息:{"code":200,"data":{},"id":"null","message":"success","method":"thing.event.property.post","version":"1.0"}
收到消息:{"params":{"CurrentTemperature":27.37,"CurrentHumidity":56.8,"version":"ver1.0.1","GeoLocation":{"Longitude":113.987817,"Latitude":34.987895,"Altitude":123.1,"CoordinateSystem":1}},"time":"2022-03-05_00:45:26"}
收到消息:{"code":200,"data":{},"id":"null","message":"success","method":"thing.event.property.post","version":"1.0"}
平臺上顯示設備已經在線:
在線
查看一下設備數(shù)據(jù)日志:
設備日志
設備物理模型數(shù)據(jù):
設備物理模型數(shù)據(jù)
源代碼已上傳:https://gitee.com/fensnote/demo_code/tree/master/mqtt-aliyun-demo