1. Setup a TCP client
      This code sets up the TCP client on port 9988.
      The console is redirected to the Loopback to allow the WebIDE to act as a simple menu system.
      You will need to edit the hardware flag , ssid and router passcode.
      Additionally you have to hardcode the IP address of the server. (we will fix this with UDP later)

      //netclient3.js
      //2 Oct 2016
      //espruino board with ESP8266
      //PICO  with ESP8266
      var IP="192.168.1.3";
      var Sport=9988;
      //var Hardware=0; //Espruino board
      var Hardware =1; //PICO
      var SSID="ssid";
      var key= "router passcode";
      var Serial;
      function test(){
      if(Hardware===1)Serial=Serial2;
      if(Hardware===0)Serial=Serial4;
      if(Hardware===1){
      digitalWrite(B9,1); // enable on Pico Shim V2
      Serial.setup(115200, { rx: A3, tx : A2 }); //Pico
      }
      if(Hardware===0)Serial.setup(115200, { rx: C11, tx : C10 }); 
      //espruino board
      console.log("Start connection process");
      var wifi = require("ESP8266WiFi_0v25").connect(Seri­al, function(err) {
      if (err)return 1;// throw err;
      console.log("Reset the ESP8266");
      wifi.reset(function(err) {
      if (err)return 1;// throw err;
      console.log("Connecting to WiFi");
      wifi.connect(SSID,key, function(err) {
        if (err)return 1;//throw err;
        wifi.getIP(function(l,ip){console.log("I­P= ",ip,"\n\r"+l);});
        console.log("Wi-Fi Connected");
      // Now you can do something,
      menu();
      ////////////////////////////////////////­////////////////////
      });
      });
      });
      }//end test
      //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx­xxxxxxxxxxxxxxxx
      function sendit(msg){
      var reply="";
      var client = require("net").connect({host: IP, port: Sport}, function() {
      //  console.log('client connected');
      client.write(msg);
      client.on('data', function(data) {
      reply+=data;
      console.log(">"+JSON.stringify(data));
      });
      client.on('end', function() {
      //    console.log('client disconnected');
      return reply;
      });
      });
      }
      
      ////////////////////////////////////////­////////////////////////////////
      var sst="";
      //Menus
      menu=function(){
      USB.print(" \n\r");
      USB.print("Select using digit and return key\n\r");
      USB.print("1 LED Off\n\r");
      USB.print("2 LED On\n\r");
      USB.print("0 Exit\n\r");
      };
      parsecmd=function(){
      USB.print("\n\r");
      switch(this.sst.charAt(0)){
      case "1"://LED off
       sst="";
       USB.print("LED Off\n\r");
       sendit("LED Off");
      break;
      case "2"://LED On
       USB.print("LED On\n\r");
       sst="";
       sendit("LED On");
      break;
      case"0"://Exit
       LoopbackA.print("USB.setConsole();\n\r")­;
       sst="";
       USB.print("Exit\n\r");
      break;
      default:
       menu();
      break;
      }//end switch sst
      };//end parsecmd
      //input cmd from terminal,send it to parsecmd()
      USB.on('data', function (data) {
      var i;
      sst+=data;
      USB.print(data);
      if(sst.length>0)
      if(sst.charAt(sst.length-1)==="\r")parse­cmd();
      });
      //Espruino replies here
      LoopbackA.on('data',function(data){
      USB.print(data); //sending data to terminal
      });
      /////////////////////////
      setTimeout(function () {
      var x;
      console.log("Start");
      LoopbackB.setConsole();
      x=test();
      if(x!==0)console.log("Try again");//error seen
      if(x===0) console.log("Exit Seen");
      }, 1000);
      

    And the WebIde output:

    >echo(0);
    =undefined
    Start
    -> LoopbackB
    <- USB
    Start connection process
    Try again
    Reset the ESP8266
    Connecting to WiFi
    >
    Select using digit and return key
    1 LED Off
    2 LED On
    0 Exit
    Wi-Fi Connected
    IP=  192.168.1.4
    null
    >1
    LED Off
    2
    LED On
    1
    LED Off
    0
    Exit
    <- LoopbackB
    =undefined
    =undefined
    >USB.setConsole();
    -> USB
    

    Notice that the 0 Exit restores the console to the USB port.
    The Server output on the Putty terminal:

    Close false
    LED Off
    Close false
    LED On
    Close false
    LED Off
    >
    

    1 Attachment

About