Server on Arduino Ethernet fails

Hello everybody !

I reordered my sketchcode and saw ... that it makes 33098 bytes ! :fearful:

So I modified it to obtain a 32000 bytes sketch.

I think I can decrease the size of the sketch by using the Ethernet2.h library (normally 2,5Ko less and the sketch will be under 30720 bytes).
The problem is that it generates more compilation errors ...

(I join a file name "Erreurs.txt" where you can see the errors returned by the IDE)

Any idea ?

Erreurs.txt (16.1 KB)

Teleinfo_Webserver_Arduino_V0_2_c.ino (33 KB)

This is the first error. I presume the errors cascade from here.

In file included from Teleinfo_Webserver_Arduino_V0_2_c.cpp:14:
C:\Program Files (x86)\arduino-1.0.1\libraries\Ethernet2/Ethernet2.h:69: error: stray '#' in program

Thanks for your response ! ^^

But what does it means ?
I don't understand why this error appears
I haven't modify the code of this library ...

I don't understand why this error appears
I haven't modify the code of this library ...

Well, we can't see how you got the library, from where, what OS you are using, etc. So, I guess we are even.

Ok.
I'm using Windows7 on a core4 processor and the IDE is 1.0.1 version.
I take the library at this address : Google Code Archive - Long-term storage for Google Code Project Hosting.. It's the r17 revision from 2008.
I make a try tomorrow (actually, it's 3:00 AM !).

Good night !

It's the r17 revision from 2008.

Try something newer. This works for me. It has a form page. Change the network settings if necessary. Compile, upload, and open the serial monitor.

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip( 192,168,0,2 );
IPAddress gateway( 192,168,0,1 );
IPAddress subnet( 255,255,255,0 );

EthernetServer server(80);

void setup()
{
  Serial.begin(9600);
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);
  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  delay(2000);
  server.begin();
  Serial.println("Ready");
}

void loop()
{
  EthernetClient client = server.available();
  if(client) {
    boolean currentLineIsBlank = true;
    boolean currentLineIsGet = true;
    int tCount = 0;
    char tBuf[64];
    int r,t;
    char *pch;
    
    Serial.print("Client request: ");
    
    while (client.connected()) {
      while(client.available()) {
        char c = client.read();

        if(currentLineIsGet && tCount < 63)
        {
          tBuf[tCount] = c;
          tCount++;
          tBuf[tCount] = 0;          
        }

        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response
          while(client.available()) client.read();
          Serial.println(tBuf);
          pch = strtok(tBuf,"?");

          while(pch != NULL)
          {
            if(strncmp(pch,"t=",2) == 0)
            {
              t = atoi(pch+2);
              Serial.print("t=");
              Serial.println(t,DEC);             
            }

            if(strncmp(pch,"r=",2) == 0)
            {
              r = atoi(pch+2);
              Serial.print("r=");              
              Serial.println(r,DEC);
            }


            pch = strtok(NULL,"& ");
          }
          Serial.println("Sending response");
          client.write("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n<html><body><H1>TEST</H1>");

          client.write("<form method=GET>T: <input type=text name=t>
");
          client.write("R: <input type=text name=r>
<input type=submit></form>");
          
          client.write("</body></html>\r\n\r\n");
          client.stop();
        }
        else if (c == '\n') {
          currentLineIsBlank = true;
          currentLineIsGet = false;
        } 
        else if (c != '\r') {
          currentLineIsBlank = false;
        }
      }
    }
    Serial.println("done");
  }
}

You may need to remove the serial output if you don't have that feature.

edit: I don't see how you would troubleshoot this without a usb/serial port. If you don't have that adapter, I recommend you get one.

Hello,
I have the Serial-USB adapter (this is the only way to load the program in an Ethernet Arduino).

The program you made is it a file server in html ?
I adapt the ethernet settings with my MAC@, IP@, gateway@ and subnet mask.

The program you made compiles good.

When I send it to the Arduino, I have this error :

Taille binaire du croquis : 11 656 octets (d'un max de 32 256 octets)
avrdude: stk500_getsync(): not in sync: resp=0x00

What is the problem ? Is it a transfert problem ?

When in use, I would need no more messages on the serial port taken out of the data only towards the SD card and the ethernet port.
Could I comment lines " Serial. " ?

Have you successfully uploaded other programs to that device before? In Windows, there is normally a driver install involved before it will recognize the Arduino usb.

edit: I use a #define for the serial debugging stuff, but you can comment them out.

// in the global area
#define serialDEBUG
// then later in the code
#ifdef serialDEBUG
  Serial.println("Test");
#endif

I integrate the conditional debug orders.
Is that code good ?

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


#define serialDEBUG

byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x62, 0x40 };
IPAddress ip( 192,168,1,2 );
IPAddress gateway( 192,168,1,1 );
IPAddress subnet( 255,255,255,0 );

EthernetServer server(80);

void setup()
{
  Serial.begin(9600);
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);
  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  delay(2000);
  server.begin();
  Serial.println("Ready");
}

void loop()
{
  EthernetClient client = server.available();
  if(client) {
    boolean currentLineIsBlank = true;
    boolean currentLineIsGet = true;
    int tCount = 0;
    char tBuf[64];
    int r,t;
    char *pch;
    
    #ifdef serialDEBUG
    Serial.print("Client request: ");
    #endif
    
    
    while (client.connected()) {
      while(client.available()) {
        char c = client.read();

        if(currentLineIsGet && tCount < 63)
        {
          tBuf[tCount] = c;
          tCount++;
          tBuf[tCount] = 0;          
        }

        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response
          while(client.available()) client.read();
          Serial.println(tBuf);
          pch = strtok(tBuf,"?");

          while(pch != NULL)
          {
            if(strncmp(pch,"t=",2) == 0)
            {
              t = atoi(pch+2);
              Serial.print("t=");
              Serial.println(t,DEC);             
            }

            if(strncmp(pch,"r=",2) == 0)
            {
              r = atoi(pch+2);
              Serial.print("r=");              
              Serial.println(r,DEC);
            }


            pch = strtok(NULL,"& ");
          }
          #ifdef serialDEBUG
          Serial.println("Sending response");
          #endif
          
          client.write("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n<html><body><H1>TEST</H1>");

          client.write("<form method=GET>T: <input type=text name=t>
");
          client.write("R: <input type=text name=r>
<input type=submit></form>");
          
          client.write("</body></html>\r\n\r\n");
          client.stop();
        }
        else if (c == '\n') {
          currentLineIsBlank = true;
          currentLineIsGet = false;
        } 
        else if (c != '\r') {
          currentLineIsBlank = false;
        }
      }
    }
    #ifdef serialDEBUG
    Serial.println("done");
    #endif
  }
}

If I understand good, I don't have to make any change on the lines like "Serial.println(tBuf);", otherwise the code don't work ... ?

I would use that on all Serial calls if you plan on having no serial port in the final application.

When I comment out "#define serialDEBUG", none of the code from "#ifdef serialDEBUG" to "#endif" will be included in the build.

So,
I must do something like that :

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


#define serialDEBUG

byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x62, 0x40 };
IPAddress ip( 192,168,1,2 );
IPAddress gateway( 192,168,1,1 );
IPAddress subnet( 255,255,255,0 );

EthernetServer server(80);

void setup()
{
  #ifdef serialDEBUG
  Serial.begin(9600);
  #endif
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);
  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  delay(2000);
  server.begin();
  
  #ifdef serialDEBUG
  Serial.println("Ready");
  #endif
}

void loop()
{
  EthernetClient client = server.available();
  if(client) {
    boolean currentLineIsBlank = true;
    boolean currentLineIsGet = true;
    int tCount = 0;
    char tBuf[64];
    int r,t;
    char *pch;
    
    #ifdef serialDEBUG
    Serial.print("Client request: ");
    #endif
    
    
    while (client.connected()) {
      while(client.available()) {
        char c = client.read();

        if(currentLineIsGet && tCount < 63)
        {
          tBuf[tCount] = c;
          tCount++;
          tBuf[tCount] = 0;          
        }

        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response
          while(client.available()) client.read();
          #ifdef serialDEBUG
          Serial.println(tBuf);
          #endif
          pch = strtok(tBuf,"?");

          while(pch != NULL)
          {
            if(strncmp(pch,"t=",2) == 0)
            {
              t = atoi(pch+2);
              #ifdef serialDEBUG
              Serial.print("t=");
              Serial.println(t,DEC);             
              #endif
            }

            if(strncmp(pch,"r=",2) == 0)
            {
              r = atoi(pch+2);
              #ifdef serialDEBUG
              Serial.print("r=");              
              Serial.println(r,DEC);
              #endif
            }


            pch = strtok(NULL,"& ");
          }
          #ifdef serialDEBUG
          Serial.println("Sending response");
          #endif
          
          client.write("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n<html><body><H1>TEST</H1>");

          client.write("<form method=GET>T: <input type=text name=t>
");
          client.write("R: <input type=text name=r>
<input type=submit></form>");
          
          client.write("</body></html>\r\n\r\n");
          client.stop();
        }
        else if (c == '\n') {
          currentLineIsBlank = true;
          currentLineIsGet = false;
        } 
        else if (c != '\r') {
          currentLineIsBlank = false;
        }
      }
    }
    #ifdef serialDEBUG
    Serial.println("done");
    #endif
  }
}

That looks good to me. Now when you comment out the define, none of the serial stuff will be compiled.

When I get close to using all the memory in a device, I will have multiple DEBUG defines do I can compile only certain serial writes into the code.

I understand.
Any byte is precious !

I tried to load the last sketch (just before !), but I always have this f*** error :

avrdude: stk500_getsync(): not in sync: resp=0x00

My arduino is recognized on com4 port and the USB2Serial Light card is "on" (lighted green LED) and th "On" and "L9" LEDS are lighted too.

Have you successfully uploaded any other code to the device? Or is this your first?

The only thing that I can think of is the Arduino is not being reset by the usb card. There has been reports of this trouble on certain devices. It requires a reset to start the bootloader. Try pressing and releasing the reset button when the IDE starts the upload.

I try resetting the Arduno just before uploading the program on the Arduino. The result is the same ...
The Rx led blinks 3 times and stops.
I reinstalled the driver (ATMEL 8U2 USB2Serial Light), I verified the com port (com 4) ... same result !

I try to install Atmel Studio 6, but it doesn't recognise .ino files ...
It's boring me a lot !

I just took a look at the online page on the Arduino Ethernet, and it does not mention the ability to program this device through the usb/serial device. It appears that only the ICSP connector is used to program it. Look here under "Programming":

It is possible to program the Arduino Ethernet board in two ways: through the 6 pin serial programming header, or with an external ISP programmer.

The 6-pin serial programming header is compatible with FTDI USB cables and the Sparkfun and Adafruit FTDI-style basic USB-to-serial breakout boards including the Arduino USB-Serial connector. It features support for automatic reset, allowing sketches to be uploaded without pressing the reset button on the board. When plugged into a FTDI-style USB adapter, the Arduino Ethernet is powered off the adapter.

@dxw00d: My bad. I misinterpreted the ISP programmer line.

I bought an ISP programmer on the Internet because I have a bootloader problem on my Arduino Ethernet.
I shall program the bootloader to finally test my sketch.

I inform you when my problem is solved ...

Now I must search an ISP programmer program to use with the USBasp AVR Programmer i've bought. As I read here, I can use the IDE :

http://arduino.cc/en/Hacking/Programmer and http://arduino.cc/en/Hacking/Bootloader