Make Your Iot Gateway Wifi Aware Using Camel And Kura

Make Your Iot Gateway Wifi Aware Using Camel And Kura

A common scenario for mobile IoT Gateways, for example those mounted on trucks or other vehicles, is to cache the data collected locally on the device storage and synchronize the data with the data center only when a trusted WiFi access point is available near the gateway. Such a trusted WiFi network could be located near the truck fleet parking.

The adoption of such an approach for less urgent data – like GPS coordinates stored for further offline analysis – ensures that the data can be delivered to the data center without the additional cost related to GPS transmission fees.

The Camel Kura WiFi component from Rhiot can be used to retrieve information about WiFi access points available within the device range. Under the hood the Kura Wifi component uses the Kura org.eclipse.kura.net.NetworkService.

SETTING UP A MAVEN PROJECT

In order to take advantage of the Camel Kura WiFi component, Maven users should add the following dependency to their POM file:

<dependency>
  <groupId>io.rhiot</groupId>
  <artifactId>camel-kura</artifactId>
  <version>0.1.3</version>
</dependency>

All the other dependencies will be pulled by the Maven transitive resolver.

CREATING A CAMEL ROUTE SCANNING THE WIFI NETWORKS

The Kura WiFi component supports both consumer and producer endpoints. In practice it means that you can either periodically scan for the WiFi networks (consumer mode) or explicitly request a single scan (producer mode).

The following Apache Camel route will use the wlan0 interface to scan for the mySsid network. If the mySsid WiFi network is found, Camel will automatically start the route responsible for the synchronization of the offline data stored on the device local storage with the data center:

from("kura:wlan0/mySsid").
      to("controlbus:route?routeId=onlineSync&action=start");
  from("file:///var/sensor/temperature").
      routeId("onlineSync").autoStartup(false).
      to("netty4-https://api.mydatacenter.com");

Keep in mind that both the network interface and SSID can be replaced with the * wildcards matching respectively all the network interfaces and SSIDs.

For example, to read all the SSID available near the device, the following route can be used:

from("kura:/").to(...);

DATA RETURNED BY THE WIFI ENDPOINTS

The Kura WiFi consumer returns a list of the org.eclipse.kura.net.wifi.WifiAccessPoint classes returned as a result of the WiFi scan:

ConsumerTemplate consumerTemplate = camelContext.createConsumerTemplate();
WifiAccessPoint[] accessPoints = consumerTemplate.receiveBody("kura:wlan0/*", WifiAccessPoint[].class);

You can also request WiFi scanning by using the producer endpoint:

from("direct:WifiScan").to("kura-wifi:/").to("mock:accessPoints");

Or using the producer template directly:

ProducerTemplate template = camelContext.createProducerTemplate();
WifiAccessPoint[] accessPoints = template.requestBody("kura-wifi:/", null, WifiAccessPoint[].class);

DEPLOYMENT OPTIONS

The Camel Kura WiFi component can be deployed either as the OSGi bundle, directly into the Kura container:

public class WifiKuraRouter extends KuraRouter {
  @Override
  public void configure() throws Exception {
    from("kura-wifi:\*/\*").to("log:availableWifiNetworks");
  }
}

… where KuraRouter is a base OSGi bundle activator for Camel routes deployable into Kura.

The other deployment option is to use Spring Boot based fat jar:

@SpringBootApplication
  public class WifiKuraRouter extends FatJarRouter {
  @Override
  public void configure() throws Exception {
    from("kura-wifi:/").to("log:availableWifiNetworks");
  }
}

In the first place the Kura Wifi component tries to locate the org.eclipse.kura.net.NetworkService instance in the Camel registry. If exactly one instance of the NetworkService is found (this is usually the case when you deploy the route into the Kura container), the instance will be used by the Kura component. Otherwise a new instance of the org.eclipse.kura.linux.net.NetworkServiceImpl will be created and cached by the KuraAccessPointsProvider.

SUMMARY

Smart WiFi connectivity is fundamental for every mobile IoT gateway solution. Camel Kura WiFi integration makes the WiFi scanning process as easy as adding a few lines of the DSL. Your gateway application should try to cache the data collected from the sensors and avoid using the expensive mobile connectivity whenever possible. An efficient IoT gateway should synchronize the stored offline data when a trusted WiFi network becomes available.