-
-
Save tkurki/b1658264bdd8493393e611f03de84698 to your computer and use it in GitHub Desktop.
Signal K to Thingsboard
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`) | |
} | |
}) | |
}) | |
}) | |
}) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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