加入星計(jì)劃,您可以享受以下權(quán)益:

  • 創(chuàng)作內(nèi)容快速變現(xiàn)
  • 行業(yè)影響力擴(kuò)散
  • 作品版權(quán)保護(hù)
  • 300W+ 專業(yè)用戶
  • 1.5W+ 優(yōu)質(zhì)創(chuàng)作者
  • 5000+ 長(zhǎng)期合作伙伴
立即加入
  • 正文
  • 推薦器件
  • 相關(guān)推薦
  • 電子產(chǎn)業(yè)圖譜
申請(qǐng)入駐 產(chǎn)業(yè)圖譜

Mqtt學(xué)習(xí)筆記--接入阿里云(2)

2023/09/07
3677
閱讀需 15 分鐘
加入交流群
掃碼加入
獲取工程師必備禮包
參與熱點(diǎn)資訊討論

在阿里云IoT平臺(tái)中,MQTT協(xié)議是一種重要的連接方式,可以用于設(shè)備與平臺(tái)之間的通信。通過配置設(shè)備的Topic和訂閱規(guī)則,設(shè)備可以在平臺(tái)上發(fā)布/訂閱消息,實(shí)現(xiàn)設(shè)備狀態(tài)的監(jiān)控、控制和數(shù)據(jù)的傳輸。同時(shí),阿里云IoT平臺(tái)還提供了MQTT SDKAPI,方便開發(fā)者快速接入平臺(tái),實(shí)現(xiàn)物聯(lián)網(wǎng)應(yīng)用的開發(fā)和部署。我們可以基于Mqtt接口,將自己的設(shè)備接入阿里云的IOT平臺(tái),這里主要記錄實(shí)現(xiàn)方法,具體阿里云上的產(chǎn)品、設(shè)備創(chuàng)建不再記錄,阿里云文檔上比較詳細(xì)了。

下載阿里云的LinkSdk

地址:https://help.aliyun.com/zh/iot/user-guide/download-device-sdks選擇C LinkSdk:

選擇C LinkSdk

我這里沒有使用LinkSdk里的mqtt,而是使用上次移植的mosquito,這里下載這個(gè)LinkSdk主要是用里面的認(rèn)證部分(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);?//?設(shè)置用戶名和密碼
????????connect(host,?port,?60);?//?連接到MQTT服務(wù)器
????}

????~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;
}

這個(gè)代碼中是定時(shí)上傳一串模擬的溫濕度數(shù)據(jù),這個(gè)Json的字段名稱和阿里云IOT平臺(tái)上的設(shè)備模型字段保持一致,這樣平臺(tái)才能夠正常解析。

{
????"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"
}

測(cè)試效果

將代碼編譯測(cè)試后,放在板子上運(yùn)行,需要注意的時(shí)候,板子上的系統(tǒng)要支持域名解析,配置好DNS。訂閱與發(fā)布的消息主題與平臺(tái)上也要保持一致。運(yùn)行:

./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"}

平臺(tái)上顯示設(shè)備已經(jīng)在線:

在線

查看一下設(shè)備數(shù)據(jù)日志:

設(shè)備日志

設(shè)備物理模型數(shù)據(jù):

設(shè)備物理模型數(shù)據(jù)

源代碼已上傳:https://gitee.com/fensnote/demo_code/tree/master/mqtt-aliyun-demo

推薦器件

更多器件
器件型號(hào) 數(shù)量 器件廠商 器件描述 數(shù)據(jù)手冊(cè) ECAD模型 風(fēng)險(xiǎn)等級(jí) 參考價(jià)格 更多信息
SN74LVC1G08DCKRE4 1 Texas Instruments Single 2-input, 1.65-V to 5.5-V AND gate 5-SC70 -40 to 125

ECAD模型

下載ECAD模型
$0.37 查看
HFBR-2412MZ 1 Broadcom Limited Receiver,
$243.12 查看
CY62167DV30LL-55BVXIT 1 Cypress Semiconductor Standard SRAM, 1MX16, 55ns, CMOS, PBGA48, 8 X 9.50 MM, 1 MM HEIGHT, LEAD FREE, VFBGA-48
暫無數(shù)據(jù) 查看

相關(guān)推薦

電子產(chǎn)業(yè)圖譜

專注嵌入式Linux應(yīng)用開發(fā),C/C++技術(shù)交流;Qt編程技術(shù)交流。