Puck.js with Node-RED on Raspberry Pi

Posted on
  • I've followed the quickstart example on my Raspberry Pi v3 and can easily recognise my Puck along with its rssi value. According to the documentation of the EspruinoHub there should be a topic /ble/advertise/DEVICE/SERVICE, but I don't get to see any information. Do I have to write code on the Puck or modify the EspruinoHub to retrieve any sensor data, or should these come automatically using defaults?

  • This comment http://forum.espruino.com/comments/13381­021/ may answer some of your questions. If you point your browser to Node-RED on your RPi3, you can set up an MQTT input node that will read those topics and take whatever actions you want. For example, this is a NR flow you can import that simply reports MQTT messages to the debug tab.

    [{"id":"76f88a3c.99fe04","type":"tab","l­abel":"Flow 2"},{"id":"6892e216.8f6d2c","type":"debu­g","z":"76f88a3c.99fe04","name":"","acti­ve":true,"console":"false","complete":"p­ayload","x":340,"y":60,"wires":[]},{"id"­:"503cfb7e.f411c4","type":"mqtt in","z":"76f88a3c.99fe04","name":"","top­ic":"#","qos":"2","broker":"98edcbd7.d02­a98","x":110,"y":60,"wires":[["6892e216.­8f6d2c"]]},{"id":"98edcbd7.d02a98","type­":"mqtt-broker","z":"","broker":"localho­st","port":"1883","clientid":"","usetls"­:false,"compatmode":true,"keepalive":"60­","cleansession":true,"willTopic":"","wi­llQos":"0","willPayload":"","birthTopic"­:"","birthQos":"0","birthPayload":""}]
    

    A more sophisticated example might look for the temp topic advertising device temperature and update a gauge node that would be dsiplayed on the Node-RED UI

  • I think that's exactly what I was after. I was confused whether I would have to write some code in NRF.setAdvertising or whether the out-of-the-box behaviour was sending a summary of all sensors.

  • Retrieving sensor data works flawlessly.

    d1:46:17:ce:38:85 - Puck.JS WZ (RSSI -59)
      1809 => {"temp":19}
      180f => {"battery":100}
    de:87:72:1f:9c:41 - Puck.JS BK (RSSI -73)
      1809 => {"temp":8}
      180f => {"battery":100}
    

    However, transmitting commands always fails:

    MQTT>/ble/write/d1:46:17:ce:38:85/nus/nu­s_tx => "digitalPulse(LED1, 1, 100);\n"
    Service         6e400001b5a3f393e0a9e50e24dcca9e
    Characteristic  6e400002b5a3f393e0a9e50e24dcca9e
    <Connect> Error Connecting
    

    Does any one know what could cause this issue?

  • Are you sure you disconnected the IDE from the puck before your attempt? I can't get mine to work at all but it's a different set of errors.

    Another problem is I believe your string is too long. I believe 20 characters is the limit.

    You could type this function into your puck (remember to disconnect after):

    function x() { digitalPulse(LED1, 1, 100); }
    

    and then change your mqtt message to "x();\n" just to try it out

  • here's a short bash script that will portion the message into multiple messages. i actually can't test this properly since my hub is messed up but hopefully you get the idea.

    #!/bin/bash
    
    if [[ -z ${1} ]]; then
      echo No message!
      exit -1
    fi
    
    echo Publishing message: $1
    msg=$1
    
    while [[ ! -z ${msg} ]]; do
      data=${msg:0:20}
      msg=${msg:20}
      mosquitto_pub -t /ble/write/c7:6e:71:dd:ee:ff/nus/nus_tx -m "$data"
      sleep 1
    done
    

    for example (assuming the script above is named 'script'):

    ./script "digitalPulse(LED1, 1, 100);\\n"
    
  • I've somehow missed the 20 character limit on commands. That could be the source of my issue. I'll try it as soon as possible.

    @dklinkman you mentioned various other errors. I initially had different errors also. Then I switched to v90.12 (http://forum.espruino.com/conversations/­297615/)

  • @AntiCat I've got v90.12 also but I'm still having problems. There's something going on inside EspruinoHub with a function within noble called discoverAllServicesAndCharacteristics().­ It should return very quickly with arrays of services and characteristics, but most of the time it doesn't and the connect process times out after 4 seconds. At the puck end I see a connection followed by an immediate disconnect. It shouldn't disconnect and that's probably why discoverAllServicesAndCharacteristics() doesn't pass back any data. These are the messages:

    <Connect> Timed out getting services. Disconnecting.
    <Connect> Disconnect error: TypeError: value is out of bounds
    

    The second error is because the timeout is trying to disconnect but the device is already disconnected.

    I wrote my own javascript that uses noble and runs at the command line to do similar and I get the same results. Oddly if I do an explicit connect followed only by an explicit disconnect, all of the above works but only one time. It's maddening. In my javascript I changed the logic to do separate discover services and discover characteristics and now that part is working better. I also tweaked the logic to allow writing up to 60 characters vs 20. I might try similar changes to the EspruinoHub logic and see if that helps at all.

  • I have a fork of the EspruinoHub that fixes the problems I have been experiencing. I think this is still related to this thread since mqtt is integral to node-red and being able to initiate and respond to events.

    https://github.com/dklinkman/EspruinoHub­

    I had trouble connecting to the puck reliably, initially, and I was able to figure out over time that this was related to something in noble. But short of it is that I changed the logic in EspruinoHub and it's a lot more reliable now. At least for me.

    I also added logic so the mqtt message text sent to the puck was no longer limited to just 20 bytes.

    So something like this works now on the RPi:

    mosquitto_pub -t /ble/write/c7:6e:71:dd:ee:ff/nus/nus_tx -m $'digitalPulse(LED3, 1, 200);\n'
    

    even while the text is > 20 chars. Note also the RPi /bin/bash $'...' syntax. Required.

    Reading a service now works. There was a bug causing a crash. For example the following now reads the puck name:

    mosquitto_pub -t /ble/read/c7:6e:71:dd:ee:ff/1800/2A00 -n
    

    and outputs the result when appropriately subscribed to mosquitto_sub -v -t /ble/data/#

    /ble/data/c7:6e:71:dd:ee:ff/1800/2a00 Puck.js eeff
    

    I also fixed a problem where the device discovery would stop after an mqtt event resulting in a watchdog timeout and restart but only by the shell script. Now the discovery restart happens more inline.

  • @dklinkman Thank you for publishing your changes. Unfortunately, it has no effect on my issue. Since my error Error Connecting appears almost instantly I assume some issues with privileges on the RPi.

  • That's unfortunate. Is the output the same as your original post above? All I can say is it's working great here. RPi3, fresh install of latest OS, updated and upgraded all packages, followed the instructions for building the hub. Of course it didn't work for me originally but since I modified the hub code it's fine.

  • I bought a RPi3 today. Identical software (same SDCard) as earlier works! So the issue must be related to my RPi2+BLE dongle.

  • Good news!

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

Puck.js with Node-RED on Raspberry Pi

Posted by Avatar for badryan @badryan

Actions