Hello everyone,
i have a Nodemcu, LCD and RTC. which are all setup and working fine. I setup my rtc through serial timer setting which is a diiferent program and the rtc is also working fine and showing correct time on the lcd. the only problem i am facing is that the program does not save (remember) the time i set on the server to trigger the relay.
(The part of the world that i live in faces load shedding in which electricity is not available to us for a few hours.)
Every time the electricity is gone or if I reset the nodemcu or if i switch off the power, I have to enter the on and off hours of the relay again on the web server.
Is there a way in which the program stores the time entered on the web server to repeat daily even if i reset the device?
Please help
#include <ESP8266WiFi.h>
#include "./DNSServer.h"
#include <ESP8266WebServer.h>
#include <RtcDS3231.h>
#include <Wire.h> // must be included here so that Arduino library object file references work
RtcDS3231<TwoWire> Rtc(Wire); //D1,D2
#include <LiquidCrystal_I2C.h>
const char *ssid = "Garden Lights"; //Set your SSID
const char *password = "123456789"; //Set your PASSWORD
const byte DNS_PORT = 53; // 53 is set as DNS port
IPAddress apIP(10, 10, 10, 1); // Network Server
DNSServer dnsServer; // DNS server object
ESP8266WebServer webServer(80); // Webserver object
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define relay1 D0 //Relay 1
String hr, minut, sec ;
int hr1, minut1, sec1;
String new_time;
/*START OF HMTL CODE*/
String style_detials = //This String defines the style attributes for webpage
"<style type=\"text/css\">"
" body{"
" background-color: #a69695;"
"}"
"button{"
" display: inline-block;"
"}"
"#buttons{"
" text-align: center;"
"}"
".controllButtons{"
" margin-top: 15px;"
"margin-left: 5px;"
"background-color: white;"
"padding: 10px;"
"border:1px solid black;"
"border-radius: 10px;"
"cursor: pointer;"
"font-size: 14px;"
"}"
".controllButtons:hover{"
" background-color: orange;"
"padding: 10px;"
"border:1px solid black;"
"border-radius: 10px;"
"cursor: pointer;"
"font-size: 14px;"
"}"
"@media only screen and (max-width: 700px) {"
" button{"
" display: block;"
"}"
"#buttons{"
" margin-top: 10%;"
"margin-left: 35%;"
"}"
" .controllButtons{"
" margin-top: 15px;"
"margin-left: 5px;"
"background-color: white;"
"padding: 15px;"
"border:1px solid black;"
"border-radius: 10px;"
"cursor: pointer;"
"font-size: 16px;"
"}"
".controllButtons:hover{"
" background-color: orange;"
"padding: 15px;"
"border:1px solid black;"
"border-radius: 10px;"
"cursor: pointer;"
"font-size: 16px;"
"}"
"}"
"</style>";
String home_screen = "" //Page 1 - Home Screen HTML code
"<!DOCTYPE html><html>"
"<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"
+ style_detials +
"<body><h1>Garden Lights</h1>"
"<div id=\"login\">"
" <form action=\"confirmation_screen.php\" method=\"get\">"
" <h5>Please enter Time in 24h format (hh:mm:ss)(21:22:23) for Light One</h5>"
" <div>On Time : <input type=\"numeric\" name=\"code\" autofocus></div>"
" <div>Off Time: <input type=\"numeric\" name=\"code\" autofocus></div>"
" <div id=\"submit\"><input type=\"submit\" value=\"Submit\"></div>"
" </form>"
"</div>"
"</body></html>";
String confirmation_screen = "" //Page 2 - If Time is Set
"<!DOCTYPE html><html>"
"<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"
+ style_detials +
"<body><h1>Time is now Set</h1>"
"</body></html>";
/*END OF HMTL CODE*/
void setup() {
pinMode(relay1, OUTPUT);
digitalWrite(relay1, HIGH);
Wire.begin(D2, D1);
lcd.begin();
Rtc.Begin();
WiFi.mode(WIFI_AP); //Set ESP in AP mode
WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
WiFi.softAP(ssid, password);
dnsServer.start(DNS_PORT, "*", apIP);
// redirect all requests to the login page
webServer.onNotFound([]() {
webServer.sendHeader("Location", String("http://www.gl.com/home_screen.php"), true);
webServer.send ( 302, "text/plain", "");
});
webServer.on("/home_screen.php", []() {
webServer.send(200, "text/html", home_screen);
});
webServer.on("/confirmation_screen.php", []()
{
if (webServer.args() > 0) {
new_time = webServer.arg(0); // Stores the Alarm time input
webServer.send(200, "text/html", confirmation_screen);} // when alarm time entered direct user to Confirmaton page
hr = new_time.substring(0, 2); // substring is used to split the data in hour and minute form
hr1 = hr.toInt(); // toInt is used to convert string into integer
minut = new_time.substring(3, 5);
minut1 = minut.toInt();
}
);
webServer.begin();
}
void loop() {
dnsServer.processNextRequest();
webServer.handleClient();
alarm();
}
void alarm ()
{
RtcDateTime now = Rtc.GetDateTime();
lcd.setCursor(0, 0);
lcd.print(" Time:");
if (now.Hour() <=9){
lcd.print("0");}
lcd.print(now.Hour(), DEC);
lcd.print(':');
if (now.Minute() <=9){
lcd.print("0");}
lcd.print(now.Minute(), DEC);
lcd.print(':');
if (now.Second() <=9){
lcd.print("0");}
lcd.print(now.Second(), DEC);
lcd.setCursor(0, 1);
lcd.print("LOGIN WWW.GL.COM");
const unsigned long TimeSecs = now.Hour() * 3600UL + now.Minute() * 60UL; //Convert the Hours And Minutes to Seconds and add them both with current Seconds
const unsigned long OnTimeSec = hr1 * 3600UL + minut1 * 60UL; //Convert the On Time to Seconds so that it can be compared to the off time for Relay1
const unsigned long OffTimeSec = hr3 * 3600UL + minut3 * 60UL; //Convert the Off Time to Seconds so that it can be compared to the on time for Relay1
if (TimeSecs >= OnTimeSec && TimeSecs < OffTimeSec) // Compare the current time with the entered time. If the current time is more than the On time set in the webserver and less then the OFf Time set in the webserver then switch on the relay
{
digitalWrite(relay1,LOW);
}
else
{
digitalWrite(relay1,HIGH); // If the above statement is not true then switch off the relay
}
}