NRF.setServices Datatypes

Posted on
  • Hi,

    i try to send some IR Codes via nodered + mqtt + puck to turn on my soundbar on chromecast activity.

    msg.payload =  [ 8.9111328125, 4.5166015625, 0.48828125, 0.6103515625, 0.48828125, 1.77001953125, 0.48828125, 0.6103515625, 0.48828125, 1.77001953125, 0.48828125, 1.77001953125, 0.45776367187, 1.77001953125, 0.48828125, 0.6103515625, 0.48828125, 1.77001953125, 0.45776367187, 0.64086914062, 0.48828125, 0.64086914062, 0.48828125, 0.6103515625, 0.48828125, 0.64086914062, 0.48828125, 0.64086914062, 0.48828125, 1.73950195312, 0.48828125, 0.64086914062, 0.48828125, 1.73950195312, 0.48828125, 0.64086914062, 0.48828125, 0.6103515625, 0.48828125, 1.77001953125, 0.48828125, 1.77001953125, 0.45776367187, 0.64086914062, 0.48828125, 0.6103515625, 0.48828125, 1.77001953125, 0.48828125, 0.6103515625, 0.54931640625, 1.708984375, 0.48828125, 1.77001953125, 0.45776367187, 0.64086914062, 0.48828125, 0.6103515625, 0.48828125 ];
    

    Topic:

    /ble/write/e9:05:8c:a7:64:ab/BCDE/ABCE
    

    The Problem is that the EspruinoHub crashes after it receives the message:

    <MQTT> /ble/write/e9:05:8c:a7:64:ab/BCDE/ABCE => "[8.9111328125,4.5166015625,0.48828125,0­.6103515625,0.48828125,1.77001953125,0.4­8828125,0.6103515625,0.48828125,1.770019­53125,0.48828125,1.77001953125,0.4577636­7187,1.77001953125,0.48828125,0.61035156­25,0.48828125,1.77001953125,0.4577636718­7,0.64086914062,0.48828125,0.64086914062­,0.48828125,0.6103515625,0.48828125,0.64­086914062,0.48828125,0.64086914062,0.488­28125,1.73950195312,0.48828125,0.6408691­4062,0.48828125,1.73950195312,0.48828125­,0.64086914062,0.48828125,0.6103515625,0­.48828125,1.77001953125,0.48828125,1.770­01953125,0.45776367187,0.64086914062,0.4­8828125,0.6103515625,0.48828125,1.770019­53125,0.48828125,0.6103515625,0.54931640­625,1.708984375,0.48828125,1.77001953125­,0.45776367187,0.64086914062,0.48828125,­0.6103515625,0.48828125]"
    <Discover> Scanning stopped.
    <Connect> e9:05:8c:a7:64:ab: Connecting...
    <Connect> Connected.
    <Connect> e9:05:8c:a7:64:ab: Getting Service...
    <Connect> e9:05:8c:a7:64:ab: found service: bcde
    <Connect> e9:05:8c:a7:64:ab: found characteristic: abce
    /home/pi/EspruinoHub/lib/util.js:36
        buf.writeUInt8(o.charCodeAt(i), i);
                         ^
    
    TypeError: o.charCodeAt is not a function
        at Object.exports.obj2buf (/home/pi/EspruinoHub/lib/util.js:36:22)­
        at /home/pi/EspruinoHub/lib/connect.js:193:­41
        at /home/pi/EspruinoHub/lib/connect.js:63:9­
        at Service.<anonymous> (/home/pi/EspruinoHub/node_modules/noble­/lib/service.js:53:7)
        at Object.onceWrapper (events.js:315:30)
        at emitOne (events.js:116:13)
        at Service.emit (events.js:211:7)
        at Noble.onCharacteristicsDiscover (/home/pi/EspruinoHub/node_modules/noble­/lib/noble.js:261:13)
        at emitThree (events.js:136:13)
        at NobleBindings.emit (events.js:217:7)
    

    If i send the hole command as a string to /ble/write/e9:05:8c:a7:64:ab/nus/nus_tx it works.

    So i'm questioning what are the limitations for self defined Services?
    Can i send the IR Codes that way?

    How "looks" the evt.data Object of the onWrite function?
    How can one read that console.log as long as the puck is connected to the IDE?

    onWrite : function(evt) { // optional
            console.log("Got ", evt.data);
          }
    

    Thank you very much.

  • EspruinoHub definitely shouldn't fail that way, and I have just fixed it - but I think you'd still hit issues either way.

    A service can only hold a maximum of 20 bytes in normal Bluetooth LE, so the write would fail. Your message seems to be 60ish elements long, or lots of characters.

    If you want to use characteristics, I'd:

    • Convert your array of floats into an array of bytes. So multiply all the numbers by 10 and round them
    • Send an array of 20 items at a time - same way you're doing it now as a string representing an array (but with the updated EspruinoHub). It'll be recognized as valid JSON and then parsed and sent as an array.
    • Send a 'special' message to tell the Puck to take what you have and output it. Maybe that could just be 255, or a totally blank message.

    nus_tx will send the command as a string and automatically splits it every 20 bytes, so you don't hit that issue when using it.

    How "looks" the evt.data Object of the onWrite function?

    It's an ArrayBuffer

    How can one read that console.log as long as the puck is connected to the IDE?

    You mean if you're not connected to the IDE?

    If you're using EspruinoHub you can send a MQTT message to /ble/notify/c7:f9:36:dd:b0:ca/nus/nus_rx­ every few seconds, and then it'll send back the contents of the console via /ble/data/c7:f9:36:dd:b0:ca/nus/nus_rx.

    You can also connect via serial as well, which is easier if you have the USB-TTL converter to do it. Failing that you'd have to save evt.data to a variable and view it later

  • just an info how far i am. no help required.

    i think the puck crashes. i have to attach a serial console.

    Puck:

    var IrData = [];
    
    function setService(){
      NRF.setServices({
        0xBCDE : {
          
          0xFF01 : { /*clear buffer*/
            writable : true,
            onWrite : function(evt) {
              //digitalWrite([LED3,LED2,LED1], evt.data[0]);
              if (evt.data[0]==255){
                IrData = [];
                LED3.set();
                setTimeout(()=>{LED3.reset();},1000);
              }
            }
          },
          0xFF02 : { /*fill buffer*/
            writable : true,
            onWrite : function(evt) {
              var array1 = new Uint8Array(evt.data);
              array1.map(val=>{
                IrData.push(val/10);
              });
            }
          },
          0xFF03 : { /*exec buffer*/
            writable : true,
            onWrite : function(evt) {
              if (evt.data[0]==255){
                LED2.set();
                setTimeout(()=>{LED2.reset();},1000);
              }
              //Puck.IR(IrData);
            }
          },
          0xFF04 : { /*reserved*/
            writable : true,
            onWrite : function(evt) {
              console.log( new Uint8Array(evt.data));
            }
          }
        }
      });
    }
    
    setService();
    
    function advertise()
    {
      var voltage = Math.round( NRF.getBattery() * 1000)/1000 ;
      voltage = voltage.toString();
    
      var data = {
        0x180F : [Puck.getBatteryPercentage()],
        0x1801 : [ voltage.split('') ],
        0x1809 : [Math.round(E.getTemperature()+1 /*device specific offset*/)]
      };
    
      var options = {
        name: "Puck2",
        interval: 1000
      };
    
      NRF.setAdvertising(data,options);
    }
    advertise();
    

    Node Red send IR Payload in chunks

     msg.payload = [ 8.9111328125, 4.5166015625, 0.48828125, 0.6103515625, 0.48828125, 1.77001953125, 0.48828125, 0.6103515625, 0.48828125, 1.77001953125, 0.48828125, 1.77001953125, 0.45776367187, 1.77001953125, 0.48828125, 0.6103515625, 0.48828125, 1.77001953125, 0.45776367187, 0.64086914062, 0.48828125, 0.64086914062, 0.48828125, 0.6103515625, 0.48828125, 0.64086914062, 0.48828125, 0.64086914062, 0.48828125, 1.73950195312, 0.48828125, 0.64086914062, 0.48828125, 1.73950195312, 0.48828125, 0.64086914062, 0.48828125, 0.6103515625, 0.48828125, 1.77001953125, 0.48828125, 1.77001953125, 0.45776367187, 0.64086914062, 0.48828125, 0.6103515625, 0.48828125, 1.77001953125, 0.48828125, 0.6103515625, 0.54931640625, 1.708984375, 0.48828125, 1.77001953125, 0.45776367187, 0.64086914062, 0.48828125, 0.6103515625, 0.48828125, 1.77001953125, 0.48828125, 1.77001953125, 0.45776367187, 0.64086914062, 0.48828125, 1.77001953125, 0.45776367187, 41.015625, 8.94165039062, 4.48608398437, 0.51879882812, 0.57983398437, 0.48828125, 1.77001953125, 0.51879882812, 0.57983398437, 0.54931640625, 1.708984375, 0.51879882812, 1.73950195312, 0.45776367187, 1.77001953125, 0.51879882812, 0.57983398437, 0.54931640625, 1.67846679687, 0.48828125, 0.64086914062, 0.54931640625, 0.57983398437, 0.51879882812, 0.57983398437, 0.54931640625, 0.57983398437, 0.54931640625, 0.57983398437, 0.51879882812, 1.708984375, 0.54931640625, 0.57983398437, 0.48828125, 1.73950195312 ];
    
    var data = msg.payload;
    
    //return if no array
    if (!Array.isArray(data)){
        msg.payload = [];
        return msg;
    }
    
    //float to byte
    var byteArray = data.map(i=> Math.min(Math.max( Math.round(i*10), 1), 255) );
    
    var len = byteArray.length;
    var steps = Math.floor(len / 20);
    var outArrays = [];
    var i = 0;
    while (i < len) {
        outArrays.push(byteArray.slice(i, i += 20));
    }
    
    outArrays.map(data=>{
        msg.payload = data;
        node.send(msg);
    })
    
    

    EspruinoHub
    commit 97871d43d66edaefd47fb4f577008aedd82b1035­
    Author: Gordon Williams gw@pur3.co.uk
    Date: Tue Feb 20 10:17:05 2018 +0000

    <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/BCDE/FF01 => "255"
    <Discover> Scanning stopped.
    <Connect> e9:05:8c:a7:64:ab: Connecting...
    <Connect> Connected.
    <Connect> e9:05:8c:a7:64:ab: Getting Service...
    <MQTT> /ble/write/e9:05:8c:a7:64:ab/BCDE/FF01 => "255"
    <Connect> e9:05:8c:a7:64:ab: found service: bcde
    <Connect> e9:05:8c:a7:64:ab: found characteristic: ff01
    <Connect> e9:05:8c:a7:64:ab: Written.
    <Connect> e9:05:8c:a7:64:ab: Written.
    <MQTT> /ble/write/e9:05:8c:a7:64:ab/BCDE/FF02 => "[89,45,5,6,5,18,5,6,5,18,5,18,5,18,5,6,­5,18,5,6]"
    <Connect> e9:05:8c:a7:64:ab: Getting Service...
    <MQTT> /ble/write/e9:05:8c:a7:64:ab/BCDE/FF02 => "[5,6,5,6,5,6,5,6,5,17,5,6,5,17,5,6,5,6,­5,18]"
    <MQTT> /ble/write/e9:05:8c:a7:64:ab/BCDE/FF02 => "[5,18,5,6,5,6,5,18,5,6,5,17,5,18,5,6,5,­6,5,18]"
    <MQTT> /ble/write/e9:05:8c:a7:64:ab/BCDE/FF02 => "[5,18,5,6,5,18,5,89,45,5,6,5,18,5,6,5,1­7,5,17,5]"
    <MQTT> /ble/write/e9:05:8c:a7:64:ab/BCDE/FF02 => "[18,5,6,5,17,5,6,5,6,5,6,5,6,5,6,5,17,5­,6,5]"
    <MQTT> /ble/write/e9:05:8c:a7:64:ab/BCDE/FF02 => "[17,5,6,5,6,5,18,5,18,5,6,5,6,5,18,5,6,­5,18,5]"
    <MQTT> /ble/write/e9:05:8c:a7:64:ab/BCDE/FF02 => "[17,5,6,5,6,5,18,5,18,5,6,5,18,5,255,89­,45,5,6,5]"
    <MQTT> /ble/write/e9:05:8c:a7:64:ab/BCDE/FF02 => "[18,5,6,5,18,5,18,5,18,5,6,5,18,5,6,5,6­,5,6,5]"
    <MQTT> /ble/write/e9:05:8c:a7:64:ab/BCDE/FF02 => "[6,5,6,5,18,5,6,5,18,5,6,5,6,5,17,5,18,­5,6,5]"
    <MQTT> /ble/write/e9:05:8c:a7:64:ab/BCDE/FF02 => "[6,5,18,5,6,5,18,5,18,5,6,5,6,5,18,5,18­,5,6,5]"
    <MQTT> /ble/write/e9:05:8c:a7:64:ab/BCDE/FF02 => "[18,5,3,6,1,42]"
    <Connect> e9:05:8c:a7:64:ab: found characteristic: ff02
    <MQTT> /ble/write/e9:05:8c:a7:64:ab/BCDE/FF03 => "255"
    <Connect> e9:05:8c:a7:64:ab: Disconnecting due to lack of use (after 10 secs)
    <Connect> e9:05:8c:a7:64:ab: Disconnecting.
    <Connect> e9:05:8c:a7:64:ab: Disconnected
    
    Wed Feb 21 2018 22:20:01 GMT+0100 (CET)
    
    45:68:9e:41:77:b4 - ? (RSSI -58)
      fe9f => {"type":"Buffer","data":[0,0,0,0,0,0,0,0­,0,0,0,0,0,0,0,0,0,0,0,0]}
    69:23:86:23:8f:47 - ? (RSSI -62)
      fe9f => {"type":"Buffer","data":[0,0,0,0,0,0,0,0­,0,0,0,0,0,0,0,0,0,0,0,0]}
    6e:d0:2f:84:f7:06 - ? (RSSI -87)
      fe9f => {"type":"Buffer","data":[0,0,0,0,0,0,0,0­,0,0,0,0,0,0,0,0,0,0,0,0]}
    6f:c6:f2:d8:2a:93 - ? (RSSI -71)
      fe9f => {"type":"Buffer","data":[0,0,0,0,0,0,0,0­,0,0,0,0,0,0,0,0,0,0,0,0]}
    googlB - ? (RSSI -83)
      fe9f => {"type":"Buffer","data":[0,0,0,0,0,0,0,0­,0,0,0,0,0,0,0,0,0,0,0,0]}
    Puck1 - Puck1 (RSSI -88)
      1801 => {"type":"Buffer","data":[51,46,53,57,57]­}
      1809 => {"temp":19}
      180f => {"battery":100}
    Puck2 - Puck2 (RSSI -87)
      1801 => {"type":"Buffer","data":[50,46,55,52,49]­}
      1809 => {"temp":23}
      180f => {"battery":89}
    
    
  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

NRF.setServices Datatypes

Posted by Avatar for werty1st @werty1st

Actions