BLE Connect/Disconnect

Posted on
  • I have a little problem that I'm stuck on.

    What I basically want to do is:

    1. with a short press of the Puck.js <0,3s a Bluetooth connection to the MDBT42Q should be established >>> works.
    2. directly after the connection, the accelerometer should execute its function and switch various pins on the MDBT when the threshold values are exceeded >>> works
    3. when pressing the Puck.js again < 0,3s the Bluetooth connection shall be disconnected again.

    When I upload the code it either doesn't work at all or once and then not anymore.

    I get following in the Console:

    Uncaught Error: Unhandled promise rejection: Error: BLE task 6 is already in progress
    Uncaught Error: Unhandled promise rejection: Error: BLE task 6 is already in progress
    Uncaught Error: Unhandled promise rejection: Error: BLE task 6 is already in progress
    Uncaught Error: Unhandled promise rejection: Error: BLE task 6 is already in progress
    Uncaught Error: Unhandled promise rejection: Error: BLE task 6 is already in progress
    Uncaught Error: BLE task 6 is already in progress
     at line 1 col 14
    k.disconnect()
                 ^
    in function "disconnect" called from line 1 col 17
    uart.disconnect();
                    ^
    in function called from system
    Uncaught Error: Unhandled promise rejection: Not connected
    Uncaught Error: Unhandled promise rejection: Not connected
    Uncaught Error: Unhandled promise rejection: Not connected
    Uncaught Error: Unhandled promise rejection: Not connected
    Uncaught Error: Unhandled promise rejection: Not connected
    Uncaught Error: Unhandled promise rejection: Not connected
    Uncaught Error: Unhandled promise rejection: Not connected
    Uncaught Error: Unhandled promise rejection: Not connecte
    

    Here the code:

    var log = print;
    var uart;
    var l;
    var connected = false;
    var avrX = 0;
    var avrY = 0;
    var accZero = Puck.accel().acc; // get the base value when we start up
    
    
    
    //BLE Connect
    
        function flag(e) {
      
        var l = e.time-e.lastTime;  
        if ((l < 0.3) && (connected == false)){
        log("Sending command");
        log("Searching for device...");
        NRF.requestDevice({ filters: [{ namePrefix: 'MDBT42Q' }]         }).then(function(device) {
        log("Connecting...");
        device.on('gattserverdisconnected', function() {
        });
        return require("ble_uart").connect(device);
        }).then(function(u) {
        log("Connected");
        digitalPulse(LED3, 1, 300);
        uart = u;
        connected = true;
        });}
    
          
        if ((e.time - e.lastTime < 0.3) && (connected == true) ){
        setTimeout(function(device) {
        uart.disconnect();
        log("Disconnected");
        connected = false;
        }, 1000);}
    
    
        }
        setWatch(flag, BTN, {repeat:true, edge:"falling"});
    
    
    
    
    ///Accelerometer
    
    
        Puck.accelOn();
    
    Puck.on('accel', function(data) {
      avrX = (avrX*0,99) + 0.01*(data.acc.x - accZero.x);
      avrY = (avrY*0,99) + 0.01*(data.acc.y - accZero.y);
    ///log (avrX, avrY);
    
       if (connected == true) {
    
           if (avrX < 90){
           uart.write("digitalWrite(LED1, 1), digitalWrite(D31, 1);\n");}
           if (avrX > 105){
           uart.write("digitalWrite(LED1, 1),digitalWrite(D27, 1);\n");}
           if (avrY < 90){
           uart.write("digitalWrite(LED1, 1),digitalWrite(D28, 1);\n");}
           if (avrY > 105){
           uart.write("digitalWrite(LED1, 1),digitalWrite(D29, 1);\n");}
    
           if (avrX > 90 && avrX < 105 && avrY > 90 && avrY < 105){
           uart.write("digitalWrite(LED1, 0),digitalWrite(D31, 0), digitalWrite(D27, 0), digitalWrite(D28, 0),                digitalWrite(D29, 0) ;\n");}
       }
    
    
    });
    
    
  • It's hard to tell from the indenting, but it looks like maybe you could be calling setWatch multiple times? If so, it'd mean that after a few runs you're going to end up so that you press the button once, and flag is called multiple times?

    One thing that could help as well - you have gattserverdisconnected but it's not being used right now. Try:

    device.on('gattserverdisconnected', function() {
      uart = undefined; // setting this to undefined tells us that we've disconnected
    });
    

    Then instead of just uart.disconnect(); do:

    if (uart) uart.disconnect();
    uart = undefined; // we already asked to disconnect
    
  • Hi Gordon,

    thanks for that help.

    What I found out is, if I delete the part with the accelerometer it works. I can connect and disconnect like it should be all the time.

    Did I made a mistake in the accelerometer section? I ask for
    if connection is true, uart.write.

    var log = print;
    var status = 0;
    var uart;
    var l;
    var connected = false;
    var avrX = 0;
    var avrY = 0;
    var accZero = Puck.accel().acc; // get the base value when we start up
    
    
    //BLE Connect
    
        function flag(e) {
      
        var l = e.time-e.lastTime;  
        if ((l < 0.3) && (connected == false)){
        log("Sending command");
        log("Searching for device...");
        NRF.requestDevice({ filters: [{ namePrefix: 'MDBT42Q' }]         }).then(function(device) {
        log("Connecting...");
        device.on('gattserverdisconnected', function() {
        uart = undefined;
        connected = false;
        });
        return require("ble_uart").connect(device);
        }).then(function(u) {
        log("Connected");
        digitalPulse(LED3, 1, 300);
        uart = u;
        connected = true;
        status = 1;
        log (status);
        log (connected);
        });}
    
    
        if ((l < 0.3) && (status == 1)){ 
          
        if (uart) uart.disconnect();
        uart = undefined;
        connected = false;
        status = 0;
        log (status);
        log (connected);
        }
    
        
          
        }
        setWatch(flag, BTN, {repeat:true, edge:"falling"});
    
    
    
    
    ///Accelerometer
    
    
    
    Puck.accelOn();
    
    
    Puck.on('accel', function(data) {
      
    
      avrX = (avrX*0,99) + 0.01*(data.acc.x - accZero.x);
      avrY = (avrY*0,99) + 0.01*(data.acc.y - accZero.y);
    ///log (avrX, avrY);
    
     if (status == 1) {
    
           if (avrX < 90){
           uart.write("digitalWrite(LED1, 1), digitalWrite(D31, 1);\n");}
           if (avrX > 105){
           uart.write("digitalWrite(LED1, 1),digitalWrite(D27, 1);\n");}
           if (avrY < 90){
           uart.write("digitalWrite(LED1, 1),digitalWrite(D28, 1);\n");}
           if (avrY > 105){
           uart.write("digitalWrite(LED1, 1),digitalWrite(D29, 1);\n");}
    
           if (avrX > 90){
           uart.write("digitalWrite(LED1, 0), digitalWrite(D31, 0);\n");}
           if (avrX < 105){
           uart.write("digitalWrite(LED1, 0),digitalWrite(D27, 0);\n");}
           if (avrY > 90){
           uart.write("digitalWrite(LED1, 0),digitalWrite(D28, 0);\n");}
           if (avrY < 105){
           uart.write("digitalWrite(LED1, 0),digitalWrite(D29, 0);\n");}
    
      }
          
      
    
    });
    
    
  • Ah and one more info, the re-connection works basically but the signals will not be sent to the MDBT42Q. The LED there is not reacting anymore after the reconnection.

    I get this message now in the console with BLE task 3:

    0
    false
    Uncaught Error: Unhandled promise rejection: Error: BLE task 3 is already in progress
    Sending command
    Searching for device...
    Connecting...
    Connected
    1
    true
    
  • Looks like in the new code you changed to checking status==1 so you also need to set status=0 in device.on('gattserverdisconnected', function() {

    You could try sending uart.write("\x03\x10digitalWrite(LED1, 0),digitalWrite(D29, 0);\n");} to the MDBT42

    Newer firmwares (2v15 I think?) will auto-clear the input line when Bluetooth disconnects/connects so it's not needed, but on earlier versions, if a send half-completed and you were left with digitalWr on the input line (for instance) you'd then end up with Espruino trying to execute:

    digitalWrdigitalWrite(LED1, 0),digitalWrite(D29, 0);
    

    Which would fail and not light any lights...

  • Hi Gordon,

    first of all thank you very much for the information, I didn´t know that.

    I have two more questions about this. If I delete this lines:

      if (avrX > 90){
           uart.write("\x03\x10digitalWrite(LED1, 0), digitalWrite(D31, 0);\n");}
           if (avrX < 105){
           uart.write("\x03\x10digitalWrite(LED1, 0),digitalWrite(D27, 0);\n");}
           if (avrY > 90){
           uart.write("\x03\x10digitalWrite(LED1, 0),digitalWrite(D28, 0);\n");}
           if (avrY < 105){
           uart.write("\x03\x10digitalWrite(LED1, 0),digitalWrite(D29, 0);\n");}
    
    

    I don´t get any feedback in the console. If I let it in, I get this message, even if it works with the connecting/disconnecting thing:

    Uncaught Error: Unhandled promise rejection: Error: BLE task CHAR_WR is already in progress
    

    The other thing is I try to implement now the button and the part of the code for the button works in another program and also if the Accel is not in that one. What is the issue, when I want to use that additional code? Is it that uart.write is busy in some way? Sometimes the signal from the button receives the MDBT but after 2 or 3 times it stops or the LED on the MDBT does not go off again.

    Here the complete Code:

    var log = print;
    var uart;
    var l;
    var connected = false;
    var avrX = 0;
    var avrY = 0;
    var accZero = Puck.accel().acc; // get the base value when we start up
    
    ///BLE Connect
    
    
        function flag(e) {
    
        var l = e.time-e.lastTime;
        if ((l < 0.3) && (connected == false)){
        log("Sending command");
        log("Searching for device...");
        NRF.requestDevice({ filters: [{ namePrefix: 'MDBT42Q' }]
        }).then(function(device) {
        log("Connecting...");
        device.on('gattserverdisconnected', function() {
        uart = undefined;
        connected = false;
        });
        return require("ble_uart").connect(device);
        }).then(function(u) {
        log("Connected");
        digitalPulse(LED3, 1, 300);
        uart = u;
        connected = true;
        log (connected);
        });}
    
        if ((l < 0.3) && (connected == true)){
    
        if (uart) uart.disconnect();
        uart = undefined;
        connected = false;
        log (connected);
    
        }
    
    
        }
        setWatch(flag, BTN, {repeat:true, edge:"falling"});
    
    
    
    
    ///Accelerometer
    
    
    
    Puck.accelOn();
    
    
    Puck.on('accel', function(data) {
    
    
      avrX = (avrX*0,99) + 0.01*(data.acc.x - accZero.x);
      avrY = (avrY*0,99) + 0.01*(data.acc.y - accZero.y);
    ///log (avrX, avrY);
    
     if (connected == true) {
    
           if (avrX < 90){
           uart.write("\x03\x10digitalWrite(LED1, 1), digitalWrite(D31, 1);\n");}
           if (avrX > 105){
           uart.write("\x03\x10digitalWrite(LED1, 1),digitalWrite(D27, 1);\n");}
           if (avrY < 90){
           uart.write("\x03\x10digitalWrite(LED1, 1),digitalWrite(D28, 1);\n");}
           if (avrY > 105){
           uart.write("\x03\x10digitalWrite(LED1, 1),digitalWrite(D29, 1);\n");}
    
           if (avrX > 90){
           uart.write("\x03\x10digitalWrite(LED1, 0), digitalWrite(D31, 0);\n");}
           if (avrX < 105){
           uart.write("\x03\x10digitalWrite(LED1, 0),digitalWrite(D27, 0);\n");}
           if (avrY > 90){
           uart.write("\x03\x10digitalWrite(LED1, 0),digitalWrite(D28, 0);\n");}
           if (avrY < 105){
           uart.write("\x03\x10digitalWrite(LED1, 0),digitalWrite(D29, 0);\n");}
    
     }
    
    });
    
    
    
    
    
    
    
         setWatch(function(c) {
    
           
           
         if (connected == true) {
    
           
         setTimeout(function () {
         {
         uart.write("\x03\x10digitalWrite(LED2,1), digitalWrite(D30, 1);\n");
         }
    
         }, 400);
         }
    
         }, BTN, { repeat: true, edge: 'rising' });
    
    
    
    
         setWatch(function(c) {
     
     
         if (connected == true) {
           
         {
         uart.write("\x03\x10digitalWrite(LED2,0), digitalWrite(D30, 0);\n");
            } 
         }
         
          }, BTN, { repeat: true, edge: 'falling' });
    
    
    
    
  • I think the issue you are hitting is that uart.write doesn't complete immediately. I believe it returns a promise which completes when the write has finished. So calling uart.write twice will cause you problems.

    You could chain the promises, but perhaps it makes more sense to do:

    cmd="";
    if (avrX > 90){
    cmd+="\x03\x10digitalWrite(LED1, 0), digitalWrite(D31, 0);\n");
    if (avrX < 105){
    cmd+="\x03\x10digitalWrite(LED1, 0),digitalWrite(D27, 0);\n");
    if (avrY > 90){
    cmd+="\x03\x10digitalWrite(LED1, 0),digitalWrite(D28, 0);\n");
    if (avrY < 105){
    cmd+="\x03\x10digitalWrite(LED1, 0),digitalWrite(D29, 0);\n");
    if (cmd) uart.write(cmd);
    
  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

BLE Connect/Disconnect

Posted by Avatar for psc1988 @psc1988

Actions