How to make a mobile IoT app for the Facebook Parse data cloud

Mikael KindborgBlogs, Tutorials

Tweet about this on TwitterShare on FacebookShare on LinkedInShare on Google+

evo-parseNow you can create mobile apps in JavaScript that communicate with BLE devices and interact with the Facebook Parse data cloud. In this tutorial we will use a Texas Instruments SensorTag to monitor temperature data and send values to the Parse cloud. Read on to learn more about setting up a Parse account and how to use Evothings Studio to develop an IoT app for the SensorTag that saves cloud data. You can even try out the example if you do not have a SensorTag. It is easy to get started!

Mobile apps for IoT and cloud data

A common use case for Internet of Things applications is to collect data from a sensor device and send that data to a cloud service. Examples include health and fitness data, temperature values, or motion tracking data. Commonly BLE devices (Bluetooth Low Energy – Bluetooth Smart) are equipped with sensors and used to track readings. One example of a Sensor device is the Texas Instruments SensorTag.

When reading data over BLE a mobile application is used to communicate with the sensor device. Thanks to high-level JavaScript libraries, it is easy to set up the BLE communication and read data from the sensors.

In this tutorial you will learn how to create an app using Evothings Studio that reads temperature values from the SensorTag and sends the data to the Parse cloud.

Note: If you don’t own a Texas Instruments SensorTag, you can still explore the example code and send data to the Parse cloud. Read on below to learn more.

What you need

Here is what you need to get started:

  • Texas Instruments SensorTag – Current example code supports the original CC2541 SensorTag, we are in the process of updating the tisensortag.js library to support the new CC2650 Bluetooth Smart SensorTag. If you do not have a SensorTag, modify the example code as outlined below to try out the Parse cloud functionality.
  • Evothings StudioDownload for OS X, Windows and Linux.
  • Evothings Client app – Install on iOS or Android phones/tablets, get it from Apple App Store or Google Play.
  • WiFi network that allows client connections – Network client isolation must be disabled (that’s a function in a router which prevents two computers and/or phones to find one another)
  • Basic knowledge of Evothings Studio – You can also relax and learn as you go through this tutorial.

Install Evothings Studio

The Evothings Studio Starter Kit explains how to install and use Evothings Studio. For the quick version just download the Studio package for your computer and while it’s loading you pick up the Evothings Client free from the app stores.

Download the example app code

Download the example code for this tutorial as a zip file. Unzip it and you will find the application files. The app is called “Parse IoT Demo” and contains the code needed to run the app using Evothings Client and Evothings Workbench.

Set up your Parse account

Sign up for a Parse account. Then follow the on-screen guides to create a Parse app. The Parse app is a functional unit where you can store data and also add server side code if needed. You can name the app anything you want, we named ours “Evothings Parse”.

Go to the Quickstart guide and click the “Data” icon. Then click “Web” and then “New project”. You are now presented with the Application ID and JavaScript key and some sample test code.




Fill in the application IDs in the mobile app

What you need to do next is to open the file index.html in the mobile app project you’ve downloaded from GitHub.

Locate the following place in the code and fill in the keys from the Parse web page found under Step 2 (this is the only thing you need to do, no need to do Step 1 or make the SDK test even if they say so):

// TODO: Insert your Parse Application ID and JavaScript key here.
Parse.initialize(
    'Parse Application ID',
    'JavaScript key')

You may also want to make temperature readings more frequent. Default setting in the example is once every minute, which could be reasonable for a production app but does not make for much action when testing. Here is the line to update:

var temperatureInterval = 60000 // Change to 5000 for 5 second intervals.

Then save the file index.html

Run the example app

To run the app, do as follows:

  • Launch Evothings Workbench
  • Drag the index file index.html to the Workbench project list window, and release
  • Start Evothings Client on your mobile or tablet – you need at least iOS 7 or Android 4.3, and the phone needs to have BLE support.
  • Connect to the Workbench from Evothings Client – Use the same WiFi network for both the Workbench and the mobile and make sure that it allows client connections. Network client isolation must be disabled.
  • Click the RUN button for “Parse IoT Demo” in the Workbench project list.
  • Activate the SensorTag (you currently need the original CC2541 SensorTag, press its side button).
  • Press the on-screen button “Start reading SensorTag”.
  • Now the app should connect to Parse, and start aggregating tag data.

Inspect data in the Parse Web UI

Now go to the page where data objects are displayed. The following link should get you there once you’re logged in, choose your newly created Parse project from the list. Select “Core” from the top menu, if the service defaults to “Analytics” and it says you haven’t collected any data yet.

https://parse.com/apps/

Click the refresh icon in the upper-right corner to see new incoming values.

The app saves data by creating objects of type SensorTagReading. There is one new object created for each temperature reading. This may not be the most optimal approach, but hopefully serves the purpose to illustrate the concept of saving sensor data to a data cloud!

What if I do not have a SensorTag?

If you do not have a SensorTag you can still test the cloud save functionality.

First comment out the initialisation of the SensorTag:

function initialise()
{
    // initialiseSensorTag()
    initializeParse()
}

Next add a timer that writes simulated “temperature” values:

function initialise()
{
    // initialiseSensorTag()
    initializeParse()
    initialiseSimulationTimer()
}

function initialiseSimulationTimer()
{
    // Timer that updates temperature values.
    setInterval(function() {
        lastTemperatureReading = ((Math.random() * 5) + 20).toFixed(2)
        },
        5000)

    // Timer that sends data to the cloud.
    setInterval(onTemperatureTimer, 5000)
}

When the app is launched or reloaded, it will automatically start sending simulated values to the Parse cloud. Do not press the “Start” button when using the above code! That will cause errors ;)

As a next step you can add support for your IoT devices. Visit the Evothings Forum to ask questions and discuss your projects. You can also combine this example with other Evothings examples to create your own apps.

So many things to parse out there – get started right away

Download Evothings Studio and get up and running within minutes! Have fun!