ethernet server problems

The EtherTen combines an Uno-equivalent Arduino-compatible board and Wiznet-based Ethernet support, along with a microSD card slot and Power-over-Ethernet support. [Product page]
Post Reply
andrewjfox
Posts:14
Joined:Tue Feb 04, 2014 1:12 am
ethernet server problems

Post by andrewjfox » Wed Jun 04, 2014 12:58 pm

Hi All,

I've seen lots of older posts with ethernet problems, so hopefully mine already has a fix. I'm using the etherten, with IDE 1.0.5 I have a test app that basically sets up a server and passes data between Arduino serial port and any client connected to the server. The code is based on various web server examples, minus the web server part, and I'm using terra term as remote client to connect to server for testing

2 main problems
1. Sometimes client can't connect to server. When I reset everything then first connection works ok. If I disconnect client (terra term) then try and reconnect it only works about 50% of the time
2. I included setting led (pin 13) when client is connected, however it's always on if the IDE serial port monitor is open, and off otherwise. It doesn't seem to matter if the client is connected or not.

The server and the serial port seem somehow tied together in underlying code, since client.connected is reporting TRUE only when serial monitor is open. See code below. Any and all constructive feedback is appreciated.

Thanks

Code: Select all

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

const int pinLed = 13;

byte mac[] = {1,3,5,7,9,11};
byte ip[] = {192,168,1,100};
byte gateway[] = {192,168,1,1};
byte subnet[] = {255,255,255,0};

String cmdStr = "";

// setup server on port
EthernetServer server = EthernetServer(3333);
EthernetClient client;

void setup()
{    
  pinMode(pinLed, OUTPUT);
  digitalWrite(pinLed, LOW);
  
  Serial.begin(115200);  
  
  // static 
  Ethernet.begin(mac,ip,gateway,subnet); 
  server.begin();
  
  cmdStr = "";
  // flush serial buffer
  while(Serial.available()) {
    Serial.read();
  }
}


void loop()
{  
  client = server.available();
  
  if(client) {
    char inChar = 0;
      
    while(client.connected()) {
      digitalWrite(pinLed, HIGH);
      if(client.available()) {
        inChar = client.read();
        cmdStr += inChar;
      }
        
      // all other commands get passed through as is
      if(inChar == '\r') {
        Serial.print(cmdStr);
        delay(10);
        cmdStr = "";
      }
      
      // pass any responses back to client  
      while(Serial.available()) {
        server.write(Serial.read());
      }        
    } // end while (connected)
    digitalWrite(pinLed, LOW);
    client.stop();  
  } // end if(client)  
  
}  // end loop()


angusgr
Freetronics Staff
Freetronics Staff
Posts:853
Joined:Tue Apr 09, 2013 11:19 pm
Location:Melbourne, Australia
Contact:

Re: ethernet server problems

Post by angusgr » Thu Jun 05, 2014 12:49 am

Hi Andrew,

I don't spot anything obvious in your code, it looks pretty good.

One thought is that opening the serial monitor will always send a "reset" to the EtherTen. Is it possible that it's getting stuck somewhere, and "Resetting" it by opening the serial monitor is bringing it back, and this is creating the impression that the connection LED is being controlled by the serial monitor?

- Angus

andrewjfox
Posts:14
Joined:Tue Feb 04, 2014 1:12 am

Re: ethernet server problems

Post by andrewjfox » Thu Jun 05, 2014 11:55 am

Thanks for reply

Possible, I'm aware the monitor resets the device but hadn't considered the scenario you suggested. I'll do some more checking, but it doesn't seem likely since the LED stays on regardless of whether the ethernet client is connected, or if the ethernet cable is disconnected, even after I reset the device. I am of course assuming that the ethernet server correctly detects when the client is disconnected, which may not be the case. Considering some of the older posts reporting these type of problems are several years old, I would hope this is fixed by now but....

I did at one stage think that the serial tx buffer was not emptying unless the monitor was running, but how would it know?


Andrew

andrewjfox
Posts:14
Joined:Tue Feb 04, 2014 1:12 am

Re: ethernet server problems

Post by andrewjfox » Mon Jun 09, 2014 12:14 am

Found the problem/s

1. after stopping client with client.stop, it was necessary to restart server with server.begin

Code: Select all

   } // end while (connected)
    client.stop();  
    server.begin();
  } // end if(client)  
2. I was using D13 as a GPIO to indicate when client was connected, however I forgot that 13 is used by SPI interface to ethernet chip, so it was never going to work as expected.

No I'm able to connect and reconnect to server

angusgr
Freetronics Staff
Freetronics Staff
Posts:853
Joined:Tue Apr 09, 2013 11:19 pm
Location:Melbourne, Australia
Contact:

Re: ethernet server problems

Post by angusgr » Mon Jun 09, 2014 11:41 pm

Ah, good catches both. I didn't realise the need to call .begin() again after the client is stopped, either. Do you still need that after pin 13 is freed up?

Post Reply