Ethernet server site doesn't work

So I had a thread started, but it went dead before all of my questions were answered. It is here, but my remaining problem is all the way at the bottom.

When I go to the displayed IP address, it is said to be unavailable, and the serial monitor doesn’t respond.

My code is:

#include <Ethernet.h>



int Svr=1;
byte mac[] = {0xAD, 0xBE, 0xDE};
IPAddress ip(129, 186, 1, 172);
EthernetServer server(80);


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

void loop()
{
 Serial.print("server1 is at ");
 Serial.println(Ethernet.localIP());
 EthernetClient client = server.available();
 while (!client)
 {
  ;
 }
 Serial.println("new client");
 Svr++;
 Serial.print("server");
 Serial.print(Svr);
 Serial.print(" is at ");
 Serial.println(Ethernet.localIP());
}

The serial monitor only displays:

server1 is at 129.186.1.172

And the webpage displays:

This site can’t be reached

129.186.1.172 took too long to respond.
ERR_CONNECTION_TIMED_OUT

How is the Arduino connected to the network? An ethernet shield, obviously, but where is the other end of the ethernet cable connected?

the browser communicates with http protocol. if the server doesn't send a http response then for the browser it is an error.

KDNXZ:

 EthernetClient client = server.available();

while (!client)
{
 ;
}

If there's no client connected to your server before the while loop begins (and it's highly unlikely if not impossible for a client to connect to your server by that time), it will loop forever. Nothing inside the while loop changes the condition of the while loop.

Christop:

… Nothing inside the while loop changes the condition of the while loop.

Good point. I copied that section of code from a example or something, I think, and I expected the example to work, especially since a loop checks the condition before repeating is what I was thinking at the time… Anyways, thanks. I’ll change my code to the following:

#include <Ethernet.h>


bool LpCndtn=false
int Svr=1;
byte mac[]={0xAD, 0xBE, 0xDE};
IPAddress ip(129, 186, 1, 172);
EthernetServer server(80);


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

void loop()
{
 Serial.print("server1 is at ");
 Serial.println(Ethernet.localIP());
 EthernetClient client = server.available();
 while (LpCndtn)
 {
   if (client)
   {
     LpCndtn=true
   }
 }
  
 Serial.println("new client");
 Svr++;
 Serial.print("server");
 Serial.print(Svr);
 Serial.print(" is at ");
 Serial.println(Ethernet.localIP());
}

But wouldn’t that do the same thing? Or would that somehow allow for server to become true when the previous version doesn’t?

Juraj: So I just have to send, or more than that

PaulS: Ethernet port 4 in the modem. I believe it is called Network 8.

That doesn't solve the problem. Try this:

void loop()
{
 EthernetClient client = server.available();
 if (client) {
   Serial.println("new client");
   Svr++;
   Serial.print("server");
   Serial.print(Svr);
   Serial.print(" is at ");
   Serial.println(Ethernet.localIP());
  } else {
    // no client
  }
}

Pieter

http requires in response at least "HTTP/1.1 200 OK", but you should send other http headers and the body of the response too. see some examples in Arduino IDE Examples menu.

here is my Web server with static files serving and Ajax json responses

PieterP: Thanks, but why would you need an else statement? And could you do it like this?

EthernetClient client = server.available();
 while (!client)
 {
  EthernetClient client = server.available();
 }

Juraj: We have two very different types of web servers. I am a newbie and don't know what to say about

...at least "HTTP/1.1 200 OK", but you should send other http headers and the body...

, but could you please tell me the required lines of code for my page?

KDNXZ:
PieterP: Thanks, but why would you need an else statement?

You don't. That was just an example.

KDNXZ:
And could you do it like this?

EthernetClient client = server.available();

while (!client)
{
 EthernetClient client = server.available();
}

You could, but that would block the entire loop, which you want to avoid most of the time.

PieterP: I see. By the second comment do you only mean void loop would not continue until a client is available? If so, I’m good with that.

By the way, when I type someone’s name that only means it is a reply to their post specifically. Anyone can reply to that section.

KDNXZ:
PieterP: I see. By the second comment do you only mean void loop would not continue until a client is available? If so, I'm good with that.

Yes. In most cases, you don't want that, so I don't see why you would type the extra line here.

KDNXZ:
Juraj: We have two very different types of web servers. I am a newbie and don't know what to say about , but could you please tell me the required lines of code for my page?

There are HTTP libraries available. Why do you want to send raw HTTP yourself?
The only real way to learn HTTP is by opening the network tab in your browser's developer options, and by reading the RFCs (RFC7230-7231), or by reading answers from angry people on Stack Overflow who have read the RFCs :slight_smile:

PieterP: For your first comment, encapsulation. OCD, and encapsulation. For your second comment, I don't want to learn and deal with unnecessary libraries, and I should be able to figure out the html functions by looking them up, seeing examples of html servers, and using inspect.

That doesn't improve encapsulation, the only difference is that you have to write the same line twice, and that you block your only thread.

The HTTP library is not at all unnecessary. Parsing HTTP is hard. You won't be able to implement it yourself, unless you spend weeks digging through the specs and reinventing the wheel.

The fact that you don't know the difference between HTML and HTTP says enough, I'm afraid :wink:

KDNXZ:
could you do it like this?

EthernetClient client = server.available();

while (!client)
{
  EthernetClient client = server.available();
}

A problem there is that you have two different variables named 'client'. The one inside the while loop doesn't exist outside the while loop and the one above the while loop doesn't get set. I think what you want is:

    EthernetClient client;
    do
    {
        client = server.available();
    }
    while (!client);

The do-while statement is what you use if you want the body to execute at least once before the loop test. If you don't like the do-while statement you can avoid calling server.available() in two places with:

    EthernetClient client = 0;
    while (!client)
    {
        client = server.available();
    }
  1. Encapsulation is a vague concept, and did I mention OCD?
  2. Blocking the thread until a client is available is completely practical in this case.
  3. I may be completely wrong about this, but last time I checked, Server.print goes straight to the following:…
    4.Sometimes this feels like reinventing the wheel :smiley:
    5.I know the difference between HTML and http. Sorry if I put one in the wrong place.

Next post on next page

johnwasser: Thanks, but I think I can do the following:

EthernetClient client = server.available();
 while (!client)
 {
  client = server.available();
 }

One more thing; Changed my code to the following:

#include <Ethernet.h>



int Svr=1;
byte mac[] = {0xAD, 0xBE, 0xDE};
IPAddress ip(129, 186, 1, 172);
EthernetServer server(80);


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

void loop()
{
 EthernetClient client = server.available();
 if(!client)
 {
  if(Svr = 1)
  {
   Serial.print("Server");
   Serial.print(Svr);
   Serial.print(" is at ");
   Serial.println(Ethernet.localIP());
  }
  while(!client)
  {
   EthernetClient client = server.available();
  } 
  Svr++;
  Serial.println("New client");
 } 
 if(client)
 {
  Server.println("<!DOCTYPE html>");
  Serial.print("Server");
  Serial.print(Svr);
  Serial.print(" is at ");
  Serial.println(Ethernet.localIP());
 }
}

, and got the following error:

C:\Users\KDNXZ\AppData\Local\Temp\arduino_modified_sketch_671153\Xprmnttn__TempUse.ino: In function ‘void loop()’:

Xprmnttn__TempUse:44: error: expected unqualified-id before ‘.’ token

Server.println("");

^

Using library Ethernet at version 1.1.2 in folder: C:\Program Files\Arduino\libraries\Ethernet
Using library SPI at version 1.0 in folder: C:\Users\KDNXZ\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\libraries\SPI
exit status 1
expected unqualified-id before ‘.’ token

you have server, not Server

KDNXZ:
3. I may be completely wrong about this, but last time I checked, Server.print goes straight to the following:…

I’m afraid you are: the server is just a TCP server. You can’t just send raw HTML over it. You have to first parse the HTTP request headers, then send HTTP response headers, and finally you send the actual HTML.

KDNXZ:
One more thing; Changed my code to the following:

#include <Ethernet.h>

int Svr=1;
byte mac = {0xAD, 0xBE, 0xDE};
IPAddress ip(129, 186, 1, 172);
EthernetServer server(80);

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

void loop()
{
EthernetClient client = server.available();
if(!client)
{
  if(Svr = 1)
  {
  Serial.print(“Server”);
  Serial.print(Svr);
  Serial.print(" is at “);
  Serial.println(Ethernet.localIP());
  }
  while(!client)
  {
  EthernetClient client = server.available();
  }
  Svr++;
  Serial.println(“New client”);
}
if(client)
{
  Server.println(”");
  Serial.print(“Server”);
  Serial.print(Svr);
  Serial.print(" is at ");
  Serial.println(Ethernet.localIP());
}
}



, and got the following error:

You’ll have to fix your while loop, as johnwasser suggested in his reply.

Juraj: Thanks. Is there a face-palm emoji? I just looked back to the reference and it looks like client.println. I must have been tired when I started this. :o

PieterP: I think I found the source of my mistake. I think I want to use client.println, not server :confused: . Will this require me to use SPI.h? Oh, and not sure how that happened :frowning: . That wasn’t my code… This is:

#include <Ethernet.h>



int Svr=1;
byte mac[] = {0xAD, 0xBE, 0xDE};
IPAddress ip(129, 186, 1, 172);
EthernetServer server(80);


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

void loop()
{
 EthernetClient client = server.available();
 if(!client)
 {
  if(Svr = 1)
  {
   Serial.print("Server");
   Serial.print(Svr);
   Serial.print(" is at ");
   Serial.println(Ethernet.localIP());
  }
  while(!client)
  {
   client = server.available();
  } 
  Svr++;
  Serial.println("New client");
 } 
 if(client)
 {
  Server.println("<!DOCTYPE html>");
  Serial.print("Server");
  Serial.print(Svr);
  Serial.print(" is at ");
  Serial.println(Ethernet.localIP());
 }
}

I did do something I think will fix the while loop. I used client without re-initializing it.