meshtastic-firmware/data/static/basic.js

44 wiersze
1.3 KiB
JavaScript
Czysty Zwykły widok Historia

2020-10-23 01:26:43 +00:00
var meshtasticClient;
var connectionOne;
// Important: the connect action must be called from a user interaction (e.g. button press), otherwise the browsers won't allow the connect
function connect() {
// Create new connection
2020-10-27 01:17:33 +00:00
var httpconn = new meshtasticjs.IHTTPConnection();
// Set connection params
let sslActive;
if (window.location.protocol === 'https:') {
sslActive = true;
} else {
sslActive = false;
}
let deviceIp = window.location.hostname; // Your devices IP here
2020-10-23 01:26:43 +00:00
// Add event listeners that get called when a new packet is received / state of device changes
2020-10-27 01:17:33 +00:00
httpconn.addEventListener('fromRadio', function (packet) { console.log(packet) });
2020-10-23 01:26:43 +00:00
// Connect to the device async, then send a text message
2020-10-27 01:17:33 +00:00
httpconn.connect(deviceIp, sslActive)
.then(result => {
2020-10-23 01:26:43 +00:00
2020-10-27 01:17:33 +00:00
alert('device has been configured')
// This gets called when the connection has been established
// -> send a message over the mesh network. If no recipient node is provided, it gets sent as a broadcast
return httpconn.sendText('meshtastic is awesome');
2020-10-23 01:26:43 +00:00
2020-10-27 01:17:33 +00:00
})
.then(result => {
2020-10-23 01:26:43 +00:00
2020-10-27 01:17:33 +00:00
// This gets called when the message has been sucessfully sent
console.log('Message sent!');
})
2020-10-23 01:26:43 +00:00
2020-10-27 01:17:33 +00:00
.catch(error => { console.log(error); });
2020-10-23 01:26:43 +00:00
}