Philippe Khin
IoT: Send Android smartphone sensor data to Microsoft Azure Cloud
14 April 2017
Welcome to the exciting world of IoT
python
java
android
sql
iot
azure

In recent years, the trend involving IoT technologies and Cloud Computing is becoming more and more popular. From people who want to automate their "smart house", to companies that are willing to make a shift to the IoT world for manufacture maintenance or monitoring. Wearable devices are for example used in the e-health industry, for monitoring purpose by sending the patient physical data like temperature or blood pressure to the hospital or clinical institution.

In this post, I will explain how to build an end-to-end IoT solution by sending the sensor data from an Android smartphone (which simulates a wearable or other sensor devices) to the Cloud, in order to visualize the data in real-time. The tools used in this project is an Android smartphone (Samsung Galaxy S4), a computer acting as a gateway, with Python application to receive, store and send the data to the cloud. The cloud used is Microsoft Azure especially the Azure IoT Hub services, the Azure Stream Analytics and Microsoft Power BI for the real-time data visualization.

Overall workflow

The following picture shows the overall workflow of the entire chain and each bloc will be detailed below.

Android smartphone 

Android application that read the built-in sensor in the smartphone and sends the output stream to the gateway. The data are from the temperature sensor (one value, in Celsius) and the accelerometer (3 values: x,y,z in m/s²). To access to the sensor data, the following Android class will be used : android.hardware.Sensor, android.hardware.SensorEvent, android.hardware.SensorEventListener and android.hardware.SensorManager. Here's the code snippet that enables the access to the sensor.

@Override
protected void onResume() {
    super.onResume();
    sensorManager.registerListener(temp_listener, sensor_temp,
                                    SensorManager.SENSOR_DELAY_FASTEST);
    sensorManager.registerListener(accelero_listener, sensor_accelero,
                                    SensorManager.SENSOR_DELAY_FASTEST);
}

@Override
protected void onStop() {
    sensorManager.unregisterListener(temp_listener);
    sensorManager.unregisterListener(accelero_listener);
    super.onStop();
}

private SensorEventListener temp_listener = new SensorEventListener() {
    @Override
    public void onAccuracyChanged(Sensor sensor, int acc) {}
    @Override
    public void onSensorChanged(SensorEvent event) {
        temperature = event.values[0];
    }
};

private SensorEventListener accelero_listener = new SensorEventListener() {
    @Override
    public void onAccuracyChanged(Sensor sensor, int acc) {}
    @Override
    public void onSensorChanged(SensorEvent event) {
        x = event.values[0];
        y = event.values[1];
        z = event.values[2];
    }
};

The data are sent to the gateway using a TCP/IP socket through WiFI. The gateway and the Android smartphone must be connected to the same WiFi. We specify the IP address of the gateway and an unused port number. Here's how the application looks like :

Gateway 

The gateway here will be used to receive, store and send to sensor data to the Azure Cloud. The gateway could be of any type, a computer, a micro-controller or a Raspberry PI. The received data via the socket will be stored in a simple SQL database and then will be converted in JSON format (supported telemetry format for Azure) and send to Azure IoT Hub. To start using Azure IoT Hub, I recommend you reading this tutorial. The full code is available on my GitHub repository. After successfully creating your Microsoft Azure account and your IoT Hub, you will be using this Hub for the device to cloud communication.

Azure IoT Hub 

Basically, the communication from the gateway to this hub will be done using the Azure IoT Hub REST API. The library Requests will be used in order to send HTTP message to your hub. Other protocol are supported by Azure such as AMQP or MQTT. To send messages, you first have to create a device ID to which you associate your physical device. This is only for naming purpose. Once a message is sent successfully, the returned status code is 204 like the figure below.

You can visualize on your IoT Hub dashboard, how many messages you successfully received, the number of devices you have created etc...

Azure Stream Analytics

This Azure service is the middle piece that connects the IoT Hub and the Business Intelligence (visualization) part. It's a fully managed real-time event processing engine and make it easy to set up real-time analytic computations on data streaming from devices, sensors etc... It ingests the output data from the Iot Hub, compute and make queries on these data, and then forward it to other sources like Blob storage, SQL database or, in my case, a real-time visualization tool like Microsoft BI. The query to get the input from IoT Hub to Microsoft BI (to plot the temperature and the accelerometer over time) is written in a SQL-like format and look like this :

SELECT
    deviceId
    ,MAX(temperature) as temperature
    ,sent_time
  ,MAX(x) as x, MAX(y) as y, MAX(z) as z
INTO
    [sensorOutput]
FROM
    [sensorInput] TIMESTAMP by sent_time
    GROUP BY TUMBLINGWINDOW(ss,2),
    deviceId, sent_time

You can also monitor the input/output event in order to detect some runtime error or change the time interval of your streaming job. One stream analytics job can handle several input and output at the same time.

Microsoft Power BI :

This is strictly not an Azure service, but a Microsoft software that can be used to connect Azure services. I recommend you reading this tutorial. Once you have started and run your Stream Analytics and start sending your data, a Streaming Dataset will pop-up and you can create a report from it. You can use a lot a visualization graph type or do aggregation on data like calculting the average or sum etc. The result is pleasant since every single movement you do with your smartphone will be captured and send these data to the cloud for a real-time visualization. To more you move your smartphone, to more there are high values. This kind of visualization can be used for instance the detect a type of movement: running, walking or resting.

For the complete code, please check my GitHub repository.

© 2020, Philippe Khin