update problems arduino ethernet

I am using the ethernet shield to connect to my server. Everything works fine except that if the information updates on the server than it is not updated in arduino. Is there something that could be done?

#include <Ethernet.h>

byte mac[] = { 02, 00, 00, 00, 00, 01 };
byte ip[] = { 85, 225, 87, 124};
byte server[] = { 69, 89, 22, 133 }; // Google
byte gateway[] = { 85, 225, 84, 1 };
byte subnet[] = { 255, 225, 252, 0 }; 

Client client(server, 80);

void setup()
{
  Ethernet.begin(mac, ip, gateway, subnet);
  Serial.begin(9600);
  
  delay(1000);
  
   // initialize the LED pins:
       for (int thisPin = 2; thisPin < 7; thisPin++) {
         pinMode(thisPin, OUTPUT);
       }
  
  Serial.println("connecting...");
  
  if (client.connect()) {
    Serial.println("connected");
    client.print("GET /~cameron5/test_etherent.html");
    client.println();
  } else {
    Serial.println("connection failed");
  }
}

void loop()
{   
  if (client.available()) {
    int inByte = client.read();
     // do something different depending on the character received.  
     // The switch statement expects single number values for each case;
     // in this exmaple, though, you're using single quotes to tell
     // the controller to get the ASCII value for the character.  For 
     // example 'a' = 97, 'b' = 98, and so forth:
     switch (inByte) {
     case 'a':    
       digitalWrite(2, HIGH);
       break;
     case 'b':    
       digitalWrite(3, HIGH);
       break;
     case 'c':    
       digitalWrite(4, HIGH);
       break;
     case 'd':    
       digitalWrite(5, HIGH);
       break;
     case 'e':    
       digitalWrite(6, HIGH);
       break;
     default:
       // turn all the LEDs off:
       for (int thisPin = 2; thisPin < 7; thisPin++) {
         digitalWrite(thisPin, LOW);
       }
     }
    char c = client.read();
    Serial.print(c);
  }
  
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    for(;;)
      ;
  }
}

It looks to me like you establish a connection in setup. Then, in loop, you check to see if the client is available. If it is, you read one byte, and do something with it.

Then, you read another byte, and print it.

Then, if you are not connected, you disconnect and go into an infinite loop.

Loop repeats. When you get to the end of the file, client.read will (or should) return a -1.

When the GET is issued, the test_ethernet.html file is streamed back.

When the end of the stream is reached, no more data is sent. This is normal behavior.

Is this not what you are expecting to happen?

Why do you read only 2 characters in each execution of loop? Why not set up a loop to read them all? Why do you not check for an end of stream condition?

What are you really trying to do?

So I asked more questions than you did/ So what? :)

Well this is perhaps how far I have got with the sketch. Not really an example of a failing sketch. although alot of this code is borrowed from other tutorials so I really don’t know what I am doing. But I have rearranged the code based on my original attempts to get the code updating.

#include <Ethernet.h>

byte mac[] = { 02, 00, 00, 00, 00, 01 };
byte ip[] = { 85, 225, 84, 123};
byte server[] = { 69, 89, 22, 133 }; // Google
byte gateway[] = { 85, 225, 84, 1 };
byte subnet[] = { 255, 225, 252, 0 }; 

Client client(server, 80);

void setup()
{
  Ethernet.begin(mac, ip, gateway, subnet);
  Serial.begin(16000);
  
  delay(1000);
  
   // initialize the LED pins:
       for (int thisPin = 2; thisPin < 7; thisPin++) {
         pinMode(thisPin, OUTPUT);
       }
  
  Serial.println("connecting...");

}

void loop()
{   
  
   if (client.connect()) {
    Serial.println("connected");
    client.print("GET /~cameron5/test_etherent.html");
    client.println();
  } else {
    Serial.println("connection failed");
  }
  
  if (client.available()) {
     
     switch (client.read()) {
     case 'a':    
       digitalWrite(2, HIGH);
       break;
     case 'b':    
       digitalWrite(3, HIGH);
       break;
     case 'c':    
       digitalWrite(4, HIGH);
       break;
     case 'd':    
       digitalWrite(5, HIGH);
       break;
     case 'e':    
       digitalWrite(6, HIGH);
       break;
     default:
       // turn all the LEDs off:
       for (int thisPin = 2; thisPin < 7; thisPin++) {
         digitalWrite(thisPin, LOW);
       }
     }
    char c = client.read();
    Serial.print(c);
  }
  
}

You are probably able to tell that I am just trying now to control a few leds via an updated html file. As per your questions.

Why do you read only 2 characters in each execution of loop?
I am a little unsure what you mean. I am using an html file that just has one figure in the file.

Why not set up a loop to read them all?
I only have one letter in the file that changes frequently.

Why do you not check for an end of stream condition?
Didn’t know that I should do it.

What are you really trying to do?
I want to use the character in the file that I am pulling from to change which led I have on.

On top of what Paul said, I would suggest using the ethernet library mentioned in this thread. That ethernet library supports DNS and DHCP with the arduino ethernet shield and is much faster. That way the only thing you have to hardcode in your code is the MAC address, everything else is gathered by the DNS and DHCP services built into that library. See the WebClientWithDNSandDHCP example in the zip, it shows dumping the contents of a web page to the serial device.

I really don't know what I am doing.

Maybe this will help.

client.connect()

This command establishes a connection with the web server.

client.print("GET /~cameron5/test_etherent.html");
client.println();

These commands get a specific file from the server. The server streams the contents of the file, along with some header data.

client.available()

This command checks to see if there was a response from the server.

client.read()

This command, and the one like it at the bottom of the loop, reads a single byte from the stream. You read the first byte, and do something with it. The 2nd byte is just written to the serial monitor.

The loop function is called in an endless loop. Each time it is called, you are requesting that the same file be sent to you again. You then read the same two bytes, and discard the rest.

For now, forget the led stuff. Do this:

char c;

void loop()
{
   if (client.connect())
   {
     Serial.println("connected");
     client.print("GET /~cameron5/test_etherent.html");
     client.println();
   }
   else
   {
      Serial.println("connection failed");
   }

   if(client.available())
   {
       while(c = client.read() != -1)
       {
           Serial.print(c);
       }
       Serial.println("End of file reached...");
   }
}

I think that you will see that there is more being streamed back to you than you are aware of.

Thank you for taking your time with such a newbie.
I have tried your code based on your specifications but I don’t think I did it properly. I tried several tactics. But this is what I thought may have been the closest to what you were thinking.

#include <Ethernet.h>

byte mac[] = { 02, 00, 00, 00, 00, 01 };
byte ip[] = { 85, 225, 87, 174};
byte server[] = { 69, 89, 22, 133 }; // Google
byte gateway[] = { 85, 225, 84, 1 };
byte subnet[] = { 255, 225, 252, 0 }; 

Client client(server, 80);
char c;

void setup()
{
  Ethernet.begin(mac, ip, gateway, subnet);
  Serial.begin(20600);
  
  delay(1000);
  
   // initialize the LED pins:
       for (int thisPin = 2; thisPin < 7; thisPin++) {
         pinMode(thisPin, OUTPUT);
       }
  
  Serial.println("connecting...");
  
 
}

void loop()
{   
   if (client.connect()) {
    Serial.println("connected");
    client.print("GET /~cameron5/test_etherent.html");
    client.println();
  } else {
    Serial.println("connection failed");
  }
  
  
  if (client.available()) {
    int inByte = client.read();
     // do something different depending on the character received.  
     // The switch statement expects single number values for each case;
     // in this exmaple, though, you're using single quotes to tell
     // the controller to get the ASCII value for the character.  For 
     // example 'a' = 97, 'b' = 98, and so forth:
     switch (inByte) {
     case 'a':    
       digitalWrite(2, HIGH);
       break;
     case 'b':    
       digitalWrite(3, HIGH);
       break;
     case 'c':    
       digitalWrite(4, HIGH);
       break;
     case 'd':    
       digitalWrite(5, HIGH);
       break;
     case 'e':    
       digitalWrite(6, HIGH);
       break;
     default:
       // turn all the LEDs off:
       for (int thisPin = 2; thisPin < 7; thisPin++) {
         digitalWrite(thisPin, LOW);
       }
     }
     while(c = client.read() != -1)
       {
           Serial.print(c);
       }
       Serial.println("End of file reached...");
  }
  
}

It does not seem to update based when I change my file and the more interesting thing is maybe based on the speed of my serial begin I receive different results. Fore instance; when I have a high speed 9600 there is always a connection failure. When it is a low speed it (20700) it show this in my serial.

Ùm
y[ch156]áÛh

yºyÜÙl[ch139][ch156]ìyºl
[ch140]Ú'<MC
+LÚ]o
VkLÛ}¡[ch156](N+LÛ=á
Ù-
y}áÛh
LÛ}á[ch156]hN[ch155]L-Má
Ù-
y}á[ch156]Ð\M#Ôm'ùMM
+L-Má
Ùm
y[ch156]áÛh

yº¡[ch156](
+LÛ}á[ch156]h
[ch155]      9[ch156]áÛh
      y}á[ch156]Ð\M#[ch148]mgùMM
+LÚ'<MCV+L[ch156]»_

+LÛ}á[ch156]h
[ch155]      9ÜáÛh
      yºá[ch156]Ð
M#[ch128]M[ch154]]MR
ñ«[ch156]
zmNç[ch154]]
+L-Má
Ùm
y[ch156]áÛh

yºá[ch156]Ð
M#Ôm'ùMM
+
Ú]h


I added an updateLEDS routine to make the code easier to read. I also added the ability to specify an ‘x’ in your data file on the server to stop the arduino from polling the server.

boolean updateLEDS(char aChar)
{
     boolean isDone = false;
     switch (aChar) {
     case 'a':
       digitalWrite(2, HIGH);
       break;
     case 'b':
       digitalWrite(3, HIGH);
       break;
     case 'c':
       digitalWrite(4, HIGH);
       break;
     case 'd':
       digitalWrite(5, HIGH);
       break;
     case 'e':
       digitalWrite(6, HIGH);
       break;
     case 'x':
         isDone = true;
         break;
     default:
       // turn all the LEDs off:
       for (int thisPin = 2; thisPin < 7; thisPin++) {
         digitalWrite(thisPin, LOW);
       }
     }
  return isDone;
}

Replace the appropriate section of your code with the following. It will poll the data file on the server every 5 seconds. I verified this works and prints the appropriate character out from your server with my setup (atmega168 based).

   Client client(server, 80);
    
    Serial.println("connecting...");

    boolean sessionDone = false;
    boolean serverDone = false;
    char c;

    while ( !serverDone )  {
      if (client.connect()) {
        Serial.println("connected");
        client.println("GET /~cameron5/test_etherent.html");
        client.println();
      
        while(!sessionDone  && !serverDone){  
          if (client.available()) {
            c = client.read();
            serverDone = updateLEDS(c);
            Serial.println("results:");
            Serial.println(c);
          }
  
          if (!client.connected()) {
             Serial.println();
             Serial.println("disconnecting.");
             client.stop();
             sessionDone = true;
           }
        }  // while(!sessionDone)
        
      
      
      } //if(client.connect())   
      else {
        Serial.println("connection failed");
      }
      sessionDone = false;
      delay(5000);
    }

It looks like the connection failure and corrupt data are being caused by a baud rate mismatch.

I was curious about the Serial.begin(16000) statement in your original code, and the 20600 in your latest code. Neither of those values are available in the serial monitor list.

Where did they come from?

thnxs guys got it working:)!!!