Created
May 25, 2024 11:09
-
-
Save obiltschnig/10ab5c559cc7c1aaca0bfc7bfff53b60 to your computer and use it in GitHub Desktop.
macchina.io EDGE - Ruuvi Tag MQTT Message Decoding
This file contains hidden or 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
function mac2string(mac) | |
{ | |
var result = ''; | |
for (var i = 0; i < mac.length; i++) | |
{ | |
if (i > 0) result += ':'; | |
result += mac[i].toString(16).padStart(2, '0'); | |
} | |
return result; | |
} | |
var mqttClientRefs = serviceRegistry.find(`io.macchina.mqtt.id == "appinf"`); | |
var mqttClient; | |
if (mqttClientRefs.length > 0) | |
{ | |
mqttClient = mqttClientRefs[0].instance(); | |
mqttClient.on('messageArrived', ev => { | |
const payloadString = ev.data.message.payload; | |
const payloadObject = JSON.parse(payloadString); | |
const data = payloadObject.data; | |
if (data) | |
{ | |
const buffer = new Buffer(data, 'HEXBIN'); | |
if (buffer[4] === 0xFF && buffer[5] === 0x99 && buffer[6] === 0x04) | |
{ | |
// RUUVI advertisement | |
if (buffer[7] === 0x05) | |
{ | |
// Data format 5 (RAWv2) | |
const payload = buffer.slice(7); | |
const values = payload.unpack('!BhHHhhhH'); | |
const temperature = values[1]*0.005; | |
const humidity = values[2]*0.0025; | |
const pressure = (values[3]+50000)/100; | |
const voltage = (((values[7] >> 5) & 0x7ff) + 1600.0)/1000.0; | |
const mac = mac2string(payload.slice(18, 24)); | |
console.log('temp=%f hum=%f press=%f voltage=%f, mac=%s', temperature, humidity, pressure, voltage, mac); | |
} | |
} | |
} | |
}); | |
mqttClient.subscribe('ruuvi/#', 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment