Simple LAN Server

I have a Uno + Ethernet Shield.

I have a simple TCP Client developed in LabVIEW running on my Laptop.

Have managed to connect the Client to the Ethernet Shield.

The only requirement is as follows :

  • The client will send one of these three phrases AD, AE, AG.
  • The Server must read these and based on the phrase received , send back a String response ( about 50 bytes )

I have managed to get the link working with following code:

void loop() { // listen for incoming clients char buf1[] = "REPORTING OK FROM HERE"; char buf2[] = "LOOKS WE HAVE A PROBLM"; char buf3[] = "WAITING FOR COMMAND..."; EthernetClient client = server.available(); server.write( "NOTHING TO REPORT" ); delay(10); }

My requirement is to be able to read the incoming message from client and respond with one of the readymade replies as above.

Any examples or ideas ??

I modified some server test code to include your AD type commands. You can test with a browser. Send your AD type commands using the text box and your replies should appear in the returned page.
Edit, added else to code

//zoomkat 12-08-12
//get submit box code
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html or use a '
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields
//note that the below bug fix may be required
// http://code.google.com/p/arduino/issues/detail?id=605 

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port

String readString; 

//////////////////////

void setup(){

  pinMode(5, OUTPUT); //pin selected to control
  readString = "Read to test";
  pinMode(5, OUTPUT); //pin selected to control
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  server.begin();

  //enable serial data print 
  Serial.begin(9600); 
  Serial.println("server text box test1"); // so I can keep track of what is loaded
}

void loop(){
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string 
          readString += c; 
          //Serial.print(c);
        } 

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString); //see what was captured

          //now output HTML data header

          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();

          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>Arduino GET test page</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");
          client.println("
");
          if(readString.indexOf("AD") >0)//checks for on
          {
            client.println("REPORTING OK FROM HERE");
           }          
           else if (readString.indexOf("AE") >0)//checks for on
          {
            client.println("LOOKS WE HAVE A PROBLM");
           }          
            else if (readString.indexOf("AG") >0)//checks for on
          {
            client.println("WAITING FOR COMMAND...");
           }                   
          else client.println("NOTHING TO REPORT");
          
          client.println("
");
          client.println("
");

          client.println("<H1>HTML form GET example</H1>");

          client.println("<FORM ACTION='/' method=get >"); //uses IP/port of web page

          client.println("Pin 5 'on5' or 'off5': <INPUT TYPE=TEXT NAME='LED' VALUE='' SIZE='25' MAXLENGTH='50'>
");

          client.println("<INPUT TYPE=SUBMIT NAME='submit' VALUE='Change Pin 5!'>");

          client.println("</FORM>");

          client.println("
");

          client.println("</BODY>");
          client.println("</HTML>");

          delay(1);
          //stopping client
          client.stop();

          /////////////////////

          if(readString.indexOf("on5") >0)//checks for on
          {
            digitalWrite(5, HIGH);    // set pin 5 high
            Serial.println("Led On");
          }
          if(readString.indexOf("off5") >0)//checks for off
          {
            digitalWrite(5, LOW);    // set pin 5 low
            Serial.println("Led Off");
          }
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}

Thanks Zoomcat. Your solution is good for communicating through a browser. But my requirement is for a discrete TCP CLient that I have developed with LabVIEW. It simply sends the required command to the server and reads back the response. The code below is working …but with a issue. It only reads the first command that I send after launching the TCP Client and sends the response to all the other two choices. If I need to get a different response then I have to stop the TCP Client and relaunch. Then it takes another command. It is as if the first command gets locked somewhere. ( I have checked my TCP Client - it does send the command that I choose ) The code is below :

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 10);

// Initialize the Ethernet server library
// with the IP address and port you want to use

EthernetServer server(9999);

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
    // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}


void loop() 
{
  // listen for incoming clients
  char buf1[] = "REPORTING OK FROM HERE";
  char buf2[] = "YOU PRESSED PB B !!...";
  char buf3[] = "WAITING FOR COMMAND...";
  
  String readString;

  EthernetClient client = server.available();

  if (client) {
    
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        if (readString.length() < 2) {
         
          readString += c; 
          
        } 
        switch (readString[1])
        {
          case 'A':
          server.write( buf1 );
          break;

          case 'B':
          server.write( buf2 );
          break;

          case 'C':
          server.write( buf3 );
          break;

          default:
          server.write("0");
          
        }
       
      }
    }
  }
   
   delay(500);
}

Could you check where is that the code is failing ?

Your solution is good for communicating through a browser.

That is why I posted code that is tested using a browser. Most popular browsers are well designed and tested, so if issues are encountered, then the issue is usually in user developed code. If my code did not work, the logical conclusion would be that my code has issues and not the browser. The problem you have is that you are using user developed arduino code and user developed labview code, and don't have a good way to determine which code (could be both) has the issue.

You are very right in that to keep the variants to minimum when things are not working. But in this case my Labview TCP client is a proven one and it has good trace facilities for debugging.

My problem is this :

Once I send the first command , it is executed by the Uno and then it gets stuck in the loop "while(client.connected()) {...}".

It is unable to come out of this loop till I stop the TCP Client physically. I have tried client.stop() at various points to no avail.

So how to get out of the loop after executing the Server.write() ?

So how to get out of the loop after executing the Server.write() ?

I suggest you go back and make simple arduino server code that will repeatedly answer the labview client request with something simple like "I got request". Once you have consistent responses to the client request, then start expanding your server code until you add something that causes the issue.

Yes it was back to basics. I downloaded a simple TCP ClientServer application from the net, just to rule out the possibility of a bug in my Labview TCP CLient.

Fortunately the behaviour was exactly the same as below:

  • Upon connection for the first time of the TCP Client to the Arduino Server, it responds as expected and sends in the expected string to the query I send from the client.
  • But subsequent different queries bring up the same response from the server.
  • I have to disconnect the Client and then reconnect to change the message.

I have now achieved the functionality by changing the statement “while (client.connected()) {…}” to “while ( count < 4 ) {…}”. The count is a char variable and reset outside the while loop.

Here is the code of the LANServer :

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 10);

// Initialize the Ethernet server library
// with the IP address and port you want to use

EthernetServer server(9999);

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
    // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}


void loop() 
{
  // listen for incoming clients
  char buf1[] = "RESPONSE FOR AAAA";
  char buf2[] = "RESPONSE FOR BBBB";
  char buf3[] = "RESPONSE FOR CCCC";
  
  String readString;
  char count;

  EthernetClient client = server.available();

  if (client) {                        // Wait for the client to send a request
    
    while (count < 3) {                // Reads the character sent by the client into a String variable. 
      if (client.available()) {
        char c = client.read();
         count ++;     
         readString += c; 
        }
    } 
        count = 0;
        Serial.println( readString);
        if ( readString[0] == 'A' ) server.write( buf1 ); 
        if ( readString[0] == 'B' ) server.write( buf2 );
        if ( readString[0] == 'C' ) server.write( buf3 );
      }
      
   delay(1);
}