Select your cookie preferences

We use essential cookies and similar tools that are necessary to provide our site and services. We use performance cookies to collect anonymous statistics, so we can understand how customers use our site and make improvements. Essential cookies cannot be deactivated, but you can choose “Customize” or “Decline” to decline performance cookies.

If you agree, AWS and approved third parties will also use cookies to provide useful site features, remember your preferences, and display relevant content, including relevant advertising. To accept or decline all non-essential cookies, choose “Accept” or “Decline.” To make more detailed choices, choose “Customize.”

Updated Jul 2025

Cellular Interface Demo (Mutual Authentication)

Introduction

FreeRTOS offers a suite of networking stacks designed for IoT applications. Applications can access communication protocols at different levels - MQTT, HTTP, Secure Sockets, etc. Common connectivity technologies such as Ethernet, Wi-Fi and BLE have been integrated with the networking stacks of FreeRTOS, with a wide selection of microcontrollers and modulesexternal_link pre-integrated.

The demos in this project demonstrate how to establish mutually authenticated MQTT connections to MQTT brokers, such as AWS IoT Core, by using cellular connectivity. The demos use the Cellular Interface library sub-moduled from an external project. The Cellular Interface library exposes the capability of a few popular cellular modems through a uniform API.

The MQTT and HTTP libraries of FreeRTOS use an abstract Transport Interface to send/receive data in a generic way. The demos in this project offer an implementationexternal_link of the Transport Interface on top of the uniform API exposed by the Cellular Interface library.

Hardware Setup

The demos in this project can be run in the FreeRTOS Windows Simulator. You will need a Windows PC and one of the supported cellular modems to run the demos. A version of Visual Studio, such as the free Community version of Visual Studioexternal_link, will be needed to build the demos.

The FreeRTOS Windows simulator makes use of a COM port to communicate with the cellular module. Set up your cellular module communication with the following steps.

  1. Connect the cellular module to your PC. Most cellular dev kits have a USB connector, so you can just connect it to your PC's USB port and look for the COM port in Window's Device Manager. For example, as shown below, you will see a new COM69 show up when you connect the modem. If your cellular dev kit does not have USB, use a USB adapter like one of theseexternal_link.

    Screenshot 1: Cellular module COM port in windows device manager
  2. Use Puttyexternal_link or any other terminal tool to verify the connection with the cellular module. Refer to your cellular module's manual for settings like baud rate, parity, and flow control. In the terminal tool, enter "ATE1". The modem should return "OK". Depending on your modem setting, you may also see an echo of "ATE1" as well. Enter "AT". The modem should return "OK".

    Screenshot 2. Testing the COM port with AT commands in putty

Components and Interfaces

This project makes use of five (5) sub-modules from other GitHub projects, shown as yellow boxes in the diagram below.

Figure 1: Components and Interfaces
  • The Cellular Demo Applicationexternal_link is largely the same functionality as the coreMQTT demoexternal_link, with added logic to set up cellular as the transport. (The original coreMQTT demo was designed for Wi-Fi on the FreeRTOS Windows Simulator.)

  • The Transport Interfaceexternal_link is needed by the MQTT library (sub-moduled from the coreMQTT project) to send and receive packets.

  • The TLS porting interfaceexternal_link is needed by the mbedTLS library to run on FreeRTOS.

  • The Comm Interfaceexternal_link is used by the Cellular Interface library to communicate with cellular modems over a UART connection.

Developer References and API Documents

Please refer to the hosted doxygen.

Source Code Organization

The demo project files for Visual Studio are named <xyz>_mqttmutual_auth_demo.sln, where _xyz is the name of the cellular modem. They can be found in the FreeRTOS_Cellular_Interface_Windows_Simulatorexternal_link repository on GitHub in the following directories:

  1. MQTT_Mutual_Auth_Demo_with_BG96external_link

  2. MQTT_Mutual_Auth_Demo_with_HL7802external_link

  3. MQTT_Mutual_Auth_Demo_with_SARA_R4external_link

1./FreeRTOS-Plus
2├── Demo
3│ ├── FreeRTOS_Cellular_Interface_Windows_Simulator
4│ │ ├── Common
5│ │ │ ├── FreeRTOSConfig.h
6│ │ │ ├── FreeRTOSIPConfig.h
7│ │ │ ├── MutualAuthMQTTExample.c
8│ │ │ ├── cellular_platform.c
9│ │ │ ├── cellular_platform.h
10│ │ │ ├── cellular_setup.c
11│ │ │ ├── comm_if_windows.c
12│ │ │ ├── core_mqtt_config.h
13│ │ │ ├── main.c
14│ │ │ └── mbedtls_config.h
15│ │ ├── MQTT_Mutual_Auth_Demo_with_BG96 ( demo project for Quectel BG96 )
16│ │ │ ├── WIN32.vcxproj
17│ │ │ ├── WIN32.vcxproj.filters
18│ │ │ ├── WIN32.vcxproj.user
19│ │ │ ├── cellular_config.h
20│ │ │ ├── demo_config.h
21│ │ │ └── mqtt_mutual_auth_demo_with_bg96.sln
22│ │ ├── MQTT_Mutual_Auth_Demo_with_HL7802 ( demo project for Sierra Wireless HL7802 )
23│ │ └── MQTT_Mutual_Auth_Demo_with_SARA_R4 ( demo project for U-Blox Sara-R4 )
24│ │
25├── Source
26│ ├── Application-Protocols
27│ │ ├── coreMQTT ( submodule : coreMQTT )
28│ ├── FreeRTOS-Cellular-Interface ( submodule : FreeRTOS-Cellular-Interface )
29│ ├── FreeRTOS-Cellular-Modules
30│ │ ├── bg96 ( submodule : Lab-FreeRTOS-Cellular-Interface-Reference-Quectel-BG96 )
31│ │ ├── hl7802 ( submodule : Lab-FreeRTOS-Cellular-Interface-Reference-Sierra-Wireless-HL7802 )
32│ │ └── sara-r4 ( submodule : Lab-FreeRTOS-Cellular-Interface-Reference-ublox-SARA-R4 )
33│ ├── Utilities
34│ │ ├── backoff_algorithm ( submodule : backoffAlgorithm )
35│ │ ├── logging
36│ │ ├── mbedtls_freertos
37│ │ │ ├── mbedtls_bio_freertos_cellular.c
38│ │ │ └── ( code for adapting mbedtls with cellular socket )
39│ │ │ ├── mbedtls_bio_freertos_plus_tcp.c
40│ │ │ ├── mbedtls_freertos_port.c
41│ │ │ └── threading_alt.h
42├── ThirdParty
43│ ├── mbedtls ( submodule : mbedtls )
44

Configure the Application Settings

Configure the cellular network

The following parameters in the cellular configuration, cellular_config.hexternal_link, (located in "MQTT_Mutual_Auth_Demo_with_<cellular_module>/cellular_config.h") must be modified for your network environment.

ConfigurationDescriptionValue
CELLULAR_COMM_INTERFACE_PORTThe cellular communication interface makes use of a COM port on your computer to
communicate with the cellular module on the Windows simulator.
Your COM port connected to the cellular module.
CELLULAR_APNThe default APNexternal_link for network registration.Specify the value according to your network operator.
CELLULAR_PDN_CONTEXT_IDPDN context id for the cellular network.The default value is CELLULAR_PDN_CONTEXT_ID_MIN.
CELLULAR_PDN_CONNECT_TIMEOUTPDN connect timeout for network registration.The default value is 100000 milliseconds.

Configure the MQTT broker

The configuration to connect to an MQTT broker can be found in the demo configuration, demo_config.hexternal_link, (located in "MQTT_Mutual_Auth_Demo_with_<cellular_module>/demo_config.h"). Refer to the documentation for the MQTT Mutual Authentication Demo) for more information.

Configure the COM port settings

Refer to your cellular module's documentation for COM port settings. Update the settings in comm_if_windows.cexternal_link if necessary.

Configure the other sub-modules

"FreeRTOSConfig.h", "mbedtls_config.h" and "core_mqtt_config.h" (located in "MQTT_Mutual_Auth_Demo_with_<cellular_module>) contain configurations for their corresponding sub-modules.

Demo Execution Step flow

The demo app performs three types of operations. By searching for the names of the functions in the diagram below, you can find the exact places these operations are performed in the source code.

  1. Register to a cellular network. (See cellular_setup.cexternal_link.)

  2. Establish a secure connection to AWS IoT with the MQTT broker. (See using_mbedtls.cexternal_link.)

  3. Perform MQTT operations. (See MutualAuthMQTTExample.cexternal_link.)

The following diagram illustrates the interactions between the demo app and other components.

Demo Flow Demo Flow Click image to enlarge

Build and run the MQTT mutual authentication demo

  1. In Visual Studio, open one of the mqtt_mutual_auth_demo.sln projects that matches your cellular modem.

  2. Set up an Amazon Web Services account and register a device as an AWS IoT thing on AWS IoT Core. See Using the AWS IoT message broker in the coreMQTT Mutual Authentication Demo for more details.

  3. Configure the thing Name, AWS IoT thing Endpoint and setup the credentials in demo_config.hexternal_link, (located in "MQTT_Mutual_Auth_Demo_with_<cellular_module>/demo_config.h").

  4. Compile and run.

The following is the console output of a successful execution of the bg96_mqtt_mutual_auth_demo.sln project.

1[INFO] [CELLULAR] [commTaskThread:287] Cellular commTaskThread started
2>>> Cellular SIM okay <<<
3>>> Cellular GetServiceStatus failed 0, ps registration status 0 <<<
4>>> Cellular module registered <<<
5>>> Cellular module registered, IP address 10.160.13.238 <<<
6[INFO] [MQTTDemo] [prvConnectToServerWithBackoffRetries:583] Creating a TLS connection to xxxxx-ats.iot.us-west-2.amazonaws.com:8883.
7
8[INFO] [MQTTDemo] [MQTTDemoTask:465] Creating an MQTT connection to xxxxx-ats.iot.us-west-2.amazonaws.com.
9
10[INFO] [MQTTDemo] [prvCreateMQTTConnectionWithBroker:683] An MQTT connection is established with XXXXXXXXXX-ats.iot.us-west-2.amazonaws.com.
11[INFO] [MQTTDemo] [prvMQTTSubscribeWithBackoffRetries:741] Attempt to subscribe to the MQTT topic testClient13:24:47/example/topic.
12
13[INFO] [MQTTDemo] [prvMQTTSubscribeWithBackoffRetries:748] SUBSCRIBE sent for topic testClient13:24:47/example/topic to broker.
14
15
16[INFO] [MQTTDemo] [prvMQTTProcessResponse:872] Subscribed to the topic testClient13:24:47/example/topic with maximum QoS 1.
17
18[INFO] [MQTTDemo] [MQTTDemoTask:479] Publish to the MQTT topic testClient13:24:47/example/topic.
19
20[INFO] [MQTTDemo] [MQTTDemoTask:485] Attempt to receive publish message from broker.
21
22[INFO] [MQTTDemo] [prvMQTTProcessResponse:853] PUBACK received for packet Id 2.
23
24[INFO] [MQTTDemo] [MQTTDemoTask:490] Keeping Connection Idle...
25
26
27[INFO] [MQTTDemo] [MQTTDemoTask:479] Publish to the MQTT topic testClient13:24:47/example/topic.
28
29[INFO] [MQTTDemo] [MQTTDemoTask:485] Attempt to receive publish message from broker.
30
31[INFO] [MQTTDemo] [prvMQTTProcessIncomingPublish:908] Incoming QoS : 1
32
33[INFO] [MQTTDemo] [prvMQTTProcessIncomingPublish:919]
34Incoming Publish Topic Name: testClient13:24:47/example/topic matches subscribed topic.
35Incoming Publish Message : Hello World!
36