Build your Smarthome App with IBM Bluemix

Monitor temperature and humidity values from your mobile app, powered via IBM Bluemix and IoTIFY.  View Source

This guide will show how to connect a virtual Raspberry Pi emulator running in IoTIFY’s virtual lab to IBM Bluemix and then connect a mobile app to Bluemix platform to monitor the data it receives. You could also connect your system with Twilio to trigger a message when temperature value exceeds the threshold. Concepts explained here are general and can be easily modified to suit your particular project needs

What you’ll learn

What you’ll need

Introduction

The project consists of an emulated Raspberry Pi connected to a digital temperature and humidity sensor from Sensirion (SHT21).

Note: This project requires a functional account with IoTIFY. If you don’t have an account yet, please sign up here.

In this guide, we present an application of IoTIFY’s virtual lab that uses the Hello IoT project to create a temperature and humidity monitoring system where the user gets notified about sensor values in real-time via a mobile app.

Project Components

The project consists of following components:-

The reference architecture of the entire system is shown below.

Software Components

As for the software, we have the following components in this project

Get the source code

The complete source code for this application is available on GitHub in this repository.

Refer to the README file for steps to configure and build the software components of this application.

So let’s dive in and build this application.

1. IBM Bluemix Setup

Follow this guide to configure IBM Bluemix platform for this use case.

2. Setup Twilio account (Optional)

To set up the Twilio service, first create an account at Twilio. During account registration, you will be asked to provide your mobile phone number and confirm its validity with a verification code sent via SMS. At the end of the registration process, you will be taken to the “Getting Started” page to build an application with Programmable SMS:

Then, you need to get a Twilio phone number: click on “Get a number” and you will be presented with a proposed number: click “Choose this Number”, take a note of the chosen number and then click “Done”.
The other information needed when creating an application that uses Twilio messaging service is your account SID and your auth token: to retrieve this data, go to the Twilio console home by clicking on the home icon in the left hand side menu:

In the console dashboard, the account details section shows your account SID and auth token:

3. Setup IOTIFY Hello IoT project

Signup with iotify.io if you haven’t yet. The free account will have sufficient credits to run this project. Once you are logged into the IoTIFY main application, go to the virtual lab and click on the Hello IoT project:

Provide a unique name for your project and select “Raspbian Jessie” as target OS. Then click “Launch Project”.

The project takes 3-5 minutes to launch. The project state is shown in your virtual lab workspace and will automatically change to running once it is ready:

You could then simply click the Open button and it will open a new tab with the Workspace of the project:

The webview has a Raspberry Pi connected to switches and LEDs; it also contains a virtual temperature and humidity sensor, with knobs that allow adjusting sensed values. The virtual sensor is an emulated Sensirion SHT2x, connected to the Raspberry Pi via an I2C bus, so that you can interact with the sensor from the Rapsberry Pi terminal.

Open the console by clicking on the small console icon on the top left corner above the directory listing, circled in red in the figure below:

This will open a terminal with a Linux shell prompt, from which you can interact with the emulated Raspberry Pi:

Install the dependencies for the Hello IoT Python script

Now we are ready to install the Python script which is the software application that runs on this simulation setup. But before that we have to install the following dependencies for it.

We can install the above two dependencies through the Python installer ‘pip’, by typing the following commands in the Raspberry Pi console:

sudo pip install ibmiotf


sudo apt-get update
sudo apt-get install -y libffi-dev libssl-dev
sudo pip install twilio

4. Configure and run hardware

If we have reached this far then, it is now time for some action. We are finally ready to run our application.
But there is one last step to be performed, which is to configure the Python script so that it registers with IBM IoT platform service using the appropriate device id & authentication token that we created earlier.

But first, let’s clone the GitHub repository of this project in the project workspace so that we have access to the Python script.

git clone https://github.com/iotify/hello-iot-bluemix.git
cd hello-iot-bluemix

The repository contains all the project sources including the mobile app as well as the other files. For the Raspberry Pi application we are only interested in the ‘bluemix.py’ file:

Open the file by double-clicking it on the left-hand side explorer (if you don’t see the newly created hello-iot-bluemix directory in the file explorer, right-click on the root entry “raspberry” and then click “Refresh List”):

Double-clicking the file on the explorer will open a file editor in the workspace:

Now you will need following configuration values from IBM and Twilio to be inserted in the Python script bluemix.py for Raspberry Pi in the Hello IoT project:

IBM Bluemix configuration

Variable Description
organization IBM account organization ID
authKey Generated API key
authToken Generated Authentication token

Twilio configuration

Variable Description
account_sid Twilio account SID
auth_token Twilio account authToken
twilionumber Twilio Number ( Your Twilio subscribed number)
receivernumber Twilio verified number (where you want to receive the SMS notifications). Should have country code, for example +18458892405

Refer to the Python script configuration section in the README file for the steps to configure the Python script. These configurations are related to IBM IoT platform and Twilio.

Once the configurations are in place, save the file and run the script

sudo python bluemix.py

WARNING : Running the Python script without these configuration settings will lead to unexpected behaviour or termination of the program.

5. Configure and build the mobile app

For building the mobile app, you need to set up the build environment on your computer. The following instructions are valid for the Ubuntu distribution, but should apply with minimal or no modifications to other Linux distributions as well.

First, open a Linux terminal and clone the source code repository:

git clone https://github.com/iotify/hello-iot-bluemix.git
cd hello-iot-bluemix

Then, refer to the mobile app configuration section in the README.md file in the root folder of the source code, for the steps to configure the javascript file in the mobile app. These configurations are related to IBM IoT platform. Once the source code is configured with your IBM Bluemix account settings, you need to set up Node.js, Cordova and Android tools in your computer to be able to rebuild the app:

To install Node.js:

curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
sudo apt-get install -y nodejs

To install Cordova and its dependencies:

sudo npm install -g cordova
sudo apt-get install -y gradle

To install Android Studio, go to https://developer.android.com/studio/index.html and click on the “Download Android Studio” link; extract the contents of the downloaded zip file in a folder of your choice, then navigate to that folder in a terminal and execute the studio.sh script in the android-studio/bin subfolder:

./android-studio/bin/studio.sh

Follow the on-screen instructions of the Android Studio setup wizard, which will download and install various software components needed for building Android apps. At the end of the installation, a welcome screen indicates that Android Studio is ready to be used:

You can now close the Android Studio window, since our app wil be built using the command line.

Once Android Studio is installed, we can build the Hello IoT app: from the Linux terminal, navigate to the HelloIoTApp subfolder in the source code for the Hello IoT application and build the Android app:

cd HelloIoTApp
cordova platform add android
ANDROID_HOME=~/Android/Sdk cordova build android

After the build, the Android app package file is available at the location platforms/android/build/outputs/apk/android-debug.apk.
To be able to install the application on an Android phone, we first have to enable USB debugging on the phone. The following instructions are known to work with Android version 5.1, but should apply with little or no modifications to other Android versions as well:

You can now connect your phone to a USB port of your computer; if a diolog popup appears on your phone asking whether to allow USB debugging, tap OK.
To install the IoT app on the phone, type the following command in the Linux terminal:

ANDROID_HOME=~/Android/Sdk cordova run android

The Hello IoT app is now installed in your phone and is launched automatically:

6. Run the Project

FINALLY!! , we are all set. Make sure that the Python script is running under the Hello IoT project. From the mobile app in your phone you can now remotely monitor your Raspberry Pi. You can change temperature and humidity values using the two knobs in the Hello IoT project Webview, and see these values reflected in the mobile app:

Before reading temperature and humidity values from the sensor, the Python script checks the status of GPIO 14, configured as input, and sends a temperature and humidity status update only if the GPIO is set to a low value. This feature can be used to pause sending status updates while keeping the script running: in the Webview, click on the GPIO 14 switch (initially set in the OFF position), to turn it to the ON position:

While GPIO 14 is in the ON position, the Raspberry Pi remains connected to IBM Bluemix but doesn’t read sensor values and doesn’t send status updates; as soon as the GPIO is switched back to the OFF position, the Raspberry Pi resumes sending periodic status updates.

If the sensed temperature reaches a critical level (whose value is set in the Python script at 40°C), the Raspberry Pi uses the Twilio messaging API to send an SMS to your mobile phone number (which you previously configured in the script) with the message “Critical temperature reached”. In addition, GPIO 24 will be switched to turn on the corresponding LED in the Webview:

The LED will be switched back off as soon as the temperature returns below the critical level.

You can also see the live temperature and humidity sensor readings on IBM IoT Service dashboard by clicking on the RASPBERRY_PI_001 device under the “DEVICES” sub-menu:

This concludes our tutorial on this proof-of-concept application; you can of course enhance it and add new fuctionalities as you see fit, both in the mobile app and in the program running in the IoTIFY virtual lab.