setConsole()

Posted on
  • Hi Gordon

    The following code moves the console to LoopbackA. 10 sec. later back to USB.
    It works as expected. But we do not have echo on the console and there is no prompt.

    Code:

    var test='Sacha';
    
    setTimeout(function() {
        USB.setConsole();
        console.log('Console back to USB');
        console.log('Content of test: '+test);
        
        },10000);
    
    
    console.log('Move console to loopback');
    LoopbackA.setConsole();
    
    
  • In addition magic 'ec' is typed on the console.

    After running the above code waiting ten seconds then hit return. We have the following output:

     _____                 _
    |   __|___ ___ ___ _ _|_|___ ___
    |   __|_ -| . |  _| | | |   | . |
    |_____|___|  _|_| |___|_|_|_|___|
              |_| http://espruino.com
     1v83 Copyright 2015 G.Williams
    >echo(0);
    Move console to loopback
    Console back to USB
    Content of test: Sacha
    Uncaught ReferenceError: "ec" is not defined
    
    
  • Just do setTimeout("LoopbackA.setConsole();",500);.

    It's because the code is executed as it is sent to Espruino (in some cases there wouldn't be enough memory to hold all your program's text and execute it too!), so you're actually moving the console out of the way right in the middle of uploading the code (while echo is off).

    Adding a setTimeout will mean it gets executed later, after code is executed and echo is turned back on.

  • Understand. Thanks. Works.

  • the USB.setConsole seems not be available anymore? Does anyone know why?

  • It's definitely still available - which board are you using, and are you using the latest firmware?

  • I get an error telling me command not found.
    I use the big one

  • Some caution is needed when using setConsole if you save the program and do the onInit().
    Be sure you have a working method to restore the console to the USB ( a pushbutton, a control-C, or a menu option).

  • Ic yes that makes sense otherwise reflashing could be an option

  • If connected to the usb and ide the Serial obe works just fine. How exactly trick Serial1 to allow the onData to work as in ide if powered from external power source?

  • I'm not quite sure I understand the question, but try just adding USB.setConsole(1).

    That will force the console to always stay on USB, allowing you to use Serial1 for whatever you want.

  • Gordon, thank you. I could not get it to work. I will be away 3 days.
    I was seeking a solution to get this simple test snippet to run, when not connected to a USB port of a computer. As I understood, the Serial port will be on USB by default. And on Serial1 by Default if unplugged. So this blocks my device (HM10). So I would like to set to USB or Serial2. I could not get it to work. I ll check later on it.

      Serial=Serial1
      Serial.setup(9600);
      Serial.on('data', function (data) {
        console.log(data);
        if (data==='1'){
          LED1.write(1);
          Serial.print("test\n");
        }
        if (data==='0'){
          LED1.write(0);
        }
        if (data==='d'){
          LED2.write(1);
    
          Serial.print("sensor: t="+temp_act+", p="+press_act+", h="+hum_act);
          LED2.write(0);
    
        }
    
  • I expected a Serial2.setConsole(true) should help, however this results in :
    Uncaught Error: Function "setConsole" not found!
    at line 41 col 9
    Serial2.setConsole(true);

  • Hi - Serial is a built-in object, so by setting it to Serial1 you're probably causing yourself a lot of trouble. Try the following:

      var serial=Serial1
      USB.setConsole(true);
      serial.setup(9600);
      serial.on('data', function (data) {
        console.log(data);
        if (data==='1'){
          LED1.write(1);
          serial.print("test\n");
        }
        if (data==='0'){
          LED1.write(0);
        }
        if (data==='d'){
          LED2.write(1);
          serial.print("sensor: t="+temp_act+", p="+press_act+", h="+hum_act);
          LED2.write(0);
        }
    });
    

    Using Serial2.setConsole should work, but I'd set Serial2 up first or it'll use the default pins. Personally I'd use USB.setConsole(true) (as above) because then the console is still available on USB for updating code.

  • It was my fault. The declination outside of init Serial=Serial1 blocked the USB.setConsole command.

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

setConsole()

Posted by Avatar for Sacha @Sacha

Actions