Puck.js is a fantastic Open Source JavaScript Beacon powered by Espruino, a JavaScript interpreter that has featured in many of my hardware projects. The tiny battery powered microcontroller has a multitude of sensors and features, most notably Bluetooth that allows it to become an iBeacon or Eddystone Beacon.
Recently I turned my Puck.js into a personal Beacon that can be configured to a multitude of environments. The first version was simple & worked well but having been hacked together in a just a couple of minutes there were many things that I thought could have been done better. Happy with the outcome of these tweaks I thought I’d share my code and findings.
Video Demo
Code
Espruino & JavaScript makes this all work is remarkably easy to code. Lets break down some of the components before moving onto the final code:
Turn on an LED
The constants LED1, LED2 & LED3 are setup for the Red, Green & Blue LEDs respectively. To turn on the Red LED simply run:
Broadcast a URL
Advertising a URL with Eddystone is easy. Simply required the module and then call advertise with your URL.
Note: Your URL should be no more than 17 characters long but you can use a URL shortener.
To stop advertising your URL just run:
Detecting button presses
Full code
Putting it all together we end up with the following source:
Analytics
I’m keen to see how this experiment goes and how effective it is. To measure this I’ll be using Google URL Shortener to provide me with basic analytics as well as shorten any URLs that I want to broadcast. I plan on having the beacon with me when I give talks and changing the URL to point to my slides and feedback questionnaires.
Conclusion
I’ve really enjoyed the approachability of coding with Puck.js and Espruino. Having used other dedicated iBeacons in the past as well as Estimote beacons the Puck.js lends itself far better to being a versatile & programmable and packs much more functionality that I’m keen to explore further.