Connecting .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.
We chose to use the temperature and humidity module, LED module, USB module, and motherboard module here, as shown below:
We will not be discussing the details of the hardware implementation of this tutorial. For this project, we have used the following configurations:
- The USB device module is plugged in to the 2# interface
- The DHT11 module is plugged in to the 14# interface
- The LED module is plugged in to the 12# interface
- 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.
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
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.
After running the code, you see the following when you open the Alibaba Cloud IoT Platform web page:
The LED turns on and off depending on the command sent.