Go Down

Topic: Using a webpage to control/monitor pins - Browser compatibility (Read 741 times) previous topic - next topic


Feb 14, 2013, 10:28 pm Last Edit: Feb 14, 2013, 10:36 pm by Darkure Reason: 1
The program below does a fair number of things but the issue I'm having is with the web server.  In Firefox it works perfectly, exactly how I want it to.  When the form is submitted the same page reloads and all pins read/write correctly.  In Chrome and Safari I get a blank page after the form is submitted.  In Internet Explorer it sends me back to the root page after the form is submitted.  In all cases the POST data is successfully sent to the Arduino but I am wanting the browser to stay on the same page.  Thoughts?

I've run the resulting HTML source code through a validator and cleared up any errors I got there but I still get the same results with the various browsers.

I'm not sure if this is an HTML issue or a webserver code issue but I've been banging my head against this for days and haven't found anything to resolve the issue.

My current code is below. (chopped out mostly irrelevant portions due to character limit, attached .ino file instead) Thanks in advance.

Code: [Select]

#include "SPI.h"
#include "Ethernet.h"
#include "WebServer.h"
#include "EthernetUDP.h"

#define PREFIX ""
WebServer webserver(PREFIX, 80);

void defaultCmd(WebServer &server, WebServer::ConnectionType type, char *, bool)
 if (type != WebServer::HEAD)
   P(helloMsg) = "<h1>Hello</h1><a href=\"private.html\">Private page</a>";

void privateCmd(WebServer &server, WebServer::ConnectionType type, char *, bool)
 /* if the user has requested this page using the following credentials
 * username = user
 * password = user
 * display a page saying "Hello User"
 * the credentials have to be concatenated with a colon like
 * username:password
 * and encoded using Base64 - this should be done outside of your Arduino
 * to be easy on your resources
 * in other words: "dXNlcjp1c2Vy" is the Base64 representation of "user:user"
 * if you need to change the username/password dynamically please search
 * the web for a Base64 library */
 if (server.checkCredentials("dXNlcjp1c2Vy"))
   if (type == WebServer::POST)
   bool repeat;
   char name[16], value[16];
     /* readPOSTparam returns false when there are no more parameters
     * to read from the input. We pass in buffers for it to store
     * the name and value strings along with the length of those
     * buffers. */
     repeat = server.readPOSTparam(name, 16, value, 16);

     /* this is a standard string comparison function. It returns 0
     * when there's an exact match. We're looking for a parameter
     * here. */
     if (strcmp(name, "R1") == 0)  //R1 through R4 are used for relay resets
       relay1Reset = true;
       digitalWrite(relayPin1, LOW);
       resetTime1 = currentTime;
     if (strcmp(name, "R2") == 0)
       relay2Reset = true;
       digitalWrite(relayPin2, LOW);
       resetTime2 = currentTime;
     if (strcmp(name, "R3") == 0)
       relay3Reset = true;
       digitalWrite(relayPin3, LOW);
       resetTime3 = currentTime;
     if (strcmp(name, "R4") == 0)
       relay4Reset = true;
       digitalWrite(relayPin4, LOW);
       resetTime4 = currentTime;
     if (strcmp(name, "Reset") == 0) //arduino reset
       resetDetect = true;
     if (strcmp(name, "Arm") == 0) //door alarm toggle
       armed = !armed;
       alarmTripTime = 0;
     if (strcmp(name, "ResetTime") == 0) //changing the relay reset delay
       int tempDelay = strtoul(value, NULL, 10);
       if (tempDelay > 0)
         resetDelay = tempDelay;
     } while (repeat);
   if (type == WebServer::GET)
     int hours = floor(currentTime/3600000);
     int mins = floor(currentTime/60000) - (hours * 60);
     int secs = floor(currentTime/1000) - (hours * 3600) - (mins * 60);
     P(msg1) = "<!DOCTYPE html><html><head><title>Tabor Remote Control</title>"
               "<script type='text/javascript'>"
               "function stopRKey(evt) {"
               "var evt = (evt) ? evt : ((event) ? event : null);"
               "var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);"
               "if ((evt.keyCode == 13) && (node.type=='text'))  {return false;}"
               "document.onkeypress = stopRKey;"
                 "font-family:'Times New Roman';"
               "<p>Running for: ";
     if (mins < 10)
     if (secs < 10)
     P(msg2) = "<br></p>"
               "<h1>Relay Reset</h1>"
               "<form action='private.html' method='post'>"
               "<button name=R1 value=1>Relay 1</button>";
     if (relay1Reset)
       server.println(" Open<br>");
       server.println(" Closed<br>");
     server.print("<button name=R2 value=1>Relay 2</button>");
     if (relay2Reset)
       server.println(" Open<br>");
       server.println(" Closed<br>");
     server.print("<button name=R3 value=1>Relay 3</button>");
     if (relay3Reset)
       server.println(" Open<br>");
       server.println(" Closed<br>");
     server.print("<button name=R4 value=1>Relay 4</button>");
     if (relay4Reset)
       server.println(" Open");
       server.println(" Closed");
     P(msg3) = "<br>"
               "<input type=text name=ResetTime value =";    
     server.println("> Relay Reset Time (ms)<br>");
     P(msg4) = "<input type=submit value=Submit>"
               "<button name=Reset value=1>Reset Arduino</button><br>"
     if (armed)
       server.println("<button name=Arm value=1>Disarm</button><br>");
       server.println("<button name=Arm value=1>Arm</button><br>");
     P(msg5) = "</form>"
               "Monitor 1: ";
     float tempValue = (5 * (monitorValue1/1023));
   /* send a 401 error back causing the web browser to prompt the user for credentials */

void setup()
 digitalWrite(deviceResetPin, HIGH);
 pinMode(deviceResetPin, OUTPUT);
 pinMode(alarmPin, INPUT);
 pinMode(relayPin1, OUTPUT);  
 digitalWrite(relayPin1, HIGH);
 pinMode(relayPin2, OUTPUT);  
 digitalWrite(relayPin2, HIGH);
 pinMode(relayPin3, OUTPUT);  
 digitalWrite(relayPin3, HIGH);
 pinMode(relayPin4, OUTPUT);  
 digitalWrite(relayPin4, HIGH);
 Ethernet.begin(mac, ip, myDns, gateway, subnet);
 webserver.addCommand("index.html", &defaultCmd);
 webserver.addCommand("private.html", &privateCmd);

void loop()
 /* process incoming connections one at a time forever */
 webserver.processConnection(buff, &len);

Go Up