Espruino + Bluetooth Slack Bookmarklet!

Posted on
  • Ever wanted to control your Espruinos from Slack? Now you can :)

    • In Chrome, open 'Bookmrk manager'
    • Go to 'Bookmarks bar', right-click and add bookmark
    • Type 'slack-espruino' as the name, then paste the code below into the URL and save:

      javascript:(function() {
      var knownMessages = [];
      
      function write(str) {
      var u = new Uint8Array(str.length);
      for (var i=0;i<str.length;i++)
        u[i] = str.charCodeAt(i);
      console.log("Writing ",JSON.stringify(str));
      bluetoothTX.writeValue(u.buffer).then(function() {
        console.log("Written!");
      })
      }
      function poll() {
      var m = document.querySelectorAll(".c-message__message_blocks");
      for (var i=0;i<m.length;i++) {
        if (knownMessages.includes(m[i])) continue;
        knownMessages.push(m[i]);
        var msg = m[i].innerText.trim();
        console.log("New message:",msg);
        if (msg.startsWith("Espruino:"))
          write(msg.substr(9).trim()+"\n");      
      }
      }
      
      var bluetoothDevice, bluetoothServer, bluetoothTX;
      navigator.bluetooth.requestDevice({ filters: [{services: ["6e400001-b5a3-f393-e0a9-e50e24dcca9e"]},{namePrefix: "Puck"}]}).then(device => {
      console.log('Connecting to GATT Server...');
      bluetoothDevice = device;
      return device.gatt.connect();
      }).then(function(server) {
      console.log("Connected");
      bluetoothServer = server;
      return server.getPrimaryService("6e400001-b5a3-f393-e0a9-e50e24dcca9e");
      }).then(function(service) {
      return service.getCharacteristic("6e400002-b5a3-f393-e0a9-e50e24dcca9e");
      }).then(function(char) {
      bluetoothTX = char;
      console.log("Completed!");
      setInterval(poll,1000);
      });
      })();
      
    • Now open Slack for the web browser. This can be a pain on Android if you have the app installed, requiring you to view the sign-in page 'as desktop browser'

    • Go to the specific chat you care about

    • Open the slack-espruino bookmark you just made (or copy/paste the code above into the address bar)

    • Choose an Espruino device in the bluetooth popup that appears

    • Any message that anyone writes in the conversation that starts with Espruino: (has to be the same capitalisation) will be sent to Espruino. For example: Espruino:LED.set() will turn the LED on!

  • That is cool. I see you got inspired by @benjaminbenben micro chat post.

  • That's cool stuff.

    Out of curiosity, would that also work with other types of chat or websites, or is there something Slack-specific in that code?

  • would that also work with other types of chat or websites

    Yes, it'd work with pretty much anything! About the only requirement is the website uses HTTPS (not HTTP)

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Espruino + Bluetooth Slack Bookmarklet!

Posted by Avatar for Gordon @Gordon

Actions