0

I'm new Arduino and I would like you to help me. I would like to have a public IP so that anyone can get access to it, not only from domestic domain. What could I do?. I'm using Arduino Uno, Arduino Ethernet Shield w5500 and a RTC. My code:

#include <SPI.h>
#include <Ethernet.h>
#include "RTClib.h"
#include <EEPROM.h>

#define time 1
// 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, 0, 177);

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
//----------------------------------------------------------------------------------------------------------------
 int i=0;
String hora="";
String readString;


RTC_DS1307 RTC;


void setup() {

  pinMode(2,OUTPUT);
  // Open serial communications and wait for port to open:
  Serial.begin(9600);   
RTC.begin(); // Inicia la comunicaci¢n con el RTC

RTC.adjust(DateTime(__DATE__, __TIME__)); // Establece la fecha y hora (Comentar una vez establecida la hora)


  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.println("Sistemas Digitales");

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

  // Check for Ethernet hardware present
  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Serial.println("Ethernet shield no se encontro.  No se puede correr el programa sin el hardware. :(");
    while (true) {
      delay(1); // do nothing, no point running without Ethernet hardware
    }
  }
  if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("El cable Ethernet no esta onectado.");
  }

  // start the server
  server.begin();
  Serial.print("El servidor esta en: ");
  Serial.println(Ethernet.localIP());
}






void loop() {
  // listen for incoming clients


delay(1000);  
DateTime now = RTC.now(); // Obtiene la fecha y hora del RTC




  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
//---------------------------------------------------------------------------------------------------------------------------------
        char c = client.read();


if(i==11 || i==12 || i==16 || i==17){
 if(i==16){
  hora +=':';
 }
  hora += c;
}


        if(readString.length()< 100){
          readString += c;
        }
        i++;
        //Este escribe el texto en pantalla letra por letra
        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') {
            Serial.println("acabo lectura");

            Serial.println(hora);
             Serial.println(readString);


client.println("HTTP/1.1 200 OK"); // Enviar una respuesta tipica
             client.println("Content-Type: text/html");
             client.println("Connection: close");  
             client.println("Refresh: 10");
           client.println();
           client.println("<!DOCTYPE HTML>");
          client.println(" <html lang='en-US'>");
          client.println("<html>");
          client.println("<head><meta charset='UTF-8'><title>Sistemas Digitales</title></head>");
          client.println("<body>");
          client.println("<div style='text-align:center;'>");
client.print("<h1>Fecha y hora del servidor:");
client.print(now.day(), DEC);
client.print('/');
client.print(now.month(), DEC);
client.print('/');
client.print(now.year(), DEC);
client.print(' ');
client.print(' ');
int horini;
horini=now.hour();
String horartc = "0";
if(horini<10){
  horartc += horini;
}else{
horartc= String(horini);
}
client.print(horartc);
client.print(':');
int minuto;
minuto=now.minute();
String mainut;
if(minuto<10){
  mainut='0'+ String(minuto);
}else{
mainut= String(minuto);
}
client.print(mainut); 
client.println("</h1>");
client.println("<form method='get'>");
client.println("<br /><h2>¿A que horas le quiere dar de comer al chucho?</h2><br />");
client.println("<input type='time' name='hora'>");
client.println("<br/><h2>que dias lo quiere alimentar?</h2><br />");
//dias en checkbox
//client.println("<label><input type='checkbox' name='cbox1' value='?LunesOn'>Lunes</label><br><label><input type='checkbox' name='cbox2' value='?MartesOn'>Martes</label><br><label><input type='checkbox' name='cbox3' value='?MiercolesOn'>Miercoles</label><br><label><input type='checkbox' name='cbox4' value='?JuevesOn'>Jueves</label><br><label><input type='checkbox' name='cbox5' value='?ViernesOn'>Viernes</label><br><label><input type='checkbox' name='cbox6' value='?SabadoOn'>Sabado</label><br><label><input type='checkbox' name='cbox7' value='?DomingoOn'>Domingo</label><br>");
client.println("<input type='submit' value='Enviar' id='enviar'> ");
client.println("</form>");
if (readString.length() >0) {
Serial.println("hora del rtc");
String horiux = horartc +':'+ mainut;
Serial.println(horiux);
EEPROM.put(0, hora);

if(EEPROM.get( 0, hora ) == horiux){
digitalWrite(2, HIGH);
delay(10000);  
digitalWrite(2, LOW);
delay(60000);
}
readString="";
i=0;
hora="";


          client.println("</body>");
          client.println("</html>");

          delay(1);
    // close the connection:
    client.stop();

        }

      }
    }

  }

  }
}

There it is a part of the code:

IPAddress ip(192, 168, 0, 177);

I would like to make that IPto be public domain so you can get access anywhere instead of neededing to be in the same ethernet connection.

Any help would be wonderful, thanks for your time.

1

There is a discussion about the same topic in Arduino's Forum here. The most valuable comment was this:

To get an internet static IP address, contact your ISP. You will probably have to pay extra for that. Most people use a dynamic IP service such as www.noip.com to forward traffic to your current internet IP address. You put your dynamic ip address url in your web page so the browser request get routed properly.

I assume (by the address) that you want to do something like this:

The NAT device might have a port forwarding feature. You can use that. Assume the WAN ip is 198.51.100.1 and the server's ip is 10.1.1.1, the server is listening on port 80. So the port forwarding configuration will be something like : 198.51.100.1 80 ---> 10.1.1.1 80 From the outside you connect to the public IP - 198.51.100.1 80 When the NAT device receives packet, it forwards it to 10.1.1.1. more info here

This is direct copy from source. So in your case WAN IP is the static IP (or another else) that is visible about you outside your network and the private one is that 192.168.0.177 which you used. Like stated in first yellow box, you can use dynamic ip service, too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.