Mqtt学习笔记--接入阿里云(2)

概述

在阿里云IoT平台中,MQTT协议是一种重要的连接方式,可以用于设备与平台之间的通信。通过配置设备的Topic和订阅规则,设备可以在平台上发布/订阅消息,实现设备状态的监控、控制和数据的传输。同时,阿里云IoT平台还提供了MQTT SDK和API,方便开发者快速接入平台,实现物联网应用的开发和部署。

我们可以基于Mqtt接口,将自己的设备接入阿里云的IOT平台,这里主要记录实现方法,具体阿里云上的产品、设备创建不再记录,阿里云文档上比较详细了。

下载阿里云的LinkSdk

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

选择C LinkSdk:

我这里没有使用LinkSdk里的mqtt,而是使用上次移植的mosquito,这里下载这个LinkSdk主要是用里面的认证部分(aiot_mqtt_sign.c)。

示例代码

下面代码中,利用aiotMqttSign函数生成clientId,用户名和密码。

aliy.cpp

c 复制代码
#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 error\n");
		return -1;
	}

    printf("clientId: %s\n", clientId);
    printf("username: %s\n", username);
    printf("password: %s\n", password);
    MyMqtt mqtt(clientId, mqtt_host, mqtt_port, username, password);
    mqtt.loop_start(); // 开始循环

    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)
    {
        // 发布消息
        mqtt.publish(NULL, "/sys/hj1skj****/FZ00**/thing/event/property/post", msg.size(), msg.c_str());
        sleep(5);
    }
    mqtt.loop_stop(); // 停止循环
    return 0;
}

这个代码中是定时上传一串模拟的温湿度数据,这个Json的字段名称和阿里云IOT平台上的设备模型字段保持一致,这样平台才能够正常解析。

json 复制代码
{
    "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"
}

测试效果

将代码编译测试后,放在板子上运行,需要注意的时候,板子上的系统要支持域名解析,配置好DNS。订阅与发布的消息主题与平台上也要保持一致。

运行:

sh 复制代码
./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"}

平台上显示设备已经在线:

查看一下设备数据日志:

设备物理模型数据:

相关推荐
chenglin01614 分钟前
阿里云——应用交付与负载均衡
阿里云·云计算·负载均衡
long3161 小时前
状态设计模式
java·学习·程序人生·设计模式·状态模式·state-pattern
天下琴川1 小时前
Dify智能体平台二次开发笔记(10):企业微信5.0 智能机器人对接 Dify 智能体
笔记·机器人·企业微信
njsgcs1 小时前
部署网页在服务器(公网)上笔记 infinityfree 写一个找工作单html文件的网站
笔记
传奇开心果编程2 小时前
【传奇开心果系列】Flet框架实现的图形化界面的PDF转word转换器办公小工具自定义模板
前端·python·学习·ui·前端框架·pdf·word
chenglin0162 小时前
阿里云——计算服务深度解析与选型
阿里云·云计算
yjx233324 小时前
并行多核体系结构基础——共享存储并行编程与针对链式数据结构的并行编程(笔记)
笔记
越前君4 小时前
如何开发一个 Raycast 扩展?
前端·笔记
人生游戏牛马NPC1号4 小时前
学习 Android (十七) 学习 OpenCV (二)
android·opencv·学习
悠哉悠哉愿意4 小时前
【机器学习学习笔记】机器学习引言
笔记·学习·机器学习