Created
March 4, 2018 18:19
-
-
Save a-a/1a835801de1a5cb01861372d65772449 to your computer and use it in GitHub Desktop.
Example code for getting Eddystone, iBeacon, or both advertising on a Puck.JS. Turns on/off with the button, and displays battery level too :)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Eddystone and iBeacon example code for Puck.js 1.92 or above | |
// How to use: paste into the web IDE, adjust to your needs, and upload to your puck. | |
// Setup which type of advertising you wish to make, Eddystone, iBeacon, or both. Uncomment ONLY ONE. | |
function doAdvertising() { | |
doEddystone(); //By default, we advertise eddystone only. | |
//doiBeacon(); | |
//advertiseBoth(); | |
} | |
// This function advertises Eddystone ONLY | |
// Please read https://www.espruino.com/Puck.js+Eddystone for guidelines on correct URLs. | |
function doEddystone(){ | |
require("ble_eddystone").advertise("goo.gl/B3J0Oc"); //set url here | |
} | |
// This function advertising iBeacon ONLY | |
// Please read https://www.espruino.com/Puck.js+iBeacon for further details. | |
function doiBeacon(){ | |
require("ble_ibeacon").advertise({ | |
uuid : [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], // ibeacon uuid | |
major : 0x0001, // optional | |
minor : 0x0001, // optional | |
rssi : -59 // optional RSSI at 1 meter distance in dBm | |
}); | |
} | |
// This function advertises both Eddystone and iBeacon | |
function advertiseBoth() { //For advertising both Eddystone and iBeacon at the same time! | |
NRF.setAdvertising([ | |
require("ble_ibeacon").get("goo.gl/B3J0Oc"), //set url here | |
require("ble_eddystone").get({ | |
uuid : [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], // ibeacon uuid | |
major : 0x0001, // optional | |
minor : 0x0001, // optional | |
rssi : -59 // optional RSSI at 1 meter distance in dBm | |
})], {interval:100}); | |
} | |
// Upon button press, we do something. All other times, we sleep, This is an interrupt. | |
// Note in sleep, NRF is awake and advertising, but JS is not executing, until the button interrupts sleep. | |
// In sleep and not advertising, 12uA power is used. | |
// In sleep and advertising at 375ms (0dBm), 25uA power is used. | |
setWatch(function() { | |
digitalPulse(LED1,1,100); | |
digitalPulse(LED3,1,100); // Blink Purple when the button is pressed | |
setTimeout(function () { // Then, show our battery status as a red through to green for low to high. | |
batteryLED(); | |
}, 500); | |
setTimeout(function () { | |
toggleAdvertising(); // Finally, toggle eddystone advertising On/Off - blue blink for adv on, red for adv off | |
}, 500); | |
}, BTN, {edge:"rising", debounce:50, repeat:true}); | |
function blinkRed(time) { | |
digitalPulse(LED1,1,time); | |
} | |
function blinkGreen(time) { | |
digitalPulse(LED2,1,time); | |
} | |
function blinkBlue(time) { | |
digitalPulse(LED3,1,time); | |
} | |
function blinkYellow(time) { // It seems to multiplex these - even though espruino is single-threaded | |
digitalPulse(LED1,1,time); | |
digitalPulse(LED2,1,time); | |
} | |
// This function blinks the LEDs depending on battery status. | |
// Puck.getBatteryPercentage is the nicer way to check your battery status | |
// For the curious: NRF.getBattery() will give you raw voltage read from the NRF IC. | |
function batteryLED(){ | |
blvl=Puck.getBatteryPercentage(); | |
if (blvl >= 75) { // If battery is above 75%, blink green | |
blinkGreen(100); | |
} | |
if (blvl < 75 && blvl > 25 ) { // If battery is lower than 75%, blink yellow | |
blinkYellow(100); | |
} | |
if (blvl < 25) { // If battery is lower than 25%, blink red! | |
blinkRed(100); | |
} | |
} | |
//This function toggles whether advertising is on or off. | |
var adv = false; | |
function toggleAdvertising() { | |
adv = !adv; | |
if ( adv === true ) { | |
// Enable Advertising, blink blue LED to reflect this. | |
setTimeout(function () { | |
blinkBlue(500); | |
}, 400); | |
doAdvertising(); //Call doAdvertising function at top | |
} else { | |
//Disable Advertising, blink red LED to reflect this. | |
setTimeout(function () { | |
blinkRed(500); | |
}, 400); | |
NRF.setAdvertising({}); //Cancel all advertising. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment