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

Hello,
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 192.168.1.2 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>


//GLOBALS
//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
;
#else
byte mac[] = {0x00, 0x08, 0xDC, 0x1D, 0x2B, 0xBA};
#endif  

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:
  Serial.begin(9600);
  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,LOW);  
  delay(1000);
  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
 Ethernet.begin(ip);
 Serial.println("using built in mac");
#else
  Ethernet.begin(mac, ip);
#endif  
  
  
  server.begin();   //start server
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
  Serial.println(Ethernet.subnetMask());
  Serial.println(Ethernet.gatewayIP());
  //Serial.println(Ethernet.dnsServerIP());
  delay(2000);
  
  
}  //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.available());
          Serial.println("client.connected && client.available");
          Serial.print("Ethernet sock = ");
          Serial.println(client);
          getReceivedText();
          
    
  }

  

  //Port 1 flash
  if(port1FlashState==true)
  {
      unsigned long port1CurrentMillis = millis();
  
     if(port1CurrentMillis - port1FlashPreviousMillis > port1FlashInterval)
        {
            port1FlashPreviousMillis = port1CurrentMillis;
        
              if( port1LedState == LOW)
                  port1LedState = HIGH;
                  
               else
                 port1LedState = LOW;
                 
                 
              digitalWrite(TIP120Pin,port1LedState);
        }
  }  //end port 1 flash

 //Port 2 flash
  if(port2FlashState==true)
  {
      unsigned long port2CurrentMillis = millis();
  
     if(port2CurrentMillis - port2FlashPreviousMillis > port2FlashInterval)
        {
            port2FlashPreviousMillis = port2CurrentMillis;
        
              if( port2LedState == LOW)
                  port2LedState = HIGH;
                  
               else
                 port2LedState = LOW;
                 
                 
              digitalWrite(TIP120Pin2,port2LedState);
        }
    }  //end port 2 flash

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

    
  }  //end if client




}

//every other loop works




void getReceivedText()
{
  char c;
  int x = 0; //test
   int charsReceived = 0;
   int charsWaiting = client.available();
   Serial.println(charsWaiting);
  // copy waiting characters into textBuff
  //until textBuff full, CR received, or no more characters
 
  
  do {
    c = client.read();
    textBuff[charsReceived] = c;
    charsReceived++;
    charsWaiting--;
    Serial.print("c = ");
    Serial.println(c);
    Serial.print("textBuff[");Serial.print(x);Serial.print("] = ");Serial.println(textBuff[x]);
    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.println();
    Serial.print("the textBuff = ");
    Serial.println(textBuff);
    Serial.println(". found");
    parseReceivedText();
    
  
    for(int y =0; y < textBuffSize; y++)
      textBuff[y]=0;
    
  
} 



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;
        client.println("1on!");
        Serial.println("1on!");
     
      }
    

  if(textBuff[1] == 'o' && textBuff[2] == 'f' && textBuff[3] == 'f' && textBuff[4] == '.')
      {
        digitalWrite(TIP120Pin, LOW);  //voltage off
        port1FlashState = false;
        port1LedState = false;
        client.println("1off!");
        Serial.println("1off!");
       
      }
      
  //start port1 flashing   
  if(textBuff[1] == 'F' && textBuff[2] == 'o' && textBuff[3] == 'n' && textBuff[4] == '.')
  {
      port1FlashState = true;
      client.println("1Fon!");
  }  
     //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;
        client.println("1Foff!");
      }
     
   if(textBuff[1] == 's' && textBuff[2] == '.')
       {
         
         
         
         if(port1FlashState == true){
           client.println("1sFOn!");
         }
           
          else if(port1LedState == true)
          {
               client.println("1sOn!");
          }
               
          else if(port1LedState == false)
          {
              client.println("1sOff!");
          }
          
          else
            client.println("1s???!");
            
            
      
       }  //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.