Server is at 0.0.0.0 & won't connect with my code. Works with example code.

So, I've hit my head against this wall enough times I thought I'd ask for some advice. I found an example online for controlling an LED over Ethernet. I tested it out & it works perfectly.

I modified the code to control a stepper motor, rather than an led, & everything died. I'm now getting a serial output of the server is at 0.0.0.0, rather than 192.168.1.3 like it was for the led example. The only things I changed were setting up additional pins for the motor, replacing the led control code with motor control code, & a couple lines of debugging code in various loops (which I've commented out now), just to see where in the code it was failing.

It appears my issue is here:

  //start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  Serial.print("Server is at ");
  Serial.println(Ethernet.localIP());
  Serial.println("Stepper Motor Controller Test 1.0");
}
//-------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------

void loop() {

  //Serial.println("Made it to loop");  //Checking to see if program made it to loop
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");

    while (client.connected())
    { 
      //Serial.println("Made it to while loop");  //Checking to see if program made it to while loop
      if (client.available())

For the led example, everything goes just fine here, it prints "Server is at 192.168.1.3 & I can access the site at http://192.168.1.3:8081.

However, for my code, it prints "Server is at 0.0.0.0". For my debugging, I added in a serial print as soon as the void loop begins & "Made it to loop" prints. However, the print after while (client.connected()) never happens. It skips that entire if (client) block & goes right down to my else print "Client not available".

In the next messages, I have the two codes posted. (Well, I will once I wait the 5 minutes newbies need apparently).

First is my code, second is the working example. Using Notepad++'s compare plugin, I've determined I didn't change anything that should be affecting the actual server itself. But I'm obviously missing something, or else things would be working.

Any suggestions?

Your issue is in the code you did NOT post....

Regards, Ray L.

RayLivingston:
Your issue is in the code you did NOT post…

Regards,
Ray L.

Right… Like I said, I had to wait 5 minutes, since I’m under 100 posts.

The working example code can be found at this link. I’ll also be posting it, once my next 5 minutes are up

Here’s my code:

/*
  Web Server
  A simple web server
  Circuit:
  Ethernet shield attached to pins 10, 11, 12, 13
*/
//-------------------------------------------------------------------------------------------------------
#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address and IP address for your controller below.
byte mac[] = {0xA8, 0x61, 0x0A, 0xAE, 0x0E, 0x5D };

// The IP address will be dependent on your local network:
// assign an IP address for the controller:

IPAddress ip(192, 168, 1, 3);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);

// Initialize the Ethernet server library with the port you want to use.
EthernetServer server(8082);
String readString;
//-------------------------------------------------------------------------------------------------------
//-------------------------------------------------
// Any extra codes for Declaration :

// Declare Pin 8 as an LED because thats what we will be connecting the LED to.You could use any other pin and would then have to change the pin number.
int led = 8;
int enablePin = 4;
int directionPin = 3;
int stepPin = 2;
int numberOfSteps = 400;
int pulseWidthMicros = 20;  // microseconds
int millisbetweenSteps = 10; // milliseconds - or try 1000 for slower steps

//-------------------------------------------------
//-------------------------------------------------------------------------------------------------------
void setup()
{
  //-------------------------------------------------

  // Extra Set up code:
  pinMode(led, OUTPUT); //pin selected to control
  pinMode(enablePin, OUTPUT);
  pinMode(directionPin, OUTPUT);
  pinMode(stepPin, OUTPUT);

  //-------------------------------------------------
  //-------------------------------------------------------------------------------------------------------
  //enable serial data print
  Serial.begin(9600);

  //start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  Serial.print("Server is at ");
  Serial.println(Ethernet.localIP());
  Serial.println("Stepper Motor Controller Test 1.0");
}
//-------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------

void loop() {

  //Serial.println("Made it to loop");  //Checking to see if program made it to loop
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");

    while (client.connected())
    { 
      //Serial.println("Made it to while loop");  //Checking to see if program made it to while loop
      if (client.available())

      {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100)

        {

          //store characters to string
          readString += c;
          //Serial.print(c);


          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 HTTP request has ended
          if (c == '\n') {
            Serial.println(readString); //print to serial monitor for debuging
            //--------------------------------------------------------------------------------------------------------
            // Needed to Display Site:
            client.println("HTTP/1.1 200 OK"); //send new page
            client.println("Content-Type: text/html");
            client.println();
            client.println("<HTML>");
            client.println("<HEAD>");

            //--------------------------------------------------------------------------------------------------------
            //-------------------------------------------------

            // what is being Displayed :

            client.println("<TITLE>Stepper Motor Control</TITLE>");
            client.println("<center>");
            client.println("</HEAD>");
            client.println("<BODY>");
            client.println("<H1>Individual Motor Control/H1>");
            client.println("<hr />");
            client.println("<center>");

            client.println("<a href=\"/?clockwise\"\">Turn Motor Clockwise</a>");
            client.println("
");
            client.println("
");
            client.println("<a href=\"/?counterclockwise\"\">Turn Motor Counter-Clockwise</a>
");

            client.println("</BODY>");
            client.println("</HTML>");

            delay(1);
            //stopping client
            client.stop();

            //Testing server set up with light on//

            digitalWrite(led, HIGH);

            Serial.println("Server is set up");


            //-------------------------------------------------
            // Code which needs to be Implemented:
            if (readString.indexOf("?clockwise") > 0) //checks for on
            {
              digitalWrite(enablePin, LOW);    // enable motor
              Serial.println("Motor Enabled");

              digitalWrite(directionPin, HIGH);
              for (int n = 0; n < numberOfSteps; n++)
              {
                digitalWrite(stepPin, HIGH);
                delayMicroseconds(pulseWidthMicros); // this line is probably unnecessary
                digitalWrite(stepPin, LOW);

                delay(millisbetweenSteps);

              }

              digitalWrite(enablePin, HIGH);
              Serial.println("Disabled");
            }

            else {
              if (readString.indexOf("?counterclockwise") > 0) //checks for off
              {
                digitalWrite(enablePin, LOW);   //enable motor
                Serial.println("Motor Enabled");

                digitalWrite(directionPin, LOW);

                for (int n = 0; n < numberOfSteps; n++) {
                  digitalWrite(stepPin, HIGH);
                  // delayMicroseconds(pulseWidthMicros); // probably not needed
                  digitalWrite(stepPin, LOW);

                  delay(millisbetweenSteps);

                }

                digitalWrite(enablePin, HIGH);
                Serial.println("Disabled");

              }
            }
            //clearing string for next read
            readString = "";

            // give the web browser time to receive the data
            delay(1);
            // close the connection:
            client.stop();
            Serial.println("client disonnected");

          }
        }
      }
    }
  }

  /*else {
    Serial.println("Client not available");  //Didn't enter client loop
  }
*/
}
[\code]



The working example code can be found at [url=https://www.instructables.com/id/Easy-Basic-Arduino-Ethernet-Controller/]this link.[/url] I'll also be posting it, once my next 5 minutes are up.

Here’s the example code, with my edits to my correct MAC & IP.

**Yes, I know the server is different. I tried a few ports, just in case one was tied up. I’ve tried them with the same & different ports, with the same results.

/*
  Web Server
A simple web server
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
*/
//-------------------------------------------------------------------------------------------------------
#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address and IP address for your controller below.
byte mac[] = {0xA8, 0x61, 0x0A, 0xAE, 0x0E, 0x5D };

// The IP address will be dependent on your local network:
// assign an IP address for the controller:

IPAddress ip(192,168,1,3);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255, 255, 255, 0);

// Initialize the Ethernet server library with the port you want to use.
EthernetServer server(8081);
String readString;
//-------------------------------------------------------------------------------------------------------
//-------------------------------------------------
// Any extra codes for Declaration :

// Declare Pin 8 as an LED because thats what we will be connecting the LED to.You could use any other pin and would then have to change the pin number.
int led = 8;

//-------------------------------------------------
//-------------------------------------------------------------------------------------------------------
void setup()
{
//-------------------------------------------------

// Extra Set up code:
pinMode(led, OUTPUT); //pin selected to control

//-------------------------------------------------
//-------------------------------------------------------------------------------------------------------
//enable serial data print
  Serial.begin(9600);

  //start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  Serial.print("Server is at ");
  Serial.println(Ethernet.localIP());
  Serial.println("LED Controller Test 1.0");
}
//-------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------

void loop()
{
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client)

  {
    Serial.println("new client");

    while (client.connected())
    {
      if (client.available())

      {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100)

        {

          //store characters to string
          readString += c;
          //Serial.print(c);


          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 HTTP request has ended
          if (c == '\n') {
            Serial.println(readString); //print to serial monitor for debuging
//--------------------------------------------------------------------------------------------------------
// Needed to Display Site:
client.println("HTTP/1.1 200 OK"); //send new page
            client.println("Content-Type: text/html");
            client.println();
            client.println("<HTML>");
            client.println("<HEAD>");

//--------------------------------------------------------------------------------------------------------
//-------------------------------------------------

// what is being Displayed :     
        
            client.println("<TITLE>Home Automation</TITLE>");
             client.println("<center>");
            client.println("</HEAD>");
            client.println("<BODY>");
            client.println("<H1>Home Automation</H1>");
            client.println("<hr />");
            client.println("<center>");

            client.println("<a href=\"/?lighton\"\">Turn On Light</a>");
            client.println("
");
            client.println("
");
            client.println("<a href=\"/?lightoff\"\">Turn Off Light</a>
");     

            client.println("</BODY>");
            client.println("</HTML>");

            delay(1);
            //stopping client
            client.stop();

            //-------------------------------------------------
            // Code which needs to be Implemented:
            if(readString.indexOf("?lighton") >0)//checks for on
            {
              digitalWrite(8, HIGH);    // set pin 8 high
              Serial.println("Led On");
            }
            else{
              if(readString.indexOf("?lightoff") >0)//checks for off
              {
                digitalWrite(8, LOW);    // set pin 8 low
                Serial.println("Led Off");
              }
            }
            //clearing string for next read
            readString="";

            // give the web browser time to receive the data
            delay(1);
            // close the connection:
            client.stop();
            Serial.println("client disonnected");

          }
        }
      }
    }
  }
}

Are you sure that IP is available on the router? That would imply there is likely only ONE other device on that network…

Why not simply use DHCP?

Regards,
Ray L.

RayLivingston:
Are you sure that IP is available on the router? That would imply there is likely only ONE other device on that network…

Why not simply use DHCP?

Regards,
Ray L.

I’m positive it is available-the only two devices connected to this router are my laptop at 192.168.1.2 & the arduino. This router isn’t actively used in any network at the moment. I’ve also scanned using Angry IP scanner & confirmed these are the only 2 devices.

I posted here about the issues I had using DHCP. Using the plain Webserver example worked fine, but DHCP did not.

I’m not very savvy when it comes to networking though, so I’m not entirely sure what I’m doing incorrectly when it comes to this.

static void begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet)

Juraj: static void begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet)

I'm not entirely sure what to do with this. I tried inserting into my code after the ip, gateway, & subnet assignments, but i still get server at 0.0.0.0.

~`~

Another development, I've been going through & changing the code line by line from the example to see what breaks it.

I added the pinMode(enablePin, OUTPUT); line & it sent back 0.0.0.0.

I changed the pin for enable to 23 & it worked.

Doing some more digging, pins 10-13 are not the only ones reserved for the shield.... Pin 4 (why pin 4??) is also taken up for the SD card reader.

Well, thanks to everyone for the help. I'll probably be back, hopefully not due to a simple problem I ballooned into a complex one again.

you missed the dns server IP parameter you should have Ethernet.begin(mac, ip, gateway, gateway, subnet); but you can use Ethernet.begin(mac, ip); because your values are the same as the default values evaluated by the library