My car isn’t what anyone would refer to as “smart.” It’s an eleven-year old Toyota Avensis, and with no hubcaps, mismatched side mirrors, and “wash me” traced in dirt on the rear window, it is—to embrace the colloquial—a real beater. I can’t complain though—it’s done right by me without being faithfully maintained.

Sean's 2007 Toyota Avensis

Sean’s 2007 Toyota Avensis

As it turns out, though, it was a smart choice for an experiment in auto instrumentation. I wanted to see if I could connect my “low-tech” car to the cloud and stream its data to New Relic. I figured this would be a good way to do a bit of car performance monitoring and play around with the New Relic Mobile Android SDK.

So, after I put everything together, I went for a drive—a good mix of start/stop city driving here in Dublin before opening things up a bit on the motorway. Here’s the New Relic Insights dashboard with some data I recorded from that trip:

Dashboard from Sean's drive

I gathered data on my speed, engine revolutions per minute (RPMs), and coolant temperatures (in Celsius, of course). I even set a New Relic alert on my speed. To make sure I kept my lead foot under control, I set an alert to trigger any time I came within 30 km/hr of the speed limit (120 km/h). This worked quite nicely, as you can see here:

Alerts from Sean's drive

So how’d I do it?

So how did I get cutting-edge data out of this ancient beater? It wasn’t that difficult!

When you bring your car into the garage to, say, get an engine-warning light diagnosed, the mechanics use a protocol called OBD-11 to connect their diagnostic tooling to a control unit in your car that provides all kinds of information about your car’s performance. This protocol can be used to read data from the instrumentation within your car, including live data like speed, RPMs, and throttle position.

OK, but how did I get this data out of the car and into New Relic? With my smartphone—obviously.

As it turns out, there’s an abundance of Bluetooth OBD-II connectors designed just for this purpose, so I purchased one. Since I wanted to run this in Android, I had to search a bit, but I eventually found an open source Android OBD reader on GitHub. Unfortunately, the repo is no longer maintained, but with a little troubleshooting, I got the OBD reader app up and running with Android Studio and a Nexus 6 emulator.

Then I followed the steps to install Android apps for New Relic Mobile, so I could pull custom events from the OBD reader app.

Some tweaks of the code

As soon as I had Mobile installed, I could see that the OBD app was reporting successfully from the Nexus 6 emulator. Luckily, the reader had the ability to stream data to a server already—all I had to do was replace that default functionality with a Mobile custom event to send it to New Relic Insights.

Here’s the original code from the reader:

if (prefs.getBoolean(ConfigActivity.UPLOAD_DATA_KEY, false)) {

       // Upload the current reading by http

       final String vin = prefs.getString(ConfigActivity.VEHICLE_ID_KEY, "UNDEFINED_VIN");

       Map<String, String> temp = new HashMap<String, String>();

       temp.putAll(commandResult);

       ObdReading reading = new ObdReading(lat, lon, alt, System.currentTimeMillis(), vin, temp);

       new UploadAsyncTask().execute(reading);

Here are the changes I made:

if (prefs.getBoolean(ConfigActivity.UPLOAD_DATA_KEY, false)) {

       // Upload the current reading by http

      final String vin = prefs.getString(ConfigActivity.VEHICLE_ID_KEY, "UNDEFINED_VIN");

      Map<String, String> temp = new HashMap<String, String>();

      temp.putAll(commandResult);

      ObdReading reading = new ObdReading(lat, lon, alt, System.currentTimeMillis(), vin, temp);

      // New Code to rearrange data and use New Relic custom event

      Map nrreading = new HashMap();

      if (temp != null) {

            for (Map.Entry<String, String> entry : temp.entrySet()) {

                 String key = entry.getKey();

                 try {

                 String value = entry.getValue().replace("NODATA", "0ND").split("(?<=\\d)\\s*(?=[a-zA-Z%:])")[0];

                 Object objectVal = (Object) Double.parseDouble(value);

                 nrreading.put(key, objectVal);

                 }catch (NumberFormatException ne) {

                 Log.d(TAG, "entry:");

                 }

            }

        }

        NewRelic.recordCustomEvent("Car", nrreading);

As you can see, I had to do a bit of data wrangling because it was originally in string format with units included. I used a regex to separate the number string from the unit string. Then I converted the number string to a double, so that it could eventually be plotted in Insights. The New Relic.recordCustomEvent() method also requires a Map <String, Object&gt; format map whereas the map provided by the OBD app is in a Map <String, String> format, so while converting the strings to doubles, the code also sorts the values into the Map <String, Object> format.

Once all this conversion and sorting is finished, the NewRelic.recordCustomEvent() method is executed, sending the custom event as the Car eventType to Insights.

The application collects custom events as it runs, and after 600 seconds (or if the app goes into the background), the agent sends this custom event data to Insights, where I can even query it.

Sean's drive custom events

Conclusion

In an era of smart vacuum cleaners, refrigerators, thermostats, and window shades, cars are no exception. From the actual Smart Car to Tesla’s Model S to Google’s soon-to-be self-driving cars, it seems like the future is today instead of tomorrow. With fleets of AI-enhanced cars on the horizon, instrumentation will be a vital part of keeping those vehicles on the road.

New Relic Mobile couldn’t have been easier to implement. And even though the Android OBD project hadn’t been maintained in years, the worst issue I had was that it took me three hours of troubleshooting to figure out why the application would work on the Nexus emulator but not on my phone. Turns out I had just run out of phone data the day before!

I also discovered that my car’s speedometer registers a speed about 5 km/h slower than my car is actually traveling. A purposeful design choice? I don’t know, but I should probably slow down a bit …

Sean Winters is a Technical Support Engineer in New Relic's Dublin office. When not helping customers harness the power of New Relic, he's doing everything it takes to win his fantasy football league. View posts by .

Interested in writing for New Relic Blog? Send us a pitch!