MQTT Client
Example Summary
This example introduces the MQTT Client API and usage for the CC35xx family of devices.
Peripherals Exercised
The board LEDs are used for status indication. To distinguish similar indications, the user needs to be aware of the executed procedure.
NOTE: the LED hardware is assembled incorrectly on the LaunchPad board. In order to fix it, the LED component needs to be flipped 180 degrees. Please refer to the Limitations chapter.The onboard sensors include a temperature sensor and an accelerometer sensor. Both are controled over I2C interface.
NOTE: the I2C bus is missing pull-ups on its lines, clock and data. In order to fix it, the I2C bus needs some pull-up resistors. Please refer to the Limitations chapter.
NOTE: the accelerometer sensor is not functional.
The following table lists all LED options.
| LED indication | Led Color for CC35xx | Interpretation |
|---|---|---|
| Solidly on | Green | Indicates CC35xx is connected to the local AP |
| Solidly on | Red | Indicates CC35xx is connected to the MQTT Broker |
| Toggling on/off | Red | Publish message received in cc35xx/ToggleLED topic with the word *red* in the message payload |
| Toggling on/off | Green | Publish message received in cc35xx/ToggleLED topic with the word *green* in the message payload |
| Toggling on/off | Blue | Publish message received in cc35xx/ToggleLED topic with the word *blue* in the message payload |
Example Usage
- Access Point (AP) Configuration
- AP information is set in wifi_settings.h file
Remote Broker Configuration
- Broker parameters can be configured in mqtt_settings.h
- The broker parameters are:
- Connection types and security options
- IPv4 connection
- IPv6 connection
- URL/IP connection
- Secure connection - not implemented
- Server Address: URL or IP
- Port number of MQTT server
- Method to tcp secured socket - not implemented
- Cipher to tcp secured socket - not implemented
- The secure Files - not implemented
- Connection types and security options
Secured socket
Not implemeneted yet.Client Authentication
Not implemeneted yet.- Topics Configuration
- The topics can be set by calling MQTT_IF_Subscribe() with the appropriate parameters
- The subscription topics can be set in the mqtt_client_app.c
- The Client subscribes to the following default topics
Broker/To/cc35xx
cc35xx/ToggleLED
cc35xx/GetFwVersion
cc35xx/GetSensors
- To publish to topics the user can call MQTT_IF_Publish() with the appropriate parameters
- The Client publish the following default topics:
- cc35xx/To/Broker - the topic will be published by pressing SW1 switch on the LaunchPad
- cc35xx/To/FwVersion - the topic will be published in response to cc35xx/GetFwVersion getting triggered
- cc35xx/To/Sensors - the topic will be published in response to cc35xx/GetSensors getting triggered
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: NoneRun the example by pressing the reset button or by running debug session through your IDE.
Once the application has completed its initialization, the application banner would be displayed, showing version details and some initialization messages.
****************************************************************** ***************** MQTT Client ******************* ***************** 1.0.0.1 ******************* ****************************************************************** I2C Initialized! cme: osi_ThreadCreate cme_Thread: thrd is up and stopped cme_Thread: thrd is running Event_Thread: thrd is running received ROM init complete..... -------------- Download IniParams -------------- Wait for IniParams complete TCPIP_IF_init:: cb_initCompleteInd TCPIP_IF_addInterface:: cb_addInterfaceDeferred cb_addInterfaceCompleteInd Default Interface Added Firmware version:1.7.0.166 Phy version:2.1.38.5.26.67 Detected TMP107 sensor with target address 0x48 Detected BMA456 sensor with target address 0x18 [TCPIP_IF_setInterfaceState] I/F - UP! cb_interfaceStatusInd STATUS:: IPv4: DOWN OnTcpipEvent(1) OnWifiEvent(4) cb_interfaceUpDeferred:: 2b- Then, it will automatically procede and try to connect to the hard coded AP (wifi_settings.h) and MQTT broker (mqtt_settings.h).
- In case connection to the hard coded SSID AP fails, the application will timeout and show scan list of surrounding APs.
The user would be instructed to rescan in case the designated AP is not in the list or to choose the index of the desgnated AP and provide its credentials.
--------------------------------------------------------------------------------------- | SSID | BSSID | RSSI | Ch | Hidden | Security --------------------------------------------------------------------------------------- 1 | HW_TP_2.4 | 30:b5:c2:de:af:e4 | -58 | 1 | NO | 6 2 | cpn84 | 44:12:44:4b:08:f0 | -79 | 1 | NO | 4 3 | ********* | 44:12:44:4b:08:f1 | -75 | 1 | NO | 4 4 | externalhotspot84 | 44:12:44:4b:08:f2 | -79 | 1 | NO | 4 5 | net4guest84 | 44:12:44:4b:08:f3 | -78 | 1 | NO | 0 6 | cpn84 | 44:12:44:4a:ea:f0 | -59 | 11 | NO | 4 7 | ********* | 44:12:44:4a:ea:f1 | -60 | 11 | NO | 4 8 | DollCase | f8:d1:11:4b:e0:be | -58 | 11 | NO | 4 9 | asus_ax6000 | e8:9c:25:d8:84:88 | -49 | 11 | NO | 4 10 | NETGEAR51 | 08:36:c9:18:1b:02 | -50 | 4 | NO | 4 --------------------------------------------------------------------------------------- please choose a network index to connect to or any other key to rescan: 8 please enter the password: 1234567890 connecting to DollCase CME: CME_MESSAGE_ID_WLAN_CONNECT called. generalData=0x2 CME: cmeWlanConnect(); just a regular ad-hoc !! CME: cmeWlanConnect: Start connection : : : [TCPIP_IF_notifyLinkChange] I/F - CONNECTED (MAC LEVEL)! OnWifiEvent(4) cb_interfaceStatusInd STATUS:: IPv4: UP (ip 192.168.1.103, gw 192.168.1.254, mask 255.255.255.0) OnTcpipEvent(0) OnWifiEvent(CONNECTED_IP): addr=192.168.1.103 mask=255.255.255.0 gw=192.168.1.254Once the connection succeeds, the relevant LEDs turn on according to the table above.
Send L2 packet of type ETHERTYPE_EAPOL dataLen:121 Send L2 packet of type ETHERTYPE_EAPOL dataLen:99 CME: cme_InternalEventHandler:is_first_connect=0 OR currently connected bssid is EQUAL to the previously connected one (0x84:1:5:65:0:1e), keep the ARP entries as is[WLAN EVENT] STA Connected to the AP: ShlomiMobile , BSSID: 1e:0:65:5:1:84 [TCPIP_IF_notifyLinkChange] I/F - CONNECTED (MAC LEVEL)! OnWifiEvent(4) cb_interfaceStatusInd STATUS:: IPv4: UP (ip 172.20.10.2, gw 172.20.10.1, mask 255.255.255.240) OnTcpipEvent(0) OnWifiEvent(CONNECTED_IP): addr=172.20.10.2 mask=255.255.255.240 gw=172.20.10.1 Connected to AP. Starting MQTT... Found address 3.64.183.88 for url:broker.hivemq.com Waiting for messages... MQTT client "sl-1234567545" connection cb: status 0 MQTT EVENT: CONNACK (200002c0) Waiting for messages... Subscription result: 0 MQTT EVENT: SUBACK (28019ef0) Subscription result: 0 MQTT EVENT: SUBACK (28019ef0) Subscription result: 0 MQTT EVENT: SUBACK (28019ef0) Subscription result: 0 MQTT EVENT: SUBACK (28019ef0)
As can be seen, the Wi-Fi is connected and an IP address is assigned via DHCP. Then, MQTT broker is connected and 4 subscriptions are registered.
- Special handling
- In case the client will disconnect (for any reason) from the remote broker, the MQTT will be restarted.
- It is possible to toggle MQTT connection by using SW2 switch on the LaunchPad.
Application Design Details
This is an MQTT Client application used to demonstrate the client side of the MQTT protocol. This application uses an MQTT module (mqtt_if.c) as an abstraction layer to the LwIP’s internal MQTT library to make it easier for developers to use MQTT.
The application flow includes:
- The application starts by performing all the necessary initializations for the peripherals (e.g. GPIO, I2C, UART and timer).
- Once that is done, the application calls WIFI_IF_init() that is responsible to start the Wi-Fi and load the containers (RAM bootloader, firmware and configuration binary), and also initialize the TCPIP network stack.
- Then, MQTT is initialized via the MQTT_IF_init().
- The application attempts to connect to an AP via the WIFI_IF_start(), using the credentials the user configured in wifi_settings.h.
- Upon successful Wi-Fi connection, the application attempts to connect to an MQTT broker via the MQTT_IF_clientConnect().
- Upon successful MQTT connection, the various topics can be controlled over the MQTT cloud with a second MQTT peer device (e.g. the mobile phone).
The following messages should appear on the terminal when invoking the different topics:
Toggle blue LED IncomingTopic: cc35xx/ToggleLED, len=17 found match IncomingData: cc35xx/ToggleLED, len=17
TOPIC: cc35xx/ToggleLED PAYLOAD: toggling blue led QOS: 1Toggle red LED IncomingTopic: cc35xx/ToggleLED, len=16 found match IncomingData: cc35xx/ToggleLED, len=16
TOPIC: cc35xx/ToggleLED PAYLOAD: toggling red led QOS: 1Toggle green LED IncomingTopic: cc35xx/ToggleLED, len=18 found match IncomingData: cc35xx/ToggleLED, len=18
TOPIC: cc35xx/ToggleLED PAYLOAD: toggling green led QOS: 1Receive a message from a connected peer IncomingTopic: Broker/To/cc35xx, len=32 found match IncomingData: Broker/To/cc35xx, len=32
TOPIC: Broker/To/cc35xx PAYLOAD: hello all from an exteral client QOS: 1Looping back the FW version IncomingTopic: cc35xx/GetFwVersion, len=24 found match IncomingData: cc35xx/GetFwVersion, len=24
TOPIC: cc35xx/GetFwVersion PAYLOAD: getting firmware versionlient QOS: 1 Received message: 5 APP_MQTT_PUBLISH_FW_VER Waiting for messages... Publish result: 0 MQTT EVENT: PUBACK (28019f98)Looping back the sensors status IncomingTopic: cc35xx/GetSensors, len=22 found match IncomingData: cc35xx/GetSensors, len=22
TOPIC: cc35xx/GetSensors PAYLOAD: getting sensors statussionlient QOS: 1 Received message: 6 APP_MQTT_PUBLISH_SENSORS temperature=27 degC axisX=0 axisY=0 axisZ=0Receive an MQTT disconnect request and then MQTT connect by toggling SW2 switch Received message: 1 APP_MQTT_CON_TOGGLE 1->0 MQTT EVENT: CLIENT_DISCONNECT (200002c0) Waiting for messages… Received message: 1 APP_MQTT_CON_TOGGLE 0->1 Found address 3.66.74.72 for url:broker.hivemq.com Waiting for messages… MQTT client “sl-1234567545” connection cb: status 0 MQTT EVENT: CONNACK (200002c0) Waiting for messages… Subscription result: 0 MQTT EVENT: SUBACK (28019f98) Subscription result: 0 MQTT EVENT: SUBACK (28019f98) Subscription result: 0 MQTT EVENT: SUBACK (28019f98) Subscription result: 0 MQTT EVENT: SUBACK (28019f98)
Limitations
- The LED hardware is assembled incorrectly on the LaunchPad board. In order to fix it, the LED component needs to be flipped 180 degrees.
- The I2C bus is missing pull-ups on its lines, clock and data. In order to fix it, the I2C bus needs some pull-up resistors.
- The accelerometer sensor is not functional.
- As an initial draft, MQTT is open, not secured.