Puck.js, node-red, IFTTT

Posted on
  • I would like to trigger 2 IFTTT events with my Puck.js, the first IFTTT event, turns on a wifi plug and the second event turns off the wifi plug, all this done in node-red.
    I’m using the following code on the Puck.js, it send a different number each time the button is pressed, a 1 or an 11.

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

    in the node-red input mqtt node, I use the following as the Topic:
    /ble/advertise/ca:c2:42:7c:05:dc/ffff

    using a function node, I was trying to test for the 2 pressCount value I was sending each time I pressed the puck.js
    I could not figure how to test for the pressCount value so I ended up testing for the length.
    The following is the code I ended up using in the node fuction:

    var newMsg = { payload: msg.payload.length };
    if (newMsg.payload == 29) {
       return [  msg, null ];
    } else  {
        return [ null, msg ];
    }
    
    

    It worked out OK testing for the length, but if someone could show me how I could test for the pressCount value that would be appreciated.

    Thanks

  • If you put the message through to the debug node then you'll be able to see what's in it.

    At the moment it appears that what you'll be seeing is {"type":"Buffer","data":[11]}, so to get the data you need to do JSON.parse(msg.payload).data[0].

    However that was never the intention. If you pull the latest version of EspruinoHub I just fixed it so it reports a JSON array. It means you can check with msg.payload=="[11]" or can do JSON.stringify(msg.payload) and get an actual array.

  • thank you, will try this out

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

Puck.js, node-red, IFTTT

Posted by Avatar for user90273 @user90273

Actions