MQTT broker and Bluetooth gateway

what makes adding or removing electronic assistants (Bluetooth, Wi-Fi and wired) easy

Mobirise

Main components:
- any single-board computer, Armbian (Debian) based in this case,
- 4.0/4.1 Bluetooth USB dongle,
- the camper’s router.

MQTT broker, that is for the small messages transportation between camper's sensors and actuators, is extremely lightweight and can be deployed on any single-board computer that is in the camper's network. What is important is to set a static IP for this computer in the camper's network, and to make this computer not sleep, not hibernate.

Since the MQTT broker's computer should work around a clock, it is better to choose the one that consumes the least electricity amount from the camper's battery. Better if this computer doesn’t have installed constantly running services, such as the desktop, HDMI, etc. and has the ability to turn off part of processor cores, reducing the frequency of cores. Looks like the best way is to deploy the MQTT broker directly on the camper's router if technical characteristics of this router allow it - there is USB on board and OpenWrt OS, and a fortiori if this router can be expanded with a regular OS (as example - in a similar way).

The Mosquitto MQTT broker was installed on a single-board computer via Windows PC. In some cases will need to switch off the PC firewall while settings. Broker's computer was available in the PC terminal via PuTTY over IP on port 22 under root user. Then were entered from PC via PuTTY terminal:

apt-get install mosquitto

apt-get install mosquitto-clients

sudo service mosquitto stop

sudo systemctl stop mosquitto.service

nano /etc/mosquitto/conf.d/ca.conf

In the opened file were entered strings:

listener 1883

protocol mqtt


listener 9001

protocol websockets


Ctrl+O, Enter, Ctrl+X

nohup mosquitto -c /etc/mosquitto/conf.d/ca.conf >> /dev/null 2>&1 &

From this point any Wi-Fi and wired devices in the Camper's network can be served by MQTT broker and be discoverables to each other, for the Camper assistant and for the camper's crew gadgets, but no Bluetooth devices.

All devices in the Camper's network can be Wi-Fi realized, but it is not effective. Most often in practice (not talk about progressive but rare Wi-Fi generations), if there are two Wi-Fi devices within a network, they divide a Wi-Fi range in half, if three devices, then at three equal parts, if four - then at four equal parts, etc, no matter if one device transfer a full-HD video while another device transfers 4 bytes per hour.

It is more efficient to use Bluetooth devices to transfer small amounts of data, and not utilize resources of Wi-Fi networks for this.

All Bluetooth devices within the Camper assistant network are divided into two classes: 

- Bluetooth sensors that only publish messages ("advertise" in Bluetooth terms) about the camper's parameters. This messages in Bluetooth RAW standard; 

- Bluetooth actuators that do some work in the physical world, reacting on commands. 

The connection between Bluetooth sensors and actuators can be direct, but more effective is to use Bluetooth gateway, which can take in account much more camper's parameters, including MQTT or not standard messages from Wi-Fi and wired devices and other Bluetooth devices.

For Bluetooth gateway the Qualcomm CSR8510 A10 Bluetooth USB dongle was used (Hama H-49218 for example) on the same computer where MQTT broker was installed. Here is the list of other Bluetooth 4.0/4.1 USB adapters that can be used analogously (being integrated on the single-board computer, or within a USB dongle). During the experiment the low price Bluetooth 4.0 dongle was tested (noname with chip AP1Z357 and only marked "CSR 4.0" on the case), and this one had been working normally too.

Because not so much but only 5-6 Bluetooth devices can be connected at one time to the Bluetooth dongle, camper's Bluetooth actuators and sensors not attached to the dongle permanently. Instead this, for each actuator the Bluetooth gateway process (program) is being done, that parses the advertise from Bluetooth sensors (with no need establish "connection" to this sensors) or parses MQTT messages or both; then makes the temporary Bluetooth connect and send commands to the its appropriate Bluetooth actuator, no occupies the Bluetooth dongle for long time. At the same time this or other one gateway process can make MQTT standard messages into the camper's network about parameters of Bluetooth sensor, had been parsed based on the sensor's Bluetooth advertising data.

In order for Bluetooth sensors and actuators can be served by Bluetooth gateway, the Node.js and the Yarn were installed (under root user):

apt-get update

apt-get install gcc g++ make

apt-get install bluetooth bluez libbluetooth-dev libudev-dev pulseaudio-module-bluetooth blueman

ln -s /usr/bin/nodejs /usr/bin/node

curl -sL https://deb.nodesource.com/setup_10.x | bash -

apt-get install -y nodejs

apt-get install libcap2-bin

setcap cap_net_raw+eip $(eval readlink -f `which node`)

curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -

echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list

apt-get update && apt-get install yarn

reboot

Note: it is not necessary to deploy the Node.js on the same computer where MQTT broker was installed. It is possible to make on any computer within the camper's network (more than - on few computers). But the Bluetooth USB dongle or integrated Bluetooth adapter (described above), must be connected to the same computer, where Node.js has been deployed.

Next under ordinary (not a root) user, were entered from PC via PuTTY terminal:

cd /usr/bin/

sudo yarn add node-gyp

sudo yarn add bluetooth-hci-socket@npm:@abandonware/bluetooth-hci-socket

sudo yarn add @abandonware/bluetooth-hci-socket

sudo yarn add noble@npm:@abandonware/noble

sudo yarn add prettyjson

sudo yarn add net

sudo yarn add bleno

sudo yarn add async

sudo yarn add mqtt

sudo yarn add node-bluetooth

sudo yarn add socket.io

sudo yarn add express@4.15.2

sudo yarn add tcp-port-used

sudo yarn add mathjs

sudo yarn install

Note: if some of the yarn packages listed above could not be installed successfully (critical errors occur), that means the selected version of Node.js can't work under this Linux version. In this case some lower Node.js version can be installed. But before this, the ready installed version of Node.js needs to purge under root user by commands "apt-get remove --purge nodejs", then "rm -r /usr/bin/node_modules". After this purge, new version of Node.js can be installed like described above, starting with command "curl -sL https://deb.nodesource.com/setup_10.x | bash -" (but with choosed version pointed), and then all listed above yarn packages need to add one more time.

From this point Node.js can serve gateway's processes for Bluetooth sensors and actuators as described in appropriate articles about the Camper assistant.

A software you can load from this site is free for a non-commercial use except for components for which their owners declared their own rights. Can be limited by local laws. No of your data will be collected with a software you can load from this site.

Subscribe for updates.

Design a site - Try here