一,創建阿里雲 IOT 產品、設備
目前阿里雲每月贈送 100 萬條流量,可以免費使用基礎版、高級版,開通后即可免費使用。
阿里雲 IOT 平台地址 https://iot.console.aliyun.com/product
登陸開通后,新建測試產品、設備。
創建產品
產品的定義是,一類硬件、功能、外形完全相同的設備。所以,添加一個產品后,我們就可以在此類別下添加成千上萬個設備。
下面的信息要選、設備、非網關,原因后面說。
設定一個產品
創建一個產品后,就需要設定這個產品的特征,它有那些功能?它可以進行什么樣的通訊?
定義兩個topic,一個發布、一個訂閱
名稱為test1、test2
添加一個設備,記下你設備的那些設備名等信息
二,下載SDK、創建項目
新建一個 .NET Frameork4 控制台項目,名稱 AlyIotIest
添加引用,把下載的 dll 添加進去
新建一個類 BackMessage.cs
把以下代碼復制進 BackMessage(清空以前的代碼),先不用管為什么
using iotxashttp2netsdk.iot.auth.common; using iotxashttp2netsdk.iot.callback; using System; using System.Collections.Generic; using System.Text; namespace AlyIotIest { public class 默認回調 : IHttp2MessageCallback { public ConsumeAction Consume(Http2ConsumeMessage http2ConsumeMessage) { Console.WriteLine("默認回調"); Console.WriteLine(http2ConsumeMessage.MessageId); if (http2ConsumeMessage.Payload.Length != 0) { Console.WriteLine("收到平台消息:"); string a = Encoding.ASCII.GetString(http2ConsumeMessage.Payload); Console.WriteLine(a); } return ConsumeAction.CommitSuccess; } } public class 自定義回調 : IHttp2MessageCallback { public ConsumeAction Consume(Http2ConsumeMessage http2ConsumeMessage) { Console.WriteLine("自定義回調 "); Console.WriteLine(http2ConsumeMessage.MessageId); if (http2ConsumeMessage.Payload.Length != 0) { Console.WriteLine("收到平台消息:"); string a = Encoding.ASCII.GetString(http2ConsumeMessage.Payload); Console.WriteLine(a); } return ConsumeAction.CommitSuccess; } } }
三,開始操作、准備工作
1,Program.cs 需要用到以下命名空間
using System; using iotxashttp2netsdk.iot.auth.common; using iotxashttp2netsdk.iot.message; using System.Net; using System.Linq; using System.Text;
2,設定一個設備客戶端對象
復制 static MessageClient client; 到Program類鍾
class Program { // 步驟 1 定義設備客戶端 # static MessageClient client;
... ... }
MessageClient 是核心,用於連接服務器、通訊、訂閱和發布Topic、觸發任務,先不管他
四,開始寫代碼
在 Main 方法中寫代碼
步驟 2,添加密鑰信息,在相應位置填上你的密鑰
// 步驟 2 從控制台獲取 productKey、deviceName、deviceSecret 信息 # // 到阿里雲IOT物聯網后台設置產品、添加設備后,可以找到 string productKey = " "; string deviceName = " i"; string deviceSecret = " ";
步驟 3
// 步驟 3 標記 設定設備唯一識別符 clientid // 阿里雲官方給的例子是 子網IP IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName()); // 客戶端設備唯一標記 string clientId = host.AddressList.FirstOrDefault( ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).ToString(); // 地域可用區 ,根據實際修改 string regionId = "cn-shanghai"; string domain = ".aliyuncs.com"; string endpoint = "https://" + productKey + ".iot-as-http2." + regionId + domain;
步驟 4
//設置配置服務和構建客戶端 //連接服務配置項 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發生異常時可以設置重,重試次數最大為3次 profile.RetryPubCount = 3; //重試間隔時間單位為s(秒) profile.RetryIntervalTime = 2; profile.GetDeviceAuthParams(); //構造客戶端 client = new MessageClient(profile);
步驟 5
// 設置訂閱和發布的 topic string topic = "/" + productKey + "/" + deviceName + "/user/test1"; string topic2 = "/" + productKey + "/" + deviceName + "/user/test2"; // 接收數據,剛連接時 // 只生效一次 // 默認回調 client.DoConnection(new 默認回調()); //回調 // 自定義一個 IHttp2MessageCallback,每次收到消息都用此回調 client.SetMessageListener(topic2,new 自定義回調()); client.DoSubscribe((string)topic ,msg=> { Console.WriteLine ("訂閱服務端消息"); 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("訂閱服務端消息"); 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
添加一個方法
public static void 發數據(string topic,string str) { //發消息 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 方法內,后面加上
while (true) { Console.WriteLine("輸入數據"); string str = Console.ReadLine(); if (str.ToUpper() == "EXIT") { break; } 發數據("/" + productKey + "/" + deviceName +"/user/test1", str); } Console.ReadKey();
運行你的項目