Pages: [1]   Go Down
Author Topic: Making internet interaction universal  (Read 749 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Karma: 0
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:


#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 =;
        // 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


Global Moderator
Online Online
Brattain Member
Karma: 498
Posts: 19060
Lua rocks!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Temple, Texas
Offline Offline
Sr. Member
Karma: 14
Posts: 361
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Offline Offline
Tesla Member
Karma: 145
Posts: 9685
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Typically you would get a dynamic IP service address like and foward incomming request at your router to the IP address being used by your arduino.

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Offline Offline
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Typically you have:

|    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
In this case, the point 2 becomes:

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


Moderator edit: [code] ... [/code] tags added. (Nick Gammon)
« Last Edit: January 01, 2013, 11:20:53 pm by Nick Gammon » Logged

Pages: [1]   Go Up
Jump to: