Arduino Web Server

Hello. I am fairly new at messing with these, only a couple months now. I’ve found a small piece of code for a webserver and have greatly expanded it. What I am doing is using a SainSmart 8ch relay board and controlling various outputs via the Arduino web. So far it seems to work really well. I have it open to the outside world so I have no links to activate the relays publically advertised. How I am doing it is using bookmarks on necessary mobile devices with the commands already included (ie arduinoweb/?AASA etc as shown below) What I would like to happen is to automatically close the web page once it inits the ?XXXX commands after a sec or 2. Below is the code I use for my web server/relay controls and below that is a sample piece of code to close the web page after 3 secs. I am just not sure how to merge the 2 sets to make them work together. Closing the web page after a few secs would allow quick access to multiple commands for my installation. Any help would be greatly appreciated!

 //----------------------------------------------------------------------------------------------
/*
 A customized home automation web server to control burglar alarm and a few doors.
 * Ethernet Shield attached to pins 10, 11, 12, 13 - DO NOT USE Shield pins!
 */
 //----------------------------------------------------------------------------------------------
 // Device Init.
 #include <SPI.h>
 #include <Ethernet.h>
 boolean reading = false;

 #define REQ_BUF_SZ   8

 // MAC setting.
    byte mac[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

 // IP setting.
    IPAddress ip(192, 168, 1, 177);
    IPAddress gateway(192, 168, 1, 1);
    IPAddress subnet(255, 255, 255, 0);

 // Server port init.
    EthernetServer server(80);
    String readString;
 //----------------------------------------------------------------------------------------------
 // Device Init.
 void setup()
 {
 // Set digi. pins 02-09 to OUTPUTS and set pins HIGH to turn off relays:
    pinMode(2, OUTPUT); // Pins to be controlled
    pinMode(3, OUTPUT); // ~
    pinMode(4, OUTPUT); // ~
    pinMode(5, OUTPUT); // ~
    pinMode(6, OUTPUT); // ~
    pinMode(7, OUTPUT); // ~
    pinMode(8, OUTPUT); // ~
    pinMode(9, OUTPUT); // ~
 //----------------------------------------------------------------------------------------------
    digitalWrite(2, HIGH); // Pins set to HIGH to turn off relays
    digitalWrite(3, HIGH); // ~
    digitalWrite(4, HIGH); // ~
    digitalWrite(5, HIGH); // ~
    digitalWrite(6, HIGH); // ~
    digitalWrite(7, HIGH); // ~
    digitalWrite(8, HIGH); // ~
    digitalWrite(9, HIGH); // ~ 
 //----------------------------------------------------------------------------------------------
 // Start Ethernet
    Ethernet.begin(mac, ip, gateway, subnet);
    server.begin();
    delay(1500); //Give time to init.
 }
 //----------------------------------------------------------------------------------------------
 // Main loop
 void loop()
 {
 // Listen for incoming clients
    EthernetClient client = server.available();
    if (client)

    boolean currentLineIsBlank = true;
    boolean sentHeader = false;

    {
     while (client.connected())
      {
       if (client.available())
        {
         char c = client.read();

 //read char. by char. HTTP request
   if (readString.length() < 250)
 {
 //store characters to string
   readString += c;
    if (c == '\n') 
 {
 //----------------------------------------------------------------------------------------------
 // Site Init:
    client.println("HTTP/1.1 200 OK"); //send new page
    client.println("Content-Type: text/html");
    client.println();
    client.println("<HTML>");
    client.println("<HEAD>");

 //----------------------------------------------------------------------------------------------
 // WEB txt:
    client.println("<TITLE>awtomazzjoni</TITLE>");  // Basic HTML
    client.println("<center>");
    client.println("</HEAD>");
    client.println("<BODY>");
    client.println("<style>");
    client.println("body {background-color:darkgrey}");
    client.println("h1   {color:blue}");
    client.println("p    {color:yellow}");
    client.println("</style>");
    client.println("<H1>Remote Controls</H1><hr />");
    client.println("<center>");
    client.println("<p>If you know the drill, make it happen.</p>
");
    client.println("<p>Otherwise, you can leave this page by clicking below:</p>
");
    client.println("<a href=\"https://www.google.com/\"\">Go to Google</a>

");
    client.println("<a href=\"https://www.yahoo.com/\"\">Go to Yahoo!</a>

");
    client.println("</BODY>");
    client.println("</HTML>");

 // Stop client
    delay(250);
    client.stop();
 //----------------------------------------------------------------------------------------------
 // Begin "ALARM" relay control:
    if(readString.indexOf("?AASA") >0) // ARM-Away
    {
    digitalWrite(5, LOW);
    delay(2500);
    digitalWrite(5, HIGH);
    }
    if(readString.indexOf("?AASS") >0) // ARM-Stay
    {
    digitalWrite(6, LOW);
    delay(2500);
    digitalWrite(6, HIGH);
    }
    if(readString.indexOf("?DAAS") >0) // DisARM
    {
    digitalWrite(7, LOW);
    delay(2500);
    digitalWrite(7, HIGH);
    }
    if(readString.indexOf("?APA") >0) // Audible PANIC
    {
    digitalWrite(8, LOW);
    delay(3000);
    digitalWrite(8, HIGH);
    }
    if(readString.indexOf("?AFA") >0) // Audible FIRE
    {
    digitalWrite(9, LOW);
    delay(3000);
    digitalWrite(9, HIGH);
    }    
 // Begin "Door" relay control:    
    if(readString.indexOf("?URM") >0) // Utility Room Momentary
    {
    digitalWrite(4, LOW);
    delay(9500);
    digitalWrite(4, HIGH);
    }
    if(readString.indexOf("?GDM") >0) // Garage Door Momentary
    {
    digitalWrite(3, LOW);
    delay(1500);
    digitalWrite(3, HIGH);
    }
    if(readString.indexOf("?FDM") >0) // Front Door Momentary
    {
    digitalWrite(2, LOW);
    delay(9500);
    digitalWrite(2, HIGH);
    }
 //----------------------------------------------------------------------------------------------
 // Begin "LATCHING" relay control:             
    if(readString.indexOf("?URL") >0) // Utility Room Latching
    {
    digitalWrite(4, LOW);
    }
 //   if(readString.indexOf("?XXX") >0) // SPARE-Latching
 //   {
 //   digitalWrite(X, LOW);
 //   }
 // All latching reset
    if(readString.indexOf("?LRC") >0) // Clears any potentially latched relays.
    {
    delay(1000);
    digitalWrite(2, HIGH); // Resets all latching, turns off all relays
    digitalWrite(3, HIGH); // ~
    digitalWrite(4, HIGH); // ~
    digitalWrite(5, HIGH); // ~
    digitalWrite(6, HIGH); // ~
    digitalWrite(7, HIGH); // ~
    digitalWrite(8, HIGH); // ~
    digitalWrite(9, HIGH); // ~
    }
    {
 // clear string for next read
    readString="";

 // Browser timeout
        delay(250);  // close the connection:
        readString="";
        client.stop();
       }
      }
     }
    }
   }
  }
 }
 // END---------------------------------------------------------------------------------------END

Code to auto close the web page after a few secs:

<script language="JavaScript">
// Set the following variable to the number of seconds the browser
// will wait before closing the window.
var gWindowCloseWait = 3;

function SetupWindowClose()
{
window.setTimeout("window.close()",gWindowCloseWait*1000);
}
// Body onload utility (supports multiple onload functions)
var gSafeOnload = new Array();
function SafeAddOnload(f)
{
isMac = (navigator.appVersion.indexOf("Mac")!=-1) ? true : false;
IEmac = ((document.all)&&(isMac)) ? true : false;
IE4 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 4.")!=-1)) ? true : false;
if (IEmac && IE4) // IE 4.5 blows out on testing window.onload
{
window.onload = SafeOnload;
gSafeOnload[gSafeOnload.length] = f;
}
else if (window.onload)
{
if (window.onload != SafeOnload)
{
gSafeOnload[0] = window.onload;
window.onload = SafeOnload;
} 
gSafeOnload[gSafeOnload.length] = f;
}
else
window.onload = f;
}
function SafeOnload()
{
for (var i=0;i<gSafeOnload.length;i++)
gSafeOnload[i]();
}

// Call the following with your function as the argument
SafeAddOnload(SetupWindowClose);

</script>
This window will close in 3 seconds...

Typically, goes before … in the stream that the server sends.

However, between the String class and all those string literals being copied, unnecessarily, into SRAM, you will likely run out of memory before you can get all that script sent, too.

I have it open to the outside world so I have no links to activate the relays publically advertised.

Publicly advertised means nothing. Your system is wide open to anyone sniffing packets.

can can put a simple http password on it. http://aquatictechtank.net/viewtopic.php?f=37&t=185#p536

I have it open to the outside world so I have no links to activate the relays publically advertised. How I am doing it is using bookmarks on necessary mobile devices with the commands already included (ie arduinoweb/?AASA etc as shown below)

You might just put the web page on the devices you want and not serve the web page from the arduino.

Closing the web page after a few secs would allow quick access to multiple commands for my installation.

Can you expand on what that activity is doing?

zoomkat: You might just put the web page on the devices you want and not serve the web page from the arduino.

Hi and thanks for your reply. The relays are controlling devices that are not otherwise able to connect to the internet, like my old burglar alarm system, an electric door strike in my front door and my garage door opener. The Arduino is attached to the SainSmart 8CH relay board and all 8 relays are in use.

zoomkat: Can you expand on what that activity is doing?

What I have done, and keeping it as secure as possible is I've embedded commands as mentioned in my original post, ie ?AASA would abbreviate to "Arm Alarm System-Away". Once activated that would trip the corresponding relay which is attached to my burglar alarm system and in turn arms it in away mode. I have my family's phones all set up with bookmarks/shortcuts to activate all necessary relays when needed and it works perfectly. What I would like to have happen is for the web page to just auto-close after a few secs in the event another command should need to be issued as for now they manually have to close the web page to reissue another command. Sometimes this doesn't always work for them because they are not the most tech-savvy bunch lol.

mistergreen: can can put a simple http password on it. http://aquatictechtank.net/viewtopic.php?f=37&t=185#p536

Hi, thanks! I will work on this tonight and see how it goes with my setup. If its able to 'remember' passwords I may go this route instead. Thanks again, passwording is another option I was exploring.

mistergreen: can can put a simple http password on it. http://aquatictechtank.net/viewtopic.php?f=37&t=185#p536

This will be great for my application, but I think I may have to go pick up a Mega this wkend as the Uno don't have the memory to handle it. Thanks again for your help!

"C" type 's'trings and the (F) macro might be of some small help here..

Doc

Yup use client.print(F("HTML stuff")) for static strings. It will save you memory. Yes, browsers or computer keychain can remember user/passwords.