[M467] The MQTT connection on NuMaker-IoT-M467 using FreeRTOS and CycloneTCP

, Jul 26, 2024|
0
6365
0
[For further inquiries, please e-mail to shchen2@nuvoton.com]
[Visit https://www.nuvoton.com/iot_startup for more IoT solutions]



The CyCloneTCP


CycloneTCP-ipv4-ipv6-osi-model.png (8.91 KiB)


CyCloneTCP, designed by Oryx Embedded, is an IPv4/IPv6 TCP/IP stack tailored for resource-constrained microcontrollers. It supports rich application protocols and complies with RFC standards. This article focuses on running a demo that utilizes CyCloneTCP with FreeRTOS on NuMaker IoT boards, specifically supporting the following boards:
  • NuMaker-PFM-M487
  • NuMaker-IoT-M487
  • NuMaker-IoT-M467
The currently available demonstrations include the MQTT client, which we will select for this tutorial. We will use Keil MDK to build the demo.

Picture1.png (10.66 KiB)



Prerequisite

Hardware
  • A NuMaker-IoT-M467 board

    Picture2.png (28.67 KiB)

  • A LAN cable
  • A Micro USB cable

    Picture3.png (11.33 KiB)

  • A Wi-Fi Router with LAN
Software Test Environment

The MQTT client demo utilizes DHCP for obtaining an IP address, enabling the NuMaker IoT board to establish an Ethernet connection with the Wi-Fi Router hosting the DHCP server. Next, it connects to test.mosquitto.org server on port 1883 using the MQTT protocol. It also support MQTT+TLS, MQTT through Websocket, etc. Then it subscribes to the following topics:
  • board/status: The online or offline status of NuMaker IoT board
  • board/leds/1 and board/leds/2: The status of the LEDR and LEDY on the NuMaker IoT board
  • board/buttons/1: The pressed or released status of BTN0 button on the NuMaker IoT board

Picture4.png (32.84 KiB)


The Steps to Build the Demo Project

Download the Demo Package
  1. Access the URL https://www.oryx-embedded.com/download/ in your web browser
  2. Click on the "Open Source Version" link to download the complete package.
  3. Extract the downloaded ZIP file, named CyCloneTCP_xxx_..._Open_x_x_x.zip, to either your Desktop or a directory of your choice.

Picture5.png (59.34 KiB)


Open the MQTT Client Project
  1. Utilize the File Manager to locate the Keil project for MQTT client demo designed for the NuMaker-IoT-M467 board. The directory path is .\demo\nuvoton\numaker_iot_m467\mqtt_client_demo\keil
  2. Subsequently, double-click on the mqtt_client_demo.uvprojx file to initiate the Keil MDK and open the project.

Picture6.png (37.42 KiB)


View and Modify the Demo Code

Prior to building the code, Oryx requests acceptance of the GPL license terms.
  1. Expand the Demo Header group by clicking on the "+" symbol.
  2. Open the os_port_config.h file by clicking on it.
  3. In the os_port_config.h file, add the following line:#define GPL_LICENSE_TERMS_ACCEPTED

Picture7.png (70.97 KiB)


Next, let's examine a portion of the code where some information will be used later.
  1. Expand the Demo Sources group by clicking on the "+" symbol.
  2. Open the main.c file by clicking on it.
  3. The default setting is enabled DHCP to get IP address via Ethernet

Picture8.png (78.3 KiB)

  1. Scroll down source code in main.cpp and note the MQTT configuration.

    APP_SERVER_NAME "test.mosquitto.org"
    • The free MQTT broker used in the demo
    APP_SERVER_PORT 1883
    • Use MQTT over TCP on port 1883
    • Other option can also be used to test. However, keep the default setting in this demo.

    Picture9.png (79.58 KiB)

  2. Select the Project Target is mqtt_client_demo_flash
  3. Click Build icon to build it

Picture10.png (78.36 KiB)


Program Firmware to Board via Nu-Link2-ME
  1. The ICESW2 switch on Nu-Linke2-ME
    • The no.1 & 2 are ON
    • The no.3 & 4 are OFF

    Picture11.png (38.57 KiB)

  2. Plug in USB cable to PC or Laptop
  3. Click Download icon to program firmware

Picture12.png (77.65 KiB)


The Steps to Run the Demo

Open the Terminal
  1. Run any terminal tool, such as TeraTerm, Putty, etc. to open a terminal for Nu-Link2-ME Virtual COM port.
  2. Select baud rate to 115200.

    Picture14.png (36.37 KiB)

  3. The PC or laptop connects to Wi-Fi router via Wi-Fi or Ethernet.
  4. The NuMaker-IoT-M467 board connects to Wi-Fi router via Ethernet

Picture13.png (24.44 KiB)


Restart NuMaker-IoT-M467 Board
  1. To view the boot message, push RESET button on NuMaker-IoT-M467 board. The message shows on terminal tool.
  2. In the demo, the board has the IP address 192.168.50.28
  3. It successfully connects to test.mosquitto.org using MQTT protocol on port 1883

Picture15.png (61.46 KiB)


Configure MQTT App
  • Execute IoT MQTT Panel on Android mobile phone.
  • Add a new connection and enter the MQTT broker information:
  • Connection name: Give this connection a name
  • Client ID: Leave blank and it will be automatically generated.
  • Broker Web/IP address: Fill in test.mosquitto.org
  • Port: 1833
  • Network protocol: TCP
  • Then press CREATE

Picture16.png (23.87 KiB)

  • Back to app main screen, press Connection Name (i.e., m467-mqtt-len in the demo) to add panels.
  • Add 4 panels for the connection.
  • Add the first panel
  • Select Text Log
  • Panel name: Status
  • Topic: board/status
  • Checked Show last message only
  • Then press CREATE

Picture17.png (18.93 KiB)

  • Add the second panel
  • Select Text Log
  • Panel name: Button
  • Topic: board/buttons/1
  • Checked Show last message only
  • Then press CREATE

Picture18.png (19.08 KiB)

  • Add the third panel
  • Select Switch
  • Panel name: LED 1
  • Topic: board/leds/1
  • Payload on: on
  • Payload off: off
  • Then press CREATE

Picture19.png (19.27 KiB)

  • Then the similar setting to add the fourth panel
  • Select Switch
  • Panel name: LED 2
  • Topic: board/leds/2
  • Payload on: on
  • Payload off: off
  • Then press CREATE

Picture20.png (14.63 KiB)


Picture21.png (45.52 KiB)

  • Make sure the connection is connected. If not, touch the cloud icon to connect again.
  • NuMaker-IoT-M467 board is running MQTT client demo and successfully connects to MQTT broker, so Board Status is online.
  • Keep pressing BTN0 on NuMaker-IoT-M467 board, and the Botton status will appear pressed. Release BTN0, and released will appear.
  • Touch the LED 1 switch to control LEDR on NuMaker-IoT-M467 board.
  • Touch the LED 2 switch to control LEDY on NuMaker-IoT-M467 board.
That all. Enjoy it.