Contents


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

Comments

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:

Chart showing architecture of solution
Chart showing architecture of solution

What you'll need to build your apps


 

1

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.

  1. Log in to your IBM Cloud account.
  2. Click Create Resource.
  3. In the Catalog, under Platform, click Internet of Things Platform Starter.
  4. 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 iottutorialdaniel.Screen capture of the Create an app dialog
    Screen capture of the Create an app dialog
  5. Click Create.
  6. 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.
2

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

  1. In the Overview view of your app, under Connections, click the Internet of Things Platform service, named something like iot<your name>-iotf-service. Screen capture that shows connections
    Screen capture that shows connections
  2. Click Launch to open the Watson IoT Platform dashboard. Screen capture that shows the service page with the                              Launch dashboard button highlighted
    Screen capture that shows the service page with the Launch dashboard button highlighted

    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.

    Screen capture of the service on the IBM Internet of                             Things Platform server and the organization ID highlighted
    Screen capture of the service on the IBM Internet of Things Platform server and the organization ID highlighted
  3. 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 be "Android" (this device type name is required by the app that you will use later).Screen capture of the Create Device Type dialog
    Screen capture of the Create Device Type dialog
  4. 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.
  5. 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".
  6. Click Next. A page is displayed where you could enter metadata about the device. Leave it blank, and click Next.
  7. Provide a value for the authentication token. Remember this value for later. Then, click Next.Screen capture showing authentication token
    Screen capture showing authentication token
  8. Click Done.
  9. Click Back.

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

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.

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.

  1. On your phone, go to Settings > Security. Under Device Administration, enable Unknown sources. Now you can install .apk files from outside of Google Play.
  2. Open the 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
  3. Search for the iotstarter-v2.1.0.apk link, and click the link to download the .apk file.
  4. Click the downloaded file, and confirm that you want to install the app.Screen capture showing apk file

    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 "Add a device that will send MQTT messages to the Watson IoT Platform"). For example, mgso1i in 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.
    Screen capture that shows the configuration parameters                             for your Android app
    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
    Screen capture of the accelerometer data from the running Android app
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 (see the start of Add a device that will send MQTT messages to the Watson IoT Platform).
  2. In the left menu, click Devices. Your Android device is displayed.Screen capture of the Devices tab that lists the Android device
    Screen capture of the Devices tab that lists the Android device
  3. Click the Device ID, and then go to the Recent Events tab. 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
    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.

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.

  1. Open your IBM Cloud dashboard (go to bluemix.net and click Dashboard.
  2. In your IBM Cloud dashboard, verify that your IBM Cloud IoT app (that you created in Step 1) is up and running.
  3. 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.
  4. On the Node-RED page for your IoT app, click Go to your Node-RED flow editor. The editor opens, containing a sample flow.Screen capture showing sample flow
    Screen capture showing sample flow
  5. 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.
  6. Download the following code (as a long single line of code) as a text file (nodeRedCode.txt) from GitHub.
  7. 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.
  8. In the Node-RED editor, press Ctrl-I to open the Import Nodes dialog. Paste the code, and click OK.Screen capture of the Import Nodes dialog
    Screen capture of the Import Nodes dialog
  9. 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.
  10. Click Deploy in the flow editor. The flow is deployed and should be active immediately.
  11. 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.
  12. 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
    Screen capture of msg.payload node in the the Node-RED editor and the debug tab that shows messages
  13. 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 Edit function node that shows the                              code for changing the color of the background on the smartphone
    Screen capture of the Edit 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.

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.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.
  2. 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.Screen capture of the host and name parameters in the manifest.yml file
    Screen capture of the host and name parameters in the manifest.yml file
  3. Open a command or terminal window, and change to the directory where you extracted the rickshaw4iot-0.2.0.zip file.
  4. Enter the following Cloud Foundry commands:
    cf api https://api.ng.bluemix.net
    cf login -u <your_bluemix_login_id>

    The cf login command 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
  5. 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:

    OK

    App <your app name> was started using this command 'node app.js'.

  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
    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.Screen capture of the Connections page and the IoT                             service highlighted
    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, in a browser, open http://<your app name>.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
    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.


Downloadable resources


Related topics


Comments

Sign in or register to add and subscribe to comments.

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Internet of Things, Cloud computing
ArticleID=1019421
ArticleTitle=Turn your smartphone into an IoT device
publish-date=12182017