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.

Image for post
Image for post

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

Image for post
Image for post

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.

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

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

Image for post
Image for post

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

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

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

Step 3: Run the Code

Run the code as follows.

Image for post
Image for post

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

Image for post
Image for post

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

Reference:https://www.alibabacloud.com/blog/connecting--net-gadgeteer-to-the-alibaba-cloud-iot-platform-using-c%23_594494?spm=a2c65.12601962.0.0

Written by

Follow me to keep abreast with the latest technology news, industry insights, and developer trends.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store