DEV Community

Cover image for Send BLE sensor data over MQTT using Javascript mqtt.js
Bleuio tech
Bleuio tech

Posted on

Send BLE sensor data over MQTT using Javascript mqtt.js

We’re living in the world of connected devices. The internet of things helps us live and work smarter, as well as gain complete control over our lives. One of the latest technological advancements in IoT is the MQTT gateway, which acts as a mediator between the cloud and IoT platforms.
MQTT stands for Message Queuing Telemetry Transport. It’s among the key communication protocols for the internet of things devices and local networks. It’s an ideal protocol for communication between smart devices or machine-to-machine communication.

What Is MQTT Gateway?

Generally, the MQTT gateway can be defined as an intermediary between any internet of things platform and sensors. It works by getting data from these sensors or smart devices and translating it into MQTT. It then transmits that data to either the internet of things platform or to the MQTT broker.

The publish/subscribe pattern

The publish/subscribe pattern (also known as pub/sub) provides an alternative to a traditional client-server architecture. In the client-server model, a client communicates directly with an endpoint. The pub/sub model decouples the client that sends a message (the publisher) from the client or clients that receive the messages (the subscribers). The publishers and subscribers never contact each other directly. In fact, they are not even aware that the other exists. The connection between them is handled by a third component (the broker). The job of the broker is to filter all incoming messages and distribute them correctly to subscribers.

MQTT Broker

A broker helps in handling clients in MQTT technology. It can manage hundreds, thousands, or millions of connected MQTT clients at once, depending on the implementation. Its main functions are;

  • Receiving information
  • Decoding and filtering the messages received
  • Determining which client will be interested in which message
  • Transmitting these messages to clients depending on their interests

The project

Let’s make a simple BLE 2 MQTT project that collects sensor data from a BLE Air quality monitor device called HibouAir and sends it to a free public MQTT broker.
For this project, we will use Flespi. You can choose any public or private MQTT broker as you like.
Steps

Requirments

BLE to MQTT

Download source file

Get the source file from https://github.com/smart-sensor-devices-ab/ble2mqtt_bleuio.git
And run npm install
In the root folder, we will see two Html files called index.html and subscribe.html and two js files called pub.js and sub.js
BLE to MQTT

Index.html file collects sensor data from a BLE Air quality monitor device called HibouAir with the help of BleuIO.
After collecting advertised data, we try to decode it and get meaningful air quality data with co2, pressure, temperature, humidity, light values. Then we publish the data to Flepsi broker using topic name HibouAirTopic
Subscribe.html file works as a subscriber which reads sensor data from the broker and shows it on the screen.
To run the index.html we can just type
parcel index.html

Project Video

https://www.youtube.com/watch?v=iXGdEljgODY

Discussion (0)