C#對(duì)接阿里云IOT平臺(tái)進(jìn)行設(shè)備開(kāi)發(fā)
一,創(chuàng)建阿里云 IOT 產(chǎn)品、設(shè)備
目前阿里云每月贈(zèng)送 100 萬(wàn)條流量,可以免費(fèi)使用基礎(chǔ)版、高級(jí)版,開(kāi)通后即可免費(fèi)使用。
阿里云 IOT 平臺(tái)地址https://iot.console.aliyun.com/product
登陸開(kāi)通后,新建測(cè)試產(chǎn)品、設(shè)備。
創(chuàng)建產(chǎn)品

產(chǎn)品的定義是,一類硬件、功能、外形完全相同的設(shè)備。所以,添加一個(gè)產(chǎn)品后,我們就可以在此類別下添加成千上萬(wàn)個(gè)設(shè)備。
下面的信息要選、設(shè)備、非網(wǎng)關(guān),原因后面說(shuō)。

設(shè)定一個(gè)產(chǎn)品
創(chuàng)建一個(gè)產(chǎn)品后,就需要設(shè)定這個(gè)產(chǎn)品的特征,它有那些功能?它可以進(jìn)行什么樣的通訊?

定義兩個(gè)topic,一個(gè)發(fā)布、一個(gè)訂閱
名稱為test1、test2

添加一個(gè)設(shè)備,記下你設(shè)備的那些設(shè)備名等信息

二,下載SDK、創(chuàng)建項(xiàng)目
新建一個(gè) .NET Frameork4 控制臺(tái)項(xiàng)目,名稱AlyIotIest
添加引用,把下載的 dll 添加進(jìn)去
新建一個(gè)類BackMessage.cs
把以下代碼復(fù)制進(jìn)BackMessage(清空以前的代碼),先不用管為什么
using iotxashttp2netsdk.iot.auth.common;
using iotxashttp2netsdk.iot.callback;
using System;
using System.Collections.Generic;
using System.Text;
namespace AlyIotIest
{
public class 默認(rèn)回調(diào) : IHttp2MessageCallback
{
public ConsumeAction Consume(Http2ConsumeMessage http2ConsumeMessage)
{
Console.WriteLine("默認(rèn)回調(diào)");
Console.WriteLine(http2ConsumeMessage.MessageId);
if (http2ConsumeMessage.Payload.Length != 0)
{
Console.WriteLine("收到平臺(tái)消息:");
string a = Encoding.ASCII.GetString(http2ConsumeMessage.Payload);
Console.WriteLine(a);
}
return ConsumeAction.CommitSuccess;
}
}
public class 自定義回調(diào) : IHttp2MessageCallback
{
public ConsumeAction Consume(Http2ConsumeMessage http2ConsumeMessage)
{
Console.WriteLine("自定義回調(diào) ");
Console.WriteLine(http2ConsumeMessage.MessageId);
if (http2ConsumeMessage.Payload.Length != 0)
{
Console.WriteLine("收到平臺(tái)消息:");
string a = Encoding.ASCII.GetString(http2ConsumeMessage.Payload);
Console.WriteLine(a);
}
return ConsumeAction.CommitSuccess;
}
}
}三,開(kāi)始操作、準(zhǔn)備工作
1,Program.cs 需要用到以下命名空間
using System; using iotxashttp2netsdk.iot.auth.common; using iotxashttp2netsdk.iot.message; using System.Net; using System.Linq; using System.Text;
2,設(shè)定一個(gè)設(shè)備客戶端對(duì)象
復(fù)制static MessageClient client; 到Program類鐘
class Program
{
// 步驟 1 定義設(shè)備客戶端 #
static MessageClient client;
...
...
}MessageClient 是核心,用于連接服務(wù)器、通訊、訂閱和發(fā)布Topic、觸發(fā)任務(wù),先不管他

四,開(kāi)始寫代碼
在 Main 方法中寫代碼
步驟 2,添加密鑰信息,在相應(yīng)位置填上你的密鑰
// 步驟 2 從控制臺(tái)獲取 productKey、deviceName、deviceSecret 信息 #
// 到阿里云IOT物聯(lián)網(wǎng)后臺(tái)設(shè)置產(chǎn)品、添加設(shè)備后,可以找到
string productKey = " ";
string deviceName = " i";
string deviceSecret = " ";步驟 3
// 步驟 3 標(biāo)記 設(shè)定設(shè)備唯一識(shí)別符 clientid
// 阿里云官方給的例子是 子網(wǎng)IP
IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());
// 客戶端設(shè)備唯一標(biāo)記
string clientId = host.AddressList.FirstOrDefault(
ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).ToString();
// 地域可用區(qū) ,根據(jù)實(shí)際修改
string regionId = "cn-shanghai";
string domain = ".aliyuncs.com";
string endpoint = "https://" + productKey + ".iot-as-http2." + regionId + domain;步驟 4
//設(shè)置配置服務(wù)和構(gòu)建客戶端
//連接服務(wù)配置項(xiàng)
Profile profile = new Profile();
profile.ProductKey = productKey;
profile.DeviceName = deviceName;
profile.DeviceSecret = deviceSecret;
profile.Url = endpoint;
profile.ClientId = clientId;
//刪除堆積消息
profile.CleanSession = true;
//qos>0消息,SDK發(fā)生異常時(shí)可以設(shè)置重,重試次數(shù)最大為3次
profile.RetryPubCount = 3;
//重試間隔時(shí)間單位為s(秒)
profile.RetryIntervalTime = 2;
profile.GetDeviceAuthParams();
//構(gòu)造客戶端
client = new MessageClient(profile);步驟 5
// 設(shè)置訂閱和發(fā)布的 topic
string topic = "/" + productKey + "/" + deviceName + "/user/test1";
string topic2 = "/" + productKey + "/" + deviceName + "/user/test2";
// 接收數(shù)據(jù),剛連接時(shí)
// 只生效一次
// 默認(rèn)回調(diào)
client.DoConnection(new 默認(rèn)回調(diào)());
//回調(diào)
// 自定義一個(gè) IHttp2MessageCallback,每次收到消息都用此回調(diào)
client.SetMessageListener(topic2,new 自定義回調(diào)());
client.DoSubscribe((string)topic ,msg=> {
Console.WriteLine ("訂閱服務(wù)端消息");
Console.WriteLine("msg.Code" + msg.Code);
Console.WriteLine("topic:" + msg.Message.Topic);
Console.WriteLine("msg.Message");
Console.WriteLine("body: " + Encoding.ASCII.GetString(msg.Body));
Console.WriteLine(msg.Message.MessageId);
});
client.DoSubscribe((string)topic2, msg=>
{
Console.WriteLine("訂閱服務(wù)端消息");
Console.WriteLine("msg.Code" + msg.Code);
Console.WriteLine("topic:" + msg.Message.Topic);
Console.WriteLine("msg.Message");
Console.WriteLine("body: " + Encoding.ASCII.GetString(msg.Body));
Console.WriteLine(msg.Message.MessageId);
});步驟 6
添加一個(gè)方法
public static void 發(fā)數(shù)據(jù)(string topic,string str)
{
//發(fā)消息
Message message = new Message();
message.Payload = Encoding.ASCII.GetBytes(str);
message.Qos = 1;
client.DoPublish(topic, message, msg =>
{
Console.WriteLine("publish topic message, messageId: " + msg.Message.MessageId
+ "|| topic:" + msg.Message.Topic
+ "|| code: " + msg.Code
+ "|| body: " + Encoding.ASCII.GetString(msg.Body));
});
}在 Main 方法內(nèi),后面加上
while (true)
{
Console.WriteLine("輸入數(shù)據(jù)");
string str = Console.ReadLine();
if (str.ToUpper() == "EXIT")
{
break;
}
發(fā)數(shù)據(jù)("/" + productKey + "/" + deviceName +"/user/test1", str);
}
Console.ReadKey();運(yùn)行你的項(xiàng)目
五,運(yùn)行測(cè)試

到此這篇關(guān)于C#對(duì)接阿里云IOT平臺(tái)進(jìn)行設(shè)備開(kāi)發(fā)的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Unity使用EzySlice實(shí)現(xiàn)模型多邊形順序切割
這篇文章主要為大家詳細(xì)介紹了Unity使用EzySlice實(shí)現(xiàn)模型多邊形順序切割,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07
c#訪問(wèn)this關(guān)鍵字和base關(guān)鍵字示例
this關(guān)鍵字引用類的當(dāng)前實(shí)例。靜態(tài)成員方法中不能使用this關(guān)鍵字,this關(guān)鍵字只能在實(shí)例構(gòu)造函數(shù)、實(shí)例方法或?qū)嵗L問(wèn)器中使用。base關(guān)鍵字用于從派生類中訪問(wèn)基類的成員。下面學(xué)習(xí)一下這二個(gè)關(guān)鍵字的使用方法2014-01-01
C#WinFrom導(dǎo)出Excel過(guò)程解析
這篇文章主要介紹了C#WinFrom導(dǎo)出Excel過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
C# 使用 OleDbConnection 連接讀取Excel的方法
這篇文章主要介紹了C# 使用 OleDbConnection 連接讀取Excel的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
C#導(dǎo)出數(shù)據(jù)到excel如何提升性能
這篇文章主要介紹了C#導(dǎo)出數(shù)據(jù)到excel如何提升性能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
C#使用游標(biāo)實(shí)現(xiàn)補(bǔ)間函數(shù)
這篇文章主要為大家詳細(xì)介紹了C#使用游標(biāo)實(shí)現(xiàn)補(bǔ)間函數(shù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
C# 二進(jìn)制數(shù)組與結(jié)構(gòu)體的互轉(zhuǎn)方法
本文將和大家介紹 MemoryMarshal 輔助類,通過(guò)這個(gè)輔助類用來(lái)實(shí)現(xiàn)結(jié)構(gòu)體數(shù)組和二進(jìn)制數(shù)組的相互轉(zhuǎn)換,對(duì)C# 二進(jìn)制數(shù)組與結(jié)構(gòu)體的互轉(zhuǎn)方法感興趣的朋友一起看看吧2023-09-09

