mirror of
https://github.com/meshcore-dev/meshcore.js.git
synced 2026-04-20 22:13:49 +00:00
add sensor telemetry example
This commit is contained in:
parent
3fcd1ec89e
commit
d1ae420d66
1 changed files with 50 additions and 0 deletions
50
examples/get_sensor_telemetry.js
Normal file
50
examples/get_sensor_telemetry.js
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
import TCPConnection from "../src/connection/tcp_connection.js";
|
||||
import CayenneLpp from "../src/cayenne_lpp.js";
|
||||
|
||||
// create connection
|
||||
const connection = new TCPConnection("10.1.0.75", 5000);
|
||||
|
||||
// wait until connected
|
||||
connection.on("connected", async () => {
|
||||
|
||||
// we are now connected
|
||||
console.log("Connected");
|
||||
|
||||
// find contact
|
||||
// const contact = await connection.findContactByName("Liam's Water Tank");
|
||||
// const contact = await connection.findContactByPublicKeyPrefix([0xc9, 0xe1, 0x50, 0x58]);
|
||||
// const contact = await connection.findContactByPublicKeyPrefix(Buffer.from("c9e15058", "hex"));
|
||||
const contact = await connection.findContactByPublicKeyPrefix(Buffer.from("c9e15058af33781743b222bc98105abe7429cf46f7212f767ff2a7ce1bea5dcb", "hex"));
|
||||
if(!contact){
|
||||
console.log("Contact not found");
|
||||
await connection.close();
|
||||
return;
|
||||
}
|
||||
|
||||
// get sensor telemetry
|
||||
console.log("Fetching telemetry...");
|
||||
const telemetry = await connection.getTelemetry(contact.publicKey);
|
||||
|
||||
// disconnect
|
||||
await connection.close();
|
||||
|
||||
// parse telemetry
|
||||
const parsedTelemetry = CayenneLpp.parse(telemetry.lppSensorData);
|
||||
|
||||
// find telemetry on channel 1
|
||||
const selfVoltageTelemetry = parsedTelemetry.find((item) => item.channel === 1 && item.type === CayenneLpp.LPP_VOLTAGE);
|
||||
|
||||
// make sure telemetry found
|
||||
if(selfVoltageTelemetry == null){
|
||||
console.log("Telemetry Missing");
|
||||
return;
|
||||
}
|
||||
|
||||
console.log({
|
||||
voltage: selfVoltageTelemetry.value,
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
// connect to meshcore device
|
||||
await connection.connect();
|
||||
Loading…
Add table
Add a link
Reference in a new issue