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.
- Reference the MQTT environment from your IoT boilerplate application. Then, click View Credentials.
- Enter your credentials in the following code:
let ORG_ID = ""
let ioTHostBase = "messaging.internetofthings.ibmcloud.com"
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"
This code sends a command over the MQTT connection:
iotfSession.send(BEEP_MSG.data(using: String.Encoding.utf8, allowLossyConversion: false),
- Test the application.
When your app is working, you can build the receiving flow on Raspberry Pi.
- 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.
- 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.