Compare hour with input time html with rtc ds3231

Hello,

I have a webserver on arduino, using the ethernet shield.

I need to make a comparison of the current schedule with the schedule scheduled by the user.
I'm trying to do this using the input time in html, and with javascript, the comparison works
but the problem is when you call the function in C to make the engine work.

Help, thanks.

I'm going to post the code of the html page that is identical to the one that is made in arduino with the javascript working the time comparison.

<html>

<head>

<script>



function test() {

  var element = document.getElementById("time").value;
  
  if (element == "") {
  alert("Please Enter Time");
    return false;  
  }
  else {
  
  // get system local time
  var d = new Date();
  var m = d.getMinutes();
  var h = d.getHours();
  if(h == '0') {h = 24}
  
  var currentTime = h+"."+m;
  console.log(currentTime);
 
  // get input time
  var time = element.split(":");
  var hour = time[0];
  if(hour == '00') {hour = 24}
  var min = time[1];
  
  var inputTime = hour+"."+min;
  console.log(inputTime);
  
  var totalTime = currentTime - inputTime;
  console.log(totalTime);
  
  if (currentTime == inputTime) {

// how to use this function in C in javascript?
      digitalWrite(pinoTransistor, HIGH);
                delay(1000);
                digitalWrite(pinoTransistor, LOW);
                delay(1000);

 
       


  } 
  else {
    alert("teste2");

  }
    }
}

</script>
</head>

<body>
<html>

<body>
  <form method="get">
    <span>Time</span>
    <input type="time" id="time" required>

    <input type="button" value="CHECK" onclick="return test();">
    

    <label id="check"></label>

    <input type="submit" name="submit" value="submit">
  </form>
</body>

</html>

</body>


</html>

I use input time html and javaascript for compare current time with scheduled time, but i need start a motor dc at the time scheduled by the user.

Your code speaks English very well. You should let it do the talking, since you can't speak, or write, English that well.

<html>

<head>

<script>



function test() {

  var element = document.getElementById("time").value;
  
  if (element == "") {
  alert("Please Enter Time");
    return false;  
  }
  else {
  
  // get system local time
  var d = new Date();
  var m = d.getMinutes();
  var h = d.getHours();
  if(h == '0') {h = 24}
  
  var currentTime = h+"."+m;
  console.log(currentTime);
 
  // get input time
  var time = element.split(":");
  var hour = time[0];
  if(hour == '00') {hour = 24}
  var min = time[1];
  
  var inputTime = hour+"."+min;
  console.log(inputTime);
  
  // compare current time w/ input time
  
  if (currentTime == inputTime) {

// i need help for work this code 

                pinoTransistor = 8;
                digitalWrite(pinoTransistor, HIGH);
                delay(1000);
                digitalWrite(pinoTransistor, LOW);
                delay(1000); 

  } 
  else {
    alert("test2");

  }
    }
}

</script>
</head>

<body>
<html>

<body>
  <form method="get">
    <span>Time</span>
    <input type="time" id="time" required>

    <input type="button" value="CHECK" onclick="return test();">
    

    <label id="check"></label>

    <input type="submit" name="submit" value="submit">
  </form>
</body>

</html>

</body>


</html>

This page html i have in WebServer arduino

i need help for exec this code in javascript in some way

pinoTransistor = 8;
                digitalWrite(pinoTransistor, HIGH);
                delay(1000);
                digitalWrite(pinoTransistor, LOW);
                delay(1000);

PaulS:
Your code speaks English very well. You should let it do the talking, since you can't speak, or write, English that well.

thanks for the help

The javascript code runs on the client. It has no impact on the server. You can not make your javascript do anything with the Arduino pins.

Why do you have one open body tag and two close body tags?
Why do you have one open html tag and two close html tags?

You have a submit button. Normally, that causes the browser to make another GET request, with the form data. You need to show us the Arduino code, so we can help you decide what to do when the client makes a GET request.

This my code in arduino,
how to make my javascript function perform the ligamotor(); function? I've seen some people doing with the innerHTML code in javascript.

I was thinking of doing with GET, but how do I call my javascript when I trigger the submit button? I need it because it compares the time

#include <SPI.h>
#include <Ethernet.h>
int pinoTransistor = 8;

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

String readString; 

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

void setup(){
pinMode(pinoTransistor, OUTPUT);

  pinMode(4, 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("servertest1"); // 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");
             }
             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>Alimentador</title>");
          client.println("<meta http-equiv='Content-Type' content='text/html; charset=ISO-8859-1'>");
          client.println("<meta name='viewport' content='width=720, initial-scale=0.5' />");
          client.println("<link rel='stylesheet' type='text/css' href='https://cdn.rawgit.com/eltonsjbv1/tcc/master/estilo.css' />");
          client.println("<script type='text/javascript' src='https://cdn.rawgit.com/eltonsjbv1/tcc/master/jss.js'> </script>");
       
          client.println("</head>");
          client.println("<body>");
          client.println("<FORM ACTION=\"http://192.168.0.41:80\" method=get >");
          client.println("<div id='wrapper'><img alt='' src=''/>
");
          client.println("<div id='div1'>Escolha as horas de alimentar o seu pet.
");
          client.println("
");
          client.println("<div <label for=on5 ></label> <input id='time' type='time' name='time' min='06:00' max='21:00' required> <span class='validacao'>");

          client.println("<input type='button' value='CHECK' onclick='return test();'>");
          client.println("</span"); 
          client.println("</div>");
          client.println("<INPUT TYPE=SUBMIT NAME='submit' VALUE='Alimentar'>");
          client.println("</div>");
          client.println("</div>");
          
          
          
         
         
          client.println("</FORM>");
          client.println("</BODY>");
          client.println("</HTML>");
             }

          delay(1);
          //stopping client
          client.stop();
          if(readString.indexOf("?ligar") >0)//checks for on
          {
              
              Serial.println("servertest3");

              ligamotor();
             
          }
          
          if(readString.indexOf("off5") >0 ){
            Serial.println("servertest2");
           
            }

          
            
         readString="";

        }
      }
    }
  }
} 

void ligamotor(){


                digitalWrite(pinoTransistor, HIGH);
                delay(1000);
                digitalWrite(pinoTransistor, LOW);
                delay(1000);
         


}

how to make my javascript function perform the ligamotor(); function?

The javascript function runs on the client (on the machine where the browser is running). You can NOT make that client do ANYTHING with the Arduino. The browser had never heard of an Arduino.

I was thinking of doing with GET, but how do I call my javascript when I trigger the submit button?

You can't. You clearly do not understand what the javascript is doing, can do, and hasn't a hope in hell of doing.

I think you're the one who is not understanding the question.

Compare the time the user enters the html input time tag with the current time and start the engine

I think you're the one who is not understanding the question.

The question was how to make the javascript make the Arduino do something. You don't accept the answer that you can't.

Compare the time the user enters the html input time tag with the current time and start the engine

THAT is a much different requirement. You first have to get the time that the user entered, from the GET request.

Before you can do that, you need to KNOW what the GET request, when the submit button is pressed, looks like.

Second, you need to get the current time. Where do you plan to get that? The Arduino does NOT know what time it is.

Third, you compare the current time to the user requested time to see if it is time to do whatever. The simplest way is to convert the current time to minutes since midnight (or seconds since midnight) and to convert the requested time to minutes since midnight (or seconds). Then, it is a simple matter of comparing two values for equality.

PaulS:
The question was how to make the javascript make the Arduino do something. You don't accept the answer that you can't.
THAT is a much different requirement. You first have to get the time that the user entered, from the GET request.

Before you can do that, you need to KNOW what the GET request, when the submit button is pressed, looks like.

Second, you need to get the current time. Where do you plan to get that? The Arduino does NOT know what time it is.

Third, you compare the current time to the user requested time to see if it is time to do whatever. The simplest way is to convert the current time to minutes since midnight (or seconds since midnight) and to convert the requested time to minutes since midnight (or seconds). Then, it is a simple matter of comparing two values for equality.

maybe,

PaulS:
The question was how to make the javascript make the Arduino do something. You don't accept the answer that you can't.
THAT is a much different requirement. You first have to get the time that the user entered, from the GET request.

Before you can do that, you need to KNOW what the GET request, when the submit button is pressed, looks like.

Second, you need to get the current time. Where do you plan to get that? The Arduino does NOT know what time it is.

Third, you compare the current time to the user requested time to see if it is time to do whatever. The simplest way is to convert the current time to minutes since midnight (or seconds since midnight) and to convert the requested time to minutes since midnight (or seconds). Then, it is a simple matter of comparing two values for equality.

to get a current update time Some library of rtc ds3231, but still do not know which one to use, would you recommend some?

zago123:
to get a current update time Some library of rtc ds3231, but still do not know which one to use, would you recommend some?

Here is what I use. It's not a library, but it does what I need it to do.

#include <Wire.h>

// variables for storing the time
//   second  minute  hour    weekday  date    month   year
byte ss=0,   mi=0,   hh=0,   wd=6,    dd=1,   mo=1,   yy=0;
 
void setup()
{
  Wire.begin();
  Serial.begin(9600); // or whatever baud rate you wish to use
 
  // clear /EOSC bit
  // Sometimes necessary to ensure that the clock
  // keeps running on just battery power. Once set,
  // it shouldn't need to be reset but it's a good
  // idea to make sure.
//  Wire.beginTransmission(0x68); // address DS3231
//  Wire.write(0x0E); // select register
//  Wire.write(0b00011100); // write register bitmap, bit 7 is /EOSC
//  Wire.endTransmission();
}
 
void loop()
{
  // read the time from the RTC, if we can
  boolean gotTheTime = grabTime();
 
  if (gotTheTime) {
    // if we are here, then the time has been successfully read
    // and stored in global variables (ss, mi, hh, wd, dd, mo, yy)
    Serial.print("Got the time: ");
    printTime();
  }
  else {
    // if we are here, then we tried to read the time but couldn't
    Serial.println("Unable to read time from RTC");
  }
 
  delay(500);
}


boolean grabTime() {
  // get time from the RTC and put it in global variables

  // send request to receive data starting at register 0
  Wire.beginTransmission(0x68); // 0x68 is DS3231 device address
  Wire.write((byte)0); // start at register 0
  Wire.endTransmission();
  Wire.requestFrom(0x68, 7); // request seven bytes (ss, mi, hh, wd, dd, mo, yy)
  // check for a reply from the RTC, and use it if we can
  if (Wire.available() >= 7) {
    // if we're here, we got a reply and it is long enough
    // so now we read the time
    ss = bcd2bin(Wire.read()); // get seconds
    mi = bcd2bin(Wire.read()); // get minutes
    hh = bcd2bin(Wire.read()); // get hours
    wd = bcd2bin(Wire.read()); // get day of week
    dd = bcd2bin(Wire.read()); // get day of month
    mo = bcd2bin(Wire.read()); // get month
    yy = bcd2bin(Wire.read()); // get year (two digits)
    // indicate that we successfully got the time
    return true;
  }
  else {
    // indicate that we were unable to read the time
    return false;
  }
}


byte bcd2bin(byte x) {
  // converts from binary-coded decimal to a "regular" binary number
  return ((((x >> 4) & 0xF) * 10) + (x & 0xF)) ;
}


void printTime() {
  // just like it says on the tin
  Serial.print ("\'");
  if (yy<10) Serial.print("0"); Serial.print(yy,DEC); Serial.print("-");
  if (mo<10) Serial.print("0"); Serial.print(mo,DEC); Serial.print("-");
  if (dd<10) Serial.print("0"); Serial.print(dd,DEC); Serial.print("(");
  switch (wd) {
    case 1: Serial.print("Mon"); break;
    case 2: Serial.print("Tue"); break;
    case 3: Serial.print("Wed"); break;
    case 4: Serial.print("Thu"); break;
    case 5: Serial.print("Fri"); break;
    case 6: Serial.print("Sat"); break;
    case 7: Serial.print("Sun"); break;
    default: Serial.print("Bad");
  }
  Serial.print(") ");
  if (hh<10) Serial.print("0"); Serial.print(hh,DEC); Serial.print(":");
  if (mi<10) Serial.print("0"); Serial.print(mi,DEC); Serial.print(":");
  if (ss<10) Serial.print("0"); Serial.print(ss,DEC); Serial.println("");
}