Multiple uart connections

Posted on
  • Hi. Is it possible for the puck to control at the same time another puck and a magic blue light? I am using the code from here (the faster version) and here, but I get error "BLE task 5 is already in progress". Is it possible to have 2 uart connection (if that is the right term) at the same time? Thanks.

  • Hi! I think it's really just Bluetooth connections from the Puck itself that are the issue. At the moment you can only have one connection at a time - but I guess you only need to change the state of the light occasionally?

    If so, you could disconnect from the Puck and connect to the light only when you needed to change it?

  • @Gordon I am trying to have 2 pucks controlling each one bulb. One puck is taking instructions from Node-Red dashboard on the Raspberry Pi, and it must relay the instruction to the other puck. I need all the system to react instantly, power-consumption is not important. What will be the faster way, do you think I should use advertising-scanning between the 2 pucks and have each puck have a constant uart connection with the bulbs?

  • Maybe you could try connecting to the bulb and getting the services and characteristics, and then keeping a reference to the characteristic variable. Next time you connect literally just connect and write to the characteristic.

    I know you say 'instant', but that's actually really speedy and might make things a lot easier for you.

    If power usage isn't a big deal then yes, I'd say use scanning for advertising packets - and then the 'middle' Puck can set its advertising data up to relay what it received to the second one. It should be pretty straightforward to implement, even with a continuous connection to the light bulb.

  • @Gordon thanks a lot for your time. I am using the code on Controlling Bluetooth Lights with Puck.js to control the bulb. Can you please elaborate on how to keep a reference to the characteristic variable. By instant I wanted to say more like having the 2 bulbs turning on/off at the same time

    I spent some (newbie) hours with advertising/scanning, couldn't make NRF.setScan() work at all, got some success with NRF.findDevices(), but it takes around 2 seconds to turn on/off the bulb on the scanning Puck2. On Puck1 ('middle') I have this running at some point:

    NRF.setAdvertising({},{manufacturer: 0x0590, manufacturerData:[0]});  //  Node-Red lights off
    // or
    NRF.setAdvertising({},{manufacturer: 0x0590, manufacturerData:[1]}); // Node-Red lights on
    

    On Puck2, I run this code to scan and use manufacturerData:

    var lastState = 0;
    // Scan and call flashLeds() based on 'manufacturerData'
    setInterval(function () {
      NRF.findDevices(function(e) { 
        for (var i = 0; i < e.length; i++) {
          if((e[i].manufacturer === 1424) && (e[i].manufacturerData)) { // Check for 0x590 and manufacturerData
            if(e[i].manufacturerData[0] !== lastState) { // Check to see if the state has changed
              lastState = e[i].manufacturerData[0];
              flashLeds(e[i].manufacturerData[0]);
              console.log(lastState);
            }
          }
        }
      }, 500);
    }, 600);
    

    Probably (surely) I am approaching the whole issue the wrong way, but that's all I came up with that worked. The value 500 and 600 are random, I tried different values without any difference I could tell. Any suggestions how to speed things up? Thanks.

  • This seems to work for me:

    function scanFunc(dev) {
      if (!dev.name || dev.name.substr(0,4)!="Puck") return; 
     // you probably want to check ID, not name
      print(dev.manufacturerData[0]);
      // do something when this changes
    }
    NRF.setScan(scanFunc)
    

    You could increase the advertising interval for setAdvertising which would help a lot:

    NRF.setAdvertising({},{interval:50, ...
    // you'd have to experiment with values. Due to the way BLE advertising works it'll probably only pick up every third advertisement
    

    In terms of fast connection, try something like this:

    var lightAddr = "98:7b:f3:61:1c:22";
    var characteristic;
    
    function setLight(isOn) {
      var gatt;
    // if we have the characteristic, connect, write, disconnect
      if (characteristic) {
        return NRF.connect(lightAddr).then(function(g) {
          gatt = g;
          return characteristic.writeValue(isOn ? 1 : 0);
        }).then(function() {
          gatt.disconnect();
          console.log("Done!");
        });
      }
      // otherwise we have to search for it first
      NRF.connect(lightAddr).then(function(g) {
        gatt = g;
        return gatt.getPrimaryService("33160fb9-5b27-4e­70-b0f8-ff411e3ae078");
      }).then(function(service) {
        return service.getCharacteristic("217887f8-0af2­-4002-9c05-24c9ecf71600");
      }).then(function(c) {
        characteristic = c;
        return characteristic.writeValue(isOn ? 1 : 0);
      }).then(function() {
        gatt.disconnect();
        console.log("Done!");
      });
    }
    
  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Multiple uart connections

Posted by Avatar for Pi @Pi

Actions