Hey guys, im doing a small DIY Misting system that the user will set interval of misting time and seconds.
Like user wants it to start either very Hour for 2 seconds or user wants it to start at specific times like 12PM 1PM 4PM...
My code works only if i set the time
but is there a way to set interval every 2h ?
i wanna give the most options..lol
for the time i can make a web form with presets that save to the memory.
i current Code:
#include <NTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <TimeLib.h>
#include <time.h>
#include <Timezone.h>
#include <ESP8266WebServer.h>
#include <WiFiClient.h>
#include <OneWire.h>
ESP8266WebServer server(80);
const char *ssid = "SSID";
const char *password = "PASS";
const int led = 2;
String formattedTime ="";
String page = "";
String day_night ="";
String date;
String t;
byte ledState = LOW;
String tTrigger;
unsigned long previousMillis = 0;
unsigned long intervalOn = 1000; //use 3000 for three seconds on
unsigned long intervalOff= 2000;//use 3600000 for one hour off
unsigned long interval = intervalOff; //start off
const char * days[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"} ;
const char * months[] = {"Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec"} ;
const char * ampm[] = {"AM", "PM"} ;
time_t local, utc;
static tm getDateTimeByParams(long time){
struct tm *newtime;
const time_t tim = time;
newtime = localtime(&tim);
return *newtime;
}
static String getDateTimeStringByParams(tm *newtime, char* pattern = (char *)"%d/%m/%Y %H:%M:%S"){
char buffer[30];
strftime(buffer, 30, pattern, newtime);
return buffer;
}
static String getEpochStringByParams(long time, char* pattern = (char *)"%d/%m/%Y %H:%M:%S"){
tm newtime;
newtime = getDateTimeByParams(time);
return getDateTimeStringByParams(&newtime, pattern);
}
static String getEpochStringByParams2(long time, char* pattern = (char *)"%d/%m/%Y %H:%M"){
tm newtime;
newtime = getDateTimeByParams(time);
return getDateTimeStringByParams(&newtime, pattern);
}
WiFiUDP ntpUDP;
int GTMOffset = 0;
NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", GTMOffset*60*60, 60*60*1000);
void setup(){
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
WiFi.begin(ssid, password);
timeClient.begin();
while ( WiFi.status() != WL_CONNECTED ) {
delay ( 500 );
Serial.print ( "." );
}
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); //Print the local IP
server.on("/", handle_OnConnect);
server.onNotFound(handle_NotFound);
Serial.println("Server started");
server.begin();
digitalWrite(LED_BUILTIN, HIGH);
timeClient.begin();
delay(100);
if (timeClient.update()){
Serial.print ( "Adjust local clock" );
unsigned long epoch = timeClient.getEpochTime();
utc = epoch;
setTime(epoch);
}else{
Serial.print ( "NTP Update not WORK!!" );
}
}
void loop() {
t = "";
date = "";
tTrigger = "";
timeClient.update();
unsigned long epoch = timeClient.getEpochTime();
utc = epoch;
TimeChangeRule usEDT = {"EDT", Second, Sun, Mar, 2, -240}; // Eastern Daylight Time = UTC - 4 hours
TimeChangeRule usEST = {"EST ", First, Sun, Nov, 2, -360}; // Eastern Standard Time = UTC - 5 hours
Timezone usET(usEDT, usEST);
local = usET.toLocal(utc);
date += days[weekday(local)-1];
date += ", ";
date += months[month(local)-1];
date += " ";
date += day(local);
date += ", ";
date += year(local);
t += hourFormat12(local);
t += ":";
if(minute(local) < 10)
t += "0";
t += minute(local);
t += " ";
t += ampm[isPM(local)];
if(t == "1:10 PM"){
startMist();
}else if(t=="2:00 PM"){
startMist();
}else if(t=="3:00 PM"){
startMist();
}else if(t=="4:00 PM"){
startMist();
}else if(t=="5:00 PM"){
startMist();
}
//digitalWrite(led, HIGH);digitalWrite(led, LOW);
server.send(200, "text/html", SendHTML(t,date));
server.handleClient();
}
void startMist(){
digitalWrite(LED_BUILTIN, LOW);
unsigned long currentMillis = millis();
Serial.print ( "Starteing MIST" );
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (ledState == LOW) {
ledState = HIGH;
interval = intervalOn;
} else {
ledState = LOW;
interval = intervalOff;
Serial.print ( "Stoped MIST" );
}
digitalWrite(LED_BUILTIN, ledState);
}
}
void handle_OnConnect() {
server.send(200, "text/html", SendHTML(t,date));
}
void handle_NotFound(){
server.send(404, "text/plain", "Not found");
}
String SendHTML(String formattedTime,String date){
String ptr = "<!DOCTYPE html> <html>\n";
ptr +="<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
ptr +="<meta http-equiv=\"refresh\" content=\"1\" >";
ptr +="<title>ESP8266 Weather Report</title>\n";
ptr +="<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
ptr +="body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;}\n";
ptr +="p {font-size: 24px;color: #444444;margin-bottom: 10px;}\n";
ptr +="</style>\n";
ptr +="</head>\n";
ptr +="<body>\n";
ptr +="<div id=\"webpage\">\n";
ptr +="<h1>ESP8266 NodeMCU Report</h1>\n";
ptr +="<p>Time: ";
ptr +=(String)formattedTime;
ptr +="\n";
ptr +=(String)date;
ptr +="</div>\n";
ptr +="</body>\n";
ptr +="</html>\n";
return ptr;
}