How to connect to the Ethernet Shield from Internet

Hi

I've been trying to connect to the Ethernet Shield from the web without any success.
It is working perfectly on the LAN but that's all...

I have a Internet Provider with the box that gives me a static IP.
Static IP : 78.XXX.XX.XXX
I have my router/box that has an IP : 192.168.0.254
My Ethernet Shield has it's IP : 192.168.0.177
and I have redirected ports in my router : External (80) - ip (192.168.0177) - internal (80)

How should I connect to my Ethernet Shield on my phone ?

I have tried like this :

#include <SPI.h>
#include <Ethernet.h>
 
#include <Servo.h>
Servo myservo;  // create servo object to control a servo
 
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0E, 0xB0, 0x52 }; //physical mac address
byte ip[] = { 192, 168, 0, 177 }; // l'arduino 
byte gateway[] = { 78, XXX, XX, XXX }; // static IP of router 
byte subnet [] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //server port
 
String readString;
int statePin = 0; 
 
//////////////////////
 
void setup(){
 
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
 
  Serial.begin(9600);
  Serial.println("server LED test 1.0"); // check 
}

But impossible to connect from my phone (i'll post the entire code at the end)

How should I manage to get a good connection. Seems that their are different good/bad ways of connecting..

Thanks for your help

my entire code :

#include <SPI.h>
#include <Ethernet.h>
 
#include <Servo.h>
Servo myservo;  // create servo object to control a servo
 
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0E, 0xB0, 0x52 }; //physical mac address
byte ip[] = { 192, 168, 0, 177 }; // ip de l'arduino 
byte gateway[] = { 78, XXX, XXX, XXX }; // 
byte subnet [] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //server port
 
String readString;
int statePin = 0; 
 
//////////////////////
 
void setup(){
 
  pinMode(6, OUTPUT); // Pin de la Led
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
 
  Serial.begin(9600);
  Serial.println("server LED test 1.0"); // verification que c'ets loadé. 
}
 
void loop(){
  // creation connexion
  EthernetClient client = server.available();
  if (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 ("lecture de C  ");
          Serial.print(readString);
          Serial.println ("   ");
        }
 
        //if HTTP request has ended
        if (c == '\n') {
 
          ///////////////
          Serial.println ("Lecture de readstring");
          Serial.println(readString); //print to serial monitor for debuging
 
          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
          client.println();
 
          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
          client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
          client.println("<link rel='stylesheet' type='text/css' href='http://homeautocss.net84.net/a.css' />");
          client.println("<TITLE>Home Automation</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");
          client.println("<H1>Home Automation</H1>");
          client.println("<hr />");
          client.println("
");
         
          client.println("<a href=\"/?lighton\"\">Turn On Light</a>");
          client.println("<a href=\"/?lightoff\"\">Turn Off Light</a>
");     
       client.println("
");   
          client.println("<a href=\"/?lightblink\"\">Blink Led</a>
");   
 
          client.println("</BODY>");
          client.println("</HTML>");
 
          delay(1);
          //stopping client
          client.stop();
 
          ///////////////////// control arduino pin
          if(readString.indexOf("?lighton") >0)//checks for on
          {
          
            Serial.println("Led On");
            statePin = 0; 
          }

          if(readString.indexOf("?lightoff") >0)//checks for off
          {
            Serial.println("Led Off");
            statePin = 1; 
          }
          
          if(readString.indexOf("?lightblink") >0)//checks for off
          {
            Serial.println("Led blink");
            statePin = 2; 
          }
          
          
          //clearing string for next read
          readString="";
 
        }
        
        
      }
      
       
    }
     
    
  }
  
    if (statePin == 0)
       { //Serial.println ("STATE PIN IS ON");
       digitalWrite (6, HIGH); 
       } 
    if (statePin == 1) 
       { 
         //Serial.println ("STATE PIN IS OFF");
         digitalWrite (6, LOW); 
       }
    if (statePin == 2) 
       { 
         Serial.println ("STATE PIN IS BLINKING");
         digitalWrite (6, HIGH);
         delay(300); 
         digitalWrite (6, LOW); 
         delay(300); 
         
       }     
  
}

On your phone browser:
http://78.xxx.xx.xxx

edit: Sometimes you see what you want to see, and not how it is. The gateway is wrong. That will cause the connection to fail when the Arduino tries to return anything to the internet.

byte mac[] = { 0x90, 0xA2, 0xDA, 0x0E, 0xB0, 0x52 }; //physical mac address
byte ip[] = { 192, 168, 0, 177 }; // ip de l'arduino 
// this is wrong
// byte gateway[] = { 78, XXX, XXX, XXX };
// should be same subnet. Usually this
byte gateway[] = { 192, 168, 0, 1 };
byte subnet [] = { 255, 255, 255, 0 }; //subnet mask

And this is wrong. You did not include a dns server. I will use the gateway for the dns.

  // Ethernet.begin(mac, ip, gateway, subnet);
  Ethernet.begin(mac, ip, gateway, gateway, subnet);

Hi

Thanks for your quick answer.
It is still not working....when I point towards the http://78.xxx.xx.xxx link. I arrive on the login/password of the back office of my internet provider. Impossible to access anything else.

When you told me
byte gateway[] = { 192, 168, 0, 1 };
What is this IP supposed to represent physically ?

Shouldn't i use the DNS of my internet provider ?

I really don't know what to do....

Thanks,

It is still not working....when I point towards the http://78.xxx.xx.xxx link. I arrive on the login/password of the back office of my internet provider. Impossible to access anything else.

Do you have a router with an interface that is assigned 78.xxx.xx.xxx? Not your ISP's equipment, your router.

When you told me
byte gateway[] = { 192, 168, 0, 1 };
What is this IP supposed to represent physically ?

This is the IP assigned to the router interface that your Arduino connects to. I just saw it in your first post. This one:

I have my router/box that has an IP : 192.168.0.254

Shouldn't i use the DNS of my internet provider ?

If this sketch used dns for any resolution like a client would, you are correct. But this doesn't, so the gateway ip is a placeholder parameter in that function call.

ok.

So my ISP's box is activated in router mode with this IP : 192.168.0.254.

the configuration is :
state of DHCP : Activated
Start DHCP : 192.168.0.10
End DHCP : 192.168.0.50
Ip Adress DMZ : 192.168.0.177 - tried putting my arduino's IP
Advanced options :
Ping answer : activated
Wake On Lan : Desactivated
Upnp : activated

Port redirection :
external port : 80 - protocol TCP - Ip of destination 192.168.0.177 internal port 80

Other ideas/question :

  • Can I redirect to another port than 80 ? Since I can't connect to anything else than the isp's back office I was wondering that maybe this port can't be used ? (if yes it means I need to change the redirections + this part ? EthernetServer server(80); //server port

  • I've checked on my ISP's website and thay say they have 2 possible DNS's we can force :
    212.27.40.240
    212.27.40.241

Sorry I can't give you more details than this...
and thanks again so much for your time

So my ISP's box is activated in router mode with this IP : 192.168.0.254.

Then that is you network gateway. All devices connected to that localnet should be using that ip as the gateway.

However, it would be up to your ISP's router to redirect the public ip to your localnet ip of the Arduino. It is that device that is assigned the public ip.

edit: If you keep getting the login screen for the ISP's router using the public ip, then you do not have things set correctly on the destination nat to the Arduino. But when it does work, you will not be able to login to the router using port 80, at least from the internet.

Insure it is not a firewall issue. Normally the login to the router is an INPUT chain, and a destination NAT may be a FORWARD chain.

Insure you are trying this connection from the internet using the public ip. Some routers have a bad time with a destination nat using the public ip from the router localnet. Each router has its own way of using a hairpin nat.

Hey

I finally made it.... after so many different changes.

I took you gateway advice.
And I just redirected the port on my router not from 80 to 80 but from 100 external to 80 internal.

  • I connected through 78.XX.XX.XXX:100

And it's now working !
thanks a lot !

I've heard some ISPs won't allow incoming request to port 80 on home accounts to prevent people from running commercial web sites on home accounts.

zoomkat:
I've heard some ISPs won't allow incoming request to port 80 on home accounts to prevent people from running commercial web sites on home accounts.

There was an article in February 2013 NutsVolts mag about this matter. See if the following link works for you, or check out "dynamic dns". Click on the magazine print page to get past the popups.
http://nutsvolts.texterity.com/nutsvolts/201302?pg=16&search_term=dynamic%20dns#pg16