1. Setup a TCP network server
    2. Setup a TCP client
    3. Modify ESP8266WiFi_0v25.js to do UDP and TCP
    4. Use UDP to allow clients to find the server IP address
    5. The UDP/TCP client.

    6. Setup a TCP network server
      The TCP server and client code are based on the examples listed under the Sockets section of
      http://www.espruino.com/Internet
      The following code sets up a TCP server on port 9988.
      You will need to edit:
      //var Hardware=0; //Espruino board
      var Hardware =1; //PICO
      var SSID="ssid";
      var key= "router passcode";

    When a client connects two commands are accepted “LED On” and “LED Off”

    //netServer3.js
    //2 Oct 2016
    //espruino board with ESP8266
    //PICO  with ESP8266
    var Sport=9988;
    //var Hardware=0; //Espruino board
    var Hardware =1; //PICO
    var SSID="ssid";
    var key= "router passcode";
    var Serial;
    var MyIP="";
    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(Serial, 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("IP= ",ip,"\n\r"+l);MyIP=ip;
          console.log("Wi-Fi Connected");
    // Now you can do something, 
    serve();
    
    ////////////////////////////////////////////////////////////
         });
        });
      });
    });
    }//end test
    
    var x;
    console.log("Start");
    x=test();
    if(x!==0)console.log("Try again");//error seen
    
    if(x===0) console.log("Exit Seen");
    
    //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    function serve(){
     var data;
     var server = require("net").createServer(function(c) {
      // A new client has connected
    //  c.write("Hello");
    /*  c.on('data', function(data) {
        console.log(">"+JSON.stringify(data));
      });
    */
       c.on('close',function(d){
       console.log("Close "+d);
       data=c.read(c.available());
       console.log(data);
       c.write(data);
         if(data==="LED Off"){
          digitalWrite(B12,0); //B12 is green LED
         }
         if(data==="LED On"){
          digitalWrite(B12,1);
         }
      });
      c.end();
    });
    server.listen(Sport);
    
    }
    

    The TCP server output:

    >echo(0);
    Start
    Start connection process
    Try again
    =undefined
    Reset the ESP8266
    Connecting to WiFi
    IP=  192.168.1.3
    null
    Wi-Fi Connected
    >
    

    At this point I disconnect the WebIDE from the TCP server hardware and reconnect using putty.
    Make a note of the server IP addess.


    1 Attachment

About