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

Added simple example of how to receive sensor data

parents
node_modules
.env
# 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](https://nodejs.org/) 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_URL=<wss://full-url-to-mqtt-server.smhi.se>
THE_USER=<username>
THE_PASS=<password>
CLIENT_ID=<client_id>
# The port that tcp data should be exposed on (i.e. the port an external TLP should connect to)
PORT=<port>
```
### 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).
## MQTT
In this example, we use the library `mqtt.js` to connect to the MQTT server over websockets.
Please refer to the documentation on https://www.npmjs.com/package/mqtt
There are many more MQTT libraries out there. Take a look at: https://mqtt.org/software/ 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;
console.log(
`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);
server.listen(process.env.PORT);
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) {
console.log("Connected");
} 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)
tlp.write(message);
}
});
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": [
"tlp",
"lightning",
"mqtt",
"websockets"
],
"author": "Jonas Thoursie <jonas.thoursie@smhi.se>",
"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