Pages: [1]   Go Down
Author Topic: WEB SERVER - How To Read An HTML Form Button  (Read 1334 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I compiled this sketch and have no errors.
The web buttons appear on the web page and when clicked they do send a code but I cannot figure how to link
the button to the action
I am requesting some basic code /help on how to read the button form displayed on the web server page to turn on digitalwrite pin
Once someone shows me how to do one button , i can work from there....

Thanks,

Joe





Code:
/* jb modified - Web_Authentication.ino - */


/* you can change the authentication realm by defining
 * WEBDUINO_AUTH_REALM before including WebServer.h */
#define WEBDUINO_AUTH_REALM "Weduino Authentication Example"

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




static uint8_t mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

static uint8_t ip[] = {
  192, 168, 1, 136 };



String readString;  
boolean tog1 = false; //rf1 on
boolean tog2 = true; //rf2 off

//-----------------------------------------------------------------------

/* This creates an instance of the webserver.  By specifying a prefix
 * of "", all pages will be at the root of the server. */
#define PREFIX ""
WebServer webserver(PREFIX, 98);

void defaultCmd(WebServer &server, WebServer::ConnectionType type, char *, bool)
{
  server.httpSuccess();
  if (type != WebServer::HEAD)
  {
    P(helloMsg) = "<h1>First entry page -Hello, World!</h1><a href=\"private.html\">ACCESS ADMIN CONTROL page</a>";
    server.printP(helloMsg);
  }
}

void privateCmd(WebServer &server, WebServer::ConnectionType type, char *, bool)
{
  /* if the user has requested this page using the following credentials
   * username = admin
   * password = joe
   * 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 "admin:joe"
   *
   * if you need to change the username/password dynamically please search
   * the web for a Base64 library */
  if (server.checkCredentials("YWRtaW46am9l"))
  {
    server.httpSuccess();
    
    if (type != WebServer::HEAD)
    {
      Serial.println(readString);
  

// HTML FORM  BUTTONS TO TURN ON / OFF  ITEM

      P(background) = "<body style=background-color:BLACK>"; //set background to BLACK
      P(helloMsg) = "<center><font color=’green’> <h1>HOME - INTERNET -- CONTROL </h1></font></center>";
      P(FORMOPEN) = "<form  method=get name=LED>";
      P(button1) = "<center><button name=tog1 value=1 type=submit style=font-weight:bold;color:GREEN;height:70px;width:145px>AC POWER RF FAN  On</button>";
      P(button2) = "<button name=tog2 value=1 type=submit style=font-weight:bold;color:red;height:70px;width:145px>AC POWER RF FAN Off</button>";
      P(FORMCLOSE) = "</form>";
      
      // DISPLAY layout - print items to web page:

      server.printP(background);

      server.printP(helloMsg);
      server.printP(FORMOPEN);
      server.printP(button1);
      server.printP(button2);
    
    }
  }

  else
  {
    // send a 401 error back causing the web browser to prompt the user for credentials
    server.httpUnauthorized();
  }
}


void setup()
{
  Ethernet.begin(mac, ip);
  webserver.setDefaultCommand(&defaultCmd);
  webserver.addCommand("index.html", &defaultCmd); // FIRST PAGE
  webserver.addCommand("private.html", &privateCmd); // ADMIN CONTROL - REQUIRES ACCESS
  webserver.begin();


  
  pinMode(5, OUTPUT); //pin selected to control pwr1 ON
  pinMode(6, OUTPUT); //pin selected to control pwr1 off
  
}
  

void loop()
{
  char buff[64];
  int len = 64;
  
  // ===============================================================================================
  
  
  // - PROBLEM AREA - HOW DO GET TOG1 / TOG 2 - TO BE READ - WHEN A WEB PAGE BUTTON IS CLICKED / SELECTED ????
  //-----------------------------------------------------------------------------------------------------------------------------------------

          if(readString.indexOf("tog1") >0)// on   for PWR MODULE ID 1 control
          {

            digitalWrite(5, HIGH);
            tog1 = true;
            delay(500);
            digitalWrite(5, LOW);

          }

          if(readString.indexOf("tog2") >0)// off   PWR MODULE ID 1 control
          {
            digitalWrite(6, HIGH);
            delay(500);
            digitalWrite(6, LOW);
            tog1 = false;

          }
//=====================================================================================================


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

      

}
« Last Edit: November 21, 2012, 05:12:15 pm by joeman » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48569
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
String readString; 
Start with getting rid of the String class. I'm getting tired of telling people not to use that buggy class.

Code:
      P(FORMOPEN) = "<form  method=get name=LED>";
You need to look at how to specify the proper action (method) when a form contains submit buttons. There are plenty of examples around.

Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

PaulS,

I have a working arduino web server with several buttons, temperature, button status, lcd  etc etc, using the html code P(FORMOPEN) = "<form  method=get name=LED>"; and it has been working well for many months.
My current arduino web server uses readString.indexOf to get the input from the buttons for this server.



I would like to add a web login feature, so i tried using the webaduino example.  (the code i posted)
I am trying to incorporate this type of function, unfortunately,  the webadaduino uses another method to read button inputs, which i am not able to figure out, nor could i find any examples!

Joe

Logged

Pages: [1]   Go Up
Jump to: