Can't connect to uno with ethernet shield

I have an arduino uno with an ethernet shield.

I have uploaded the ethernet=>web server example to my uno then then connected it to my adsl modem with an appropriate cable.

If I look in my ADSL modem gateway page I can see the arduino uno under the Ethernet/Wired devices.

I then set up port forwarding to uno

However no IP address is assigned to the uno and I can access by typing my public IP address into Firefox.

My ADSL modem's internal IP address is 10.0.0.138 and I have a Raspberry Pi connected to my network via wifi whose internal IP address is 10.0.0.80

So I simply chose 10.0.0.81 for the IP address in the example sketch.

So what is it that I have done wrong?

I just reset my ADSL modem and the device that was my Arduino disappeared.
So now, with the following sketch, it does not appear on my netwerk at all.

* 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 <Ethernet.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(10, 0, 0, 81);

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);

void setup() 
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) 
  {
    // wait for serial port to connect. Needed for Leonardo only
  }


  // 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 bCurrentLineIsBlank = 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' && bCurrentLineIsBlank) 
        {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");  // the connection will be closed after completion of the response
          client.println("Refresh: 5");  // refresh the page automatically every 5 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          client.println("<body>");
          client.println("<font face=\"Arial\" size=5 color=\"red\">HELLO FROM GREG'S ARDUINO UNO</font>");
          client.println("</body>");
          client.println("</html>");
          break;
        }
        if (c == '\n') 
        {
          // you're starting a new line
          bCurrentLineIsBlank = true;
        }
        else if (c != '\r') 
        {
          // you've gotten a character on the current line
          bCurrentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disconnected");
  }
}

Try that code using DHCP instead of a static IP assignment, just as a test. Does the ADSL modem see it then?

SurferTim:
Try that code using DHCP instead of a static IP assignment, just as a test. Does the ADSL modem see it then?

OK, so I changed "Ethernet.begin(mac, ip);" to "Ethernet.begin(mac);" and now my ADSL can see the arduino and has assigned it an IP address.

So why won't the static IP address work?

Some routers have a problem with devices that do not communicate with them first. Some have problems with static assignments that fall in the range of the DHCP server. I don't know which is your case.

I have used a DNS resolution in the setup function to start a communication with the router. If your problem is the former, that will help.

SurferTim:
Some routers have a problem with devices that do not communicate with them first. Some have problems with static assignments that fall in the range of the DHCP server. I don't know which is your case.

I have used a DNS resolution in the setup function to start a communication with the router. If your problem is the former, that will help.

Is there any way to add multiple http servers in the port forwarding setting of the adsl modem?

It seems that you can add only one instance of port 80.

I tried setting my arduino (in the sketch) as port 81, while the raspberry pi is port 80.

I can access the raspberry pi web page but no the uno's web page.

You can't port forward one port to two different devices. You can port forward different ports to different localnet IPs.Use port 8080 on the public IP and use port 8080 on the Arduino,

SurferTim:
You can't port forward one port to two different devices. You can port forward different ports to different localnet IPs.Use port 8080 on the public IP and use port 8080 on the Arduino,

I have a raspberry pi and an arduino uno. How would I go about providing access to both through a web page.

Is there any reason why I can set the arduino up on port 81 say in the sketch? And how do you setup the port forwarding to access that port as a html sever? I tried simply changing the port number in the port forwarding settings and then tried to access it like this "10.0.0.6/80". But no joy. So how would you do this "10.0.0.6/8080" perhaps?

I have also given both devices the names "RaspberryPi" and "Arduino" in the port forwarding settings.

That seems to break the port forwarding as in http://101.182.130.194
It appears as though the device has to have the name HTTP Server in order for this to work.

But what I have actually done is set those two names up in http://freedns.afraid.org.

I can then access the raspberry pi as GregsRaspberryPi.privatedns.org
I tried setting the uno up as 101.182.130.194/81 in freedns but that does not seem to work.
Should this be 101.182.130.194/8081 perhaps?

I have a raspberry pi and an arduino uno. How would I go about providing access to both through a web page.

Which ONE of them is serving up the web page?

PaulS:
Which ONE of them is serving up the web page?

Both of them…theoretically

I have been reading this

And I tried the port forwarding settings in the attached image, but it didn’t work.

Perhaps I am misinterpreting what the a fore mentioned web page is telling me…i.e. you can have multiple ports but all them must be different services (one of each type)

My aim is to link to more than one MCU device on my network and manipulate them through a web page interface. You must be able to do it but how?

From this:

Sure it can. The links in the web page to the arduinos that did not serve the original page need to be urls with the IP address of the arduinos. You may need to have the arduinos to send back the status 204: so the browser does not try to load a new page. Below are examples of full urls. You would use ip address.

Code: [Select]

Bird|
Sony|
Window|
Fireplace|

P/T Cam|
Fan|
Troll|
Lamp|
I-mouse|

It looks as though you have to have one ‘master’ MCU that serves up the web page to external requests and then it provides access to its own interface as well as the web page interfaces of all the other devices.

It looks as though you have to have one 'master' MCU that serves up the web page to external requests and then it provides access to its own interface as well as the web page interfaces of all the other devices.

Yes, that is one way. Probably the easiest way. At least, it's how most web servers/pages work.

Compile and upload this test sketch. Post the results. edit: Baud rate (as usual for my sketches) is 115200.

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

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {  
  0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };

void setup() {
 // Open serial communications and wait for port to open:
  Serial.begin(115200);
  // this check is only needed on the Leonardo:
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);
  
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    for(;;)
      ;
  }
  // print your local IP address:
  Serial.print("IP: ");
  Serial.println(Ethernet.localIP());
  Serial.print("Gateway: ");
  Serial.println(Ethernet.gatewayIP());
  Serial.print("Subnet: ");
  Serial.println(Ethernet.subnetMask());
  Serial.print("DNS: ");
  Serial.println(Ethernet.dnsServerIP());
  Serial.println();
}

void loop() {
}

SurferTim:
Compile and upload this test sketch. Post the results. edit: Baud rate (as usual for my sketches) is 115200.

10.0.0.93
10.0.0138
255.255.255.0
10.0.0.138

So the IP address of my arduino has changed which would explain why I could get it to port forward again.

So how can I set a static IP address given that the original example Web Server sketch doesn't work with my ADSL modem?

That is what I suspected. you are using the wrong gateway. Look for my two comments “// SurferTim…” after the declaration of ip and Ethernet.begin().

* 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 <Ethernet.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(10, 0, 0, 81);


// SurferTim added this
IPAddress gateway(10, 0, 0, 138);

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);

void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial)
  {
    // wait for serial port to connect. Needed for Leonardo only
  }


  // start the Ethernet connection and the server:


// SurferTim changed this

  Ethernet.begin(mac, ip, gateway, gateway);
  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 bCurrentLineIsBlank = 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' && bCurrentLineIsBlank)
        {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");  // the connection will be closed after completion of the response
          client.println("Refresh: 5");  // refresh the page automatically every 5 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          client.println("<body>");
          client.println("<font face=\"Arial\" size=5 color=\"red\">HELLO FROM GREG'S ARDUINO UNO</font>");
          client.println("</body>");
          client.println("</html>");
          break;
        }
        if (c == '\n')
        {
          // you're starting a new line
          bCurrentLineIsBlank = true;
        }
        else if (c != '\r')
        {
          // you've gotten a character on the current line
          bCurrentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disconnected");
  }
}

edit: My bad. I put 10.0.0.131 for the gateway. It has been corrected to 10.0.0.138.

SurferTim:
That is what I suspected. you are using the wrong gateway. Look for my two comments "// SurferTim..." after the declaration of ip and Ethernet.begin().

OK I think I get it....I was trying to allocate a static IP address on a non-existent DNS server.
The function defaults to some invalid (for me) IP address if you don't pass the function the last to params?

That is correct. If you do not specify the DNS server, gateway or netmask, it uses these defaults where xx.xx.xx is the first three octets of your ip address:
DNS server xx.xx.xx.1
Gateway xx.xx.xx.1
Netmask 255.255.255.0

I accidentally used 10.0.0.131 as the gateway, and that was incorrect. I just edited my last post to show the gateway as 10.0.0.138.

My ADSL still does not accept the static IP address even though I have specified all the parameters.

I am starting to come to the conclusion that arduino is more trouble than it is worth with this sort of thing - the Raspberry Pi is far easier.

IPAddress ip(10, 0, 0, 81), 
          gateway(10, 0, 0, 138),
          primarydns(61, 9, 133, 193),
          subnet(255, 255, 255, 0);

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);

void setup() 
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) 
  {
    // wait for serial port to connect. Needed for Leonardo only
  }


  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip, primarydns, gateway, subnet);

I’m not one to give up that easy. I would try this with your static ip assignment. Does it resolve “pool.ntp.org” or not?

// add these four lines to the global area of your sketch
#include <EthernetUdp.h>
#include <Dns.h>
DNSClient dnClient;
IPAddress timeServer; // pool.ntp.org NTP server

// then add this after you call Ethernet.begin();
  dnClient.begin(Ethernet.dnsServerIP());
  
  if(dnClient.getHostByName("pool.ntp.org",timeServer) == 1) {
    Serial.print(F("ntp = "));
    Serial.println(timeServer);
  }
  else Serial.print(F("dns lookup failed"));

The only possible conclusion is that the shield itself has a fixed IP address (10.0.0.6) and won't accept a different one from the arduino software?

At any rate I can now type http://10.0.0.6/ into my web browser and the wep page generated by my arduino pops up.

So that means I can have this "boylesg.privatedns.org" land on the web page on my raspberry pi and then it links to my arduino at http://10.0.0.6/ and any other devices I add to the network.