1.10.34. fejezet, MQTT

Kapcsolódó hivatkozások

STDIn - soros port - MQTT adat továbbító

import { SerialPort } from "serialport";
import { ReadlineParser } from "@serialport/parser-readline"
import mqtt from "mqtt";
import readline from "readline";
 
const client = mqtt.connect("mqtt://localhost");
client.on("connect", () => {
    client.subscribe("serialmessages", (err) => {
        if (!err) {
            console.log("Connected to serialmessages")
        } else {
            console.log("Error on connection: " + err)
            client.end();
        }
    });
});
 
client.on("message", (topic, message) => {
    console.log(message.toString());
});
 
const port = new SerialPort({ path: '/dev/ttyACM0', baudRate: 19200 })
const parser = port.pipe(new ReadlineParser({ delimiter: '\r\n' }))
 
parser.on('data', (stream) => {
    client.publish("serialmessages", stream);
})
 
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
    terminal: false
});
 
rl.on('line', (line) => {
    port.write(line + "\r\n")
});
 
rl.once('close', () => {
    client.end();
});

MQTT - soros port üzenet továbbító

import { SerialPort } from "serialport";
import { ReadlineParser } from "@serialport/parser-readline"
import mqtt from "mqtt";
 
const client = mqtt.connect("mqtt://localhost");
client.on("connect", () => {
    client.subscribe("IN", (err) => {
        if (!err) {
            console.log("Connected to IN messages")
        } else {
            console.log("Error on connection: " + err)
            client.end();
        }
    });
    client.subscribe("OUT", (err) => {
        if (!err) {
            console.log("Connected to OUT messages")
        } else {
            console.log("Error on connection: " + err)
            client.end();
        }
    });
});
 
client.on("message", (topic, message) => {
    if (topic == "OUT") {
        console.log(message.toString());
    } else if (topic == "IN") {
        port.write(message.toString() + "\r\n")
    }
});
 
const port = new SerialPort({ path: '/dev/ttyACM1', baudRate: 19200 })
const parser = port.pipe(new ReadlineParser({ delimiter: '\r\n' }))
 
parser.on('data', (stream) => {
    client.publish("OUT", stream);
})

MQTT kliens STDIn továbbítással

import mqtt from "mqtt";
import readline from "readline";
 
const client = mqtt.connect("mqtt://localhost");
client.on("connect", () => {
    client.subscribe("IN", (err) => {
        if (!err) {
            console.log("Connected to IN messages")
        } else {
            console.log("Error on connection: " + err)
            client.end();
        }
    });
    client.subscribe("OUT", (err) => {
        if (!err) {
            console.log("Connected to OUT messages")
        } else {
            console.log("Error on connection: " + err)
            client.end();
        }
    });
});
 
client.on("message", (topic, message) => {
    if (topic == "OUT") {
        console.log(message.toString());
    }
});
 
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
    terminal: false
});
 
rl.on('line', (line) => {
    client.publish("IN", line);
});
 
rl.once('close', () => {
    client.end();
});

package.json

{
  "name": "homework4serialport",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "tsc",
    "dev": "nodemon"
  },
  "nodemonConfig": {
    "watch": [
      "src"
    ],
    "ext": "ts",
    "exec": "ts-node src/index.ts"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "ts-node": "^10.9.1",
    "typescript": "^5.0.4"
  },
  "dependencies": {
    "@types/node": "^20.1.1",
    "dotenv": "^16.3.1",
    "mqtt": "^5.5.1",
    "nodemon": "^2.0.22",
    "serialport": "^11.0.1"
  }
}

Üzenet küldés Mosquitto kliensel

#Üzenet küldés
mosquitto_pub -t IN -m "STATUS"
 
#Üzenet fogadás
mosquitto_sub OUT