Web Server Questions

Using Ethernet Shield2
Using Arduino Uno R3
Using Internet Explorer

Coping a web server example as a starting point, I was trying to get a sort of generic web server template going that would load files from a SD card and handle requests from web page forms. This is the code I have and it is, well, maybe there is a better way.

Edit: To simplify things, I have removed the code as I did not post in a good way and have learned more and may not be asking the right questions.

Problems:

  1. Edit: Problem 1 removed

  2. The css file will not load. This is the load line in the html code :

(Ahhh - just thought I may need to add it to my code to handle the request for that file)

Anyway, there may be a better way. Please help. Thanks.

// Start of Code ----------------------------------------------------------------------------------------

Edit: Code removed

// End of code -----------------------------------------------------------------------------------

It would help to know what the expected result of problem #1 is.
The original code probably did NOT have italics.

To post code:

  1. Use CTRL-T in the Arduino IDE to autoformat your code.
  2. Paste the autoformatted code between code tags (the </> button)
    so that we can easily see and deal with your code.

Before posting again, you should read the three locked topics at the top of the Programming Questions forum, and any links to which these posts point.

Good Luck!

This server sketch handles htm, css, js, jpg, gif, ico and a few other file types served from the SD card. Maybe it will be of some help.
http://playground.arduino.cc/Code/WebServerST

SurferTim:
This server sketch handles htm, css, js, jpg, gif, ico and a few other file types served from the SD card. Maybe it will be of some help.
Arduino Playground - HomePage

Thanks so much for the suggestion. As near as I can determine, you are the author of this code. I am looking at it with great interest and do have some questions. Is here a good place to post them?

Yes.

Did you intend on commenting out the loopCount?

// if packet, reset loopCount
// loopCount = 0;
char c = client.read();

Since I am using Ethernet Shield 2, what should I set this at?

#include <utility/w5100.h>

For me, I would like to move the following to the main loop so the code is not so nested and easier for me to follow. I can, perhaps make the variables global for now.

EthernetClient client = server.available();
if(client) {
boolean currentLineIsBlank = true;
boolean currentLineIsGet = true;
int tCount = 0;
char tBuf[65];
int r,t;
char *pch;
char methodBuffer[8];
char requestBuffer[48];
char pageBuffer[48];
char paramBuffer[48];
char protocolBuffer[9];
char fileName[32];
char fileType[4];
int clientCount = 0;

requestNumber++;
#ifdef ServerDEBUG
Serial.print(F("\r\nClient request #"));
Serial.print(requestNumber);
Serial.print(F(": "));
#endif

// this controls the timeout
int loopCount = 0;

while (client.connected()) {
...

I see this variable requestNumber++;
Where is the declaration?

That is it for now. Thanks.

loopCount should not be commented out. I'll correct the playground code when I get home.

SurferTim:
loopCount should not be commented out. I'll correct the playground code when I get home.

You know your code better than I, but I think you may just want to remove that statement from your code completely as a few lines above it you have just declared it and set it to zero.

How about my other questions? Especially this one:

Do I need to change this for the Ethernet Shield 2?
#include <utility/w5100.h>

Actually, loopCoumt should be commented out. The entire request must arrive within 10 seconds, or your server will be subject to a DDoS attack.

As far as the library goes, I'm using the Wiznet library on Github. I'm on my iPhone. I'll post a link to it in a few hours.

Now that I seem to have worked out my IDE problems I have some questions.

In your code, is it safe to comment out the MyStuff() procedure and related items?

jack0987:
Now that I seem to have worked out my IDE problems I have some questions.

In your code, is it safe to comment out the MyStuff() procedure and related items?

No. It does the checkSockStatus function call. That frees up frozen sockets if you are attacked by a hacker or DDoS app.

SurferTim:
No. It does the checkSockStatus function call. That frees up frozen sockets if you are attacked by a hacker or DDoS app.

OK. In view of that, the includes

#include <utility/w5100.h>
#include <utility/socket.h>

I think may need to be changed for my environment. Do you know the proper setting?

No. Those should not need to be changed. The w5100.h file will include the correct header for your Wiznet IC.

edit: That presumes you changed the define at the top of the w5100.h file. This is how I have mine set for the w5100 IC.

#define W5100_ETHERNET_SHIELD // Arduino Ethenret Shield and Compatibles ...
//#define W5200_ETHERNET_SHIELD // WIZ820io, W5200 Ethernet Shield 
// #define W5500_ETHERNET_SHIELD   // WIZ550io, ioShield series of WIZnet

This is how it is set by default.

//#define W5100_ETHERNET_SHIELD // Arduino Ethenret Shield and Compatibles ...
//#define W5200_ETHERNET_SHIELD // WIZ820io, W5200 Ethernet Shield 
#define W5500_ETHERNET_SHIELD   // WIZ550io, ioShield series of WIZnet

Thanks for your reply. I am starting to understand your code better.

For clarity for me as a newbee, I would like to break up your code into four segments.
I am thinking the loop() would look something like this:

Boolean badRequest = true;

void loop()
{
EthernetClient client = server.available();
if (client)
{
WaitForRequest(client);

ParseReceivedRequest();

If (!badRequest)
{
PerformRequestedCommands();
}

client.stop();

myStuff();

}
}

What are your thoughts, comments, suggestions?

You are cleared to modify that code any way you wish. I had to keep it relatively simple because it was approaching the memory limit for the Uno. If I divided it up too much, it wouldn't fit in an Uno.