Hey all,
I am working on an Arduino Uno/Ethernet Shield project, and I have it working to a point but have hit a snag.
My code makes the arduino serve an html page that has 4 buttons that control 2 LEDs (LED1-on, LED1-off, LED2-on, and LED2-off). The LEDs are on 2 analog pins.
The program has worked perfectly with a linksys router. With the arduino hooked up to the router, and with the router forwarding port 80, I can type in "174.4X.16X.1X:80" (the public IP Address that my router is pulling from my ISP) into any outsite internet connected device's browser and communicate with the Arduino perfectly with the webpage GUI.
My problem comes into play when I decide that I don't want to use a router. With the router out of the equation, and with the arduino programmed for DHCP, I connect the internet (from the ISP) directly to the arduino. I also programmed the arduino to display the dynamic IP Address that it pulls from my ISP, and display it on the serial monitor. It does, but when I type in the Public IP Address that it pulled from the ISP into an outsite internet device, I get webpage cannot be displayed. I have been racking my brain on this one for about 3 days on and off, trying different segments of code all over the internet, and even from these forums, but have come up blank.
I just want to use only the Arduino as a webserver for communication with these LEDs and NOT any other sites(like dynDNS) or devices(like pc's or routers)...If that is even possible.
I'll appreciate any help. I am somewhat self taught in networking, so it is possible I've overlooked something.
Here is my code:
#include <SPI.h>
#include <Ethernet.h>
EthernetServer server = EthernetServer(80); //port 80
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; //MAC Address Manual Setup
int Analog5 = 19; //Analog pin 5
int Analog4 = 18; //Analog pin 4
void setup()
{
Serial.begin(9600);
//Ethernet.begin(mac); //HINT: for DHCP Setup
//Ethernet.begin(mac, ip, gateway, subnet); //HINT: for manual setup
//byte ip[] = {192,168,1,230}; //IP Address
//byte gateway[] = { 192, 168, 0, 1 }; //Manual setup
//byte subnet[] = { 255, 255, 255, 0 }; //Manual setup
Ethernet.begin(mac);
server.begin();
Serial.println(Ethernet.localIP()); //Open the Arduino Serial Monitor (Top right corner) to view what DHCP IP Address has been pulled so you can navigate to the web page.
pinMode(Analog5, OUTPUT);
pinMode(Analog4, OUTPUT);
}
#define BUFSIZ 100 //Buffer size for getting data
char clientline[BUFSIZ]; //string that will contain command data
int index = 0; //clientline index
void loop()
{
index=0; //reset the clientline index
EthernetClient client = server.available();
if (client) {
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if(index<BUFSIZ) //Only add data if the buffer isn't full.
{
clientline[index]=c;
index++;
}
if (c == '\n' && currentLineIsBlank)
{
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("<h1><center>LED/RELAY Control</h1></center>
<center><form method=get action=/?><input type=submit name=A5 value=1>A5=ON
<input type=submit name=A5 value=0>A5=OFF
");
//***********************Analog Channel A5 Check BEGIN***********************
client.print("<center>");
int AnaChan5 = 5;
client.print("The Light on Analog Pin A");
client.print(AnaChan5);
client.print(" is ");
if(analogRead(AnaChan5)<500) //965 = on, 0 = off
{
client.print(" Off ");
}
else
{
client.print(" On ");
}
client.println("
");
client.print("Analog Channel ");
client.print(AnaChan5);
client.print(" Reading: ");
client.print(analogRead(AnaChan5));
client.println("
");
client.println("
");
//***********************Analog Channel A5 Check END***********************
client.println("</center>
<center><form method=get action=/?><input type=submit name=A4 value=1>A4=ON
<input type=submit name=A4 value=0>A4=OFF
</form></center>");
//***********************Analog Channel A4 Check BEGIN***********************
client.print("<center>");
int AnaChan4 = 4;
client.print("The Light on Analog Pin A");
client.print(AnaChan4);
client.print(" is ");
if(analogRead(AnaChan4)<500) //965 = on, 0 = off
{
client.print(" Off ");
}
else
{
client.print(" On ");
}
client.println("
");
client.print("Analog Channel ");
client.print(AnaChan4);
client.print(" Reading: ");
client.print(analogRead(AnaChan4));
client.println("
");
client.println("
");
//***********************Analog Channel A4 Check END***********************
break;
}
if (c == '\n') {
currentLineIsBlank = true;
}
else if (c != '\r') {
currentLineIsBlank = false;
}
if(strstr(clientline,"/?A5=1")!=0) { //look for the command to turn the Analog5 on
digitalWrite(Analog5, 1); //turn the Analog5 on
} else if(strstr(clientline,"/?A5=0")!=0) { //look for command to turn Analog5 off. Note: If Analog5 is on, it will stay on until command is given to turn it off, even if multiple computers are on the site
digitalWrite(Analog5, 0); //turn Analog5 off
}
else if(strstr(clientline,"/?A4=1")!=0) { //look for the command to turn the Analog4 on
digitalWrite(Analog4, 1); //turn the Analog4 on
} else if(strstr(clientline,"/?A4=0")!=0) { //look for command to turn Analog4 off. Note: If Analog4 is on, it will stay on until command is given to turn it off, even if multiple computers are on the site
digitalWrite(Analog4, 0); //turn Analog4 off
}
}
}
delay(1);
client.stop();
}
}