forked from pycom/pycom-libraries
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Send_BT_to_LoPy.js
78 lines (66 loc) · 2.21 KB
/
Send_BT_to_LoPy.js
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
73
74
75
76
77
78
var puckID = '690d'; // change!!
// Are we busy?
var busy = false;
// The device, if we're connected
var connected = false;
// The 'tx' characteristic, if connected
var txCharacteristic = false;
function pad(num, size) {
var s = "000000000" + num;
return s.substr(s.length-size);
}
function getHEXValue() {
light = Puck.light() * 1000;
lightHEX = Math.round(light).toString(16);
lightHEX = pad(lightHEX,3);
temperature = E.getTemperature() * 10;
temperatureHEX = Math.round(temperature).toString(16);
temperatureHEX = pad(temperatureHEX,3);
battery = NRF.getBattery() * 100;
batteryHEX = Math.round(battery).toString(16);
batteryHEX = pad(batteryHEX,3);
return lightHEX+temperatureHEX+batteryHEX;
}
// Function to call 'toggle' on the other Puck
function sendToggle() {
if (!busy) {
busy = true;
if (!connected) {
NRF.requestDevice({ filters: [{ name: 'LoPy01' }] }).then(function(device) {
return device.gatt.connect();
}).then(function(d) {
digitalPulse(LED3, 1, 1500); // light blue
connected = d;
return d.getPrimaryService("36353433-3231-3039-3837-363534333231");
}).then(function(s) {
return s.getCharacteristic("36353433-3231-3039-3837-363534336261");
}).then(function(c) {
hexValue = puckID + getHEXValue();
console.log(hexValue);
c.writeValue(hexValue);
busy = false;
}).then(function() {
if (connected) connected.disconnect();
connected=false;
digitalPulse(LED2, 1, 1000); // light green
}).catch(function() {
if (connected) connected.disconnect();
connected=false;
digitalPulse(LED1, 1, 1000); // light red if we had a problem
busy = false;
});
} else {
console.log("already connected!");
if (connected) connected.disconnect();
connected=false;
busy = false;
}
} else {
console.log("already busy!");
if (connected) connected.disconnect();
connected=false;
busy = false;
}
}
// Call this function when the button is pressed
setWatch(sendToggle, BTN, { edge:"rising", debounce:50, repeat: true });