Introduction

This workshop shows how to connect the SimpleLink™ Wi-Fi® CC32xx LaunchPad™ directly to the Watson IoT Platform, send data to the cloud, and how to push data to the Quickstart service for easy web browser display.

IBM Watson IoT Platform is a fully managed, cloud-hosted service that makes it simple to build and deploy apps for IoT devices, sensors and gateways. It provides solutions for device registration, connectivity, control, rapid visualization and storage of data derived from the IoT. When combined with the IBM Bluemix® environment and access to secure Watson APIs, the platform allows you to integrate and analyze predictive, cognitive, and contextual analytics for stronger decision-making.

Prerequisites

Software

  • Code Composer Studio v10.3 or newer
    • SimpleLink Wi-Fi CC32xx Wireless MCUs support installed
    • Make sure that CCS is using the latest updates: HelpCheck for Updates
  • CC32xx SDK v5.20 or newer
  • Terminal emulator program such as TeraTerm or PuTTY

Hardware

Task 1: Setup

We recommended you first complete the MQTT Client demo from the Wi-Fi MQTT SimpleLink Acadamy training to review MQTT using a generic example. This IBM MQTT training will use the foundations of the Wi-Fi MQTT module.

  1. Import the mqtt_client CC32xx project from the CC32xx SDK or TI Resource Explorer (simplelink_cc32xx_sdk_x_xx_xx_xx\examples\rtos\CC3220x_LAUNCHXL\demos\mqtt_client\tirtos\ ccs\mqtt_client_CC3220SF_LAUNCHXL_tirtos_ccs).

  2. Define your Access Point parameters (SSID_NAME, SECURITY_TYPE and SECURITY_KEY) in network_if.h. Note: use SL_WLAN_SEC_TYPE_WPA_WPA2 for SECURITY_TYPE if you have a WPA/WPA2 AP.

Task 2: Configure/modify MQTT Client example

  1. MQTT Settings
    Settings for the demo can be found in the macros at the beginning of the mqtt_client_app.c source file. We need to modify the following:

    • Modify MQTT_CONNECTION_ADDRESS to point to the IBM Quickstart Broker
            #define MQTT_CONNECTION_ADDRESS           "quickstart.messaging.internetofthings.ibmcloud.com"
      

      mqtt_client_app.c :: LOCAL DEFINES

    • Add a buttonToggle int and publish_data char array. This will be used when publishing data to IBM.
            int buttonToggle = 0;
            char publish_data[100] = {'\0'};
      

      mqtt_client_app.c :: GLOBAL VARIABLES

    • Modify the WILL message.

            #define MQTT_WILL_TOPIC    "iot-2/evt/status/fmt/json"
            #define MQTT_WILL_MSG      "{\"d\":{\"state\":\"LastWillAndTestament\"}}"
            #define MQTT_WILL_QOS      MQTT_QOS_0
            #define MQTT_WILL_RETAIN   false
      

      mqtt_client_app.c :: LOCAL DEFINES

  2. Remove Topic Subscription

    • Remove the code for topic subscription (since QuickStart service is publish only and doesn’t allow subscriptions). We are going to comment out this section in mainThread() to make it easy to add back in.

      /*
      * In case a persistent session is being used, subscribe is called before connect so that the module
      * is aware of the topic callbacks the user is using. This is important because if the broker is holding
      * messages for the client, after CONNACK the client may receive the messages before the module is aware
      * of the topic callbacks. The user may still call subscribe after connect but have to be aware of this.
      */
      /*ret = MQTT_IF_Subscribe(mqttClientHandle, "Broker/To/cc32xx", MQTT_QOS_2, BrokerCB);
      ret |= MQTT_IF_Subscribe(mqttClientHandle, "cc32xx/ToggleLED1", MQTT_QOS_2, ToggleLED1CB);
      ret |= MQTT_IF_Subscribe(mqttClientHandle, "cc32xx/ToggleLED2", MQTT_QOS_2, ToggleLED2CB);
      ret |= MQTT_IF_Subscribe(mqttClientHandle, "cc32xx/ToggleLED3", MQTT_QOS_2, ToggleLED3CB);
      if(ret < 0){
        while(1);
      }
      else{
        LOG_INFO("Subscribed to all topics successfully\r\n");
      }*/
      

      mqtt_client_app.c :: mainThread()

  3. Configure the Client ID

    • First, change the size of the ClientID in the global variable section.
            /* Client ID                                                                  */
            /* If ClientId isn't set, the MAC address of the device will be copied into   */
            /* the ClientID parameter.                                                    */
            char ClientId[64] = {'\0'};
      

      mqtt_client_app.c :: GLOBAL VARIABLE

    • Then, delete the existing code in the SetClientIdNamefromMacAddress() function and add our modified version to work with IBM.

            int32_t SetClientIdNamefromMacAddress()
            {
                int32_t ret = 0;
                uint16_t macAddressLen = SL_MAC_ADDR_LEN;
                uint8_t macAddress[SL_MAC_ADDR_LEN];
      
                /*Get the device Mac address */
                ret = sl_NetCfgGet(SL_NETCFG_MAC_ADDRESS_GET, 0, &macAddressLen,
                                   &macAddress[0]);
                // Format ClientID, using MAC address
                sprintf(ClientId, "d:quickstart:ti-simplelink:%02x%02x%02x%02x%02x%02x",
                 macAddress[0],
                 macAddress[1],
                 macAddress[2],
                 macAddress[3],
                 macAddress[4],
                 macAddress[5]);
      
               UART_PRINT("\t MQTT ClientId: %s", ClientId);
               UART_PRINT("\r\n");
      
                return(ret);
            }
      

      mqtt_client_app.c :: SetClientIdNamefromMacAddress()

    • Finally, we'll add an include for stdio.h to be able to use sprintf().
            /* Standard includes                                                          */
            #include <stdlib.h>
            #include <stdio.h>
      

      mqtt_client_app.c :: Standard includes

  4. Configure the message

    • We’ll send a JSON payload of the LaunchPad button state (SW2) which we will toggle each time the button is pressed. We also need to send the message as MQTT_QOS_0. Modify the if(queueElement.event == APP_MQTT_PUBLISH) case in the while(1) loop of mainThread() so that the JSON-formatted message with the button state is sent to IBM MQTT broker on a button event.

        while(1){
      
            mq_receive(appQueue, (char*)&queueElement, sizeof(struct msgQueue), NULL);
      
            if(queueElement.event == APP_MQTT_PUBLISH){
                buttonToggle = !buttonToggle;
                LOG_INFO("APP_MQTT_PUBLISH\r\n");
      
                sprintf(publish_data, (const char *)"{\"d\":{\"button\":%d}}", buttonToggle);
                MQTT_IF_Publish(mqttClientHandle,
                                "iot-2/evt/status/fmt/json",
                                publish_data,
                                strlen(publish_data),
                                MQTT_QOS_0);
      
                GPIO_clearInt(CONFIG_GPIO_BUTTON_0);
                GPIO_enableInt(CONFIG_GPIO_BUTTON_0);
        }
      

      mqtt_client_app.c :: mainThread()

Task 3: Rebuild and Run

  1. Rebuild the MQTT application with these changes and reload/run the application. You should see the following on the serial console.

  2. Each time the User button (SW2) is pressed, a message with the button state will be sent.

    Buttons on the LaunchPad

    Press the button listed below for your device.

    CC32xx Rev A LaunchPad: SW2

    CC3220S/SF Rev B LaunchPad: SW3

    MSP432P4 LaunchPad: S1

    MSP432E4 LaunchPad: SW1

    For more information on CC3220 LaunchPad revisions, refer to the CC3220 LaunchPad Development Kit Hardware User's Guide.

  3. Visualizing the data using IBM QuickStart

    • Now connect to IBM QuickStart to view the data by navigating to this link in your web browser: https://quickstart.internetofthings.ibmcloud.com/
    • Use the MAC address of the LaunchPad as shown on the serial console to see the data! You will need to remove the colons (:) from the MAC address

Congratulations! Your SimpleLink MCU Platform CC32xx Wi-Fi LaunchPad is now connected to the IBM Watson Platform!

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.