Redirecting the USB port for use by a program

Posted on
  • Loopback2.js 22 Aug 2016
    Used the Loopback devices to allow the USB port to act as program input.
    Commands sent to the USB port trigger sub commands that get sent to the Espruino console.
    0: Exit,
    1: LEDon
    2: LEDoff
    The left pane of the WebIde or a terminal program connected to the USB port can be used to send the commands.

    //Loopback2.js
    //22 Aug 2016
    
    //use the left pane of the WebIde or
    //use putty or other termial program on com assigned to USB
    //use webide to load and run the program
    //disconnect webide
    //start putty on com port at 115200,8 no flow control
    //typing in putty should appear followed by connect in putty screen
    
    
    var Sp1=USB;
    var Sp2=LoopbackA;
    
    
    Sp1.setup(115200);
    Sp2.setup(115200);
    LoopbackB.setup(115200);
    LoopbackB.setConsole();
    
    Sp1.print("Enter 1 to turn the LED on:\n\r");
    Sp1.print("Enter 2 to turn the LED off:\n\r");
    Sp1.print("Enter 0 to Exit:\n\r");
    
    
    Sp1.on('data', function (data) {
      Sp1.print(data+"\n\r"); 
      switch(data){
        case "1"://LED on
         Sp2.print("digitalWrite(B12,1);\n\r");
         Sp1.print("digitalWrite(B12,1);\n\r");
        break;
        case "2"://LED off
         Sp2.print("digitalWrite(B12,0);\n\r");
         Sp1.print("digitalWrite(B12,0);\n\r");
        break;
        case"0"://Exit
         Sp2.print("reset();\n\r");
         Sp2.print("USB.setConsole();\n\r");
         Sp1.print("USB.setConsole();\n\r");
      }//end switch
    });
    

    The output:

    >echo(0);
    Enter 1 to turn the LED on:
    Enter 2 to turn the LED off:
    Enter 0 to Exit:
    echo(1)
    1
    digitalWrite(B12,1);
    2
    digitalWrite(B12,0);
    1
    digitalWrite(B12,1);
    2
    digitalWrite(B12,0);
    1
    digitalWrite(B12,1);
    2
    digitalWrite(B12,0);
    1
    digitalWrite(B12,1);
    2
    digitalWrite(B12,0);
    1
    digitalWrite(B12,1);
    2
    digitalWrite(B12,0);
    1
    digitalWrite(B12,1);
    2
    digitalWrite(B12,0);
    0
    USB.setConsole();
    
    

    1 Attachment

  • Some improvements:

    //Loopback3.js
    //22 Aug 2016
    
    //use the left pane of the WebIde or
    //use putty or other termial program on com assigned to USB
    //use webide to load and run the program
    //disconnect webide
    //start putty on com port at 115200,8 no flow control
    //typing in putty should appear followed by connect in putty screen
    
    
    var Sp1=USB;
    var Sp2=LoopbackA;
    
    function run(){
    Sp1.setup(115200);
    Sp2.setup(115200);
    LoopbackB.setup(115200);
    LoopbackB.setConsole();
    }
    
    function menu(){
    Sp1.print("Enter 1 to turn the LED on:\n\r");
    Sp1.print("Enter 2 to turn the LED off:\n\r");
    Sp1.print("Enter 0 to Exit:\n\r");
    }
    
    
    Sp1.on('data', function (data) {
      Sp1.print(data+"\n\r"); 
      switch(data){
        case "1"://LED on
         Sp2.print("digitalWrite(B12,1);\n\r");
         Sp1.print("digitalWrite(B12,1);\n\r");
         menu();
        break;
        case "2"://LED off
         Sp2.print("digitalWrite(B12,0);\n\r");
         Sp1.print("digitalWrite(B12,0);\n\r");
         menu();
        break;
        case"0"://Exit
         Sp2.print("USB.setConsole();\n\r");
      }//end switch
    });
    
    
    
    function test(){
     run();
     menu();
    }
    
    var a=setInterval (function () {
      test();
      clearInterval(a);
    }, 1000);
    

    The output:

    >echo(0);
    =undefined
    -> LoopbackB
    Enter 1 to turn the LED on:
    Enter 2 to turn the LED off:
    Enter 0 to Exit:
    1
    digitalWrite(B12,1);
    Enter 1 to turn the LED on:
    Enter 2 to turn the LED off:
    Enter 0 to Exit:
    2
    digitalWrite(B12,0);
    Enter 1 to turn the LED on:
    Enter 2 to turn the LED off:
    Enter 0 to Exit:
    0
    <- LoopbackB
    =undefined
    =undefined
    >1
    =1
    > 
    

    1 Attachment

  • Great! Where you're sending commands to Loopback, you actually just execute them directly as JavaScript if you wanted to, or use eval?

    Loopback's probably most useful for things like this: http://www.espruino.com/Espruino+Home+Co­mputer

    Where it helps to have access to the multi-line editing and stuff like that. For instance you could connect something like an nRF24 radio module and then allow Espruino to be programmed over that.

  • An even better version is attached.

    Why is this being done?
    It is a way of providing an end user menu interface so they don't need to know Espruino.
    Typing LEDon() and LEDoff() or other such commands is replace by a menu selection.

    This is basic work for the crypto-message project that I'm working on in another project.
    Ideally I'd like to have the crypto-client Pico plug into a USB port and serve up a webpage to the computer using PPP over the USB port. The client clicks on buttons on the web page that get sent to the Client Pico. The Client Pico sends the crypto-message via Wi-Fi to the Server Pico. The reverse path applies as well.
    The Server Pico decrypts the crypto-message, validating the message integrity and authentication, and then performs the requested task.
    Do the Espruino Wi-Fi functions take care of the PPP connection, or do I need to add code that does the initial handshake and setup?


    1 Attachment

  • I'm afraid there's no PPP option at the moment. I'd be really interested if you get it working (you could use the NetworkJS module so you can get access to http/etc).

    The really cool thing (imo) would be to be able to serve the proper website version of the Web IDE over it - so you could then access the board without having to have any software on the host at all.

  • The problem with using a browser is no local file or I/O access.
    Around the year 2000, I found Simple Server, and used it to access RTU's via serial ports, access a database etc. It served HTML pages so that I could access the RTUs and database.
    Some promising links from Googling "Simple Server" :

    https://github.com/balupton/simple-serve­r

    https://www.npmjs.com/package/simple-ser­ver

    http://techngr.com/t-mtn-simple-server-d­ownload-and-settings

    http://www.analogx.com/contents/download­/Network/sswww/Freeware.htm

    For PPP this link looks promising

    https://github.com/adamdunkels/contiki-1­.x/blob/master/contiki/ppp/ppp.c

  • Perhaps this post about SerialIP on the Arduino forum will interest you.

  • Alas I'm using Windows 7 and the days of XP are numbered
    The SLIP protocol is simple enough.

    /*
    //SLIP protocol
    // https://en.wikipedia.org/wiki/Serial_Lin­e_Internet_Protocol
    var END=0xC0;
    var ESC=0xdb;
    var ESC_END=0xdc;
    var ESC_ESC=0xdd;
    
    // END+Packet+END
    //if the END byte occurs in the data to be sent, the two byte sequence
    // ESC, ESC_END is sent instead,
    //if the ESC byte occurs in the data, the two byte sequence ESC, ESC_ESC // is sent.
    */
    

    PPP link:
    https://en.wikipedia.org/wiki/Point-to-P­oint_Protocol

    LCP link:
    https://en.wikipedia.org/wiki/Link_Contr­ol_Protocol
    SLIP protocol link:
    https://en.wikipedia.org/wiki/Serial_Lin­e_Internet_Protocol
    CHAP protocol link:
    https://en.wikipedia.org/wiki/Challenge-­Handshake_Authentication_Protocol

    Another link:
    https://en.wikibooks.org/wiki/Serial_Pro­gramming/IP_Over_Serial_Connections

    Setting up PPP on Windows 7
    https://mikebeach.org/2012/08/30/install­ing-and-configuring-a-ppp-null-modem-con­nection-on-windows-7/

    So when I try to start the PPP dial-up from windows, I captured the following:

    // with window trying to dial up
    Xdump();
    CLIENTCLIENTCLIENTCLIENTecho(0);
    console.log("<","<<",JSON.stringify(proc­ess.env),">>",">");echo(1);
    4
    
    //disconnect and reconnect Webide only
    Xdump();
    echo(0);
    console.log("<","<<",JSON.stringify(proc­ess.env),">>",">");echo(1);
    4
    

    Windows is sending CLIENTCLIENTCLIENTCLIENT
    So what do I need to reply? Dig into the RFC's and read the Vogon poetry for a while?

  • Dig into the RFC's and read the Vogon poetry for a while?

    Quite likely...

    Or you could actually connect via some device that uses SLIP and capture the packets?

    My worry is that it may actually be sending data at the packet level, which would mean that you'd need TCP/IP on the Espruino (which isn't there at the moment)

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

Redirecting the USB port for use by a program

Posted by Avatar for ClearMemory041063 @ClearMemory041063

Actions