Flic Hub SDK errors when setting UDP setMulticastTTL and setTTL



  • SUMMARY: When I receive a button event I want to sent a UDP multicast message to several Raspberry Pi's on my network (as they all handle different parts of my home control/automation). When using setMulticastTTL or setTTL I get the error:

    ReferenceError: identifier 'checekType' undefined
    

    ASIDE: this topic is mostly to log the possible typo in duktape or dgram. It is also possible/probable that I am using multicast in the wrong way, so any help with using multicast would be great too :)

    CODE: Here is the relevant code snippet (buttons.js) showing just the UDP parts; you can swap out setMulticastTTL for setTTL to get the same error:

    const dgram = require('dgram');
    const server = dgram.createSocket('udp4');
    
    const PORT = 31091;
    const MCAST_ADDR = '234.0.0.0'; // use a class D (multicast) IP address https://en.wikipedia.org/wiki/Classful_network
    
    server.bind({
      port: PORT
    },function(){
      server.setBroadcast(true);
      server.setMulticastTTL(128);
      server.addMembership(MCAST_ADDR);
      console.log('# server.address: ', JSON.stringify(server.address(),null,2));
    });
    

    ERROR: Here is the error message in the console (line numbers will not match above snippet):

    ReferenceError: identifier 'checekType' undefined
        at [anon] (duktape.c:81026) internal
        at setMulticastTTL (dgram.js:248)
        at [anon] (root/Test/buttons.js:21)
        at [anon] (events.js:27)
        at emit (events.js:59)
        at [anon] (dgram.js:117) preventsyield
        at runInit () native strict preventsyield
        at handlePacket (pipe_communication.js:48)
        at readCallback (pipe_communication.js:93) preventsyield
    

    EARLY ANALYSIS: It looks like there is a typo of checekType (should this be checkType?) in duktape.c or possibly dgram.js


  • FlicTeam

    @jitmo I'm not a multicast expert, but I would suggest you to use any example code you can find on the web for Node.js for server and client, make that work, and then just put it on the Flic hub.



  • Hey @Emil, thanks for the quick response!

    Any tips for sending a broadcast UDP packet to multiple Raspberry Pi's (also using Node). Do I need to set the MCAST_ADDR when calling server.bind on the Flic Hub SDK? (I have tried many combinations of ports and addresses on the Flic Hub SDK and Pi's).

    To send a broadcast message from the Flic Hub SDK I use the following:

    const buttonManager = require('buttons');
    const dgram = require('dgram');
    const server = dgram.createSocket('udp4');
    
    const PORT = 31091;             // TCP/UDP port
    const MCAST_ADDR = '234.0.0.0'; // use a class D (multicast) IP address https://en.wikipedia.org/wiki/Classful_network
    
    server.on('message', function(msg, rinfo) {
      console.log('@ sever.on.message');
      console.log('# msg: ', msg);
      console.log('# rinfo: ', JSON.stringify(rinfo,null,2));
    });
    
    server.bind({
      port: PORT
    },function(){
      server.setBroadcast(true);
      server.addMembership(MCAST_ADDR);
      console.log('# server.address: ', JSON.stringify(server.address(),null,2));
    });
    
    buttonManager.on('buttonSingleOrDoubleClickOrHold', function(obj) {
    
      var button = buttonManager.getButton(obj.bdaddr);
      var clickType = obj.isSingleClick ? 'click' : obj.isDoubleClick ? 'double_click' : 'hold';
    
      var message = button.name+' '+clickType+' '+button.batteryStatus;
      console.log(message);
    
      server.send(message, 0, message.length, PORT, MCAST_ADDR, function(err){
        console.log('@ server.send callback');
        console.log('# err: ', err);
        console.log('# server.address: ', JSON.stringify(server.address(),null,2));
      });
    
    });
    

    Here is a relevant code snippet on the Pi (for brevity I am not showing the error, connect or listening event handlers) :

    const dgram = require('dgram');
    const client = dgram.createSocket('udp4');
    
    client.on('message', (msg, rinfo) => {
      console.log('@ client.on.message');
      console.log(`'${msg}' from ${rinfo.address}:${rinfo.port}`);
    });
    
    client.bind({
      address: '234.0.0.0',
      port: 31091,
      exclusive: false
    });
    

    When the Flic Hub SDK sends the UDP message I can see the UDP on.message event on the Flic SDK console, although the on.message event is not triggered on the Pi.

    On the Pi's I have tried address 0.0.0.0 as well as 255.255.255.255, I have tried omitting port. None of these combinations work.

    Could this be due to the TTL issue or am I simply doing something silly like wrong port/address combinations?

    UPDATE: I can get the Flic Hub to send UDP to just one Pi by sending server.send(message, 0, message.length, PORT, <RaspPi_IP>) from the Flic Hub SDK and omitting the address param of client.bind on the Pi (which defaults to 0.0.0.0)... although I'm looking for a broadcast to multiple Pi's.



  • This post is deleted!

  • FlicTeam

    @alastairjamieson seems like a bug. Will fix later in August.


Log in to reply