Hello to all. In this article I would like to show an example of using the bundle TI SensorTag, Raspberry PI, Apache Camel with output to the web part. As a result, there will be a web application displaying real-time data from sensors and a database storing readings with an intermediate connecting node in the form of an Apache Camel application.
Install Raspbian on the Raspberry PI from here .
Eclipse Kura is a framework for building applications that work with IoT on platforms such as Raspberry PI. Allows you to customize applications through the web interface or through the API. Applications are deployed via OSGi. Also takes care of working with mqtt broker. [one]
For simplicity, let us become a root and update the list of sudo -i && apt-get update
packages. Install Java (better to use Oracle java, apt-get install oracle-java8-jdk
). Download Kura version Raspbian (Model 2) - Stable from here (download the latest version at the time of the article: wget http://mirror.onet.pl/pub/mirrors/eclipse//kura/releases/3.0.0/kura_3.0.0_raspberry-pi-2-3_installer.deb
) and install: dpkg -i kura_*_installer.deb && apt-get install -f
. Restart Raspberry PI: shutdown -r now
.
Now you can open Kura WEB UI at http: // \ <rpi-device-ip> with login and password admin / admin. Here you can configure the network, wi-fi access point, device name and other parameters.
The version from the package manager should come quite well. Therefore, we perform:
apt-get install -y libusb-dev libdbus-1-dev libglib2.0-dev libudev-dev libical-dev libreadline-dev
apt-get install bluez
Launch and add a bluetooth service to autoload: systemctl enable bluetooth && systemctl start bluetooth
. Turn on the wireless interface: hciconfig hci0 up
. Check that the interface is turned on and everything works: hciconfig -a
. If errors occur in bluez, you can try to download and compile the latest version, for example, under article [3] [4].
Mosquitto is an open message broker using the MQTT protocol. We need it to send data from the SensorTag to the Camel backend application.
It can be installed either on a Rasperry PI or on another machine, or you can use cloud solutions (IBM, Azure). But for the web part to work, it is necessary that mosquitto be configured to work through mqtt over websockets (This can be done on another broker instance, and not on the local broker).
Standard repository version
apt-get install mosquitto systemctl enable mosquitto
To enable websocket in mosquitto, add the following lines to /etc/mosquitto/mosquitto.conf
# Websocket listener 1883 listener 9001 protocol websockets
and run systemctl start mosquitto
. To install the newest mosquttio version with websockets support. [2]
wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key apt-key add mosquitto-repo.gpg.key cd /etc/apt/sources.list.d/ wget http://repo.mosquitto.org/debian/mosquitto-jessie.list apt-get update apt-get install mosquitto
Kura contains an example application for working with TI SensorTag. It connects to the device via BLE once every n seconds, reads the data using the Generic Attribute Profile (GATT) and sends it to the broker. It turns out rather slowly, but for starters it goes.
For a start we will clone a repository with kura. You can use either the original https://github.com/eclipse/kura or the forked project https://github.com/leadex/kura , which adds a reading of the temperature metrics from the barometer.
We pump out:
git clone https://github.com/leadex/kura cd kura
I only managed to compile the code on linux. To build run ./build-all.sh
. For the subsequent assembly only ble example:
cd kura/kura/examples/org.eclipse.kura.example.ble.tisensortag mvn clean install -Dmaven.test.skip=true
Next, copy the resulting jar to the Raspberry PI.
cd kura/examples/org.eclipse.kura.example.ble.tisensortag scp target/org.eclipse.kura.example.ble.tisensortag-*-SNAPSHOT.jar pi@<rpi-device-ip>:/tmp
Connect to the OSGi console from Raspberry PI telnet localhost 5002
or remotely telnet <rpi-device-ip> 5002
. Enter the command to install our compiled application install file:///tmp/org.eclipse.kura.example.ble.tisensortag-1.0.3-SNAPSHOT.jar
and then enter ss
for verification. See the installed but not running bundle at the end: 75 INSTALLED org.eclipse.kura.example.ble.tisensortag
.
Run the command start 75
, where 75 is the application id from the previous step. Now it is running: 75 ACTIVE org.eclipse.kura.example.ble.tisensortag
and you can view the logs on the Raspberry PI: tail -f /var/log/kura.log
[5].
The Kura web UI has a new service BluetoohLe. Open and configure it.
After restarting Kura, our deployed bundle will disappear, so you can use the instructions on the permanent operation of the bundle.
To quickly launch MongoDB + Mosquitto + Webview, you can run docker-compose up -d
at the root of the project. This will create 3 containers and bind mqtt, websocket, http (8081) ports to localhost. Immediately after this, the web part will be available at http: // localhost: 8081 . And mqtt broker on localhost: 1883, so it will be possible to indicate your IP in Kura MqttDataTransport.
Sample web part:
iot-backend-camel contains an Apache Camel project that subscribes to a message broker and receives KuraPayload.
EclipseKura messages are transmitted in Google Protobuf encoded format. Therefore, we need a library from Google to decode com.google.protobuf:protobuf-java
.
Next, the message gets the metrics, is serialized in json and sent to the subscribers of the web part in the broker, as well as stored in the MongoDB database.
To run the Camel application, run: gradlew clean build
and java -jar ./build/libs/iot-backend-camel-1.0.0-SNAPSHOT.jar
.
webview contains a docker nginx based image with html & js. The web part uses the Paqho mqtt over websocket library and highcharts to dynamically display the value of the sensors (Temperature from the IR sensor and the level of illumination).
The site is signed by the broker for data updates. Probably to work, you will have to change the mqtt host in webview. This can be done in webview/dist/view.js
In line:
// Create a client instance client = new Paho.MQTT.Client("localhost", Number(9001), "webview_" + parseInt(Math.random() * 1000000, 10));
You can restart webview using docker-compose up -d --build
. This will rebuild the docker image and restart if it has changed.
Source: https://habr.com/ru/post/332428/
All Articles