Where Am I Collecting GPS Data With Rhiot

Where Am I Collecting GPS Data With Rhiot

One of the most common requirements for field devices used in IoT systems is to capture the current GPS location of the device and send this information to the data center. The article features a presentation of how  Apache Camel can turn a full-stack Linux microcomputer – such as Raspberry Pi – into a device collecting GPS coordinates.

WHICH GPS UNIT TO CHOOSE?

There is a myriad of GPS receivers available in the market. BU353 is one of the most popular as well as one of the least expensive GPS units. It can be connected to the computer device via a USB port. If you are looking for a fairly good and cheap GPS receiver for your IoT solution, you should definitely consider purchasing BU353.

The pictures below present a BU353 connected to a Raspberry Pi device via a USB port.

You may as well equip your Pi with an external mobile battery, such as GOODRAM Power Bank P661. With the external power supply fitted in, you can now take your mobile GPS system to the car or somewhere outdoors. The battery will significantly simplify testing the part of your solution which is in the field.

HOW CAN CAMEL HELP YOU?

The Camel GPSD component from a Rhiot project can be used to read the current GPS information from the device. With Camel GPSD you can easily connect the receiver to your computer’s USB port and read the GPS data; the component will ensure that a GPS daemon is up, running and switched to the NMEA mode. The component also takes care of parsing the NMEA data read from the serial port, so you can enjoy the GPS data wrapped into the io.rhiot.component.gpsd.ClientGpsCoordinates POJO objects, which are in turn forwarded to your Camel routes.

SHOW ME THE CODE

In order to take advantage of Camel GPSD  you have to add the following dependency to your Maven project:

<dependency>
  <groupId>io.rhiot</groupId>
  <artifactId>camel-gpsd</artifactId>
  <version>0.1.2</version>
</dependency>

The GPSD component supports only consumer endpoints – it makes sense as GPSD component is used to read, and not write, GPS data. The GPSD consumer is a polling one, i.e., it periodically asks the GPS device for the current coordinates. The Camel endpoint URI format for the GPSD consumer is as follows:

gps-bu353:label

Where label can be replaced with any text label:

from("gps-bu353:current-position").
  to("file:///var/gps-coordinates");

The Camel route presented above reads the current GPS coordinates every 5 seconds and saves the readings into a /var/gps-coordinates directory. Each GPS coordinates pair is saved into a dedicated file. The complete runnable example of the route above is presented in the code snippet below. I use Camel Spring Boot support to start the Camel context and load the route definition:

@SpringBootApplication
public class GpsReader extends FatJarRouter {
  public void configure() throws Exception {
    from("gps-bu353:current-position").
      to("file:///var/gps-coordinates");
  }
}

BU353 consumer receives the io.rhiot.component.gpsd.ClientGpsCoordinates instances. The ClientGpsCoordinates class is a convenient POJO wrapper for the GPS data:

 ConsumerTemplate consumerTemplate = camelContext.createConsumerTemplate();
  ClientGpsCoordinates currentPosition = 
    consumerTemplate.receiveBody("gps-bu353:current-position", ClientGpsCoordinates.class);
  String deviceId = currentPosition.clientId();
  Date taken = currentPosition.timestamp();
  double latitude = currentPosition.lat();
  double longitude = currentPosition.lng();

The ClientGpsCoordinates class name is prefixed with the Client keyword to indicate that these coordinates were created on the device, and not on the server side of the IoT solution.

PROCESS MANAGER

Process manager is used by the BU353 component to execute Linux commands responsible for starting the GPSD daemon and configuring the GPS receiver to provide GPS coordinates in the NMEA mode. If – for some reason – you would like to change the default implementation of the process manager used by Camel (i.e. io.rhiot.utils.process.DefaultProcessManager), you can set it at the component level:

GpsBu353Component bu353 = new GpsBu353Component();
bu353.setProcessManager(new CustomProcessManager());
camelContext.addComponent("gps-bu353", bu353);

The custom process manager may be particularly useful if – for example – your Linux distribution requires executing some unusual commands in order to make the GPSD up and running.

WHAT’S NEXT?

Do geographical capabilities of Apache Camel seem compelling to you? Then you may also take advantage of the Camel Geocoder component, which can be used to easily convert GPS coordinates collected via Camel into the human readable street address. Stay tuned for the geofencing features coming soon to augment the Rhiot project.