Bluetooth not work when using battery

Posted on
  • Hi all,
    I using the following code to control the On board LED by the Bluetooth ,when using USB cable to power the code below is work .but when i unplug the USB and using battery to power up is nothing happen .

    //Setup the BT Serial
    function onInit() { 
      Serial1.setup(9600);
    }
    
    var trace = ""; 
    Serial1.on('data', function (data) {
    
    	trace = +data;
     digitalWrite(LED3,0);
    
    if(trace  == "1" ){
      digitalWrite(LED2,1);
    }
      
    if(trace == "0" ){
      digitalWrite(LED2,0);
    }
      
    });
    
    save();
    
  • I have a code is working before but since the code is using Serial1.onData when i upload my code is get warning so i change the code above.

    the older code

    function onInit() { Serial1.setup(9600);}
    var trace = "";
    Serial1.onData(function (e) {
      print("--->"+e.data);
      trace = +e.data;
    
    if(trace  == "1" ){
      digitalWrite(LED3,1);
    }
    if(trace == "0" ){
     digitalWrite(LED3,0);
    }
    
    });
    save();
    
    
  • The problem is that the actual console (what you'd have on the left of the Web ide) is moving to bluetooth.

    If you just want to control an led you can send the string "digitalWrite(LED3,1);\n" directly over bluetooth and it will be executed.

    However if you want to receive data then you'll have to move the console out of the way. In onInit, just add USB.setConsole ()

  • Dear Gordon,
    I add the USB.setConsole () now my code is working good.
    by the way may i know is this code mean?

    Thank You

  • Well, the 'console' - the bit on the left side of the Web IDE - can be put in any serial device on Espruino. When you start Espruino when it's plugged in to USB, it's on USB - but when you start Espruino without USB it's on Serial1 - so you can use the Web IDE through Bluetooth.

    But if the console is on a Serial Port, you can't read the data going into it. So what you want to do is to force the console to go onto USB (even though it's not connected) - that's what USB.setConsole() does.

    Having said that, if you start Espruino with USB plugged in, and then unplug USB, it will still swap back I'm afraid. It's easy enough to get around by just resetting it though.

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

Bluetooth not work when using battery

Posted by Avatar for jacklee @jacklee

Actions