Program the alarm via a webpage... HELP

Hi, i´m trying to program a alarm via a webpage, I have this line of code to create the button but I can´t read the values… :frowning:

cliente.println("Hour: Min: ");

Thanks

I have this line of code to create the button but I can´t read the values...

Why can't you? Post ALL of your code to illustrate that you even tried.

Here is my code…

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

int rele1 =  2;

int AlarmHour = 0;

int AlarmMinute = 0;

EthernetServer servidor(8080);

byte mac[]={0xDE,0xAD,0xBE,0xEF,0xFE,0xED}; //MAC 

IPAddress ip(169, 254, 23, 17); //IP 

String readString=String(30); //le os caracteres de uma sequencia em cadeia.

String state=String(6);

void setup() {
  
Ethernet.begin(mac, ip); //Inicializamos com as direccões designadas

servidor.begin(); 

  Serial.begin(9600);
  
  setTime(0,0,0,1,1,11); // define hora
  
  pinMode(rele1, OUTPUT);

  digitalWrite(rele1, LOW);

  state="OFF  ";

}

void loop()
{

digitalClockDisplay();

Alarm.delay(1000);

//EthernetClient Crea um cliente
EthernetClient cliente= servidor.available(); 
if(cliente) {
boolean lineaenblanco=true; 
while(cliente.connected()) {
if(cliente.available()) {
char c=cliente.read(); 
if(readString.length()<30) {
readString.concat(c);
//Cliente conectado
} 
if(c=='\n' && lineaenblanco)
{
         AlarmHour = readString.indexOf("H");
         AlarmMinute = readString.indexOf("M");

// vamos verificar se o LED deve ser ligado
          
          if(readString.indexOf("r1")>=0)
          {
            // O Led vai ser ligado
           digitalWrite(rele1, HIGH);
           state="ON  ";
          }
          // Se a string possui o texto L=Desligar
          if(readString.indexOf("d1")>=0)
          {
            // O Led vai ser desligado
            digitalWrite(rele1, LOW);
            state="OFF  ";
          }

//HTTP estándar
cliente.println("HTTP/1.1 200 OK"); 
cliente.println("Content-Type: text/html"); 
cliente.println(); //Página Web en HTML 
cliente.println("<html>");
cliente.println("<head>"); 
cliente.println("<title>Arduino</title>"); 
cliente.println("</head>");
cliente.println("<body width=100% height=100%>"); 
cliente.println("<center>");
cliente.print("<B><span style=\"color: #000080;\">");
cliente.print("<h1>O Arduino do....</h1>");
cliente.println("<input type=submit value='RELE 2:ON' style=width:200px;height:35px onClick=location.href='./?r1\'>"); 
cliente.println("<input type=submit value='RELE 2:OFF' style=width:200px;height:35px onClick=location.href='./?d1\'>"); 
cliente.print("<B><span style=\"color: #800000;\">");
cliente.println("<B><span style=\"font-size:11px;\">");
cliente.print("  ESTADO: ");
cliente.print(state);
cliente.print("

"); 
          cliente.print("Alarm: ");
          if (AlarmHour<10)
            cliente.print("0");
          cliente.print(AlarmHour);
          cliente.print(":");
          if (AlarmMinute<10)
            cliente.print("0");
          cliente.print(AlarmMinute);
          cliente.println("


");
          cliente.println("<form method=get>Hour:<input type=text size=2 name=H> Min:<input type=text size=2 name=M>&nbsp;<input name=E type=submit value=Submit></form>");
cliente.println("</center>"); 
cliente.println("</body>"); 
cliente.println("</html>"); 
cliente.stop();
//Coneção terminada com o cliente 
readString="";
}
}
}
}
}
  
 void digitalClockDisplay()
{
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.println(); 
}

void printDigits(int digits)
{
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}
String readString=String(30); //le os caracteres de uma sequencia em cadeia.

This is identical to:

String readString="30";

I guess I’m failing to see how that is a reasonable initial value.

if(cliente) {
boolean lineaenblanco=true; 
while(cliente.connected()) {
if(cliente.available()) {
char c=cliente.read(); 
if(readString.length()<30) {
readString.concat(c);
//Cliente conectado
}

Collect the
complete
client response
but don’t print
it because
it might
provide
a clue.
And, we
certainly
don’t
want
that.

         AlarmHour = readString.indexOf("H");
         AlarmMinute = readString.indexOf("M");

So, now you know where in the String the H and M are.

cliente.print("

"); 
          cliente.print("Alarm: ");
          if (AlarmHour<10)
            cliente.print("0");
          cliente.print(AlarmHour);
          cliente.print(":");
          if (AlarmMinute<10)
            cliente.print("0");
          cliente.print(AlarmMinute);

Nonsense. The position of the H or M in the data from the client is NOT equal to any kind of time.

Sorry, is my frist arduino program, I think is normal have some issues... but I´m trying to solve it... I will try to understand your answer

I think is normal have some issues

Of course it is. Programming wouldn’t be so much fun if it wasn’t challenging.

I will try to understand your answer

I’ll help. After:

if(cliente)
{
   boolean lineaenblanco=true; 
   while(cliente.connected())
   {
      if(cliente.available())
      {
         char c=cliente.read(); 
         if(readString.length()<30)
         {
            readString.concat(c);
         }
      }
   }
}

add:

Serial.print("The client request is: [");
Serial.print(readString);
Serial.println("]");

Then, show is the serial output.

I will try that at home... :wink:

If I understand whith that code you want me to see what I do whenever I click a button, right?

If I understand whith that code you want me to see what I do whenever I click a button, right?

I want (you) to see how the client's GET request changes as you interact with the web page, initially requesting the page and re-requesting it as you interact with the page.

Are you trying to say that my problem is always make two requests?

Are you trying to say that my problem is always make two requests?

No. When your browser asks the Arduino to serve up the page, it does so by making a GET request. When you click on a button on the page, the browser makes another GET request, with the name and state of the button as added data. Exactly how the two GET requests differ is what is important, in order to be able to distinguish one from the other and in order to parse the requests to see what to do.

Now I think I understand, I have to send the request I want in a different way, so that the arduino has no trouble identifying the request that I want

I have to send the request I want in a different way, so that the arduino has no trouble identifying the request that I want

Nope. The browser handles all of that for you. You just need to pay attention to what the browser is asking for.

I include this line of code, I guess now I can already read the values ??that I want , I just need to know how …

if(c=='\n' && lineaenblanco)
         {
          if (readString.indexOf("?H") > 1)
          {       
          AlarmHour = readString.indexOf("H");
          AlarmMinute = readString.indexOf("M");
          }

Some button test code that prints info to the serial monitor for debugging.

//zoomkat 8-04-12
//simple button GET server code to control servo and arduino pins 5, 6, and 7
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use ' instead of " in the html 
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields
///note that the below bug fix may be required
// http://code.google.com/p/arduino/issues/detail?id=605 

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // 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(5, OUTPUT); //pin selected to control
  pinMode(6, OUTPUT); //pin selected to control
  pinMode(7, OUTPUT); //pin selected to control
  pinMode(8, 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("server multi pin button test 1.0"); // 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 

          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>");
          
          // For simple testing, pin 5, 6, 7, and 8 are used in buttons
          // DIY buttons
          client.println("<a href=/?on2 >ON</a>"); 
          client.println("<a href=/?off3 >OFF</a>"); 
          client.println("&nbsp;<a href=/?off3579 >ALL OFF</a>

"); 

          // mousedown buttons
          client.println("<input type=button value=ON onmousedown=location.href='/?on4'>"); 
          client.println("<input type=button value=OFF onmousedown=location.href='/?off5'>");        
          client.println("&nbsp;<input type=button value='ALL OFF' onmousedown=location.href='/?off3579'>

");        
                   
          // mousedown radio buttons
          client.println("<input type=radio onmousedown=location.href='/?on6'>ON</>"); 
          client.println("<input type=radio onmousedown=location.href='/?off7'>OFF</>"); 
          client.println("&nbsp;<input type=radio onmousedown=location.href='/?off3579'>ALL OFF</>

");    
   
          
          // custom buttons
          client.print("<input type=submit value=ON style=width:100px;height:45px onClick=location.href='/?on8'>");
          client.print("<input type=submit value=OFF style=width:100px;height:45px onClick=location.href='/?off9'>");
          client.print("&nbsp;<input type=submit value='ALL OFF' style=width:100px;height:45px onClick=location.href='/?off3579'>");

          client.println("</BODY>");
          client.println("</HTML>");
 
          delay(1);
          //stopping client
          client.stop();

          ///////////////////// control arduino pin
          if(readString.indexOf('2') >0)//checks for 2
          {
            digitalWrite(5, HIGH);    // set pin 5 high
            Serial.println("Led 5 On");
          }
          if(readString.indexOf('3') >0)//checks for 3
          {
            digitalWrite(5, LOW);    // set pin 5 low
            Serial.println("Led 5 Off");
          }
          
          if(readString.indexOf('4') >0)//checks for 4
          {
            digitalWrite(6, HIGH);    // set pin 6 high
            Serial.println("Led 6 On");
          }
          if(readString.indexOf('5') >0)//checks for 5
          {
            digitalWrite(6, LOW);    // set pin 6 low
            Serial.println("Led 6 Off");
          }
          
           if(readString.indexOf('6') >0)//checks for 6
          {
            digitalWrite(7, HIGH);    // set pin 7 high
            Serial.println("Led 7 On");
          }
          if(readString.indexOf('7') >0)//checks for 7
          {
            digitalWrite(7, LOW);    // set pin 7 low
            Serial.println("Led 7 Off");
          }     
          
            if(readString.indexOf('8') >0)//checks for 8
          {
            digitalWrite(8, HIGH);    // set pin 8 high
            Serial.println("Led 8 On");
          }
          if(readString.indexOf('9') >0)//checks for 9
          {
            digitalWrite(8, LOW);    // set pin 8 low
            Serial.println("Led 8 Off");
          }         
             
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}
          if (readString.indexOf("?H") > 1)
          {       
          AlarmHour = readString.indexOf("H");
          AlarmMinute = readString.indexOf("M");
          }

You know that there is an ?H in the stream of data. So, what value is being assigned to AlarmHour? Does that value make sense?

How can the POSITION of the H in the stream mean ANYTHING?

The condition solves the problem whenever I click a button don´t change the alarm.
E
very time a send a request I have something like this....

The client request is: [GET /?H=124&M=126&E=Submit HTT]

So I know that I want the first 2 values ??of the expression H = and the value must be a number lower or equal to 23, and the 2 values ??of M = expression and the value must be a number lower or equal to 59.

Since the button accepts more than two elements, I can't count characters, I was waiting with my first formula that the program assumed the values ??of H and M, but it seems not, now I know what to do but I don´t know howto do it .... :frowning:

Since the button accepts more than two elements

What? This doesn't make sense. Buttons are for clicking. They don't "accept elements".

I can't count characters

You'll have to.

You know that the stream contains ?H. From there, you determine where the H is. Then, you determine where the & is (that follows the H). You count the number of characters between the two positions. Well, actually, you do a little math to calculate the length of the hour substring. Then, since you insist on pissing away resources using Strings, you create another String using String::substring() and the position and length (or two positions; you get to look up which it is).

cliente.println("<form method=get>Hour:<input type=text size=2 name=H> Min:<input type=text size=2 name=M>&nbsp;<input name=E type=submit value=Submit></form>");

I’m trying to say, since the button is two input text it acepts more than 2 characters… so count characters don´t seemed logical at the moment …

Then, since you insist on pissing away resources using Strings

Theres another way? Can you explain me what, for i cant search for it?

I'm trying to say, since the button is two input text it acepts more than 2 characters.... so count characters don´t seemed logical at the moment ...

I'm tired of this thread. You seem completely clueless on the whole topic of client/server architecture and on html construction. You don't even seem interested in learning.

The input FIELD is NOT a BUTTON!