Connected car with Microsoft Azure ( CAN Gateway to Stream Analytics)

Introduction

Couple of months ago, I had attended the Toyota hackathon here in Silicon Valley, and learned about the new capabilities Toyota is building in their car. Recently I spent sometime with Azure Stream Analytics as well, and thought of connecting the car with Azure cloud. Once we get the live car data (eg:- engine speed, location, vehicle speed, fuel level etc) to cloud, possibilities are end-less. This article will provide simple visualization using Power BI, and we can extend this use-case to any of the telemetry  platform use cases (eg:- travel safety, predictive maintenance etc).

CAN-Gateway ECU System

The CAN-Gateway ECU allows users to read data – including GPS location, fuel cCan_Overviewconsumption, engine revolution speed, odometer and more – from various sensors in the vehicle. The CAN-Gateway ECU has Bluetooth LE interface, and smartphone application can send the signals to the CAN-Gateway ECU, the Gateway provides the data requested by the application.

CAN Gateway to Android Application

For this demo, I will be using drive simulator application( on Android) which acts as CAN Gateway ECU system.  I have another Android application to read the data from the Gateway (or Simulator).  Here comes the snippets about the data buffer formation, and reading the signal.

Android Application to Azure Event Hub

Please refer my earliest post about Android to Event Hub interaction, and I am using the same approach here as well. Here I am adding many more attributes as part of the JSON as given below.

 Azure Event Hubs to Stream Analytics

Create the new Stream Analytics from the Azure portal as shown below. We could save some data transfer cost if we keep both Event Hubs and Stream Analytics in same region.  NewStreamAnalyticsOnce we create the new stream analytics, its time to add the input, output and query.

Input:- Click on “Add Input”, and select “Data Stream” and “Event Hub”, and select your Azure subscription and Event hubs name.  Select “JSON/UTF8” as event serialization format, and test the connection.

Output:- Stream Analytics provide different output options ( including Power BI), and in this demo, I am using the Table Storage option, so that I can play around the data at later stage as well.

Query:- Query is integral part of the Stream Analytics, and this is where the live data filtering really happening.  In this demo, I am selecting all the attributes from the event hub.  But we can do all sort of filtering here (eg:- find the cars who are crossing speed limit in specific area)

 See it in Action (Demo Time)

Now we have connected all the required pieces for this demo.

  • Start the Stream Analytics by clicking “Start” icon at the bottom portion in Azure portal.
  • Start the car simulator in Android phone
  • Start the sample app in another Android phone and connect the app to simulator via Bluetooth
  • Check out the Table Storage Explorer to see the live data or even Power BI dashboard

Summary

You might be wondering what is the big deal in getting the car live data to Azure table storage / Azure SQL and this can be even done easily with Azure Mobile Service. Then you are missing the point.  Think about a vehicle fleet system, where hundreds of car data (read it as Uber car data) coming to the Event Hubs, and fleet administrators configure various queries in Stream Analytics service to process the live data, and take further action.  With Azure Event Hubs and Stream Analytics, Connected Things (IoT) got better future, and looking forward to see many more awesome apps.

Send events from Android app to Microsoft Azure Event Hubs

Event Hubs is a highly scalable publish-subscribe ingestion system that can process millions of events per second. This article will explain how to publish event from an Android application to Azure event hub.

Create an Event Hub

  1. Log on to the Azure management portal, and click NEW at the bottom of the screen.
  2. Click App Services, then Service Bus, then Event Hub, then Quick Create.create_eventhub
  3. Enter the event hub name (eg:- AndroidHub) , select the region and namespace, and create the event hub.
  4. Select the newly created event hub, and select the Configure tab
  5. Create the shared access policy (eg:- android). We will require the policy name, key, namespace and event hub name at later stage to create the signature.shared_access_policy

Create Android application

Let us create anew_project simple android application using Android Studio. Select the minimum SDK (eg:- API 19), Blank Activity template, and  default activity (eg:- MainActivity), and then click “finish”to create the project.

Now add a button to the main layout to send sample event to Azure event hub.  Updated layout is given below.

We need to update the manifest file to support the network operation by including the following permission.

Connecting to EventHub:- Sending events to an Event Hub is accomplished either using HTTP POST or via an AMQP 1.0 connection. The choice of which to use when depends on the specific scenario being addressed. AMQP 1.0 connections are metered as brokered connections in Service Bus and are more appropriate in scenarios with frequent higher message volumes and lower latency requirements as they provide a persistent messaging channel. I will be using the HTTP post to publish the events.  I have been using the Android AsyncHttpClient library for a while to do asynchronous operation, and thought of using the same here as well. Let us update the build.gradle file with the following to add the AsyncHttpClient package.

Now update the button click handler with the following code.

Replace the namespace and eventhub name with the actual values from the Azure portal, and ClientId can be anything to identify the publisher. In real life scenario there will be multiple devices/apps publishing events to same event hub.   I have created another Azure website to generate the SAS signature, and let us use this site to create signature for this demo application.  In production application, it is better to generate the signature in Android app itself (or get it from a secure cloud service).

Once we update all the placeholders, it is time to execute the application, and eventhub_dashboardclick the button to send events. We should see the updates in Event hub dashboard as shown here. If not, check out the AsyncHttpClient callback handler and see the response from the server after the POST request.

Hope, many more Android apps will start publishing events to Azure Event hubs 🙂