Building an IoT System Using Alibaba Cloud: A Step-by-Step Guide

1 Registering and Authenticating Your Alibaba Cloud Account

2 Building an IoT System Using Alibaba Cloud IoT Platform

2.1 Creating an IoT Product

2.2 Connecting ESP8266 NodeMCU Device to Alibaba Cloud

2.2.1 Library Installation

2.2.2 Example Program

2.3 Remote Development Example on IoT Platform – Remote Control of Light

2.3.1 Introduction to MQTT Protocol

2.3.2 Product Function Definition

2.3.3 Remote Online Debugging

2.2.4 IoT Application Development

1 Registering and Authenticating Your Alibaba Cloud Account

Go to the official Alibaba Cloud website and click on the Console option as shown in the image below.

Building an IoT System Using Alibaba Cloud: A Step-by-Step Guide

Then Alibaba Cloud will ask the user to log in. You can log in using one of the login methods. If you don’t have an account, click on the location shown in the image below to register for free.

Building an IoT System Using Alibaba Cloud: A Step-by-Step Guide

If it is a newly registered account, in order to use related products later, after logging in, you must complete the real-name authentication. Click on the avatar in the upper right corner to enter account management, and you can complete real-name authentication by clicking on the left option.

Building an IoT System Using Alibaba Cloud: A Step-by-Step Guide

Once real-name authentication is completed, expand the menu in the upper left corner of the console. In the Products and Services tab, first click on IoT, and then you can select IoT Platform.

Building an IoT System Using Alibaba Cloud: A Step-by-Step Guide

2 Building an IoT System Using Alibaba Cloud IoT Platform

2.1 Creating an IoT Product

Open the Alibaba Cloud “IoT Platform” and enter the free public instance.

Building an IoT System Using Alibaba Cloud: A Step-by-Step Guide

In the left navigation bar under “Products”, click on “Create Product” to create a new product. Product Name: you can write “Smart Home”; Category: “Custom Category”; Node Type: “Directly Connected Device”; Networking and Data: “Wi-Fi”. Other parameters can be left as default. Click “Confirm”.

Building an IoT System Using Alibaba Cloud: A Step-by-Step Guide

Building an IoT System Using Alibaba Cloud: A Step-by-Step Guide

In the left navigation bar under “Devices”, click on “Add Device” and fill in the product to which the new device belongs, device name, and notes. Once created, the device page will look like the image below. Since the device has not yet been connected, the “Status/Enabled Status” should be “Not Activated”.

Building an IoT System Using Alibaba Cloud: A Step-by-Step Guide

Building an IoT System Using Alibaba Cloud: A Step-by-Step Guide

Click “View” to enter the device details page, as shown in the image, you can see the “Region” is East China 2 (Shanghai), and click on “View” at DeviceSecret.

Building an IoT System Using Alibaba Cloud: A Step-by-Step Guide

At this point, the device will be assigned triplet information by the system, including ProductKey (Product Key, paired with ProductSecret), Devicename (Device Name), and DeviceSecret (Device Password), used for the subsequent connection and authentication of IoT devices.

Building an IoT System Using Alibaba Cloud: A Step-by-Step Guide

2.2 Connecting ESP8266 NodeMCU Device to Alibaba Cloud
2.2.1 Library Installation

To quickly connect to the Alibaba Cloud IoT platform using Arduino IDE, you need to install the following 5 libraries.

(1) Install the AliyunIoTSDK library: Alibaba Cloud development toolkit based on Arduino. The installation of the next 4 libraries is due to the AliyunIoTSDK library calling its internal functions (not installing will cause errors).

(2) Install the ArduinoJson library: A development toolkit that supports JSON data format.

(3) Install the PubSubClient library: The most popular MQTT development toolkit in Arduino IDE, implementing the publish/subscribe functionality for clients.

(4) Install the Crypto library: Encryption development toolkit, containing SHA256 encryption hash function.

(5) Install the AWS-SDK-ESP8266 library: AWS development toolkit for ESP8266 and Arduino compatible devices.

Note: The default installation path for libraries is C:\Users\Administrator\AppData\Local\Arduino15\staging\libraries

2.2.2 Example Program

Use Arduino IDE to write C code for ESP8266 NodeMCU to establish an MQTT connection with the platform. The Alibaba Cloud IoT platform provides a dedicated SDK for developers – AliyunIoTSDK, available to developers in the form of Arduino libraries, which can be searched in the Arduino library store (search for AliyunIoTSDK). In addition to searching directly in the Arduino library store, you can also manually clone the project from GitHub and place it in the Arduino library. GitHub address: https://github.com/yu-tou/arduino-aliyun-iot-sdk, this page also provides usage examples for reference.

Building an IoT System Using Alibaba Cloud: A Step-by-Step Guide

Change the triplet and WiFi information in the example usage to match the triplet of the ESP8266 NodeMCU device created in the Alibaba Cloud IoT platform.

Building an IoT System Using Alibaba Cloud: A Step-by-Step Guide

In the device manager, check the current COM port used by the ESP8266 NodeMCU, and select the corresponding COM port in Arduino IDE. Finally, compile and upload the project. You can see whether the ESP8266 NodeMCU is connected to the WiFi network and the connection status with the Alibaba Cloud MQTT server in the “Serial Monitor”.

Building an IoT System Using Alibaba Cloud: A Step-by-Step Guide

Once the ESP8266 NodeMCU successfully connects to the configured WiFi, open the IoT platform, and in the device window, you can see that the device has been successfully activated and is in “online” status.

Building an IoT System Using Alibaba Cloud: A Step-by-Step Guide

At this step, the connection between the ESP8266 NodeMCU development board and the Alibaba Cloud MQTT server has been achieved.

2.3 Remote Development Example on IoT Platform – Remote Control of Light

2.3.1 Introduction to MQTT Protocol

The device access of the Alibaba Cloud IoT platform means establishing an MQTT connection between the device and the platform, after which both parties can receive and send messages through the topics defined on the platform.

MQTT – (Message Queuing Telemetry Transport) is a “lightweight” communication protocol based on the publish/subscribe model, an application layer protocol built on top of the TCP/IP protocol.

Implementing the MQTT protocol requires communication between the client and the server. In the communication process, there are three identities in the MQTT protocol: Publisher, Broker, and Subscriber. The message publisher and subscriber are both clients, while the message broker is the server.

In an MQTT protocol connection, the broker acts as the server, establishing connections with all devices, and on this basis, defining virtual topics for devices to subscribe to. After subscribing to a topic, the subscriber will receive the message content (payload) of that topic, so the message publisher can also be a subscriber. The network structure they form is shown in the image below.

Building an IoT System Using Alibaba Cloud: A Step-by-Step Guide

1. MQTT Client: An application or device that uses the MQTT protocol, which always establishes a network connection to the server. The client can:

(1) Publish information that other clients may subscribe to;

(2) Subscribe to messages published by other clients;

(3) Unsubscribe or delete messages from the application;

(4) Disconnect from the server.

2. MQTT Server: The MQTT server is called the “message broker” and can be an application or a device. It is located between the message publisher and subscriber, and can:

(1) Accept network connections from clients;

(2) Accept application information published by clients;

(3) Process subscription and unsubscription requests from clients;

(4) Forward application messages to subscribed clients.

2.3.2 Product Function Definition

Based on the functions that the product needs to have for IoT control and display, set the topics for publishing/subscribing in the MQTT protocol. In the Alibaba Cloud IoT platform, the properties, services, and events that all devices under the product have correspond to different topics. Here, we will introduce using the product’s properties as an example. If you need to control the light on and off, you can define the property “Switch Light (led_onoroff)”, the web or mobile end will periodically publish messages to the “Switch Light” topic, and the ESP8266 NodeMCU will subscribe to the “Switch Light” topic, receiving specific messages, and then control the light’s on and off based on this message. If you need to display the temperature and humidity of the temperature and humidity sensor, you can define two topics: “Temperature (temperature)” and “Humidity (humidity)”, the ESP8266 NodeMCU will periodically publish temperature and humidity values to these two topics, and the web or mobile end that subscribed to these two topics will display the messages on the interface.

In “Products”, select “View” to enter the product details page, select “Function Definition”, choose “Edit Draft” – “Add Module”.

Building an IoT System Using Alibaba Cloud: A Step-by-Step Guide

Building an IoT System Using Alibaba Cloud: A Step-by-Step Guide

In the “Default Module”, “Add Custom Function” can add properties, services, and events. Here we add “Property”, which can be defined by yourself or selected from existing properties. In the image, the existing property “LED Switch” is selected, automatically filling in the identifier, data type, boolean value, and read/write type (all of which can be modified).

Building an IoT System Using Alibaba Cloud: A Step-by-Step Guide

Building an IoT System Using Alibaba Cloud: A Step-by-Step Guide

The identifier is the topic in the MQTT protocol, and messages are transmitted between clients based on the topic. You can add multiple properties. Finally, be sure to click “Publish Online” for the property information to be available for application development platforms. Finally, publish the product.

Building an IoT System Using Alibaba Cloud: A Step-by-Step Guide

2.3.3 Remote Online Debugging

By writing the following code, you can read the message about the property identifier “LEDSwitch” sent from the console and control the light’s on and off based on the message content.

AliyunIoTSDK::bindData(“LEDSwitch”,powercallback);

void powercallback(JsonVariant p)

{

int powerswitch=p[“LEDSwitch”];

if(powerswitch==1)

{

// Activate device

}

}

The code for the device to send messages is:

AliyunIoTSDK::send(“Property Identifier”, specific message content);

After the program is uploaded, the device is successfully activated and is in “online” status. You can use the “Monitoring Operations” – “Online Debugging” function to send commands from the console to the device. In “Debug”, select “Set”; you can also read messages from the device to the console, selecting “Get” in “Debug”.

Building an IoT System Using Alibaba Cloud: A Step-by-Step Guide

If online debugging is successful, you can proceed with IoT application development.

2.2.4 IoT Application Development

This section will utilize the “IoT Application Development (IoT Studio)” provided by Alibaba Cloud for visual interface development on mobile or web.

In this screen, click on “Public Instance” to return to the upper menu, and in the “Value-Added Services” of the “IoT Platform”, find “IoT Studio” and click “Go to Use”.

Building an IoT System Using Alibaba Cloud: A Step-by-Step Guide

Building an IoT System Using Alibaba Cloud: A Step-by-Step Guide

Select either “Web Visual Development” or “Mobile Visual Development” (the development process is basically the same), the example will introduce “Web Visual Development”.

Note: The mobile end can only create one application, while the web end can create multiple.

Building an IoT System Using Alibaba Cloud: A Step-by-Step Guide

Building an IoT System Using Alibaba Cloud: A Step-by-Step GuideBuilding an IoT System Using Alibaba Cloud: A Step-by-Step Guide

Create a new application named “Room 1”. In the “Components” section, select the “Switch” and “Dashboard” components and drag them to the middle canvas.

Building an IoT System Using Alibaba Cloud: A Step-by-Step Guide

First, set the “Interaction” function of the switch component, where the “Event” can be set to “Click”, the “Action” set to “Set Device Property”, the “Product” set to “Smart Home”, the “Designated Device” set to “ROOM1”, and the property set to “LED Switch”, and the “Set Value” choose the component value “Switch-akmc” from “Parameter Source”. This setting allows the value of the device “ROOM1″‘s “LED Switch Property” to change according to the switch component value when clicked (i.e., when the switch component value changes to 0 or 1 due to clicking, the value of the “LED Switch Property” will correspondingly change to 0 or 1). The “Dashboard” component can be set by everyone as they wish. After completing the settings, save and publish. Publishing requires “Binding Domain Name”, and after successful binding, you can see the publication status change to “Published”. You can debug through preview, and if there are no issues, you can send the URL via “Publish Address” for others to use, enabling remote control.

Building an IoT System Using Alibaba Cloud: A Step-by-Step GuideBuilding an IoT System Using Alibaba Cloud: A Step-by-Step Guide

Building an IoT System Using Alibaba Cloud: A Step-by-Step Guide

Building an IoT System Using Alibaba Cloud: A Step-by-Step Guide

Finally, the Web end can control the light on and off in Room 1, and display the indoor temperature and humidity of Room 1.

Building an IoT System Using Alibaba Cloud: A Step-by-Step Guide

Leave a Comment

×