Web relay sketch not working well updated

Hi all,
I downloaded this sketch by Claudio Vella - Malta the other day. I uploaded it into my Mega.

When I try to use the sketch/app, I get the menu once in awhile and it will control the ports. Then it goes to a screen full of garbage or buttons all over the screen that do not work. If I try to log back in to the page, it doesn't connect at all. I tried with firefox, IE, and safari. All the same problems.

If anyone can help me get this working, that would be great.
Thanks.

Web_relay.ino (18.2 KB)

Please read #7 below on how to post code instead of attaching it.

http://forum.arduino.cc/index.php/topic,148850.0.html

I'm wondering if you downloaded the code before scolding me. It is a very large file. (for Arduino)

Everywhere it shows something like client.print("<td><INPUT TYPE=\"button\" VALUE=\"Switch OFF All Pins");it should be client.print(F("<td><INPUT TYPE=\"button\" VALUE=\"Switch OFF All Pins"));

Funny how so many say the sketch works... go figure. What's the F for? Thanks for your help.

The F() makes the compiler leave the constant strings in PROGMEM, where they belong, instead of wasting precious RAM.

I have no idea if it will make the sketch work or not, but it can help.

You said "something like". Do you mean all the client.print statements or just the "all on or all off"?
I don't have a memory problem with the mega. Is that F() thing causing the pages not to load or only load once?

Everywhere there's a client.print ("yada") or a client.println ("yada") i.e. everywhere there's a constant string.

Ok. I'll have to do that later. gotta get some sleep first. thanks again AWOL

Inserted 3 F()'s. Ran it and it loaded the page once. Buttons never worked then displayed html code and buttons on the left side of the screen. Thanks for helping so far. I know almost nothing about html coding.

Inserted 3 F()'s.

There are waaaaaay more than that.

I know almost nothing about html coding.

I'm afraid here, it's a case of the visually-impaired leading the partially-sighted 8)

You're a funny guy. Don't mistake humble for stupid. :slight_smile:

I think your code is too large to run on my arduino so I can't see how your page looks. If the page reloads on each action, it will probably be slow. I suggest you start simple and then start adding frills. Below is simple web server control test code that controls the arduino pins.

//zoomkat 5-30-15
//simple button GET with iframe code
//open serial monitor to see what the arduino receives
//use the ' instead of " in html ilnes 
//address will look like http://192.168.1.102:84/ when submited
//for use with W5100 based ethernet shields

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //ethernet shield mac address
byte ip[] = {192, 168, 1, 102 }; // arduino IP in lan
byte gateway[] = {192, 168, 1, 1 }; // internet access via router
byte subnet[] = {255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port

String readString; 

//////////////////////

void setup(){

  pinMode(4, OUTPUT); //pin selected to control
  pinMode(5, OUTPUT); //pin selected to control
  pinMode(6, OUTPUT); //pin selected to control
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  server.begin();

  //enable serial data print 
  Serial.begin(9600); 
  Serial.println("multibutton server test 5-30-15"); // so I can keep track of what is loaded
}

void loop(){
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string 
          readString += c; 
          //Serial.print(c);
        } 

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString); //print to serial monitor for debuging 

            //now output HTML data header
          if(readString.indexOf('?') >=0) { //don't send new page
            client.println("HTTP/1.1 204 Zoomkat\r\n\r\n");
          }
          else {
            client.println("HTTP/1.1 200 OK"); //send new page
            client.println("Content-Type: text/html");
            client.println();

            client.println("<HTML>");
            client.println("<HEAD>");
            client.println("<TITLE>Arduino GET test page</TITLE>");
            client.println("</HEAD>");
            client.println("<BODY>");

            client.println("<H1>Zoomkat's simple Arduino button</H1>");


            client.println("<a href='/?on4' target='inlineframe'>ON 4</a>"); 
            client.println("<a href='/?off4' target='inlineframe'>OFF 4</a>"); 

            client.println("<a href='/?on5' target='inlineframe'>ON 5</a>"); 
            client.println("<a href='/?off5' target='inlineframe'>OFF 5</a>"); 

            client.println("<a href='/?on6' target='inlineframe'>ON 6</a>"); 
            client.println("<a href='/?off6' target='inlineframe'>OFF 6</a>"); 

            client.println("<IFRAME name=inlineframe style='display:none'>");          
            client.println("</IFRAME>");

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

          delay(1);
          //stopping client
          client.stop();

          ///////////////////// control arduino pin
          if(readString.indexOf("on4") >0)//checks for on4
          {
            digitalWrite(4, HIGH);    // set pin 4 high
            Serial.println("Led 4 On");
          }
          if(readString.indexOf("off4") >0)//checks for off4
          {
            digitalWrite(4, LOW);    // set pin 4 low
            Serial.println("Led 4 Off");
          }
          if(readString.indexOf("on5") >0)//checks for on5
          {
            digitalWrite(5, HIGH);    // set pin 5 high
            Serial.println("Led 5 On");
          }
          if(readString.indexOf("off5") >0)//checks for off5
          {
            digitalWrite(5, LOW);    // set pin 5 low
            Serial.println("Led 5 Off");
          }          
          if(readString.indexOf("on6") >0)//checks for on6
          {
            digitalWrite(6, HIGH);    // set pin 6 high
            Serial.println("Led 6 On");
          }
          if(readString.indexOf("off6") >0)//checks for off6
          {
            digitalWrite(6, LOW);    // set pin 6 low
            Serial.println("Led 6 Off");
          }
          //clearing string for next read
          readString="";
        }
      }
    }
  }
}

This is not my code. I downloaded it. Claudio Vella wrote it. Thank you. Maybe this will help me learn html.

How do I get the On/off buttons in a column?

You would add the html line break
code like below. A google search for "html tutorial" will bring up sites that explain basic things like buttons, frames, and similar which are useful. You probably should keep the serial print to the serial monitor in your code for debugging purposes. Also, please read #7 below on how to post code in the discussion:

http://forum.arduino.cc/index.php/topic,148850.0.html

            client.println("<a href='/?on3' target='inlineframe'>Relay 1 On</a>");
            client.println("<a href='/?off3' target='inlineframe'> Off</a>

");
           
            client.println("<a href='/?on5' target='inlineframe'> Relay 2 On</a>");
            client.println("<a href='/?off5' target='inlineframe'>Off</a>

");

            client.println("<a href='/?on6' target='inlineframe'>Relay 3 On</a>");
            client.println("<a href='/?off6' target='inlineframe'>Off</a>

");

            client.println("<a href='/?on7' target='inlineframe'>Relay 4 On</a>");
            client.println("<a href='/?off7' target='inlineframe'>Off</a>");

Thank you. I tried to delete the code from the msg. and repost it properly. I could not delete it.