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);
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