-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcardReader.js
More file actions
26 lines (21 loc) · 865 Bytes
/
cardReader.js
File metadata and controls
26 lines (21 loc) · 865 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
require("dotenv").config();
const mqtt = require("mqtt");
const SerialPort = require("serialport");
const Readline = require("@serialport/parser-readline");
const port = new SerialPort(process.env.comPort, {
baudRate: parseInt(process.env.baudRate, 10),
autoOpen: false
});
const client = mqtt
.connect(process.env.borkerURL, { protocolId: "MQIsdp", protocolVersion: 3 })
.on("error", error => console.log(error))
.on("message", (topic, message) => console.log(`[${topic}] ${message.toString()}`))
.on("connect", () => {
client.publish("reader/info", ">>> MQTT Connected <<<");
port
.on("open", () => client.publish("reader/info", ">>> Serial Connected <<<"))
.pipe(new Readline({ delimiter: "\r\n" }))
.on("data", data => client.publish("reader/card", data));
port.open(console.error);
})
.subscribe("reader/#");