Go Down

Topic: Making internet interaction universal (Read 1 time) previous topic - next topic


So I currently have this code loaded into my Arduino w/ Ethernet shield and it works perfectly, however it only works locally. The Arduino acts as a server and receives incoming HTTP requests which dictate whether an LED is turned on or off by a "$1" or "$2" at the end of the URL. But of course it only works locally at the moment because I'm connecting through the Arduino's IP address. My question is how might I be able to make this work for anybody who accesses the server from an internet connection anywhere? Would I have to get a DNS or port forward, something along those lines? Or perhaps change the code? Here is the current code:

Code: [Select]

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

boolean incoming = 0;

byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x50, 0x9A }; //MAC address
IPAddress ip(192,168,2,2); //IP

// 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()
  pinMode(2, OUTPUT);

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

void loop()
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // 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
        //reads URL string from $ to first blank space
        if(incoming && c == ' '){
          incoming = 0;
        if(c == '$'){
          incoming = 1;
        //Checks for the URL string $1 or $2
        if(incoming == 1){
          if(c == '1'){
            digitalWrite(2, HIGH);
          if(c == '2'){
            digitalWrite(2, LOW);

        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
    // close the connection:

//$1 turns on LED
//$2 turns off LED


Port forwarding should be all you need. Port forward 80 (or some other port if you change the sketch) from the router to your IP address ( and that shield should be available to the world.

You need to tell other people your global IP address (not Google "my ip address" to find what that is.
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics


The short answer is to forward a port on your router.  What kind of router do you have?



Typically you would get a dynamic IP service address like JackSac67.on-ip.com and foward incomming request at your router to the IP address being used by your arduino.
Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0


Jan 02, 2013, 03:15 am Last Edit: Jan 02, 2013, 05:20 am by Nick Gammon Reason: 1
Typically you have:

Code: [Select]
|    external ip (example):   |
|                                          |
|    YOUR ROUTER                           |
|                                          |
|   manages internal ip ranges like        |
| -          |
|       |          |              |   
|     PC#1        PC#2      Personal Computer#3
ARDUINO server
(internal/local IP: port 80)

Then you need to:
1. From a PC, enter into the router (usually and configure it to forward the (internal) port 80 of the address (Arduino) to the 80 (external) port. (You can also map the internal 80 to another external port, for example 8080, 65000, ...)
2. Access Arduino from INTERNET using the external address of your router (example): (or, or, using the port configured in point 1)

NOTE: since the (external) address of the router could change in the time, most router have a function called DDNS (Dynamic DNS.. wikipedia), where you can link your (changing) current router external address to a fixed web address. Try services like tzo, no-ip or ddns.org.
In this case, the point 2 becomes:

2.b Access your Arduino from an address like: www.youraddressname.ddns.org:80 (or :8080, or :65000, ... as in point 1)


Moderator edit: [code] ... [/code] tags added. (Nick Gammon)

Go Up