Digital Developer Conference: Hybrid Cloud. On Sep 22 & 24, start your journey to OpenShift certification. Free registration

Turn your smartphone into an IoT device

This code pattern is part of the 2020 Call for Code Global Challenge.

It 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:

Chart showing architecture of solution

What you’ll need to build your apps

Step 1 Create an IoT app in the IBM Cloud

Work through the steps in this tutorial to create a Node-RED and Watson IoT Platform starter app in IBM Cloud.

As a result, you will have a Node-RED Starter Kit application connected to the IBM IoT platform.

Step 2 Add a device that will send MQTT messages to the IBM Watson IoT Platform

After the last step in the tutorial in Step 1, the IBM Watson IoT Platform console is opened.

  1. 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 l0y3u1, which is displayed under your login information in the upper right corner of the dashboard.Screen capture of the service on the IBM Internet of Things Platform server and the organization ID highlighted
  2. Click tab Device Types, then click Add 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 be “Android” (this device type name is required by the app that you will use later).
  3. 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 tutorial. Just click Finish.Screen capture of the add device type page
  4. 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”. Screen capture of the page where device parameters are entered
  5. Click Next. A page is displayed where you could enter metadata about the device. Leave it blank, and click Next.
  6. On the security page, enter a value for the authentication token. Remember this value for later. Then, click Next. Screen capture showing authentication token
  7. Click Finish.

Now you are ready to send MQTT messages from a device to the IBM Watson IoT Platform.

Step 3 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 the 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.

  1. On your phone, go to Settings > Apps & notifications. Tap Advanced. Scroll down up to end of the page and tap Special app access
  2. Select Install unknown apps
  3. Click Chrome and enable trust this source

    Note: on versions other than Android 9, the settings are slightly different.

Now you can install .apk files from outside of Google Play using Chrome.

  1. Open the Chrome browser on your phone, and enter this URL: https://github.com/deveops/iot-starter-for-android/releases

    Screen capture that shows the .apk file in a browser window to download and install
  2. Open Assets and click the iotstarter-v2.1.0.apk link to download the .apk file. Confirm the warning saying that .apk files might be dangerous.

  3. Once downloaded, click Install to install the app.

The IoT Starter app is now installed on your Android device.

Next, you need to configure your Android app.

  1. Start the IoT Starter app.
  2. Click Skip tutorial.
  3. Enter the following parameters:
    • Organization: The organization ID that was displayed on the IBM IoT server (at the start of ” “). For example, l0y3u1 in this tutorial.
    • Device ID: The device ID that you configured above. For example, “112233445566” in this tutorial.
    • Auth Token: The authorization token that you specified earlier.
    • Make sure that Use SSL is checked.Screen capture that shows the configuration parameters for your Android app
  4. 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.
Screen capture of the accelerometer data from the running Android app

Step 4 Verify that messages are being sent from your smartphone to the Watson IoT Platform

  1. Back on your computer, open the IBM Watson IoT Platform page for your organization again
  2. In the left menu, click Devices. Your Android device is displayed.Screen capture of the Devices tab that lists the Android device
  3. Click the arrow icon on the right of your Device ID. This opens an new page where you can see “Recent Events”. You should see events coming from your smartphone.
  4. Click one of the events. The messages that are sent from your smartphone are in JSON format. They contain acceleration and position data.Screen capture that shows the pop-up window with the incoming messages from the Android device

Now you are ready to work with the message data on IBM Cloud.

Step 5 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.

First you need to import the nodes ibm iot in and ibm iot out into your Node-RED application. These nodes allow you to easily connect to Watson IoT, but they are not in the palette by default. To do so, you need to create a continuous delivery toolchain and then add dependencies to the application.

NOTE: There is also the possibility to directly import new nodes to the palette in the Node-RED editor. This would not require to set up a toolchain. But the free trial version of IBM Cloud does not provide enough memory for that option to work.

  1. Open the Overview page of the Node-RED starter application, which you created in Step 1.
  2. In the continuous delivery section of the dashboard, click View Toolchain.
  3. Back on the Toolchain page, click the Eclipse Orion Web IDE icon. The IDE opens. Here you can change the code of your application.
  4. Select the file package.json on the left side. In the editor on the right, add a line in the dependencies section: “node-red-contrib-scx-ibmiotapp”: “0.x”Edit package.json in the Eclipse Orion IDE
  5. Select menu File > Save. Then click the git icon in the left menu.Eclipse Orion IDE
  6. On the Git page, first click Commit, then Push. This pushes your change from the Git repository to the Application on the IBM Cloud.Commit and push on the GIT page
  7. Click the back arrow to get back to the Toolchain page. Now click Delivery pipeline.
  8. On the Delivery Pipeline page, stop your app, and then click the play icon next to Build Stage.Run delivery pipeline

The build pipeline will take 5 – 10 minutes to complete.

Now you can create the actual Node-RED flow which processes messages.

  1. Open the Node-RED visual programming editor of your Node-Red starter app. (Review the tutorial you completed in Step 1 for details.)
  1. Using the drag-and-drop features of this editor, you can plug together a flow of messages. You can create your own flow here, but now we will import the code. Download the following code from GitHub and save it as a text file.
  2. 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.
  3. In the Node-RED editor, press Ctrl-I to open the Import Nodes dialog. Paste the code, and click OK.Import code to Node-RED editor
  4. 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.
  5. Click Deploy in the flow editor. The flow is deployed and should be active immediately.
  6. 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.
  7. 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.Screen capture of msg.payload node in the the Node-RED editor and the debug tab that shows messages
  8. 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.Screen capture of the Function node that shows the code for changing the color of the background on the smartphone

You now have two-way communication between your smartphone and the first IBM Cloud IoT app.

Step 6 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.

  1. Download the rickshaw4iot-0.2.2.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.
  2. From the folder where you extracted the rickshaw4iot-0.2.2.zip file, open the manifest.yml file in a text editor.
    1. 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 iotvisualizer<your name>.
    2. Change the domain to the IBM Cloud location where you intend to run the app. In a free IBM Cloud account, all apps should run at the same location. Open your IBM Cloud Resource List to find your location. For example, if the Location is London, use eu-gb.mybluemix.net for the domain.
    3. Save the file.Screen capture of the host and name parameters in the manifest.yml file
  3. In the preparation, you have installed the IBM Cloud CLI and Developer Tools. Open a command or terminal window, and change to the directory where you extracted the rickshaw4iot-0.2.2.zip file.
  4. Enter the following IBM Cloud CLI command: ibmcloud login
  5. Deploy the application to IBM Cloud by entering the following command. Use the name you specified in the manifest.yml: ibmcloud 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 messages similar to the following ones:

    Messages from cf push command
  6. In IBM Cloud, click Dashboard. You should see the app you just deployed. Click it to see its details.Screen capture of the Overview page for your app
  7. Click Create connection. Select the Internet of Things service that you defined in Step 1, and click Connect. Leave “Auto Generate” in the popup window and click Connect again.Screen capture of the Connections page and the IoT service highlighted
  8. Click Restage to restage the app. Now the app can receive messages from the Watson IoT Platform.
  9. To verify the visualizer app, click the “Visit App URL” link in the app overview page. This opens http://_your-app-name_._your-location_.mybluemix.net
  10. In the Device drop-down box, select your device id.
  11. Move your smartphone around. You should see the acceleration changes in the chart.Screen capture of the IoT visualization app with the device selected and the sensor data displayed in a line chart

Conclusion

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.