Problems with Arduino Server

I’m trying to set up a server on an Arduino Uno to display some values.

The shield I have is a Seeeduino W5200 Ethernet shield V2, made in 2014. I have bought a static ip for my router, and have set up port forwarding. The ip address for the shield is set at 192.168.1.125. I have played around with the port, as I want this to be on port 8080. Neither this port, nor the standard port 80, or other ports work.

Aside from changing the IP address and port, I have not changed anything else in the example code.

My problem is that I will connect to the server, but my web browser will say my connection was reset while the page was loading.

What gives?

/*
  Web Server
 
 A simple web server that shows the value of the analog input pins.
 using an Arduino Wiznet Ethernet shield. 
 
 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 * Analog inputs attached to pins A0 through A5 (optional)
 
 created 18 Dec 2009
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe
 
 */

#include <SPI.h>
#include <EthernetV2_0.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,125);

// Initialize the Ethernet server library
// with the IP address and port you want to use 
// (port 80 is default for HTTP):
EthernetServer server(8080);
#define W5200_CS  10
#define SDCARD_CS 4

void setup() {
 // Open serial communications and wait for port to open:
 Serial.begin(9600);
 pinMode(SDCARD_CS,OUTPUT);
 digitalWrite(SDCARD_CS,HIGH);//Deselect the SD card
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}


void loop() {
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connnection: close");
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
                    // add a meta refresh tag, so the browser pulls again every 5 seconds:
          client.println("<meta http-equiv=\"refresh\" content=\"10\">");
          // output the value of each analog input pin
          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
            int sensorReading = analogRead(analogChannel);
            client.print("analog input ");
            client.print(analogChannel);
            client.print(" is ");
            client.print(sensorReading);
            client.println("
");       
          }
          client.println("</html>");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } 
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disonnected");
  }
}

Please correct your post above and add code tags around your code:
[code]`` [color=blue]// your code is here[/color] ``[/code].

It should look like this:// your code is here
(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)

———

try to change the delay(1); into delay(1000); just to see if this is part of the issue

Tried it, no change.

I would like to mention that at one point during my troubleshooting I was able to connect, but when I tried again without having changed anything I could no longer connect again. This makes me think I might have a network problem, but I have no idea how to diagnose that.

Edit: Another thing to note, once I have connected for the first time, I can never reconnect. As in no additional dialog will appear in the Serial monitor saying a browser is trying to connect. I have to restart the Arduino to try again

@OP

Following information may be helpful for you.

1. When I Have executed this program: IDE--->Examples---->Ethernet---->DhcpAddressPrinter, I have got IP address for my W5100 chip:
smIPAd.png

2. When I have executed the following sketch (taken from net), I have seen my UNO to serve the following web page:

/*--------------------------------------------------------------
  Program:      eth_websrv_page

  Description:  Arduino web server that serves up a basic web
                page. Does not use the SD card.
  
  Hardware:     Arduino Uno and official Arduino Ethernet
                shield. Should work with other Arduinos and
                compatible Ethernet shields.
                
  Software:     Developed using Arduino 1.0.3 software
                Should be compatible with Arduino 1.0 +
  
  References:   - WebServer example by David A. Mellis and 
                  modified by Tom Igoe
                - Ethernet library documentation:
                  http://arduino.cc/en/Reference/Ethernet

  Date:         7 January 2013
 
  Author:       W.A. Smith, http://startingelectronics.org
--------------------------------------------------------------*/

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

// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 0, 108); // IP address, may need to change depending on network
EthernetServer server(80);  // create a server at port 80

void setup()
{
    Ethernet.begin(mac, ip);  // initialize Ethernet device
    server.begin();           // start to listen for clients
}

void loop()
{
    EthernetClient client = server.available();  // try to get client

    if (client) {  // got client?
        boolean currentLineIsBlank = true;
        while (client.connected()) {
            if (client.available()) {   // client data available to read
                char c = client.read(); // read 1 byte (character) from client
                // last line of client request is blank and ends with \n
                // respond to client only after last line received
                if (c == '\n' && currentLineIsBlank) {
                    // send a standard http response header
                    client.println("HTTP/1.1 200 OK");
                    client.println("Content-Type: text/html");
                    client.println("Connection: close");
                    client.println();
                    // send web page
                    client.println("<!DOCTYPE html>");
                    client.println("<html>");
                    client.println("<head>");
                    client.println("<title>Arduino Web Page</title>");
                    client.println("</head>");
                    client.println("<body>");
                    client.println("<h1>Hello from Arduino!</h1>");
                    client.println("<p>A web page from the Arduino server</p>");
                    client.println("</body>");
                    client.println("</html>");
                    break;
                }
                // every line of text received from the client ends with \r\n
                if (c == '\n') {
                    // last character on line of received text
                    // starting new line with next character read
                    currentLineIsBlank = true;
                } 
                else if (c != '\r') {
                    // a text character was received from client
                    currentLineIsBlank = false;
                }
            } // end if (client.available())
        } // end while (client.connected())
        delay(1);      // give the web browser time to receive the data
        client.stop(); // close the connection
    } // end if (client)
}

smWeb.png

smWeb.png

smIPAd.png

We are using different examples, but your comment did encourage me to try something else.

First off, I'm using an example from the Ethernet_Shield_W5200 Library, not the standard Ethernet library. This library is meant for the shield I am using.

Second, I went and tried to use the IP address given in the example code, as Hail Mary more than anything else, but no dice.

why don't you start testing from the LAN before trying to go through the WAN? (ie ensure the device you use to contact the arduino is within the same 192.168.1.xxx (likely) network

then of course if you try to reach your arduino going through the Internetand coming in through your router, then you should use the public IP + port (ie 4G phone --> internet --> your home router ---> your arduino on specific port)

Currently I am only testing Lan. The computer is on wifi, the shield is Ethernet, but the same network.

Also, I was able to reach the server again. Then after that I have no been able to connect to it. This is the second time I have been able to connect with no real changes in the code, port configuration, or IP address change. Something is going on with the connection.

Any tips on how I can troubleshoot that?

Really odd problem here, I am tearing my hair out.

I have an Seeeduino W5200 Ethernet Shield v2, made in 2014, with an Arduino Uno. I am using the W5200 library’s Web Server example.

The only things I’ve changed in the example code are the Ip address- changed to 192.168.1.125, and the port - changed to 8080. I have set up port forwarding in the router, shown in one of the attachments. The last thing I have changed is the wait time before client disconnect. I have changed that from 100 millis to 10 000 millis, it doesn’t seem to affect me

When I try to connect, I can see the connection being initialized in the Serial monitor, shown in one of the attachments, except the connection never sends the information to the browser. After a while of waiting, the browser will return a message saying “The connection was reset”.

HOWEVER I have been able to connect to the server twice randomly while troubleshooting. I will be able to connect, see the data that, and then not be able to connect again. Nothing will change in the code.

Currently I am only trying to connect via LAN.

/*
  Web Server
 
 A simple web server that shows the value of the analog input pins.
 using an Arduino Wiznet Ethernet shield. 
 
 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 * Analog inputs attached to pins A0 through A5 (optional)
 
 created 18 Dec 2009
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe
 
 */

#include <SPI.h>
#include <EthernetV2_0.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,125);

// Initialize the Ethernet server library
// with the IP address and port you want to use 
// (port 80 is default for HTTP):
EthernetServer server(8080);
#define W5200_CS  10
#define SDCARD_CS 4

void setup() {
 // Open serial communications and wait for port to open:
 Serial.begin(9600);
 pinMode(SDCARD_CS,OUTPUT);
 digitalWrite(SDCARD_CS,HIGH);//Deselect the SD card
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}


void loop() {
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connnection: close");
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
                    // add a meta refresh tag, so the browser pulls again every 5 seconds:
          client.println("<meta http-equiv=\"refresh\" content=\"10\">");
          // output the value of each analog input pin
          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
            int sensorReading = analogRead(analogChannel);
            client.print("analog input ");
            client.print(analogChannel);
            client.print(" is ");
            client.print(sensorReading);
            client.println("
");       
          }
          client.println("</html>");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } 
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(10000);
    // close the connection:
    client.stop();
    Serial.println("client disonnected");
  }
}

Open a telnet connection and manually send data to see what’s going on?

is MAC address and IP address unique on local network?

why port forwarding? do you access it from Internet?

We need to isolate the problem and point it to hardware or software or setup or multiple for which we need another set of Arduino and W5200 based shield. It is because the OP is reporting an intermittent problem.

I saw 60-year old Design Engineer to come down to field with a 'full set of redundant parts' to troubleshoot his own designed gadget.

@The-Devils-Engineer

Please do not cross post.
Topics MERGED.

Also could you read this before making any further posts.

Bob.

Juraj:
is MAC address and IP address unique on local network?

This is key.

For MAC, you'd have to be staggeringly unlucky to have a conflict, unless you have another ethernet capable Arduino running the same code.

IP could well clash though - what address does the router start out at when it's handling DHCP requests? Easy enough to test - turn the arduino off and ping that address.