[SOLVED] ethernet loop() problem (wiz550io module)

I’ve bee racking my brain for several days now on an issue I’m having. I was hoping someone here might have an idea around the problem.

I am using a Duemilanove with the wiz550io. The product is a dual 12V controller (relay) using TIP120s. I believe the hardware is configured properly because I am able to turn the 12V ports on and off, it’s just every other time I send a command.

For testing I am using OSX’s Terminal with the nc command. Ex. nc 5000.

The problem is this: When I send a command to turn on a 12V LED it works fine. When I send a second command it does not work. When I send a third command it works again. This repeats. So, every other command does work properly.

My code has a lot of test serial outputs so I can see what’s going on. In the loop I check client.available() and see there are, say, 6 chars available. I then jump into a parse function and read it again. It now says there are 0 chars available. But this happens only every other loop pass. Each odd pass does give me the correct char count for both client.available() checks.

Any recommendations would be greatly appreciated. There’s a bit of code but decided to not cut anything out, sorry about that.

(My code was rejected because it was too long so I am cutting un-needed parts out).


This version closes connection after each command.

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

//pin setup
//wiz550io RDY pin goes high when device is ready to communicate
int WizCheckPin = 8;  //Wiz550io RDY pint
int WizResetPin = 6;  //reset Wiz550io pin
int TIP120Pin = 5;   //pin 5 on arduino for Poart 1 activation via TIP120 transistor
int TIP120Pin2 = 4;  //pin 6 on arduino for Port 2 activation 

//LED status
bool on = false;

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
#if defined(WIZ550io_WITH_MACADDRESS) // Use assigned MAC address of WIZ550io
byte mac[] = {0x00, 0x08, 0xDC, 0x1D, 0x2B, 0xBA};

IPAddress ip(10,5,1,0);
IPAddress subnet(255,255,252,0);
IPAddress gateway(192,168,1,1);
IPAddress dns_server(8,8,4,4);

#define textBuffSize 9 //length of longest command string plus two spaces for CR + LF
char textBuff[textBuffSize]; //someplace to put received text

unsigned long timeOfLastActivity; //time in milliseconds of last activity

EthernetServer server = EthernetServer(5000);
EthernetClient client = 0;   //will init object later

//flashing globals
long port1FlashInterval = 2000;
long port2FlashInterval = 2000;
bool port1FlashState = false; 
bool port2FlashState = false;
long port1FlashPreviousMillis = 0;
long port2FlashPreviousMillis = 0;
int port1LedState = LOW;
int port2LedState = LOW;

//end GLOBALs

void setup() {
 // Open serial communications and wait for port to open:
  pinMode(WizCheckPin,INPUT);  //will read status of wiz readiness
  pinMode(WizResetPin,OUTPUT);   //used to reset the wiz
  pinMode(TIP120Pin,OUTPUT);    //used to activate the TIP120 #1
  pinMode(TIP120Pin2, OUTPUT);  //used to activate the TIP120 #2
  digitalWrite(TIP120Pin,LOW);   //default value sent to TIP120 #1 is off  (or LOW)
  digitalWrite(TIP120Pin2,LOW);  //default value sent to TIP120 #2 is off (or LOW)
  digitalWrite(WizResetPin,HIGH);    //reset wiz

   while (!Serial) {

  // start the Ethernet connection and the server:
while(digitalRead(WizCheckPin) == LOW){

#if defined(WIZ550io_WITH_MACADDRESS)

 //Ethernet.begin(ip,dns_server,gateway,subnet);   //start ethernet connection
 Serial.println("using built in mac");
  Ethernet.begin(mac, ip);
  server.begin();   //start server
  Serial.print("server is at ");
}  //end setup

void loop()
 timeOfLastActivity = millis();  //used for flashing
 client = 0;
  //if server has a remote connection, create client
  EthernetClient client = server.available();
  if (client) {
    Serial.println("client is true.");
  // check to see if text received
  if (client.connected() && client.available()) 
          Serial.print("client.available() = ");
          Serial.println("client.connected && client.available");
          Serial.print("Ethernet sock = ");


  //Port 1 flash
      unsigned long port1CurrentMillis = millis();
     if(port1CurrentMillis - port1FlashPreviousMillis > port1FlashInterval)
            port1FlashPreviousMillis = port1CurrentMillis;
              if( port1LedState == LOW)
                  port1LedState = HIGH;
                 port1LedState = LOW;
  }  //end port 1 flash

 //Port 2 flash
      unsigned long port2CurrentMillis = millis();
     if(port2CurrentMillis - port2FlashPreviousMillis > port2FlashInterval)
            port2FlashPreviousMillis = port2CurrentMillis;
              if( port2LedState == LOW)
                  port2LedState = HIGH;
                 port2LedState = LOW;
    }  //end port 2 flash

    client.stop();  //according to code this will send a TCP FIN to remote connection
    Serial.println("Client Stopped.");

  }  //end if client


//every other loop works

void getReceivedText()
  char c;
  int x = 0; //test
   int charsReceived = 0;
   int charsWaiting = client.available();
  // copy waiting characters into textBuff
  //until textBuff full, CR received, or no more characters
  do {
    c = client.read();
    textBuff[charsReceived] = c;
    Serial.print("c = ");
    Serial.print("textBuff[");Serial.print(x);Serial.print("] = ");Serial.println(textBuff[x]);
  while(charsReceived <= textBuffSize && (c != '.' || c != '\n') && charsWaiting > 0);
    //why would the above code properly display (via Serial.println) the buffer
    //but the below shows a blank line
    Serial.print("the textBuff = ");
    Serial.println(". found");
    for(int y =0; y < textBuffSize; y++)

void parseReceivedText()
  // look at first character and decide what to do
  switch (textBuff[0]) {
    case '1' : doPort1Command();        break;
    case '2' : doPort2Command();       break;
    default: break;  // do nothing


void doPort1Command()
  if(textBuff[1] == 'o' && textBuff[2] == 'n' && textBuff[3] == '.')
        digitalWrite(TIP120Pin,HIGH);  //voltage on
        port1FlashState = false;
        port1LedState = true;

  if(textBuff[1] == 'o' && textBuff[2] == 'f' && textBuff[3] == 'f' && textBuff[4] == '.')
        digitalWrite(TIP120Pin, LOW);  //voltage off
        port1FlashState = false;
        port1LedState = false;
  //start port1 flashing   
  if(textBuff[1] == 'F' && textBuff[2] == 'o' && textBuff[3] == 'n' && textBuff[4] == '.')
      port1FlashState = true;
     //stop port1 flashing   
  if(textBuff[1] == 'F' && textBuff[2] == 'o' && textBuff[3] == 'f' && textBuff[4] == 'f' && textBuff[5] == '.')
        port1FlashState = false; 
        digitalWrite(TIP120Pin, LOW);  //voltage off
        port1LedState = false;
   if(textBuff[1] == 's' && textBuff[2] == '.')
         if(port1FlashState == true){
          else if(port1LedState == true)
          else if(port1LedState == false)
       }  //end if
}  //end doPort1Command()

void doPort2Command()

EDIT: I'd like to add that I've been using Wireshark. The commands being sent to the Arduino are correct. I've read up on the TCP stack to make sure nothing unusual is happening. It all looks good. Simply getting nothing back from the Arduino.

(my previous post was too long to add this EDIT).

OK. I found my own problem. Poor coding on my part.

I did not have a global EthernetClient client. It was local to my loop. I don't know why it was working every other loop though. It should not have worked at all.