Staging:CC3120 MQTT Server

Overview
This application will demonstrate the usage of the MQTT library as both a server and a client.

For demo application using the Simplelink device as MQTT Client, please refer to CC3120 MQTT Client application.

Application Details
MQTT (Message Queue Telemetry Transport) protocol is an extremely light weight machine to machine connectivity protocol. It is based on publish/subscribe messaging model and is designed to be used on the top of TCP/IP protocol. Key point of this protocol includes small code footprint and low network bandwidth requirement. Other features include faster response time, low power requirement, ease of scalability. All these advantages makes it an ideal candidate for communication protocol in embedded devices intended to implement IOT(internet of things) applications. More information regarding MQTT protocol can be obtained from the latest MQTT Protocol specification.

A Simple MQTT infrastructure contains a broker(like a central hub) connected to multiple clients, each of which has the capability of publishing on any topic(token). Broker has the responsibility of sending the message published on any topic to all the subscribers of that topic. This is a very simplistic description of a MQTT network, which has a lot more features and configuration parameters.

In this example application, the Simplelink Wi-Fi device is running a MQTT server which allows local MQTT client to communicate with each other. Simultaneously, it is also running a client which is connected to the online broker. The interface between the on-board client and the server is such that the local clients can also communicate with the remote MQTT clients, which are connected to the same online broker as the on-board client.

The general concept of this application is identical to the CC3120 MQTT Client application, but with the introduction of a MQTT bridge that utilizes the services of the MQTT server and MQTT client running in parallel on the same platform. The MQTT server manages the local MQTT network whereas, the MQTT client connects to a cloud server / broker. The bridge application enables data exchange between local-server and the cloud server. To receive data from the cloud server, the MQTT bridge, through the MQTT client, subscribes to certain topics. It also forwards the data / topics received from the local MQTT server to the cloud-server. The local MQTT server handles the connections from the multiple MQTT clients (also shown as Launchpads in the following figure, but can be running on another platform) in the local network.

This application makes use of the APIs from MQTT client library to communicate with an IBM web client using the IBM broker. Two LEDs on the host platform can be controlled from a web client by publishing msg on appropriate topics. Similarly, message can be published on pre-configured topics(in the code) by pressing the buttons on the host platform.



The motivation behind this application is to showcase a working setup where Two local MQTT clients can communicate with each other as well as talk to a remote client via an external broker. For the sake of simplicity following abbreviations are used:
 * 1) LC1 - Local MQTT client 1 (e.g. a CC3220 Launchpad)
 * 2) LC2 - Local MQTT client 2 (e.g. a CC3220 Launchpad)
 * 3) RC - Remote MQTT client (e.g. the IBM MQTT Web Client)
 * 4) AS(Application Server) - MQTT server running on the Simplelink device
 * 5) AC(Application Client) - MQTT client running on the Simplelink device
 * 6) BR - External broker/cloud server (e.g. the IBM MQTT broker: messagesight.demos.ibm.com)
 * 7) AP - Access Point

The use-case consists of two Local MQTT client LC1 and LC2, which could be running on a PC or Smartphone or even can be CC32xx clients(as shown in the above figure), communicating with each other using the MQTT server(AS). These Local clients can also communicate with a Remote MQTT client(RC) through an external broker(BR). Both these cases are explained below. Please refer to the following diagrams for more clarification.

Local Communication

 * 1) LC1 connects to AS
 * 2) LC2 connects to AS
 * 3) LC1 subscribes on a particular topic.
 * 4) LC2 publish message on the same topic.
 * 5) LC1 should get the message published by LC2. This will conclude the Local MQTT Network.

Remote Communication

 * 1) RC and AC should be connected to the same external broker(BR)
 * 2) AC has already subscribed to the topic SUB_TOPIC
 * 3) RC will publish a message on SUB_TOPIC
 * 4) Whatever message is received by AC will be passed to the AS.
 * 5) If any of the local client has subscribed to the SUB_TOPIC, it will receive the message published by RC.
 * 6) AC has also enrolled for the topic ENROLLED_TOPIC, which means if AS receives any message on ENROLLED_TOPIC from any local client, it will be passed to the AC. Which in turn will published to the BR.
 * 7) If RC has subscribed on ENROLLED_TOPIC, it will receive the message published by the Local Client.

Source Files Details

 * main.c - Initializes the device in default configuration and creates two tasks - 'mainTask' and 'MqttClientServer'

MQTT Library Details
MQTT library abstracts out the underlying intricacies of MQTT network and provides you with intuitive and easy to use APIs to implement the MQTT protocol on the Simplelink Wi-Fi device.

The MQTT Library API documentation can be found under the /docs directory:
 * mqtt_client.chm - API document for the MQTT Client library.
 * mqtt_server.chm - API document for the MQTT Server library.
 * sl_mqtt_client.chm - API document for the simple link wrapper around MQTT Client library.
 * sl_mqtt_server.chm - API document for the simple link wrapper around MQTT Server library.

Prerequisite

 * 1x CC3120BOOST
 * 1x MSP432-EXP432P401R
 * 2x MQTT client devices (can be CC3120+MSP432 or CC3220 platforms)
 * 1x Micro-USB cable
 * 1x 2.4GHz AP with internet access
 * Access to the IBM MQTT Web Client

Configurations
Configurations here are very similar to the MQTT Client example. Additional configuration fields are added for server.

Example Use Cases and Outputs
This application is essentially the same as the MQTT Client, with the difference of having a MQTT broker and multiple MQTT clients in the same local network.

Publishing the topic "/Broker/To/Simplelink" from the website will result in both LC1 and LC2 will receive the same message and toggle their LED.
 * Publishing from the IBM MQTT Web Client

Pressing the button on either LC1 or LC2 will publish the topic "/Simplelink/ButtonPressTopic1" to all client devices.
 * Publishing from a local MQTT client

Additional Information
To print about the packet level details of MQTT packets in the console, define DEBUG_NET_DEV as the predefined symbol while building the mqtt library.

Limitations/Known Issues

 * Client can connect to at most 4 brokers at a time