Reading driving data via the on-board diagnostic port
Programming Snapshot – Driving Data
A connector plugged into the diagnostic port of Mike Schilli's cars sends current information such as speed, acceleration, and fuel economy via the mobile phone network to a cloud service. An app and a programmable API read out the data and provide stunning visualizations.
I find it absurd to invest half a year's salary on a new car. Instead of more expensive German craftsmanship, I opt for second-hand Japanese cars with screaming VTEC engines from the 1990s. Alas, this type of car won't get you the electronic bells and whistles nowadays commonly associated with the "connected car." In my adopted home of San Francisco, a startup company called Automatic has developed a connector to the car's on-board diagnostic (OBD) port [1] (Figure 1) that continually reads data from the car's on-board computer and feeds the data either to the driver's cell phone or straight to an account in the cloud via the 3G mobile network.
The data is collated on Automatic's servers. From this data, the driver can then check out their driving route, how often they stepped on the gas like a madman or hit the brakes in the same way, how much gasoline the car consumed, and what the trip cost.
Figure 2 shows that the Integra GSR under my care achieved a fuel economy of 27.3 miles per gallon. The low rating of only 29 out of a maximum of 100 points for economical driving could thus be greatly improved.
Even for Fleets
If someone has several cars, like me, each needs its own Automatic connector. The OBD-II port is often found under the dash in the footwell area, usually on the driver side, but sometimes also on the front passenger side (Figure 3). Once registered, the adapter then integrates seamlessly with various family cell phones on which the Automatic app is installed.
The adapter comes in two versions: Automatic Lite, a less expensive one that connects itself with the driver's cell phone via Bluetooth, and Automatic Pro, which has its own GPS unit, 3G mobile network card, and transmits data directly to the cloud. I've had the El Cheapo version for a while, but found it to work unreliably. At least 20 percent of the time, the adapter would fail to find the phone and not record any data at all. The Pro version is much better; it still misses the occasional trip, but it's probably 99 percent accurate.
Upon arrival at the destination, the adapter notices that the car is no longer moving; it then saves the current address as the destination of the trip and automatically uploads the data via the Internet to the server, without the driver even noticing.
You can display the trip data later via the app or on request in the browser on a desktop computer (Figures 4 and 5). Automatic also provides an API that returns machine-readable data via JSON to registered users for downstream script processing and viewing.
Now, Where Did I Park?
We urbanites tend to crawl around the block endlessly until we finally find a free parking space. But later on, an important question often arises: "Now, where did I park?" Thanks to the Automatic connector, the answer is easy as pie; after all, in the Automatic universe, the car must be parked at the geographical end of a trip, unless it happens to be driving around somewhere in the meantime.
You can retrieve the data with the help of the Automatic API, for example, by using a Perl script at the command line. This requires the user first to register the application on the Automatic developer site at https://developer.automatic.com. After an OAuth token dance, you are then assigned an access token with which an API client, such as the one in the script in Listing 1 [2], can retrieve private data from the server on behalf of its user.
Listing 1
wheresmycar
01 #!/usr/local/bin/perl -w 02 use strict; 03 use OAuth::Cmdline::Automatic; 04 use JSON qw( from_json ); 05 06 my $oa = OAuth::Cmdline::Automatic->new(); 07 $oa->raise_error( 1 ); 08 09 my $vehicles = 10 from_json $oa->http_get( 11 "https://api.automatic.com/vehicle" ); 12 13 for my $car ( 14 @{ $vehicles->{ results } } ) { 15 16 my $trips = 17 from_json $oa->http_get( 18 "https://api.automatic.com/trip", 19 [ vehicle => $car->{ id } ] 20 ); 21 22 for my $trip ( 23 @{ $trips->{ results } } ) { 24 printf "%20s: %s\n", 25 "$car->{ make }-$car->{ model }-" . 26 "$car->{ year }", 27 $trip->{ end_address }->{ name }; 28 last; 29 } 30 }
Listing 1 first uses the API call to vehicle
to pick up the descriptions of all of the user's registered cars in lines 10 and 11. The for
loop from line 13 then iterates through the fleet and calls the trip
API method for each vehicle in lines 17 to 19 with the ID of the current vehicle. The API calls deliver data in JSON format to the client, and the from_json()
function exported from the CPAN JSON module then converts the data to Perl's internal format, in which the code can poke around by accessing hashes or arrays.
The output in Figure 6 shows that the Acura Integra GSR, a model typically driven by belligerent urban youth, is now parked on 24th Street in San Francisco, whereas the Honda Fit, the commuter vehicle used by my better half, is currently parked in Oakland, where my wife works.
Logging a Trip
The resulting data is stored under the results
key as an array, and, in the case of the call to trip
, each of the elements contains a series of trip objects that in turn contain the coordinates of a trip, with information on the speed, the number of acceleration and braking maneuvers, and even the street addresses of the starting point and trip destination. The trips appear in reverse chronological order; thus, the first trip in the list is the last one to have been completed.
The destination stated under the end_address
entry is the address at which the vehicle was last parked. The only task remaining now is to distinguish between the different vehicles in the fleet and output the brand, the model, and the year of manufacture of the automobile in question. (My garage actually used to accommodate two Integras of different vintages a while back.) Armed with this information, plus the name
field containing the trip destination, it is then clear where the respective car is located. The last
command in line 28 stops processing the trip data after the first record; the for
loop in line 13 starts a new round with the next car in the fleet.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more and more Linux systems are getting targeted by ransomware, Halcyon is stepping up its protection.
-
Valve and Arch Linux Announce Collaboration
Valve and Arch have come together for two projects that will have a serious impact on the Linux distribution.
-
Hacker Successfully Runs Linux on a CPU from the Early ‘70s
From the office of "Look what I can do," Dmitry Grinberg was able to get Linux running on a processor that was created in 1971.
-
OSI and LPI Form Strategic Alliance
With a goal of strengthening Linux and open source communities, this new alliance aims to nurture the growth of more highly skilled professionals.
-
Fedora 41 Beta Available with Some Interesting Additions
If you're a Fedora fan, you'll be excited to hear the beta version of the latest release is now available for testing and includes plenty of updates.
-
AlmaLinux Unveils New Hardware Certification Process
The AlmaLinux Hardware Certification Program run by the Certification Special Interest Group (SIG) aims to ensure seamless compatibility between AlmaLinux and a wide range of hardware configurations.
-
Wind River Introduces eLxr Pro Linux Solution
eLxr Pro offers an end-to-end Linux solution backed by expert commercial support.
-
Juno Tab 3 Launches with Ubuntu 24.04
Anyone looking for a full-blown Linux tablet need look no further. Juno has released the Tab 3.
-
New KDE Slimbook Plasma Available for Preorder
Powered by an AMD Ryzen CPU, the latest KDE Slimbook laptop is powerful enough for local AI tasks.
-
Rhino Linux Announces Latest "Quick Update"
If you prefer your Linux distribution to be of the rolling type, Rhino Linux delivers a beautiful and reliable experience.