- What you'll need to build your apps
- 1. Create an IoT app in the IBM Cloud
- 2. Add a device that will send MQTT messages to the Watson IoT Platform
- 3. Install and configure the Android app
- 4. Verify that messages are being sent from your smartphone to the Watson IoT Platform
- 5. Process messages in a Node-RED flow
- 6. Create an IBM Cloud app to visualize sensor data
- Downloadable resources
- Related topics
Turn your smartphone into an IoT device
Create IBM Cloud apps to process, visualize, and store sensor data that is sent to and from your smartphone
This tutorial shows how you can send sensor data that is generated by your smartphone to the IBM Watson IoT Platform cloud-hosted service, and then create Cloud Foundry apps on the IBM Cloud that process, visualize, and store the data. Lastly, it shows you how to create an Android application for a smartphone.
Here is an overview of the architecture:
What you'll need to build your apps
- An IBM Cloud account; if you do not have an existing IBM Cloud account, start your free trial
- Download and install the Cloud Foundry command line interface.
- A smartphone (an Android or iOS device)
Create an IoT app in the IBM Cloud
The Internet of Things Platform Starter boilerplate contains a Node-RED engine that you will use later to process IoT messages. For now, you will create an Internet of Things service to send and receive MQTT messages to and from the IBM Watson IoT Platform.
- Log in to your IBM Cloud account.
- Click Create Resource.
- In the Catalog, under Platform, click Internet of Things Platform Starter.
- Enter a name for your application. Because this name is also used as
the host name, it must be unique within the IBM Cloud, for example,
"iot<your name>". For example, I might use
- Click Create.
- After your app is created, in the left pane, click Overview. Notice that your app contains two connections, one to a Cloudant NoSQL database and another to an Internet of Things Platform service.
Add a device that will send MQTT messages to the Watson IoT Platform
- In the Overview view of your app, under Connections, click the
Internet of Things Platform service, named something like
- Click Launch to open the Watson IoT Platform
The IBM Watson IoT Platform dashboard is displayed, which is a service that is independent of the IBM Cloud. An organization ID is assigned to your app, and you will need this ID later when developing the mobile app. In the following image, the organization ID is
mgso1i, which is displayed under your login information in the upper right corner of the dashboard.
- On the left menu, which pops out when you hover over it, click
Devices. Then, click Add a device
type. In your organization, you can have multiple device
types each with multiple devices. A device type is a group of
devices that share characteristics; for example, they might provide
the same sensor data. In our case, the device type name must
Android" (this device type name is required by the app that you will use later).
- Click Next. A page is displayed where you can enter metadata about the device type, such as a serial number or model. You don't need to specify this information for this demo. Just click Done.
- Click Register Devices. Enter the device ID. The device ID can be, for example, the MAC address of your smartphone. However, it must be unique within your organization only. Therefore, you might enter, as I did here, something like "112233445566".
- Click Next. A page is displayed where you could enter metadata about the device. Leave it blank, and click Next.
- Provide a value for the authentication token. Remember this value for later. Then, click Next.
- Click Done.
- Click Back.
Now you are ready to send MQTT messages from a device to the IBM Watson IoT Platform.
Install and configure the Android app
You will use the IoT Starter for Android app to read and send sensor data
on your smartphone. The source code and documentation of the app are in
iot-starter-for-android GitHub project.
If you are experienced in Android development, you can download the code from GitHub, import it into your Android development environment, and then build the apk file. Otherwise, to get the app installed and running quickly, follow these steps.
Note: The .apk file is compiled for Android version 5.0.1, but it also has been tested and works on Android 6. If you have installed an older version of this app, you need to uninstall it before installing the this one.
- On your phone, go to Settings > Security. Under Device Administration, enable Unknown sources. Now you can install .apk files from outside of Google Play.
- Open the browser on your phone, and enter this URL:
- Search for the
iotstarter-v2.1.0.apklink, and click the link to download the .apk file.
- Click the downloaded file, and confirm that you want to install the
The IoT Starter app is now installed on your Android device.
Next, you need to configure your Android app.
- Start the IoT Starter app.
- Click Skip tutorial.
- Enter the following parameters:
- Organization: The organization ID that was
displayed on the IBM IoT server (at the start of "Add a device that will send MQTT messages to the Watson IoT
Platform"). For example,
mgso1iin this tutorial.
- Device ID: The device ID that you configured,
at the end of "Add a device that will send MQTT messages to the Watson IoT
Platform." For example,
112233445566" in this tutorial.
- Auth Token: The authorization token that you specified in "Add a device that will send MQTT messages to the Watson IoT Platform."
- Check Use SSL.
- Organization: The organization ID that was displayed on the IBM IoT server (at the start of "Add a device that will send MQTT messages to the Watson IoT Platform"). For example,
- Click Activate Sensor. Now the app collects data from the acceleration sensor in your smartphone and sends the data to the IBM IoT server. The app displays the accelerometer data and the number of messages that were published or received.
Verify that messages are being sent from your smartphone to the Watson IoT Platform
- Back on your computer, open the IBM Watson IoT Platform page for your organization again (see the start of Add a device that will send MQTT messages to the Watson IoT Platform).
- In the left menu, click Devices. Your Android device is displayed.
- Click the Device ID, and then go to the Recent Events tab. You should see events coming from your smartphone.
- Click one of the events. The messages that are sent from your smartphone are in JSON format. They contain acceleration and position data.
Now you are ready to work with the message data on IBM Cloud.
Process messages in a Node-RED flow
In this section, you will enhance your IBM Cloud IoT app by using a Node-RED flow to process messages from your smartphone, and then send messages back to your smartphone. The phone will react on these messages by changing the background color in the app.
- Open your IBM Cloud dashboard (go to bluemix.net and click Dashboard.
- In your IBM Cloud dashboard, verify that your IBM Cloud IoT app (that you created in Step 1) is up and running.
- In a browser, open <yourappname>.mybluemix.net, where <yourappname> is the name for your IoT app. Follow the wizard steps to set a user name and password for the Node-RED editor.
- On the Node-RED page for your IoT app, click Go to your Node-RED flow editor. The editor opens, containing a sample flow.
- Using the drag-and-drop features of this editor, you can plug together a flow of messages. Although you can create your own flow here, we will import the code below. But first, select all existing nodes, and delete them by pressing the Delete key.
- Download the following code (as a long single line of code) as a text
nodeRedCode.txt) from GitHub.
- Open the file in a text editor. Make sure that all the code is on a single line. Remove any line breaks. Copy the line of code.
- In the Node-RED editor, press Ctrl-I to open the Import Nodes dialog. Paste the code, and click OK.
- Now you need to adapt the flow to your specific parameters. The only
relevant parameter is the Device ID. Double-click the node IBM
IoT App out. In the pop-up window, enter the Device ID
that you used earlier (for example,
112233445566), and click Import.
- Click Deploy in the flow editor. The flow is deployed and should be active immediately.
- Move your smartphone around; flip and tilt it. The background color of the app on your phone should now change colors, depending on the orientation of the z-axis.
- In the Node-RED editor, click the rectangle next to the msg.payload node, and click the debug tab to enable debugging. You should see messages that are sent from your phone. The data is in JSON format.
- Inspect the flow. Double-click the calc color node. It calculates the red, green, and blue values based on the incoming z-acceleration value, and passes them on as JSON data.
You now have two-way communication between your smartphone and the first IBM Cloud IoT app.
Create an IBM Cloud app to visualize sensor data
In this step, you create an additional app in the IBM Cloud, which receives the messages from your smartphone (now an IoT device) and visualizes the data.
- Download the rickshaw4iot-0.2.0.zip file from the rickshaw4iot Github project for IoT Visualization. Extract the files to a local directory. Alternatively, you can clone or download the Github repository.
- From the folder where you extracted the rickshaw4iot-0.2.0.zip
file, open the manifest.yml file in a text editor. Change the
host and name parameters to a
unique name. Again, it must be unique within the IBM Cloud, because it
is used as a host name. For example, use
iot<your name>visualize. Save the file.
- Open a command or terminal window, and change to the directory where you extracted the rickshaw4iot-0.2.0.zip file.
- Enter the following Cloud Foundry
cf api https://api.ng.bluemix.net
cf login -u <your_bluemix_login_id>
cf logincommand is required only the first time you use the cloud foundry command line tool. You might also need to enter your IBM Cloud password. If you have more than one IBM Cloud Organization (not the default case), you are asked to select one of them.
The above command logs on to the US South region. Your account might be in a different region:
cf login -a https://api.eu-gb.bluemix.net // United Kingdom cf login -a https://api.eu-de.bluemix.net // Germany cf login -a https://api.au-syd.bluemix.net // Sydney
- Deploy the application to IBM Cloud by entering the following command:
cf push <your_application_name>
This command looks for the file manifest.yml and uses the parameters from that file. It then uploads the code to IBM Cloud. After a while, you should see a message similar to the following message:
App <your app name> was started using this command 'node app.js'.
- In IBM Cloud, click Dashboard. You should see the app you just deployed. Click it to see its details.
- Click Create connection. Select the Internet of Things service that you defined in Step 1, and click Connect.
- Click Restage to restage the app. Now the app can receive messages from the Watson IoT Platform.
- To verify the visualizer app, in a browser, open http://<your app name>.mybluemix.net
- In the Device drop-down box, select your device id.
- Move your smartphone around. You should see the acceleration changes in the chart.
In this tutorial you learned how to easily turn your smartphone into a sensor device, connect it to the IBM Watson IoT Platform, and send and receive data. You also learned how to process and visualize device data on the IBM Cloud. With these two apps, you can recognize the value of IBM Cloud for the Internet of Things, and all you need is your own smartphone.
- Watson IoT Platform Documentation
- Try other developerWorks tutorials using the Internet of Things service on IBM Cloud
- Try other IoT samples on IBM Code