Skip to content

Instantly share code, notes, and snippets.

@tkurki

tkurki/index.js Secret

Last active November 7, 2019 19:49
Show Gist options
  • Save tkurki/b1658264bdd8493393e611f03de84698 to your computer and use it in GitHub Desktop.
Save tkurki/b1658264bdd8493393e611f03de84698 to your computer and use it in GitHub Desktop.
Signal K to Thingsboard
const mqtt = require('mqtt')
const WebSocket = require('ws')
const client = mqtt.connect('mqtt://demo.thingsboard.io', {
username: process.env.ACCESS_TOKEN
})
client.on('connect', function () {
console.log('connected')
const ws = new WebSocket('ws://localhost:3000/signalk/v1/stream');
ws.on('message', function incoming(data) {
const msg = JSON.parse(data)
msg.updates && msg.updates.forEach(update => {
update.values && update.values.forEach(pathValue => {
console.log(`${pathValue.path}=${pathValue.value}`)
const msgOut = {}
msgOut[pathValue.path] = pathValue.value
const theMsg = JSON.stringify(msgOut)
client.publish('v1/devices/me/telemetry', theMsg, {}, (err) => {
if (err) {
console.error(err)
} else {
console.log(`==> Published ${theMsg}`)
}
})
})
})
})
})
{
"name": "thingsboard-test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Teppo Kurki <teppo.kurki@iki.fi> (https://github.com/tkurki)",
"license": "ISC",
"dependencies": {
"mqtt": "^3.0.0",
"ws": "^7.2.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment