Skip to content

Commit 568fdf9

Browse files
authored
Merge pull request #4 from jaredfiacco2/master
added bleeverything app
2 parents d2f0de9 + 6701add commit 568fdf9

File tree

4 files changed

+139
-3
lines changed

4 files changed

+139
-3
lines changed

apps.json

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,16 @@
229229
"storage": [
230230
{"name":".bootcde","url":"app.js"}
231231
]
232-
}
233-
234-
232+
},
233+
{ "id": "bleeverything",
234+
"name": "Bluetooth Advertiser - Everything!",
235+
"icon": "icon.png",
236+
"version":"0.01",
237+
"description": "Upload this code and your Puck will advertise temperature (f), battery percentage, LED states, Voltage and Light data each minute. It will also advertise button, acceleration, magnetic field and nfc field state data when it senses a change. You can use EspruinoHub and NodeRed MQTT In nodes to record and process the advertised data.",
238+
"tags": "bluetooth,nfc",
239+
"needsFeatures":["BLE","ACCEL","GYRO","NFC"],
240+
"storage": [
241+
{"name":".bootcde","url":"app.js"}
242+
]
243+
}
235244
]

apps/bleeverything/ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.01: New App!

apps/bleeverything/app.js

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
var state =1;
2+
NRF.setTxPower(4); // Full Power advertising
3+
4+
//Send MQTT Advertisements & Console Logs Every Minute while Button State === 1
5+
setInterval(function () {
6+
setTimeout(function(){
7+
if(state === 1){
8+
console.log("temperature : " + [Math.round((E.getTemperature()*9/5)+32)]);
9+
NRF.setAdvertising({
10+
0x1809 : [Math.round((E.getTemperature()*9/5)+32)]
11+
});
12+
}
13+
},1);
14+
15+
setTimeout(function(){
16+
if(state === 1){
17+
console.log("battery : " + [Puck.getBatteryPercentage()]);
18+
NRF.setAdvertising({
19+
0x180F : [Puck.getBatteryPercentage()]
20+
});
21+
}
22+
},5000);
23+
24+
setTimeout(function(){
25+
if(state === 1){
26+
console.log("Voltage : " + [String(Math.round(analogRead(D30)*6.6 * 100) / 100)]);
27+
NRF.setAdvertising({
28+
0x182a : [String(Math.round(analogRead(D30)*6.6 * 100) / 100)]
29+
});
30+
}
31+
},10000);
32+
33+
setTimeout(function(){
34+
if(state === 1){
35+
console.log("Light : " + [String(Puck.light())]);
36+
NRF.setAdvertising({
37+
0x182b : [String(Puck.light())]
38+
});
39+
}
40+
},15000);
41+
42+
setTimeout(function(){
43+
if(state === 1){
44+
console.log("LEDs: " + [[digitalRead(LED1).toString(16)], [digitalRead(LED2).toString(16)], [digitalRead(LED2).toString(16)]]);
45+
NRF.setAdvertising({
46+
0x183d : [[digitalRead(LED1).toString(16)], [digitalRead(LED2).toString(16)], [digitalRead(LED2).toString(16)]]
47+
});
48+
}
49+
},20000);
50+
},60000);
51+
52+
53+
//Button Press
54+
//Turn Off/On MQTT Advertising
55+
var pressCount = 0;
56+
setWatch(function() {
57+
pressCount++;
58+
state = (pressCount+1)%2;
59+
if ((pressCount+1)%2) digitalPulse(LED3,1,1500); //long flash blue light
60+
else
61+
digitalPulse(LED3,1,100); //short flash blue light
62+
console.log('button_press_count : [' + pressCount + ']');
63+
console.log('button_state : [' + (pressCount+1) + ']');
64+
console.log('state: ' + state);
65+
NRF.setAdvertising({
66+
0xFFFF : [pressCount],
67+
0x183c: [((pressCount+1)%2)],
68+
});
69+
}, BTN, { edge:"rising", repeat:true, debounce:50 });
70+
71+
72+
//Movement Sensor
73+
require("puckjsv2-accel-movement").on();
74+
var idleTimeout;
75+
Puck.on('accel',function(a) {
76+
digitalWrite(LED1,1); //turn on red light
77+
if (idleTimeout) clearTimeout(idleTimeout);
78+
else
79+
if (state === 1) {
80+
console.log('movement : 1');
81+
NRF.setAdvertising({
82+
0x182e: [1],
83+
});
84+
}
85+
idleTimeout = setTimeout(function() {
86+
idleTimeout = undefined;
87+
digitalWrite(LED1,0);//turn off red light
88+
if (state === 1) {
89+
console.log('movement : 0');
90+
NRF.setAdvertising({
91+
0x182e: [0],
92+
});
93+
}
94+
},500);
95+
});
96+
97+
98+
//Magnetic Field Sensor
99+
require("puckjsv2-mag-level").on();
100+
Puck.on('field',function(m) {
101+
digitalPulse(LED2, 1, 200);//flash green light
102+
if (state === 1) {
103+
console.log('magnetic_field : [' + m.state + ']');
104+
NRF.setAdvertising({
105+
0x183a: [m.state],
106+
});
107+
}
108+
});
109+
110+
111+
//NFC Detection
112+
NRF.nfcURL("http://espruino.com");
113+
NRF.on('NFCon', function() {
114+
digitalPulse(LED2, 1, 500);//flash on green light
115+
console.log('nfc_field : [1]');
116+
NRF.setAdvertising({
117+
0x183e: [1],
118+
});
119+
});
120+
NRF.on('NFCoff', function() {
121+
digitalPulse(LED2, 1, 200);//flash on green light
122+
console.log('nfc_field : [0]');
123+
NRF.setAdvertising({
124+
0x183e: [0],
125+
});
126+
});

apps/bleeverything/icon.png

20.7 KB
Loading

0 commit comments

Comments
 (0)