Pages: [1]   Go Down
Author Topic: Making internet interaction universal  (Read 629 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
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:

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);
  server.begin();
  Serial.begin(9600);
}

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){
          Serial.println(c);
         
          if(c == '1'){
            Serial.println("ON");
            digitalWrite(2, HIGH);
          }
          if(c == '2'){
            Serial.println("OFF");
            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
    delay(1);
    // close the connection:
    client.stop();
  }
}

//LINKS:
//http://192.168.2.2/$1 turns on LED
//http://192.168.2.2/$2 turns off LED

Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
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 (192.168.2.2) and that shield should be available to the world.

You need to tell other people your global IP address (not 192.168.2.2). Google "my ip address" to find what that is.
Logged

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 354
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?

John
Logged

0
Offline Offline
Tesla Member
***
Karma: 114
Posts: 8894
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

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

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

Typically you have:

Code:
INTERNET
|
|
___________________________________________
|    external ip (example): 252.66.75.43   |
|                                          |
|    YOUR ROUTER                           |
|                                          |
|   manages internal ip ranges like        |
|   192.168.1.1 - 192.168.255.255          |
--------------------------------------------
|       |          |              |   
|     PC#1        PC#2      Personal Computer#3
|     
ARDUINO server
(internal/local IP: 192.168.2.2 port 80)
Then you need to:
1. From a PC, enter into the router (usually 192.168.1.1) and configure it to forward the (internal) port 80 of the address 192.168.2.2 (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): 252.66.75.43:80 (or 252.66.75.43:8080, or 252.66.75.43:65000, 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)

Bye!

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: