Hi
I had a simple web server set up yesterday with the original code below.
It was working fine on my local IP and from my external IP address
Today, I've modified the code, but only taking unwanted HTML code out and changing the button sizes.
Now it won't display the page.
I've tried the examples, and they work ok. Send a ping from CMD works fine, so I know its not cable or router issues.
Original code
#include <SPI.h>
#include <Ethernet.h> // Initialize the libraries.
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //I left the MAC address and IP address blank.
byte ip[] = { 192,168,0,21 }; // You will want to fill these in with your MAC and IP address.
EthernetServer server(XXXX); // Assigning the port forwarded number. It's almost always 80.
String readString; // We will be using strings to keep track of things.
int val; // Using val as a variable for the PIR status.
//////////////////////
void setup(){
pinMode(2, OUTPUT);
Ethernet.begin(mac, ip);
}
void loop(){
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) { // This is all where we start up the server and strings.
char c = client.read();
if (readString.length() < 100) {
readString += c;
}
if (c == '\n') {
Serial.println(readString); // And here we begin including the HTML
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("<hmtl>");
client.println("<head>");
client.println("ARDUINO PAGE");
client.println("</head>");
client.println("<title>");
client.println("ARDUINO + ETHERNET Page");
client.println("</title>");
client.println("<body bgcolor=black>");
client.println("<font color=white>");
client.println("<meta http-equiv=\"refresh\" content=\"4\">"); // This is used to refresh the page so
client.println("<center>"); // we can see if there is Motion or not.
client.println("<b>");
client.println("Greetings! Here you will find a somewhat interactive page served up by my Arduino!");
client.println("
");
client.println("As well, you can interact with my Arduino! It's pretty basic, and you really can't see it,");
client.println("
");
client.println("but as you press a button, you turn on or off an LED or move a Servo! Have fun!");
client.println("</b>");
client.println("<p>");
client.println("<table border=0 width=200>");
client.println("<tr>");
client.println("<td align=center>");
client.println("<font color=white>");
client.println("The Temperature is:");
client.println("</td>");
client.println("</tr>");
client.println("<tr>");
client.println("<td align=center>");
client.println("<font color = turquoise size=10>");
//int temp = (((5*analogRead(5)*100/1024)*1.8)+32); // This replaces the 00 with a temperature in F.
//client.println(temp);
client.println("* F");
client.println("</td>");
client.println("</tr>");
client.println("</table>");
client.println("<p>");
client.println("<FORM>");
client.println("<INPUT type=button value=LED1-ON onClick=window.location='/?lighton1\'>");
client.println("<INPUT type=button value=LED1-OFF onClick=window.location='/?lightoff1\'>");
client.println("</FORM>"); // Above and below you'll see that when we click on a button, it adds a little snippet
// to the end of the URL. The Arduino watches for this and acts accordingly.
client.println("</center>");
client.println("</font>");
client.println("</body>");
client.println("</html>");
delay(1);
if(readString.indexOf("?lighton") >0) // these are the snippets the Arduino is watching for.
{
digitalWrite(2, HIGH);
}
else{
if(readString.indexOf("?lightoff") >0)
{
digitalWrite(2, LOW);
}
}
readString="";
client.stop(); // End of session.
}
}
}
}
}
Modified code:
#include <SPI.h>
#include <Ethernet.h> // Initialize the libraries.
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //I left the MAC address and IP address blank.
byte ip[] = { 192,168,0,21 }; // You will want to fill these in with your MAC and IP address.
EthernetServer server(XXXX); // Assigning the port forwarded number. It's almost always 80.
String readString; // We will be using strings to keep track of things.
int val; // Using val as a variable for the PIR status.
//////////////////////
void setup(){
pinMode(2, OUTPUT);
Ethernet.begin(mac, ip);
}
void loop(){
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) { // This is all where we start up the server and strings.
char c = client.read();
if (readString.length() < 100) {
readString += c;
}
if (c == '\n') {
Serial.println(readString); // And here we begin including the HTML
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("<hmtl>");
client.println("<head>");
client.println("ARDUINO PAGE");
client.println("</head>");
client.println("<title>");
client.println("ARDUINO + ETHERNET Page");
client.println("</title>");
client.println("<body bgcolor=black>");
client.println("<font color=white>");
client.println("<meta http-equiv=\"refresh\" content=\"4\">"); // This is used to refresh the page so
client.println("<center>"); // we can see if there is Motion or not.
client.println("<b>");
client.println("Simple LED Switch");
client.println("
");
client.println("<FORM>");
client.println("<INPUT type=button value=LED1-ON style='width: 200px; height: 60px' onClick=window.location='/?lighton1\'>");
client.println("<INPUT type=button value=LED1-OFF style='width: 200px; height: 60px' onClick=window.location='/?lightoff1\'>");
client.println("</FORM>"); // Above and below you'll see that when we click on a button, it adds a little snippet
// to the end of the URL. The Arduino watches for this and acts accordingly.
client.println("</center>");
client.println("</font>");
client.println("</body>");
client.println("</html>");
delay(1);
if(readString.indexOf("?lighton") >0) // these are the snippets the Arduino is watching for.
{
digitalWrite(2, HIGH);
}
else{
if(readString.indexOf("?lightoff") >0)
{
digitalWrite(2, LOW);
}
}
readString="";
client.stop(); // End of session.
}
}
}
}
}
Yes the port isn't set to XXXX in reality.
Any ideas?