×
Community Blog Connecting .NET Gadgeteer to the Alibaba Cloud IoT Platform Using C#

Connecting .NET Gadgeteer to the Alibaba Cloud IoT Platform Using C#

In this article, we'll show you how to connect .NET Gadgeteer to the Alibaba Cloud IoT Platform Using C#

Join us at the Alibaba Cloud ACtivate Online Conference on March 5-6 to challenge assumptions, exchange ideas, and explore what is possible through digital transformation.

(By Yunfeng Liu)

Currently, a variety of software and hardware is available to connect to the Alibaba Cloud IoT Platform, as well as various languages to implement the connection, including C/C++, Java, JS, Python, and C#. However, the C# version only provides the code for connecting PCs to the cloud platform; the embedded version is not available yet. This article introduces connecting to the Alibaba Cloud IoT Platform using C#, based on the STM32F429 chip.

We will introduce hardware for connecting to the Alibaba Cloud IoT Platform, called the .NET Gadgeteer suite, which has 14 interfaces and can be used to connect nearly 100 modules.

1

We chose to use the temperature and humidity module, LED module, USB module, and motherboard module here, as shown below:

2

We will not be discussing the details of the hardware implementation of this tutorial. For this project, we have used the following configurations:

  1. The USB device module is plugged in to the 2# interface
  2. The DHT11 module is plugged in to the 14# interface
  3. The LED module is plugged in to the 12# interface
  4. The Ethernet module is plugged in to the 4# interface

Step 1: Set Up Alibaba Cloud IoT Platform

Create a product and the corresponding device on the Alibaba Cloud IoT Platform.

Unlike the official sample provided by Alibaba Cloud, we added an attribute LED, which is capable of read and write and uses enumerated variables, with 0 indicating to turn the lights off and 1 indicating to turn the lights on.

3

When this is defined, we create the device and get the device trituples.

Step 2: Implement Alink Protocol

Implement the Alink protocol based on the official MQTT C# codebase M2Mqtt.NetMf42 embedded version.

(1)Upload data to the cloud

    byte[] bytData = new byte[4];
 float T = 0;
 float H = 0;
 int ret = gs.IOControl((int)(6*16+11)); //PG11
 if (ret ! = -1)
 {
 bytData[0] = (byte)(ret & 0xFF);
 bytData[1] = (byte)(ret >> 8 & 0xFF);
 bytData[2] = (byte)(ret >> 16 & 0xFF);
 bytData[3] = (byte)(ret >> 24 & 0xFF);
 
 H = Byte2Float(bytData[0], bytData[1]);
 T = Byte2Float(bytData[2], bytData[3]);
 Debug.Print("H = " + H.ToString() + " T = " + T.ToString());
 
 string payload_json = "{" +
 "\"id\": " + DateTime.Now.Ticks + "," +
 "\"params\":{" +
 "\"temperature\":" + T + "," +
 "\"humidity\":" + H + "," +
 "}," +
 "\"method\":\"thing.event.property.post\"" +
 "}";
 string Data = Guid.NewGuid(). ToString();
 mqttClient.Publish(post_topic, Encoding.UTF8. GetBytes(payload_json), MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE, false);
 Debug.Print(payload_json);
 }

Read the temperature (T) and humidity (H) of the module and push them to the IoT Platform.

(2)Send a control command to the device

4

When "Send Command" is clicked on in the cloud, the MqttMsgPublishReceived event of the device receives data in the following format:

{"method":"thing.service.property.set","id":"196011725","params":{"LED":1},"version":"1.0.0"}

When the LED object is declared, we can switch the LED according to this information (as follows)

OutputPort led = new OutputPort((Cpu.Pin)(7*16+9),false);

Then, we can perform the following in the MqttMsgPublishReceived event:

string json = "";
 if (e.Message.Length > 0)
 {
 //{"method":"thing.service.property.set","id":"196011725","params":{"LED":1},"version":"1.0.0"}
 json = new string(System.Text.UTF8Encoding.UTF8. GetChars(e.Message));
 Debug.Print("Message:" + json);
 
 string strLED =json.Substring(json.IndexOf("LED")+5,1);
 led.Write((strLED == "1"));
 }

Step 3: Run the Code

Run the code as follows.

5

After running the code, you see the following when you open the Alibaba Cloud IoT Platform web page:

6

The LED turns on and off depending on the command sent.

0 0 0
Share on

GXIC

16 posts | 3 followers

You may also like

Comments

GXIC

16 posts | 3 followers

Related Products

  • IoT Platform

    Provides secure and reliable communication between devices and the IoT Platform which allows you to manage a large number of devices on a single IoT Platform.

    Learn More
  • Message Queue

    Message Queue is a distributed message queue service that supports reliable message-based asynchronous communication among microservices, distributed systems, and serverless applications.

    Learn More
  • API Gateway

    API Gateway provides you with high-performance and high-availability API hosting services to deploy and release your APIs on Alibaba Cloud products.

    Learn More
  • AliwareMQ for IoT

    A message service designed for IoT and mobile Internet (MI).

    Learn More