Ethernet webserver example, problem

Hi,

My Ethernet shield is based on Enc28j60. I downloaded the special lib and made the replacements so that to use the code example coming with IDE. I got to a problem explained here. No ip addressing issue exists.

Final project goal:
A Java client socket running on pc, contacts the server socket of Arduino to update the servo position.

Code modification:

#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,0, 7 };

Server server(80);

void setup()
{
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.begin(9600);
}

void loop()
{
  Client client = server.available();
  if (client) {
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        Serial.print("received:  ");
        Serial.print(c);
        Serial.println();

        if (c == '\n' && currentLineIsBlank) {
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();

           client.print("Arduino Ethernet is alive...");
           client.println();
          break;
        }
        if (c == '\n') {
          currentLineIsBlank = true;
        } 
        else if (c != '\r') {
          currentLineIsBlank = false;
        }
      }
    }
    delay(1);
    client.stop();
  }
}

Now what happens is that when I address http://192.168.0.7, I have a long long wait, then nothing comes out. On Serial window I receive "GET /..." as expected, but then I continue to receive unknown characters which are printed as squares.

Pinging the Arduino+Ethernet circuit and all other experiments prove that connection is ok (0% loss).

Now if I change the code like:
if (c == '\n' && currentLineIsBlank || c=='G')

that is:

...

        if (c == '\n' && currentLineIsBlank || c=='G') {
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();

           client.print("Arduino Ethernet is alive...");
           client.println();
          break;
        }

...

which makes the if part to happen right after it receives 'Get/...' from the browser call and not to wait for the '\n' from the browser, again after a wait - but not as long as before - I get the magical "Arduino Ethernet is alive..." there on the browser and continue to receive those strange squares on serial window.

Please tell me what is the problem and how to deal with those squares?

I think use wrong library the library Ethernet.h is for Wiznet 5100...for ENC28J60 is another library you can find it google nuelectronics....i think

The ethernet card ENC28J60 need restart every 15 minutes,i made an external circuit with I.C timer 555 for this problem....the wiznet w5100 never need restart is better.... with easy code...

This may or may not help.

I haven't got around to updating it to Arduino 1.0 yet.

it's not an issue of lib type

I explicitly wrote enc28j60 to point to that I'm using the right lib.

Also, if it was the case, in no way I could get any kind of output on the browser (it didn't work at all), but I get the right response but not the ideal way.

Si:
This may or may not help.

Dr. Monk's DIY Electronics Blog: Simplified Ethernet Library for 28J60 Shield

I haven't got around to updating it to Arduino 1.0 yet.

Thank you very much, this was exactly what I needed, no more checking like "c=='\n'", very nice methods provided :slight_smile: