Table of Contents
Example Summary
This example introduce the MQTT Client Server library API and usage.
Peripherals Exercised
- The board LEDs are used for status indication. To distinguish similar indications, the user needs to be aware of the executed procedure.
The following table lists all options.
LED indication | Led Color for CC3220 | Led Color for CC3235 | Interpretation |
---|---|---|---|
Solidly on | Green | Green | Indicate Simplelink is properly up - Every Reset / Initialize |
Blinking | Red | Blue | Device is trying to connect to AP - Every Reset / Initialize |
Solidly off | All | All | Device connected and working - Only after connection |
Example Usage
- Access Point (AP) Configuration
- AP information is set in ‘network_if.h’ file.
The application have two MQTT entities
MQTT Client Role - Can connect to remote broker
- In order to activate this role, ENABLE_CLIENT must be defined
- Remote Broker Configuration
- Broker parameters can be configured in Mqtt_ClientCtx parameter which can be found in ‘mqtt_server_app.c’
- The broker parameters are:
- Connection types and security options
- IPv4 connection
- IPv6 connection
- URL connection
- Secure connection
- skip domain name verfication in secure connection
- skip certificate catalog verfication in secture connection
- Server Address: URL or IP
- Port number of MQTT server
- Method to tcp secured socket
- Cipher to tcp secured socket
- Number of files for secure transfer
- The secure Files
- Connection types and security options
Secured socket
In order to activate the secured example, SECURE_CLIENT must be defined ‘mqtt_server_app.c’ file ( certificates should be programmed ).- Client Authentication
In order to activate the Client authentication by the server, CLNT_USR_PWD must be defined ( ClientUsername and ClientPassword must be defined ). - Topics Configuration
- The topics can be set by changing the definitions in ‘mqtt_server_app.c’ file
- The subscription topics can be set in the SUBSCRIPTION_TOPICX definitions
- The Client is subscribe to the following default topic
“/Broker/To/cc32xx”
- The publish topic and data can be set in the PUBLISH_TOPICX and PUBLISH_TOPICX_DATA definitions
- The Client publish the following default topic “/cc32xx/ButtonPressEvtSw2” - the topic will be published by pressing SW2 on the board
MQTT Server Role - Broker that is ready for external client connection
- In order to activate this role, ENABLE_SERVER must be defined
- Local Broker Configuration
- Broker parameters can be configured in Mqtt_Server parameter which can be found in ‘mqtt_server_app.c’
- The broker parameters are:
- Port number of MQTT server
- Method to tcp secured socket
- Cipher to tcp secured socket
- Number of files for secure transfer
- The secure Files
- Secured socket In order to activate the secured example, SECURE_SERVER must be defined ( certificates should be programmed ).
- Client Authentication In order to activate the Client authentication by the server, SRVR_USR_PWD must be defined ( ClientUsername and ClientPassword must be defined ).
- Topics Configuration
- The topics can be set by changing the definitions in ‘mqtt_server_app.c’ file
- The server can subscribe to a topic (loopback topic), this topic can be set in the ENROLLED_TOPIC definition
- The Server is subscribed to the following default topics “/cc32xx/To/Broker”
- Each client connected to the Broker, can publish on that topic, and the internal client will forward that topic to the remote broker
Both MQTT roles have internal loopback to allow topic forwarding.
Build the project and flash it by using the Uniflash tool for cc32xx, or equivalently, run debug session on the IDE of your choice.
Open a serial port session (e.g. ‘HyperTerminal’,‘puTTY’, ‘Tera Term’ etc.) to the appropriate COM port - listed as ‘User UART’.
The COM port can be determined via Device Manager in Windows or vials /dev/tty*
in Linux.The connection should have the following connection settings:
Baud-rate: 115200 Data bits: 8 Stop bits: 1 Parity: None Flow Control: None
Run the example by pressing the reset button or by running debug session through your IDE.
Green LED
turns ON to indicate the Application initialization is completeOnce the application has completed it’s initialization and the network processor is up,
the application banner would be displayed, showing version details:============================================ MQTT client server Example Ver: 1.1.1 ============================================ CHIP: 0x30000019 MAC: 2.0.0.0 PHY: 2.2.0.5 NWP: 3.3.99.2 ROM: 0 HOST: 2.0.1.17 MAC address: 04:a3:16:45:89:8e ============================================
- At this point
Board_LED0
will blink until the device will connect to the hard coded AP.- In case connection to the hard coded SSID AP fails, user will be requested to fill the SSID of an open AP it wishes to connect to.
- If no AP is available or connection failed, the example reset and tries to connect again.
- Once the connection success all LEDs turn off.
- In case connection to the hard coded SSID AP fails, user will be requested to fill the SSID of an open AP it wishes to connect to.
- Special handling
- In case the internal client will disconnect (for any reason) from the remote broker, the MQTT won’t be restarted.
The user can change that behavior by adding gResetApplication = true to MQTT_CLIENT_DISCONNECT_CB_EVENT case in ‘Client_server_cbs.c’ file.
- In case the internal client will disconnect (for any reason) from the remote broker, the MQTT won’t be restarted.
Application Design Details
This example provides users the ability to work with both Client and Server (Broker) MQTT Entities by combine the two entities to one example, we also allow the user to provide a loop back between internal and external clients (using the enrolled topic)
Server/Broker Allows full Broker capabilities (up to 4 clients)
Clients can connect/subscribed/publish to the broker at any timeClient Allows full mqtt client abilities
The Client can connect to remote broker, subscribe and publishLoopback The internal Client will be connected to the internal Broker, with enrolled topic (subscribed).
Any published data on that topic, that arrived to the broker, will be passed to the internal client, and will be forward to the remote broker.
Any topic that the client is subscribed to at the remote broker, that was published, will be followed to the internal broker, and from there to all subscribed clients This will allow full forwarding of topics.
References
MQTT Org - MQTT Home page
MQTT v3.1.1 specification
MQTT v3.1 specification
For further information please refer to the user programmers guide: CC3X20 Programmer’s Guide