Project 20:MQTT-Based Data Subscription

Last lesson, we learned about network communication protocols. This lesson, we'll start studying Internet of Things (IoT) systems. Today, we'll use MQTT, a common IoT protocol, to complete data subscription and reception. 


Project Objectives


Knowledge Objectives


1.Understand the MQTT protocol and its principles.
2.Understand local and cloud IoT servers.
3.Understand methods of subscribing and receiving data from IoT platforms.


Practical Objective 


Use the UNIHIKER K10 to connect to a wireless network, subscribe to server topics, and receive and display data from the IoT platform.


Materials List

HARDWARE LIST
1 UNIHIKER K10
1 USB Cable

Preparations


Hardware


Connect the UNIHIKER K10 to the computer using a USB Cable.

 

Software


Open Mind+, switch to“Upload Mode”, connect the UNIHIKER K10 as shown in the figure below, and load the UNIHIKER K10 library.

Hands-on Practice


Next, we'll subscribe to IoT topics and receive data from the IoT platform in two ways.
Task 1: Subscribe to and Receive Data from the Cloud Server Platform
First, we'll learn to subscribe to topics on the cloud server platform (EasyIOT) through the Internet, receive data, and display it on the UNIHIKER K10 screen. 
Task 2: Subscribe to and Receive Data from the Local Server Platform
Then, we'll learn how to quickly set up a local server, and subscribe to and display topics and data from the local server platform (SIoT platform).


Task 1: Subscribe to and Receive Data from the Cloud Server Platform


Code 


Similar to getting network data, subscribing to and receiving data from a cloud server platform needs finding and selecting a suitable server. Due to the MQTT protocol's requirements, it's necessary to create and subscribe to a topic. 
Specifically, it can be divided into the following steps:
“Enter the platform to create a topic → Network configuration → IoT platform settings → Subscribe to the topic → Set up reception and display”
1.Go to the EasyIOT Platform and Create a Topic
Enter the URL of the EasyIOT platform in your browser: https://iot.dfrobot.com. 

After accessing the platform, register your own account.

After completing the registration, log in to your account and access the IoT platform. Click on Workshop in the top menu, where you can create your own new device.

Here, we need to remember the following items: IoT_id, IoT_pwd, and the Topic for different devices, for subsequent use in the program.


2.Network Configuration


Next, we can use a program to complete the network configuration. As we did in the last lesson, load the Wi-Fi graphical library.

Next, we'll write the code follow the core logic of "connect to the network→wait for successful connection→output the IP address", as shown below:

3.IoT Platform Settings


Load the MQTT library: click "Extension" → select "Module" → search for MQTT → click to download the MQTT library → click to load it again, then click "Return" and you will see the corresponding library instructions.

Next, we can complete the code in this order: "Initial parameter settings→Initiate the connection→Determine the connection status and give prompts". Notably, in the initial parameter settings, we need to fill in information based on the earlier-obtained EasyIOT platform data.

4.Subscribe to Topics


There are two ways to subscribe to topics: fill in the topic directly when configuring initial parameters or within the MQTT Subscribe instruction.

5.Set Up the Reception and Display Code


According to different subscription methods, there are corresponding different reception instructions, as shown below:

We will complete the program in a way that allows us to intuitively view the subscribed topics. The complete program code is shown below:

Run the Code


Click the“Upload”button. Wait for the upload to complete and then wait a moment. If the network and server connection is successful, you'll first see“Network connection successful”and the IP address of UNIHIKER K10 on the screen. After waiting a bit more, “Server connection successful”will appear.
Note: If unsuccessful, please check your network connection.
Next, go back to the subscribed topic tab in the EasyIoT platform, and click "Send" to enter the sending page.

Then, enter a message and click "Send" — the received message will be displayed on the screen of the UNIHIKER K10.

Code Review

Category

Blocks

Function

It is used to set the initial parameters of the server supported by MQTT, which need to be configured according to different IoT platforms.

It is used to connect to the server using the MQTT protocol.

It is used to determine the current connection status of the server supported by the MQTT protocol.

It is used to subscribe to the server topics supported by the MQTT protocol.

It is used to receive topic messages that have been subscribed and set in the initial parameters.

It is used to receive topic messages that have been set and subscribed to, and the created topic needs to be filled in.


Task 2: Subscribe to and Receive Data from the Local Server Platform


Cloud servers have high network requirements and consume data traffic. For small-scale applications not needing network data, a local server can be built, and topic subscription and remote reception can be achieved via a small LAN. Next, we'll select the SIoT platform and complete subscription and data reception based on Task 1 code. 


Code


Unlike cloud-platform operations, we need to set up a local area network first, start the SIoT software on the computer, then enter the IoT service platform and create a topic. 


1.Network Configuration


First, check that your computer and the UNIHIKER K10 are connected to the same wireless network, to ensure they are on the same local area network.

2.Start the SIoT platform program.


Download the Windows version of SIoT V2 and extract the SIoT_V2_Win.zip file.
After extraction, double-click start SIoT.bat to launch the new version of SIoT. Once launched, a small black command window pops up to start the server. Don't close it while the project is running, and note your computer's IP address.

Note: It's recommended to turn off all computer firewalls before starting, or external devices may not access.
After starting the server, open your browser, enter the local IP address followed by :8080 (e.g., 192.168.35.1:8080) to access the server login interface.

The login username is siot and password is dfrobot. Enter them to access the SIoT data management interface. The server setup is now complete.
3.Create a Topic
After successful login, create a new topic named "test" on the SIoT platform webpage.
Note: After creation, double-click the topic name in the label (e.g., siot/test) to auto-copy the full topic name. The full topic name is in the siot/[topic name] format, with "siot/" added auto and no manual entry needed.


After creation, you can see the topic name. Click View Details to enter the message sending and receiving interface of this topic.


4.Modify Configuration Information and Subscribe to a Topic


Now, modify IoT-related parameters in Task 1 to complete topic subscription on the local server platform and data reception. The complete code is as follows:

Run the Code


Click the“Upload”button and wait for the upload to finish. If the network and server connection is successful, you'll first see "Network connection successful" and the UNIHIKER K10's IP address on the screen. Then, after a short wait, you'll see "Server connection is successful". 
Note: If the connection fails, please check the network connection and whether the computer firewall has been turned off.

Next, return to the subscribed topic tab in the SIoT platform. Click Send Message to enter the sending page.


Then, enter a message and click Send. The received message will be displayed on the screen of the UNIHIKER K10.

Knowledge Base


Next, let's learn and summarize the hardware knowledge used in this lesson.
Today we used the MQTT protocol to receive data from the IoT platform. What exactly is the MQTT protocol?


What Is the MQTT Protocol


MQTT (Message Queuing Telemetry Transport) is a client-server based publish/subscribe messaging protocol. It is lightweight, simple, open, and easy to implement, making it widely applicable.
There are three roles in the MQTT protocol: publisher, broker, and subscriber. They communicate by subscribing to the same topic. 
The publisher sends messages to a specific topic, and the subscriber subscribes to one or more topics to receive new messages. The broker (server) is the protocol's core. It receives messages from publishers and forwards them to corresponding subscribers. In short, publishers can't communicate directly with subscribers; communication is done through the broker.

In today's practice, we saw that when the UNIHIKER K10 acts as a subscriber and subscribes to a topic, it can receive data from the broker. In later lessons, we'll further explore the process of an IoT publisher publishing data and the broker forwarding it via practical projects. 
We've learned what the server is in the MQTT protocol. But we only used two types of IoT service platforms. So, what is an IoT service platform?


local IoT server & cloud IoT server


IoT service platform, also known as IoT open platform, is a software platform based on IoT technology. It enables users to quickly build and manage IoT applications and devices. Users can use cloud computing, big data and other technologies to connect and manage various IoT devices, and realize data collection, transmission, storage and analysis. 
An IoT open platform can transmit data via the Internet or a LAN. Internet transmission relies on a cloud platform. The Easy IoT platform is a free and simply - operated IoT service platform. Ensure your network can access the entire Internet properly when using it. 
When we do not need Internet data or want to better protect local data security, we can use a lighter local service platform to build an IoT system, such as the SIoT platform we used today.It is a LAN-based data transmission platform that is stable, lightweight, simple, and very suitable for IoT teaching.

Method for Subscribing to and Receiving Data from IoT Platform
Finally, let’s summarize the data subscription methods for the two types of IoT platform servers:

Challenge Task


Try to use the received messages to control the status of the onboard RGB light –for example, sending "on" turns the RGB light on, and sending "off" turns it off.
Tip: In the message receiving section, simply judge the content of the message to control the RGB light, e.g.:

icon project 20.zip 12KB Download(0)
License
All Rights
Reserved
licensBg
0