• I have managed to run the mqtt example from: espruino.com/Puck.js+Node-RED
    I can receive the advertise and rssi. Now I'm trying to send a MQTT message when the button is pressed.
    Any suggestions, examples please?

  • Hi,

    The simplest way to do it would be to advertise a 'button press count' variable. If you upload the following:

    var pressCount = 0;
    setWatch(function() {
      pressCount++;
      NRF.setAdvertising({
        0xFFFF : [pressCount]
      });
    }, BTN, { edge:"rising", repeat:true, debounce:50 });
    

    And then disconnect from Puck.js, whenever you press the button you'll see a service UUID '0xFFFF' broadcast that has a number in it. Each time the number changes, an MQTT event will get fired, and you can hook on to that in Node-red (by default you'll still get an event fired by EspruinoHub every 60 seconds even it it hasn't changed, but there's a node-red block that'll filter that out by only passing a message when the value changes).

    The nice thing about doing it this way is there's no connection, and also if you walk out of range, press the button and then walk back in range, the event will still fire when Puck.js gets back in range

  • Hello
    Amazing! the button works fine. Thank you very much.
    I'm trying to get some feedback information back to puck, like LED.
    When I send: /ble/write/e7:e0:57:ad:36:a2/nus/nus_tx (with my address obviously) I get en error:

    MQTT>/ble/write/c9:a2:6b:fd:3f:9d/nus/nu­s_tx => "LED1.set();\n"
    Service 6e400001b5a3f393e0a9e50e24dcca9e Characteristic nus_tx
    Error Connecting: Error: Command Disallowed

    Any clue what is wrong please?

  • Have you updated the EspruinoHub code from GitHub recently.

    I have seen this problem before... @dklinkman do you remember what the cause was?

  • Hi
    I have updated all npm packages, git pull EsprinoHub and tryied installed it again. I got error:

    usb@1.2.0 install /root/EspruinoHub/node_modules/usb
    > node-pre-gyp install --fallback-to-build
    
    node-pre-gyp ERR! Tried to download: https://github.com/tessel/node-usb/relea­ses/download/1.2.0/usb_bindings-v1.2.0-n­ode-v47-linux-arm.tar.gz 
    node-pre-gyp ERR! Pre-built binaries not found for usb@1.2.0 and node@5.12.0 (node-v47 ABI) (falling back to source compile with node-gyp) 
    make: Entering directory '/root/EspruinoHub/node_modules/usb/buil­d'
      CC(target) Release/obj.target/libusb/libusb/libusb/­core.o
      CC(target) Release/obj.target/libusb/libusb/libusb/­descriptor.o
      CC(target) Release/obj.target/libusb/libusb/libusb/­hotplug.o
      CC(target) Release/obj.target/libusb/libusb/libusb/­io.o
      CC(target) Release/obj.target/libusb/libusb/libusb/­strerror.o
      CC(target) Release/obj.target/libusb/libusb/libusb/­sync.o
      CC(target) Release/obj.target/libusb/libusb/libusb/­os/poll_posix.o
      CC(target) Release/obj.target/libusb/libusb/libusb/­os/threads_posix.o
      CC(target) Release/obj.target/libusb/libusb/libusb/­os/linux_usbfs.o
      CC(target) Release/obj.target/libusb/libusb/libusb/­os/linux_udev.o
    ../libusb/libusb/os/linux_udev.c:40:21: fatal error: libudev.h: No such file or directory
     [#include](https://forum.espruino.com/se­arch/?q=%23include) <libudev.h>
                         ^
    compilation terminated.
    libusb.target.mk:131: recipe for target 'Release/obj.target/libusb/libusb/libusb­/os/linux_udev.o' failed
    make: *** [Release/obj.target/libusb/libusb/libusb­/os/linux_udev.o] Error 1
    make: Leaving directory '/root/EspruinoHub/node_modules/usb/buil­d'
    gyp ERR! build error 
    gyp ERR! stack Error: `make` failed with exit code: 2
    gyp ERR! stack     at ChildProcess.onExit (/usr/lib/node_modules/npm/node_modules/­node-gyp/lib/build.js:276:23)
    gyp ERR! stack     at emitTwo (events.js:100:13)
    gyp ERR! stack     at ChildProcess.emit (events.js:185:7)
    gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:204:12)
    gyp ERR! System Linux 4.8.4-sunxi
    gyp ERR! command "/usr/bin/nodejs" "/usr/lib/node_modules/npm/node_modules/­node-gyp/bin/node-gyp.js" "build" "--fallback-to-build" "--module=/root/EspruinoHub/node_modules­/usb/src/binding/usb_bindings.node" "--module_name=usb_bindings" "--module_path=/root/EspruinoHub/node_mo­dules/usb/src/binding"
    gyp ERR! cwd /root/EspruinoHub/node_modules/usb
    gyp ERR! node -v v5.12.0
    gyp ERR! node-gyp -v v3.4.0
    gyp ERR! not ok 
    node-pre-gyp ERR! build error 
    node-pre-gyp ERR! stack Error: Failed to execute '/usr/bin/nodejs /usr/lib/node_modules/npm/node_modules/n­ode-gyp/bin/node-gyp.js build --fallback-to-build --module=/root/EspruinoHub/node_modules/­usb/src/binding/usb_bindings.node --module_name=usb_bindings --module_path=/root/EspruinoHub/node_mod­ules/usb/src/binding' (1)
    node-pre-gyp ERR! stack     at ChildProcess.<anonymous> (/root/EspruinoHub/node_modules/usb/node­_modules/node-pre-gyp/lib/util/compile.j­s:83:29)
    node-pre-gyp ERR! stack     at emitTwo (events.js:100:13)
    node-pre-gyp ERR! stack     at ChildProcess.emit (events.js:185:7)
    node-pre-gyp ERR! stack     at maybeClose (internal/child_process.js:850:16)
    node-pre-gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:215:5)
    node-pre-gyp ERR! System Linux 4.8.4-sunxi
    node-pre-gyp ERR! command "/usr/bin/nodejs" "/root/EspruinoHub/node_modules/usb/node­_modules/.bin/node-pre-gyp" "install" "--fallback-to-build"
    node-pre-gyp ERR! cwd /root/EspruinoHub/node_modules/usb
    node-pre-gyp ERR! node -v v5.12.0
    node-pre-gyp ERR! node-pre-gyp -v v0.6.30
    node-pre-gyp ERR! not ok 
    Failed to execute '/usr/bin/nodejs /usr/lib/node_modules/npm/node_modules/n­ode-gyp/bin/node-gyp.js build --fallback-to-build --module=/root/EspruinoHub/node_modules/­usb/src/binding/usb_bindings.node --module_name=usb_bindings --module_path=/root/EspruinoHub/node_mod­ules/usb/src/binding' (1)
    npm WARN optional SKIPPING OPTIONAL DEPENDENCY: xpc-connection@~0.1.4 (node_modules/bleno/node_modules/xpc-con­nection):
    npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for xpc-connection@0.1.4: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"arm"})
    npm WARN optional SKIPPING OPTIONAL DEPENDENCY: xpc-connection@~0.1.4 (node_modules/noble/node_modules/xpc-con­nection):
    npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for xpc-connection@0.1.4: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"arm"})
    npm WARN EspruinoHub@0.0.0 No repository field.
    npm WARN EspruinoHub@0.0.0 license should be a valid SPDX license expression
    npm WARN optional SKIPPING OPTIONAL DEPENDENCY: usb@1.2.0 (node_modules/usb):
    npm WARN optional SKIPPING OPTIONAL DEPENDENCY: usb@1.2.0 install: `node-pre-gyp install --fallback-to-build`
    npm WARN optional SKIPPING OPTIONAL DEPENDENCY: Exit status 1
    
    

    IT looks like the EspruinoHub is not installed properly.
    I use Olimex A20 platform with Armbian.
    Any suggestions please?

  • @Gordon I don't think I have seen that particular error before. But from the output it's odd that the service appears to have been translated to its UUID format but the characteristic has not. Maybe there is a subtle problem with the mqtt publish and/or interpretation by the hub.

  • @dklinkman thanks - that's a good spot!

    I don't know why it would fail though - looking at the code, it's identical to get the characteristic and service, so the only thing I can think is perhaps there was a character at the end of the topic - a stray space maybe?

    (and yes - I just looked at the message that's posted, and @Grzegorz it's because you put "/ble/write/e7:e0:57:ad:36:a2/nus/nus_tx­ " as the topic and not "/ble/write/e7:e0:57:ad:36:a2/nus/nus_tx­")

    @Grzegorz I'm not sure how to help with this most recent problem. You had this working before, so presumably you have more of a chance of working out what you did then than I do now :)

  • I have just mended mqtt subscription to "/ble/write/c9:a2:6b:fd:3f:9d/nus/nus_tx­"
    Still receiving error unfortunately:
    MQTT>/ble/write/c9:a2:6b:fd:3f:9d/nus/nu­s_tx => "LED1.set();\n"
    Service 6e400001b5a3f393e0a9e50e24dcca9e
    Characteristic 6e400002b5a3f393e0a9e50e24dcca9e
    Error Connecting: Error: Command Disallowed

    I can receive the advertise and rssi thou.

  • Ok, thanks for letting me know.

    @Eric I know here you were having "Command Disallowed" errors - did you ever find out why that was happening? It was only on a Raspberry Pi 2 with external BLE dongle?

  • @Gordon

    I found this in one of my old posts. This is the specific syntax I had to use to get my /ble/write to work properly from the linux command line. It has to do with how the linefeed character gets passed through if I recall. But I don't know if this relates at all to the problem @Grzegorz is having.

    mosquitto_pub -t /ble/write/c7:6e:71:dd:ee:ff/nus/nus_tx -m $'digitalPulse(LED3, 1, 200);\n'
    
  • Ahh I just Googled the message and found this:

    Some BLE adapters like Sena UD-100 (Cambridge Silicon Radio, Ltd Bluetooth Dongle (HCI mode)) cannot connect to a peripheral while they are scanning. You will get the following message when trying to connect :
    Error: Command disallowed
    You need to stop scanning before trying to connect in order to solve this issue.

    So it looks like it's something funny with your Bluetooth dongle

    I've just updated EspruinoHub - please could you git pull it and see if that helps things?

  • No luck, still the same issue. I have got Plugable BLE adapter
    I can get an other one, could you recommend some which would work please.

  • I use the Plugable USB dongle on my Windows 7 Pro machine to talk to the Puck with good results. On my RPi2 hub I used this one from ameriDroid (via Amazon) with good results prior to my switching to the RPi3. The RPi3 works well also.

    https://www.amazon.com/gp/product/B01LEX­8Z6W/ref=oh_aui_search_detailpage?ie=UTF­8&psc=1

    I got that one since I really wanted to use my Odroid XU4 as my Espruino Hub. And that is the official BT/BLE dongle for it.

  • I could not find the ameriDroid on UK market, I have bought an other one different BT dongle and the issue persist. Could be the error platform depended? I don't use R pi. Is the error caused definitely by BT dongle?
    Any suggestions what can I test/check/try to fix it please?

  • No experience with Olimex A20 unfortunately. I did easily get the hub to work on an Odroid, however, so I know it can work on other platforms. Though once I tried it on the RPi 3 I just left it there.

  • I cant send a write command to the puck:

    <Config> Config loaded
    <MQTT> Connecting...
    <HTTP> Server is listening on 1888
    <MQTT> Connected
    <HTTPProxy> Bleno State poweredOn
    <HTTPProxy> Bleno.startAdvertising Success
    <HTTPProxy> Bleno.setServices Success
    <Discover> Starting scan...
    <Discover> Scanning started.
    <MQTT> /ble/write/e9:05:8c:a7:64:ab/nus/nus_tx => "digitalPulse(LED3, 1, 200);\n"
    <Discover> Scanning stopped.
    <Connect> e9:05:8c:a7:64:ab: Connecting...
    <Connect> e9:05:8c:a7:64:ab: Error Connecting: Error: Command Disallowed (0xc)
    <Discover> Scanning started.
    

    I only found this information:
    https://github.com/sandeepmistry/noble/i­ssues/658

    I had a look into the EspruinoHub code and i saw that the scanning is interruptet if a write message needs to be sent.

    /* Write to the given device */
    exports.write = function(device, service, characteristic, data) {
      if (isBusy) {
        queue.push(function() { exports.write(device,service,characteris­tic,data); });
        return;
      }
      isBusy = true;
      getConnectedDevice(device, function(err, connection) {
        if (err) return setNotBusy();
    
        connection.getCharacteristic(util.uuid2n­oble(service), 
                                     util.uuid2noble(characteristic),
         function(err,char) {
           if (err) return setNotBusy();;
           writeToCharacteristic(char, util.obj2buf(data), function() {
               log(connection.name+": Written.");
               setNotBusy();
             });
         });
      });
    };
    

    and

    function serviceQueue() {
      if (!queue.length) {
        if (connections.length==0) // no open connections
          discovery.restartScan();
        return;
      }
      if (connections.length < MAX_CONNECTIONS) {
        var job = queue.shift();
        discovery.stopScan();
        setTimeout(job, 1000);  
      }
    }
    
    function setNotBusy() {
      isBusy = false;
      serviceQueue();
    }
    

    Can it still be related to the dongle i use?

  • The dongel has Bluetooth 4.0 and not 4.1.
    https://github.com/sandeepmistry/noble#b­leno-compatibility
    With a Raspi 3.0 its working.

  • Great - thanks for the update! Which Bluetooth Dongle was it out of interest?

  • Its a

    1. ID 0a5c:21e8 Broadcom Corp. BCM20702A0 Bluetooth 4.0
    2. ID 050d:065a Belkin Components F8T065BF Mini Bluetooth 4.0 Adapter

    Do you know why that happends? Doesn't the code stop scanning before sending?

  • Which one of the two adaptors was causing problems? I was pretty sure the BCM20702A0 would be ok.

    Honestly, I don't know why it happens. As you say, it seems the code does stop scanning - however perhaps there needs to be some time delay between stopping scanning and trying to connect.

  • With both of them. I already increased the

    setTimeout(job, 1000);
    

    to 2000 without success.

    I will try this command later:
    https://github.com/sandeepmistry/node-bl­uetooth-hci-socket#linux-1

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

Puck js, how to send MQTT message with button pressed?

Posted by Avatar for Grzegorz @Grzegorz

Actions