Summary
The example of connecting to Alibaba cloud Internet of things platform based on open-source MQTT Client Java and Python language is introduced in the previous section. Here, M2Mqtt Client C is used for testing, combining with the up and down of custom Topic demonstration message.
Operation steps
1, installation M2Mqtt
2,Code sample
using System; using System.Text; using uPLibrary.Networking.M2Mqtt; using uPLibrary.Networking.M2Mqtt.Messages; namespace MQTTDemo { class Program { //private MqttClient client; static void Main(string[] args) { string ServerUrl = "*****.iot-as-mqtt.cn-shanghai.aliyuncs.com"; string UserName = "******"; string Password = "******"; string clientId = "******"; ConnectMqtt(ServerUrl, clientId, UserName, Password); Console.ReadKey(); } static void ConnectMqtt(string targetServer, string mqttClientId, string mqttUserName, string mqttPassword) { MqttClient client = new MqttClient(targetServer); client.ProtocolVersion = MqttProtocolVersion.Version_3_1_1; client.Connect(mqttClientId, mqttUserName, mqttPassword, false, 60); client.MqttMsgPublishReceived += Client_MqttMsgPublishReceived; // Create the corresponding custom Topic in the product console, and then obtain it in the device Topic list string PubTopic = "/******/******/user/Data1"; string SubTopic = "/******/******/user/Data2"; // Publish message to custom Topic string content = "{'content':'msg from :" + mqttClientId + ", Hello World'}"; var id = client.Publish(PubTopic, Encoding.UTF8.GetBytes(content)); // Subscribe to messages from a custom Topic client.Subscribe(new string[] { SubTopic }, new byte[] { 0 }); } /// <summary> ///Set subscription callback /// </summary> /// <param name="sender"></param> /// <param name="e"></param> static void Client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e) { // handle message received string topic = e.Topic; string message = Encoding.UTF8.GetString(e.Message); Console.WriteLine("Device receive message topic :" + topic + " ,the message body is " + message); } } }
Obtaining reference of signature method link.
3. Message uplink view
4. Downlink message test
More reference
C ා language. NET platform access
Connect alicloud IoT based on open source JAVA MQTT Client
Alicloud Internet of things platform Qucik Start
Connect alicloud IoT based on open source Python MQTT Client
>>Alicloud double 1.1 received a subsidy of 100 million yuan, and made a lot of good gifts such as iPhone 11 Pro and sweater. Click here to participate: http://t.cn/Ai1hLLJT
This is the original content of yunqi community, which can not be reproduced without permission.