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