-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfundamentals_meshtastic_web.mjs
More file actions
72 lines (55 loc) · 2.01 KB
/
fundamentals_meshtastic_web.mjs
File metadata and controls
72 lines (55 loc) · 2.01 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
BONUS! QUICK START FOR DEVELOPERS
This is a quick boilerplate for Meshtastic Web javascritp library input and output.
It is not used by Node-RED module but could help you to understand how to use the library.
Daniel B.P. (www.danbp.org)
11/11/2025
How to run:
npm install @mesthastic/transport-http
node ./fundamentals_meshtastic_web.mjs
*/
// Import the required libraries
// To install the NPM packages, use "npm install @meshtastic/transport-http", the core package will come as a requirement
import { MeshDevice } from "@meshtastic/core";
import { TransportHTTP } from "@meshtastic/transport-http";
//Minimum settings
let ip = "meshtastic.local";
let fetchInterval = 5000;
let tls = false;
try {
const transport = await TransportHTTP.create(ip, tls);
transport.fetchInterval = fetchInterval;
const device = await new MeshDevice(transport);
//Get the device the configuration
//device.configure()
// Subscribes to an event (onMessagePacket) and prints the received message
device.events.onMessagePacket.subscribe(function (message) {
//Execute when a message was received
console.log(">>>>> RECEIVED A MESSAGE >>>>> " + message.data);
});
//Subscribe to the device status
device.events.onDeviceStatus.subscribe(function (status) {
console.log(">>>>> STATUS CODE >>>>> " + status);
if (status == 5) {
//Wait for the connected status (5)
console.log(">>>>> SENDING MESSAGE NOW >>>>> ");
//Test message (sent every time the device status changes to online)
let message = "I am alive!!";
let destination = "broadcast"; //4294967295
let wantAck = false;
let channel = 0;
device
.sendText(message, destination, wantAck, channel)
.then((id) => {
console.log(">>>>> MESSAGE SENT >>>>> " + message);
})
.catch((e) => {
console.log(">>>>> ERROR SENDING MESSAGE >>>>> " + e);
});
}
});
// Use this to disconnect from the device
//device.disconnect();
} catch (error) {
console.log(error);
}