我是靠谱客的博主 着急巨人,最近开发中收集的这篇文章主要介绍通过mqtt工具,连接阿里云iot进行快速调试,部署基于.net core3.1对接阿里云iot的mqtt协议的源代码(wpf),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

基于.net core3.1对接阿里云iot的mqtt协议的源代码(wpf)

通过阿里云的ProductKey,DeviceName,DeviceSecret,RegionId三元组数据,执行以下代码,

app.config文件

<add name="ProductKey" connectionString="a1eAKoQBN"/>
<add name="DeviceName" connectionString="test"/>
<add name="DeviceSecret" connectionString="2d9c5027f05f8e047dba4ef7bd8e24"/>
<add name="RegionId" connectionString="cn-shanghai"/>
<add name="PubTopic" connectionString="user/update"/>
<add name="SubTopic" connectionString="user/get"/>

main文件

using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;
//iot
private static readonly string ProductKey = ConfigurationManager.ConnectionStrings["ProductKey"].ConnectionString;
private static readonly string DeviceName = ConfigurationManager.ConnectionStrings["DeviceName"].ConnectionString;
private static readonly string DeviceSecret = ConfigurationManager.ConnectionStrings["DeviceSecret"].ConnectionString;
private static readonly string RegionId = ConfigurationManager.ConnectionStrings["RegionId"].ConnectionString;
private static readonly string PubTopicUser = "/" + ProductKey + "/" + DeviceName + "/" + ConfigurationManager.ConnectionStrings["PubTopic"].ConnectionString;
private static readonly string SubTopicUser = "/" + ProductKey + "/" + DeviceName + "/" + ConfigurationManager.ConnectionStrings["SubTopic"].ConnectionString;
private static readonly string PubTopic = "/sys/" + ProductKey + "/" + DeviceName + "/" + "thing/event/property/post";
private static readonly string SubTopic = "/sys/" + ProductKey + "/" + DeviceName + "/" + "thing/service/property/set";								

IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());
string clientId = host.AddressList.FirstOrDefault(ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).ToString();
string t = Convert.ToString(DateTimeOffset.Now.ToUnixTimeMilliseconds());
string signmethod = "hmacmd5";
Dictionary<string, string> dict = new Dictionary<string, string>
{
    { "productKey", ProductKey },
    { "deviceName", DeviceName },
    { "clientId", clientId },
    { "timestamp", t }
};

string mqttBrokerAddress = ProductKey + ".iot-as-mqtt." + RegionId + ".aliyuncs.com";
string mqttClientId = clientId + "|securemode=3,signmethod=" + signmethod + ",timestamp=" + t + "|";
string mqttUserName = DeviceName + "&" + ProductKey;
string mqttPassword = Sign(dict, DeviceSecret, signmethod);

获得BrokerAddress,ClientId,UserName,Password,然后填写到mqtt.fx即可连接

在这里插入图片描述

可参考阿里官网的topic

在这里插入图片描述

  1. 使用参数:securemode=3,不需要对ssl/tls进行设置;
  2. 加密方式:使用hmacmd5加密方式;
  3. 此处没有上传生成的exe文件。如需其他网关盒子进行对接阿里云平台的资源。
  4. github项目拉取:https://github.com/karrem/aliyunIot-mqtt-tool

最后

以上就是着急巨人为你收集整理的通过mqtt工具,连接阿里云iot进行快速调试,部署基于.net core3.1对接阿里云iot的mqtt协议的源代码(wpf)的全部内容,希望文章能够帮你解决通过mqtt工具,连接阿里云iot进行快速调试,部署基于.net core3.1对接阿里云iot的mqtt协议的源代码(wpf)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(39)

评论列表共有 0 条评论

立即
投稿
返回
顶部