bluetooth failing when not connected to USB

Posted on
  • Hi

    I have a little test script running fone when connected to USB:

    var s = require("servo").connect(B13);
    
    var curBuff = "";
    
    Serial1.on('data', function (data) {
      try{
      if (data==","){
        s.move(parseFloat(curBuff));
        curBuff = "";
      }
      else{
        curBuff += data;
      }
      }
      catch(err){
      }
    });
    
    
    // keep track of the next LED
    var next_LED = 1;
    // keep track of the ID, see later
    var timeout_ID;
    function swap() {
      // remove the timeout to turn of all LEDs when the user pressed the button
      if (timeout_ID !== undefined) {
        clearTimeout(timeout_ID);
      }
      // determine which LED to turn on/off
      switch(next_LED) {
        case 1:
          LED3.write(1);
          LED1.write(0);
          s.move(0.5);
          break;
        case 2:
          LED2.write(1);
          LED3.write(0);
          s.move(0);
          break;
        case 3:
          LED1.write(1);
          LED2.write(0);
          s.move(1);
          break;
      }
      // determine the next LED to turn on
      next_LED = Math.wrap(next_LED, 3) + 1;
      // prepare a timeout to turn off all LEDs after a while
      // we capture the ID here, so that we can use it in a next call to this function
      timeout_ID = setTimeout(function () { LED1.write(0); LED2.write(0); LED3.write(0); timeout_ID = undefined; }, 5000);
    }
    
    setWatch(swap, BTN1, {repeat:true, edge:"rising"});
    

    If I send messages to the bluetooth connection from an app on my phone, first a string of a float (e.g. '0.1') then post another BT message containing just a comma ',' the servo moves.

    I can disconnect from the WebIDE. It still works.

    However! If I unplug the USB, I get a BT message back from the board to my phone:

    'Console Moved from USB'
    Now it fails to work. I send it the float, it echos it back. I send the comma, its sends error:
    "Uncaught SyntaxError: Got ',' expected EOF at line 1 col 1
    ,
    ^"

    Any help appreciated. :)
    Phil

  • Hi Phil,

    Yes - it's because the console (the thing that would have been on the left-hand side of the Web IDE) automatically moves to Bluetooth (Serial1) when the board isn't connected to USB (so you can actually program it through bluetooth).

    All you need to do is put the code USB.setConsole() in onInit(). That'll move the console away from Serial and on to USB even though it isn't connected.

    With the console put on Bluetooth you can actually send
    JavaScript commands straight over though, for instance "s.move(5)\n"

  • Oh I love the idea of sending commands as script to execute :)

    Yep that got it working.

    Many thanks!

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

bluetooth failing when not connected to USB

Posted by Avatar for user49319 @user49319

Actions