IoT Data Assistant | UNIHIKER Tutorial: Learn Python with Graphical Programming for Beginners 11

0 2172 Medium

Do you remember how our physical education teachers record physical test data in school? During an exam, the PE teacher needs to record students’ performance data on a paper list one by one, which is slow and laborious.

The emergence of IoT (Internet of Things) brings positive changes to the educational sector and transforms the way of learning and teaching used to work, both for students and educators. In this lesson, we are going to make an IoT data assistant to help teachers simplify their work.

projectImage

Goal

Send the lung capacity data of every student to the SIoT platform in the form of Student ID + lung capacity data.

projectImage

Get to Know

1. The concept of IoT

2. The SIoT platform

3. The MQTT protocol

4. What a character string is

5. How to use UNIHIKER to receive or send data

Preparation

Hardware:
HARDWARE LIST
1 UNIHIKER
1 Type-C&Micro 2-in-1 USB Cable

Software: Mind+

Download address: https://www.mindplus.cc

projectImage

Hands-on Practice

In the last lesson, we learned how to store the detected lung capacity data in a list. Although data can be stored in a list, once the UNIHIKER is powered off, the data in the list will be lost. To handle this, we will learn how to store data in the SIoT platform in this lesson, which will be discussed in three parts.

Task 1: UNIHIKER sends data to the SIoT platform

First, we will learn how to use the MQTT command on UNIHIKER, and enable its SIoT server and send data to SIoT via UNIHIKER. Then we can view the data in the corresponding topic on the SIoT platform after logging in.

Task 2: UNIHIKER receives data from the SIoT platform

The UNIHIKER receives data from SIoT. Print the received data on the Mind+ terminal.

Task 3: UNIHIKER sends lung capacity data to the SIoT platform

Send the detected lung capacity data to the SIoT platform in real time.

Task 1: UNIHIKER sends data to the SIoT platform

1. Hardware Connection

Connect UNIHIKER to the computer via a USB cable.

projectImage
2. Software Preparation

Open Mind+, and complete the software preparation according to the figure below.

projectImage
3. Write a Program

In this task, we will use UNIHIKER to send a message “Hello” to the SIoT platform. First, let’s see what we should prepare for that.

Note: For more information about IoT and SIoT, please refer to the “Knowledge Base”.

(1)Check if SIoT is enabled

Long press the Home key on the UNIHIKER to enter the menu. Then, click “Toggle settings” to check if the SIoT service is enabled, if not, click on the SIoT option to enable it.

projectImage

(2)Add MQTT-py Library

We need to use the SIoT-related command to send data to the SIoT platform, but now we cannot find them. What should we do? Here we have to add the MQTT-py Library first. Click Extension->Official Library and then click to load the MQTT library.

projectImage

(3)Initialize MQTT

After the SIoT commands are added, we need to set it up using the MQTT initial block. Click the settings icon in that block, and configure the SIoT server as the default IP address of the UNIHIKER connected to the computer, which is 10.1.2.3.

projectImage

When the initial setup is done, use the block connect MQTT to connect MQTT and make it stay connected using MQTT stay connected, forever.

projectImage

To send data to the SIoT platform, we need to subscribe to the topic to be sent to the SIoT platform through the command MQTT subscribe to. Please note that the subject filled in the MQTT subscription should be in the fixed format of "Project ID / Device Name".

projectImage

(4)UNIHIKER sends data to the SIoT platform

When the four steps “Initialize MQTT->Connect MQTT->Stay Connected->Subscribe” arecompleted, we can send data to the SIoT platform using the block MQTT publish to.

projectImage

Send a text data “hello” to the SIoT every 5 seconds.

projectImage
4. Run the Program

Run the program. The words “Connection result: The connection is successful” will be printed on the Mind+ terminal.

projectImage

The program is designed to send a “Hello” text message to the SIoT every 5 seconds, but thereis no reaction on the UNIHIKER screen when the data is sent successfully. How do we view thetransmitted data?

We need to access the UNIHIKER service webpage to view data. Enter 10.1.2.3 in a browser address bar.

projectImage

Click “Service Toggle” on the UNIHIKER service page, and then click “Open Page” to enter the SIoT interface.

projectImage

Input the account id and password, and log in.

Account ID: siot

Password: dfrobot

projectImage

After that, click “Device list” to find the subscribed topic.

projectImage

Click “View message” to view the transmitted data.

projectImage
5. Have a try

If we want to send a “hello” message to the SIoT platform after pressing down button A, how should we revise our codes?

Tip: Use the command When the button A is pressed.

projectImage

Task 2: UNIHIKER receives data from the SIoT platform

1. Write a Program

In the last task we learned how to send data to the SIoT platform. Next, we will get to know how to receive data from the SIoT platform and print data in the Mind+ terminal.

(1)Send messages from the SIoT platform to UNIHIKER

There is a message send box in the SIoT message view interface. Input“Hello, UNIHIKER!” in it and click on the Send icon. When the message is sent successfully, the message “Hello, UNIHIKER!” will appear on the topic.

projectImage

(2)UNIHIKER receives data from the SIoT platform

The command on MQTT received MQTT message from MQTT topic will be used for the UNIHIKER to receive data from the SIoT platform.

projectImage

To receive the data, you first need to make sure if the data is coming from the topic "data / Class1", and the topic “data / Class1” should use quotation marks in the English input method. It should be noted that the data type of the MQTT message is a string.

projectImage

If the MQTT topic is determined to be "data / Class1", print the received MQTT message on the Mind+ terminal.

projectImage

The complete program is as follows:

projectImage
2. Run the Program

Click to run the program. After the program has been run successfully, the message "Connection result: The connection is successful" appears on the Mind + terminal. Then, if we enter the message " Hello, UNIHIKER!" in the message box of the SIoT platform and send it out, it will be printed on the Mind+ terminal.

projectImage
projectImage
3. Have a try

The UNIHIKER can receive the data sent by the SIoT platform, and can print out the received data in the Mind + terminal. Next, let’s try displaying the received data on the UNIHIKER screen, as shown in the figure below:

projectImage

Task 3: UNIHIKER sends lung capacity data to the SIoT platform

1. Write a Program

In task 1 of lesson 11, we detected the lung capacity data. Next, let’s add functions for selecting student IDs through the "+ / -" button and transmitting data. Press button A to start detecting lung capacity, with a progress bar reflecting the vital capacity. Press the B key to send the data to the topic "data / Class1" of the SIoT platform. The final effect displayed on the UNIHIKER screen is shown below:

projectImage

(1)Select student ID

Press down the button“+” or “-” to increase or decrease the student ID by 1 respectively. The commands Object name show text at x y font size color, Object name add button at x y width height click callback function, and variables used in this feature are all learned before, so they will not be explained in detail here.

projectImage

(2)Get MQTT ready

To enable UNIHIKER to communicate with the SIoT platform, we should get the MQTT ready first.

projectImage

(3)Send lung capacity data

Press button B to send the detected lung capacity data to the topic “Lung capacity data /Class 1” on the SIoT platform. The block When the button B is pressed will be used to determine whether the B button has been pressed and the block MQTT publish to will be used to send data to the SIoT platform.

projectImage

Add feedback for successful data transmission. After the data is successfully transmitted, the buzzer will make sounds and the detected data and progress bar will be cleared.

projectImage

(4)Send lung capacity data with student ID

In the above program, the MQTT publish to publishes the lung capacity data without student IDs. In this case, when the SIoT platform receives the data, it does not know which student the data belongs to. To handle this, we can join the student ID and lung capacity data before sending it to the SIoT platform using the command join, which can be found in the “Text” category.

Note: For more information about “Char string”, please refer to the Knowledge Base.

projectImage

Revise the content in the MQTT publish to to the joined student ID and lung capacity data.

projectImage

The complete program is as follows:

projectImage
2. Run the Program

Click to run the program. After the program runs successfully, use the "+/-" button on the UNIHIKER screen to select the corresponding student ID. Then press button A to start measuring lung capacity. After the measurement is complete, press button B to send the lung capacity data with the student ID to the SIoT platform.

projectImage
3. Have a Try

To avoid repeatedly measuring the lung capacity data, UNIHIKER should stop sending lung capacity data to the SIoT platform after all the students in one class have completed their tests. For example, if there are ten students in a class, UNIHIKER should disconnect the MQTT connection and stop sending messages to the SIoT platform once all ten students have finished their tests. Now try improving the program.

Tip: Use the block disconnect MQTT when ten students complete their tests.

projectImage

Knowledge Base

1. What is IoT?

The Internet of Things (IoT) describes the network of physical objects—“things”—that are embedded with sensors, software, and other technologies for the purpose of connecting and exchanging data with other devices and systems over the internet. For example, in a smart home scenario, devices such as lights, fans, cameras, and smartphones can be interconnected through wireless networks in a room, creating a network of interconnected devices. This is known as the "Internet of Things.

projectImage

What is the principle of the Internet of Things?

In smart home scenarios, an IoT system consists of three parts: servers, smart terminals, and mobile terminals. We can use our smartphones to control and view the status of devices such as lights, fans, and cameras. The smartphone is the mobile terminal in the IoT system. Smart home appliances such as lights and cameras that can connect directly to the internet are the smart terminals in the IoT system. Additionally, in the IoT system, the mobile terminal and smart terminal cannot directly transmit data to each other. They need a hub to store and exchange data, which is the server.

projectImage

UNIHIKER can also build a smart home system with smart terminals, mobile terminals, and servers. If we only have one UNIHIKER device, we can use it as a smart terminal to connect to devices such as lights, fans, and cameras, and use the built-in SIoT platform of UNIHIKER as the IoT server. We can use our phones or computers as mobile terminals.

projectImage

It should be noted that UNIHIKER in this lesson is used as both a smart terminal and a server. The computer corresponds to the mobile terminal. We used a USB cable to connect the UNIHIKER to the computer and view the server’s data.

projectImage

Actually, the mobile terminal can also access server data by wireless communication, which can be implemented through the WIFI module on the UNIHIKER. For more details, please refer to the “IoT project” on the official UNIHIKER website.

2. SIoT Platform

The servers in the Internet of Things (IoT) are implemented through IoT platforms. Commonly used IoT platforms include Easy IoT and SIoT. To achieve device communication in IoT, HTTP or MQTT protocols are usually used. In this lesson, we use the SIoT platform and the MQTT protocol. Next, let's get to know the SIoT platform and the MQTT protocol.

The UNIHIKER has a built-in SIoT service that allows you to quickly create an MQTT broker through wired or wireless means. SIoT focuses on the collection and export of IoT data and is one of the best choices for collecting scientific data.

projectImage

3. MQTT Protocol

What is MQTT Protocol?

MQTT stands for Message Queuing Telemetry Transport. It is an extremely simple and lightweight messaging protocol (subscribe and publish) designed for limited devices and networks with high latency, low bandwidth, or unreliable networks.

The MQTT protocol is lightweight, simple, open, and easy to implement, which makes it widely applicable.

projectImage

What role does the MQTT protocol play in the UNIHIKER and the SIoT platform?

The MQTT protocol serves as a communication bridge in the UNIHIKER and SIoT, during which it plays three roles: publisher, broker (server), and subscriber. The message publishers and subscribers are both device endpoints (UNIHIKER), and the message broker is the server. A message publisher can also be a subscriber at the same time.

In simpler terms, we can understand the message broker as a group chat. If you want to send a message in this group chat, you first need to join the group, which is done through the MQTT protocol. Once you join the group, you become a publisher, the group is the server, and other group members are subscribers. You can also see the messages you send, so the publisher can also be a subscriber.

4.What is a string?

A string is a data type. There are six standard data types in Python, including strings (text), numbers, lists, tuples, sets, and dictionaries.

projectImage

Please note that the strings in the text-related commands above are enclosed in a pair of quotation marks. We can understand a string as a series of characters enclosed in quotes. In simple terms, a word or sentence made up of punctuation marks, letters, numbers, and text is a string. The punctuation marks, letters, numbers, and text that make up a string are characters, and a string is a collection of characters.

Now you understand why in Task 2, we needed to enclose "data/Class1" in quotes when determining the MQTT topic.

projectImage

The data type of an MQTT topic is a string. If we don't enclose "data/Class1" in quotes, the two data cannot be compared because they have different data types. To give a simple example, suppose we have two sets of data, one is 23 and the other is "23". The data type of the first set is numeric, while the second is a string. They cannot be compared since they are different data types. Only when the data types of the two sets are the same can they be compared. Therefore, "data/Class1" needs to be enclosed in quotes to indicate that it is a string.

projectImage

Why do we need to use the join command? Let’s look at this example. There are two strings “hello,” and “UNIHIKER!”. If they are not combined, they are just individual strings. The purpose of the join command is to combine these strings into a complete sentence.

projectImage

5. Command Learning

projectImage

Challenge

After combining the student ID and lung capacity data, the student ID was successfully sent. But if the student ID has two or more digits, it will be difficult to distinguish between two types of data. To separate the student ID and lung capacity data, we can add a colon (:) between them. The data format of the sent data will be "student ID: lung capacity data". Let's try it together!

Tip: Add a colon by nesting the join block.

projectImage

The messages received by the SIoT are shown below:

projectImage
icon IoT Data Assistant.rar 1.14MB Download(5)
License
All Rights
Reserved
licensBg
0