Commit 8379c57e authored by Jonas Thoursie's avatar Jonas Thoursie
Browse files

Added simple example of how to receive sensor data

# mqtt-client-example
This is a test project to show how you could connect to MQTT over websockets with Basic Authentication.
The data is binary.
## How
This example is written in javascript, and needs [nodejs]( to run.
### Prepare
Create a `.env` (in the same directory as `index.js`) with the following content:
# You will need these properties to be able to connect
# (Provided by SMHI Open Data)
# The port that tcp data should be exposed on (i.e. the port an external TLP should connect to)
### Install dependencies
npm install
### Run
npm start
As soon as you receive data, it will be forwarded to the TCP socket (if there is an active connection).
In this example, we use the library `mqtt.js` to connect to the MQTT server over websockets.
Please refer to the documentation on
There are many more MQTT libraries out there. Take a look at: to see if there's another library that is more suitable for you.
\ No newline at end of file
const mqtt = require("mqtt");
const net = require("net");
const format = require("date-fns/format");
const CLIENT_ID = process.env.CLIENT_ID;
const USERNAME = process.env.THE_USER;
const PASSWORD = process.env.THE_PASS;
const URL = process.env.THE_URL;
`Authenticate client (client_id=${CLIENT_ID}) using user: ${USERNAME}`
const auth = Buffer.from(`${USERNAME}:${PASSWORD}`, "utf8").toString("base64");
console.log(`Connecting to ${URL}`);
const client = mqtt.connect(URL, {
keepalive: 30,
clientId: CLIENT_ID,
wsOptions: {
headers: {
Authorization: `Basic ${auth}`,
let tlp = undefined;
const onClientConnected = (socket) => {
// In a real environment, we probably want to keep track of all clients connected,
// instead of replacing the active one.
console.log("Client connected, this will be the only client we treat as our TLP");
tlp = socket;
const server = net.createServer(onClientConnected);
console.log(`TCP server is listening on port ${process.env.PORT}`);
client.on("connect", () => {
// The topic to subscribe to
const topic = "sensor/se/*";
client.subscribe(topic, (err) => {
if (!err) {
} else {
console.error("Error", err);
client.on("message", (topic, message) => {
// message is binary data (Buffer). It makes no sense to just print it out,
// but we do it to see that we're receiving data
console.log(format(new Date(), "[yyyy-MM-dd HH:mm:ss.SSS]"), message.toString());
if (tlp) {
// Write data to TCP socket (if connected)
This diff is collapsed.
"name": "mqtt-client-example",
"version": "1.0.0",
"description": "An example of how to receive lightning sensor data over websockets/MQTT",
"main": "index.js",
"scripts": {
"start": "nodemon -r dotenv/config index.js"
"keywords": [
"author": "Jonas Thoursie <>",
"license": "ISC",
"dependencies": {
"date-fns": "^2.21.1",
"dotenv": "^8.2.0",
"mqtt": "^4.2.6"
"devDependencies": {
"nodemon": "^2.0.7"
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment