This is part 4 in my IoT series of blogs. The first parts did cover the following topics:
- Introduction into the planned scenario
- Building a Raspberry Pi based Smart Gateway
- ESP8266 and DHT22 based smart sensor
This blog will describe how to enable the Smart Gateway to handle, enhance and forward incoming messages.
As for now, our environment is made of
- a sensor, build with an ESP8266 and a DHT22
- Smart Gateway, build with a Raspberry Pi 2 and JBoss Fuse, JBoss A-MQ running in their separate Docker container
When both taken into service, the sensor will be able to connect to the Smart Gateway via MQTT and published messages will be received but not processed in any way.
Planned Scenario
The DHT22 from our sensor is capable of measuring temperature and humidity, while the ESP8266 can measure the voltage of his powersource. Each of these are published by the ESP8266 into their own topic, with the ID of the ESP8266 as part of the topic name.
As an example, the temperature measured by the sensor with the ESP8266 processor ID ‘4711’ will be published to
iotdemo/temperature/4711
For each measured value, the message published to it’s topic consists of a consecutively numbered counter and the value itself, separated with a comma. So the first measured temperature of e.g. 21 Celsius, will be published as a message containing
1,21
For later processing, this dataset itself is not enough, as applications working with this data will not know what it is, where it comes from, etc.
For this reason, our Smart Gateway needs to extend this data – and while we are at it, convert it into XML.
At the end, what we want the Smart Gateway to forward for later processing, should be an XML-Document with the following content
<dataset> <deviceType/> <!-- 'temperature', 'humidity', etc --> <deviceID/> <!-- unique identifier of the sensor --> <counter/> <!-- consecutively numbered counter --> <payload/> <!-- the measured data --> <timestamp/> <!-- timestamp for measured data --> </dataset>
Implementation
While JBoss A-MQ will be the Broker for the MQTT based communication, JBoss Fuse will be responsible for transforming the message and forwarding it.
To be more specific, we will implement this task in Apache Camel, a fundamental part of JBoss Fuse.
The visual representation of the implementation in Camel, called a Camel route, is very simple.
The four “boxes” from the image above do the following tasks, in the order of the image
- subscribe to a specific topic, with ‘iotdemo’ as fixed value and the other variable
- custom POJO which extracts the ‘deviceType’ and ‘deviceID’ from the MQTT Topic, creates a timestamp and them to the data
- unmarshall the data to an XML string
- forward to a JMS queue for later processing
In Spring XML the above Camel route is rather simple
[code language=”java”]
<camelContext id="spring-context" xmlns="http://camel.apache.org/schema/spring">
<dataFormats>
<bindy type="Csv" classType="com.redhat.demo.smart_gateway.Dataset" id="bindyDataFormat"/>
</dataFormats>
<route>
<from uri="mqtt:mqtt.temp.receiver host=tcp://activemq:1883&amp;amp;amp;subscribeTopicNames=iotdemo/temperature/#&amp;amp;amp;userName=admin&amp;amp;amp;password=change12_me"/>
<bean ref="myHelper" method="enhanceMessage" beanType="com.redhat.demo.smart_gateway.MyHelper"/>
<unmarshal ref="bindyDataFormat"/>
<to uri="activemqRemote:queue:message.to.router"/>
</route>
</camelContext>
[/code]
Getting up and running
Now that we did take a look at the components and logic of the “Smart” in Smart Gateway, let’s put the bits and pieces together.
For the sake of simplicity, I assume that you will run all the required steps on your Raspberry Pi. This includes building the container and building the Java code. If you prefer to, e.g. build the Java code on another environment, that is fine. Please only take care, that the created deployable artifact will end up in the corresponding folder on the Raspberry Pi.
To get started on the Raspberry, you will need to make sure that the required Linux packages are installed
pi@raspberrypi:~ $ sudo apt-get install maven2 git Reading package lists... Done Building dependency tree Reading state information... Done git is already the newest version. maven2 is already the newest version. 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Then you need to clone the Github repository with all the Docker and Java artifacts
pi@raspberrypi:~ $ git clone https://github.com/PatrickSteiner/IoT_Demo_Gateway
If you haven’t done so by following the previous blog on creating the Smart Gateway, please do build the base Docker image.
pi@raspberrypi:~ $ cd ~/IoT_Demo_Gateway/Base pi@raspberrypi:~ $ docker build --rm -t psteiner/base .
With this done, we can create the war-File for JBoss Fuse
pi@raspberrypi:~ $ cd IoT_Demo_Gateway/Smart_Gateway/ pi@raspberrypi:~/IoT_Demo_Gateway/Smart_Gateway $ mvn clean install ... lines deleted ... [INFO] Installing /home/pi/IoT_Demo_Gateway/Smart_Gateway/target/smart_gateway.war to /home/pi/.m2/repository/com/redhat/demo/smart_gateway/0.0.1-SNAPSHOT/smart_gateway-0.0.1-SNAPSHOT.war [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1 minute 38 seconds [INFO] Finished at: Sat Jan 16 07:48:55 UTC 2016 [INFO] Final Memory: 31M/76M [INFO] ------------------------------------------------------------------------
If you should decide to build this war file on another system ( to save space and time ), please do make sure to copy it to with to the Raspberry Pi as /home/pi/.m2/repository/com/redhat/demo/smart_gateway/0.0.1-SNAPSHOT/smart_gateway-0.0.1-SNAPSHOT.war
Last step is the creation of the Docker images, if you didn’t do this before, please do take a look at ‘Building a Raspberry Pi based Smart Gateway‘ for more detailed instructions.
pi@raspberrypi:~/IoT_Demo_Gateway/Smart_Gateway $ cd .. pi@raspberrypi:~/IoT_Demo_Gateway $ docker-compose build ... lines deleted ... Successfully built d58462663f2a pi@raspberrypi:~/IoT_Demo_Gateway $
With all this done, it’s time to start the Smart Gateway
pi@raspberrypi:~/IoT_Demo_Gateway $ docker-compose up -d Recreating iotdemogateway_activemq_1... Recreating iotdemogateway_fuseeap_1...
Allow a bit of time for the two Docker images to start and for the software within the images to start as well. You can check the status of the two images with
i@raspberrypi:~/IoT_Demo_Gateway $ docker-compose logs Attaching to iotdemogateway_fuseeap_1, iotdemogateway_activemq_1 fuseeap_1 | Changing ownership of mounted volume ... lines deleted ... fuseeap_1 | 08:10:47,187 INFO [org.jboss.as] (Controller Boot Thread) JBAS015874: JBoss EAP 6.4.0.GA (AS 7.5.0.Final-redhat-21) started in 124295ms - Started 526 of 563 services (80 services are lazy, passive or on-demand)
Running the Smart Gateway
With everything up and running, this is what we created
Task | URL | User | Passwd | Install instructions |
---|---|---|---|---|
JBoss A-MQ ( ActiveMQ) Console | <ip of your Pi>:8161 | admin | admin | here |
MQTT Broker | <ip of your Pi>:1883 | admin | admin | here |
JBoss Fuse Console | <ip of your Pi>:8080 | psteiner | change12_me | here |
As of now, the Smart Gateway will be able to receive your messages, will enhance the data and forward it. As we have nothing to forward it to at the moment, your creativity is asked.
… or stay tuned for more to come!
One reply on “Putting the Smart into the Smart IoT Device”
[…] Putting the Smart into the Smart IoT Device […]