node package manager

puck-mqtt

puck-mqtt

a hacky BLE & MQTT forwarder.

tl;dr You should use EspruinoHub.

The difference with this:

  • subscribe & publish arbitrary channels on the puck itself
  • limited number of pucks can be connected at one time (depends on OS, maybe 6)
  • likely to be disastrous for battery life

Running

# connect any pucks found to test.mosquitto.org
puck-mqtt
 
# connect to your own server
puck-mqtt -h mqtt://test.example.org
 
# only connect "Puck.js 1f10" and "Puck.js bf82"
puck-mqtt -p af10 -p bf82
 
# or, using longer hash (note, no dashes)
puck-mqtt -p 7597340abcdf1097848fd39ecd6291cb12

Puck.js code

// a list of topics to subscribe to 
const MQTT_subs = [
  '/foo/bar',
  '/baz/foo',
  '/fez/#'
];
 
// helper function for publishing messages 
function MQTT_publish(topic, message) {
  console.log("\n<~" + btoa(topic + ' ' + message) + "~>\n");
}
 
// (implement this) - a handler for incoming messages 
function MQTT_handle(topic, message) {
  console.log("got a message!", message);
}
 
 
// publish events on button press 
setWatch(function() {
  MQTT_publish('/puck/btn', 'pressed');
}, BTN, { repeat:true, edge:"rising", debounce:50 });

How

This script creates a BLE UART connection to the puck and listens out for messages of a particular format <~BASE64~>.

Topic subscriptions are read on connect by running:

;(this.MQTT_subs||[]).forEach(s => console.log('\n<~' + btoa(s) + '~>\n'));

Message handlers are called by injecting:

;((fn, a, b)=>{ if(fn) fn(atob(a), atob(b)) })
(this.MQTT_handle, 'BASE64_ENCODED_TOPIC', 'BASE64_ENCODED_MESSAGE');

Each puck is connected to an individual mqtt connection.

todo

  • look at reconnections
  • check for handler & subs, display warning if not found
  • handle case where > 6 pucks
  • live ui
    • connection states
    • battery levels

Resources

A lot of the connection logic comes via: