Invoke the MQTT command on Watson IoT Platform from the iOS application

In this section, you’ll write a simple Swift application for iOS by using Xcode on a Mac. You need two buttons for your user interface: beep and dock. If you don’t want to build the interface, you can use the application code in GitHub.

When you build the application, you must add an MQTT client and reference it in the package manager of your choice. You might want to choose the protocol pod MQTTClient, which is available through CocoaPods.

  1. Reference the MQTT environment from your IoT boilerplate application. Then, click View Credentials.
  2. Enter your credentials in the following code:
    "iotCredentialsIdentifier": "???????????",
    "mqtt_u_port": 1883,
    "mqtt_s_port": 8883,
    "org": "",
    "apiKey": "",
    "apiToken": ""

    let ORG_ID = ""
    let ioTHostBase = ""
    let C_ID = "a: :RaspberryPiCreate2App"
    let DEV_TYPE = "RPi3"
    let DEV_ID = "RaspberryPi3iRobotCreate2"
    let BEEP_MSG = "1" //play beep
    let DOCK_MSG = "2" //dock
    let IOT_API_KEY = ""
    let IOT_AUTH_TOKEN = "”

    With these parameters when a button is pressed, you should create a connection to MQTT queues:
    let host = ORG_ID + "." + ioTHostBase
    let clientId = "a:" + ORG_ID + ":" + IOT_API_KEY
    let CMD_TOPIC = "iot-2/type/" + DEV_TYPE + "/id/" + DEV_ID + "/cmd/cmdapp/fmt/json"
    to: host,
    port: 1883,
    tls: false,
    keepalive: 30,
    clean: true,
    auth: true,
    user: IOT_API_KEY,
    pass: IOT_AUTH_TOKEN,
    will: false,
    willTopic: nil,
    willMsg: nil,
    willQos: MQTTQosLevel.atMostOnce,
    willRetainFlag: false,
    withClientId: clientId)

    This code sends a command over the MQTT connection:
    iotfSession.send( String.Encoding.utf8, allowLossyConversion: false),
    topic: CMD_TOPIC,
    qos: MQTTQosLevel.exactlyOnce,
    retain: false)

  3. Test the application.
    When your app is working, you can build the receiving flow on Raspberry Pi.
  4. Import the flow from GitHub to the Node-RED environment on Raspberry Pi. Just like in Lab 1, you’ll use the existing flow and configure it to connect to the Raspberry Pi.
  5. Update the credentials that you recorded from the Step 1 of this lab and verify all other fields. Then, click Deploy.
    If the iRobot Create2 is not connected through the USB serial port adapter, you might encounter an error as shown in the following image:

    Even if you get this error, you can still test the application and see the output in the debug nodes. When you connect the iRobot Create2, the connector changes.