#include "RTClib.h"
RTC_DS1307 rtc;
#if defined(__AVR__)
const uint8_t sda = 4;
const uint8_t scl = 5;
const uint8_t dev0 = 2;
const uint8_t dev1 = 4;
const uint8_t dev2 = 7;
const uint8_t dev3 = 7;
#elif defined(ESP8266)
// ESP8266 specific code here
const uint8_t sda = 0;
const uint8_t scl = 2;
const uint8_t dev0 = 1;
const uint8_t dev1 = 3;
const uint8_t dev2 = 3;
const uint8_t dev3 = 7;
#elif defined(ARDUINO_ESP8266_NODEMCU)
const uint8_t sda = 4;
const uint8_t scl = 5;
const uint8_t dev0 = 2;
const uint8_t dev1 = 0;
const uint8_t dev2 = 3;
const uint8_t dev3 = 7;
#elif defined(ESP32)
const uint8_t sda = 21;
const uint8_t scl = 22;
const uint8_t dev0 = 2;
const uint8_t dev1 = 18;
const uint8_t dev2 = 15;
const uint8_t dev3 = 19;
#else
#error "No MCU";
#endif
// dev 0
String tosend;
String chms,cmd;
unsigned long previousMillis;
int interval=1000;
//---default values -----------------------------------
// Current Time
bool dev0on = true, dev1on = true, dev2on = true, dev3on = true;
// default values
byte enas=15; // all timers are enabled
bool ena0=true,ena1=true,ena2=true,ena3=true;
byte nhh = 5, nmm = 58, nss = 00; // 05:58 am
String chh = "05",cmm = "58", css = "00"; // 05:58 am
// dev 0
// dev0 s1
byte nhh011= 6, nmm011=0,nhh010= 6, nmm010=1; // dev0 s1
byte nhh021= 17, nmm021=0,nhh020= 17, nmm020=1; // dev0 s2
byte nhh111= 6, nmm111=1,nhh110= 6, nmm110=2; // dev1 s1
byte nhh121= 17, nmm121=1,nhh120= 17, nmm120=2; // dev1 s2
byte nhh211= 6, nmm211=2,nhh210= 6, nmm210=3; // dev2 s1
byte nhh221= 17, nmm221=2,nhh220= 17, nmm220=3; // dev2 s2
byte nhh311= 6, nmm311=3,nhh310= 6, nmm310=4; // dev3 s1
byte nhh321= 17, nmm321=3,nhh320= 17, nmm320=4; // dev3 s2
word d011,d010,d021,d020;
word d111,d110,d121,d120;
word d211,d210,d221,d220;
word d311,d310,d321,d320;
word hhmm;
void setup () {
Serial.begin(115200);
Serial.println();
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
// check whether the nvram has been initialized
uint8_t nvramset=rtc.readnvram(0);
if (nvramset==0) { // not initialized
rtc.writenvram(0, 1); // initialize
rtc.writenvram(1, enas);
writeTimers();
}
loadnvram();
pinMode(dev0, OUTPUT);
pinMode(dev1, OUTPUT);
pinMode(dev2, OUTPUT);
pinMode(dev3, OUTPUT);
rtc.adjust(DateTime(__DATE__, "05:59:56"));
/*
DateTime now = rtc.now();
Serial.println(now.hour(),DEC);
Serial.println(now.minute(),DEC);
Serial.println(now.second(),DEC);
*/
}
void loop () {
while (Serial.available() > 0) {
tosend=Serial.readStringUntil('\n');
showInputCmd(tosend);
}
loop2();
}
void loop2() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
DateTime now = rtc.now();
nhh=now.hour();
nmm=now.minute();
nss=now.second();
hhmm=nhh*60+nmm;
chh=String(nhh);
cmm=String(nmm);
css=String(nss);
chh=(chh.length()==1?"0"+chh:chh);
cmm=(cmm.length()==1?"0"+cmm:cmm);
css=(css.length()==1?"0"+css:css);
chms=chh+":"+cmm+":"+css;
showtime();
updata(); // send current status to client
// dev0
d011=nhh011*60+nmm011;
d010=nhh010*60+nmm010;
d021=nhh021*60+nmm021;
d020=nhh020*60+nmm020;
if ((hhmm>=d011 && hhmm<d010) || (hhmm>=d021 && hhmm<d020))
digitalWrite(dev0,ena0);
else digitalWrite(dev0,LOW);
dev0on=digitalRead(dev0);
// dev1
d111=nhh111*60+nmm111;
d110=nhh110*60+nmm110;
d121=nhh121*60+nmm121;
d120=nhh120*60+nmm120;
if ((hhmm>=d111 && hhmm<d110) || (hhmm>=d121 && hhmm<d120)) digitalWrite(dev1,ena1);
else digitalWrite(dev1,LOW);
dev1on=digitalRead(dev1);
// dev2
d211=nhh211*60+nmm211;
d210=nhh210*60+nmm210;
d221=nhh221*60+nmm221;
d220=nhh220*60+nmm220;
if ((hhmm>=d211 && hhmm<d210) || (hhmm>=d221 && hhmm<d220)) digitalWrite(dev2,ena2);
else digitalWrite(dev2,LOW);
dev2on=digitalRead(dev2);
// dev3
d311=nhh311*60+nmm311;
d310=nhh310*60+nmm310;
d321=nhh321*60+nmm321;
d320=nhh320*60+nmm320;
if ((hhmm>=d311 && hhmm<d310) || (hhmm>=d321 && hhmm<d320)) digitalWrite(dev3,ena3);
else digitalWrite(dev3,LOW);
dev3on=digitalRead(dev3);
// delay(1000);
}
}
void showInputCmd(String inputcmd){
// valid prefixes
// N -- new client
// E -- set timers schedules
// D -- enable /disable devices
// T -- set the current time of the devices
char pfcmd=inputcmd[0];
Serial.println(pfcmd);
switch (pfcmd) {
case 'N':
showstat();
break;
case 'E':
setTimers(inputcmd);
break;
case 'D': // // format str: DEEEE
// sets the enabled/disabled status of the timers
devices(inputcmd);
break;
case 'S': // set as default
writeTimers();
break;
}
}
//---------------------------------
void showoutputcmd(String devstat){
//---------------------------------
// valid prefixes
// N -- broadcast the current time and timer settings
// U -- broadcast the status of disabled/enabled timers after settings
// D -- broadcast enabled/disabled devices
// T -- broadcast the current time of the devices
}
//---------------------------------
void showstat() { // show the status to the NEW client
String ledis0 = (ena0 and dev0on) ? "1" : "0";
//---------------------------
String pref = "N";
String chm011 = String(nhh011) + ":" + String(nmm011) + "|";
String chm010 = String(nhh010) + ":" + String(nmm010) + "|";
String chm021 = String(nhh021) + ":" + String(nmm021) + "|";
String chm020 = String(nhh020) + ":" + String(nmm020) + "|";
String sena0 = (ena0) ? "E" : "D";
String sdev0 = ledis0 + sena0 + "|" + chm011 + chm010 + chm021 + chm020;
String ledis1 = (ena1 and dev1on) ? "1" : "0";
String chm111 = String(nhh111) + ":" + String(nmm111) + "|";
String chm110 = String(nhh110) + ":" + String(nmm110) + "|";
String chm121 = String(nhh121) + ":" + String(nmm121) + "|";
String chm120 = String(nhh120) + ":" + String(nmm120) + "|";
String sena1 = (ena1) ? "E" : "D";
String sdev1 = ledis1 + sena1 + "|" + chm111 + chm110 + chm121 + chm120;
String ledis2 = (ena2 and dev2on) ? "1" : "0";
String chm211 = String(nhh211) + ":" + String(nmm211) + "|";
String chm210 = String(nhh210) + ":" + String(nmm210) + "|";
String chm221 = String(nhh221) + ":" + String(nmm221) + "|";
String chm220 = String(nhh220) + ":" + String(nmm220) + "|";
String sena2 = (ena2) ? "E" : "D";
String sdev2 = ledis2 + sena2 + "|" + chm211 + chm210 + chm221 + chm220;
String ledis3 = (ena3 and dev3on) ? "1" : "0";
String chm311 = String(nhh311) + ":" + String(nmm311) + "|";
String chm310 = String(nhh310) + ":" + String(nmm310) + "|";
String chm321 = String(nhh321) + ":" + String(nmm321) + "|";
String chm320 = String(nhh320) + ":" + String(nmm320) + "|";
String sena3 = (ena3) ? "E" : "D";
String sdev3 = ledis3 + sena3 + "|" + chm311 + chm310 + chm321 + chm320;
// show the current time and the current timer settings
String stat = "N|" + chms +"|"+sdev0 + sdev1 + sdev2 + sdev3;
webSocket_broadcastTXT(stat);
}
//----------------------------------------
void webSocket_broadcastTXT(String stat){
//----------------------------------------
Serial.println(stat);
}
// ----------------------------
void devices(String devstat) {
// enables/disables schedules
// --------------------------
cmd = devstat.substring(1); // DEEEE
Serial.print("devices=");
Serial.println(cmd);
ena0 = (cmd.substring(0, 1) == "E") ? true : false;
ena1 = (cmd.substring(1, 2) == "E") ? true : false;
ena2 = (cmd.substring(2, 3) == "E") ? true : false;
ena3 = (cmd.substring(3, 4) == "E") ? true : false;
webSocket_broadcastTXT(devstat);
}
//--------------------------------
void setTimers(String devstat) {
// set time Schedules by the user
//--------------------------------
// cmd = "E"
// format dev0+dev1+dev2+dev3
//dev0=t011+t010+t021+t020
//dev1=t011+t010+t021+t020
// set the devices schedules
Serial.print("set timers=");
Serial.println(devstat);
cmd = devstat.substring(1);
//dev 0
int pos=0;
nhh011 = cmd.substring(pos+0,pos+2).toInt(); // dev0 h s1 on
nmm011 = cmd.substring(pos+2,pos+4).toInt(); // dev0 m s1 on
nhh010 = cmd.substring(pos+4,pos+6).toInt(); // dev0 m s1 off
nmm010 = cmd.substring(pos+6,pos+8).toInt(); // dev0 m s1 off
nhh021 = cmd.substring(pos+8,pos+10).toInt(); // dev0 h s2 on
nmm021 = cmd.substring(pos+10,pos+12).toInt(); // dev0 m s2 on
nhh020 = cmd.substring(pos+12,pos+14).toInt(); // dev0 h s2 off
nmm020 = cmd.substring(pos+14,pos+16).toInt(); // dev0 m s2 off
// dev1
pos+=16;
nhh111 = cmd.substring(pos+0,pos+2).toInt(); // dev1 h s1 on
nmm111 = cmd.substring(pos+2,pos+4).toInt(); // dev1 m s1 on
nhh110 = cmd.substring(pos+4,pos+6).toInt(); // dev1 h s1 off
nmm110 = cmd.substring(pos+6,pos+8).toInt(); // dev1 m s1 off
nhh121 = cmd.substring(pos+8,pos+10).toInt(); // dev1 h s2 on
nmm121 = cmd.substring(pos+10,pos+12).toInt(); // dev1 m s2 on
nhh120 = cmd.substring(pos+12,pos+14).toInt(); // dev1 h s2 off
nmm120 = cmd.substring(pos+14,pos+16).toInt(); // dev1 m s2 off
// dev2
pos+=16;
nhh211 = cmd.substring(pos+0,pos+2).toInt(); // dev2 h s1 on
nmm211 = cmd.substring(pos+2,pos+4).toInt(); // dev2 m s1 on
nhh210 = cmd.substring(pos+4,pos+6).toInt(); // dev2 h s1 off
nmm210 = cmd.substring(pos+6,pos+8).toInt(); // dev2 m s1 off
nhh221 = cmd.substring(pos+8,pos+10).toInt(); // dev2 h s2 on
nmm221 = cmd.substring(pos+10,pos+12).toInt(); // dev2 m s2 on
nhh220 = cmd.substring(pos+12,pos+14).toInt(); // dev2 h s2 off
nmm220 = cmd.substring(pos+14,pos+16).toInt(); // dev2 m s2 off
//dev 3
pos+=16;
nhh311 = cmd.substring(pos+0,pos+2).toInt(); // dev3 h s1 on
nmm311 = cmd.substring(pos+2,pos+4).toInt(); // dev3 m s1 on
nhh310 = cmd.substring(pos+4,pos+6).toInt(); // dev3 h s1 off
nmm310 = cmd.substring(pos+6,pos+8).toInt(); // dev3 m s1 off
nhh321 = cmd.substring(pos+8,pos+10).toInt(); // dev3 h s2 on
nmm321 = cmd.substring(pos+10,pos+12).toInt(); // dev3 m s2 on
nhh320 = cmd.substring(pos+12,pos+14).toInt(); // dev3 h s2 off
nmm320 = cmd.substring(pos+14,pos+16).toInt(); // dev3 m s2 off
}
//-------------------
void showtime(){
//-------------------
// Serial.print("Time: ");
// Serial.println(chms);
// Dev 0 S1
//showTimers();
}
//------------------------
void updata() {
//------------------------
// sends the current time,enabled/disabled status battery level
// called by loop2. When no websocket event
// output "U|hhmmss"
String ledis0 = (ena0 and dev0on) ? "1" : "0";
String ledis1 = (ena1 and dev1on) ? "1" : "0";
String ledis2 = (ena2 and dev2on) ? "1" : "0";
String ledis3 = (ena3 and dev3on) ? "1" : "0";
String slevel=String(analogRead(A0));
String toSend = "U|" + chms+"|" + ledis0 + ledis1 + ledis2 + ledis3+"#"+slevel;
webSocket_broadcastTXT(toSend);
}
//
// ------------------------------
void showTimers(){
// for debugging only
//------------------------------
String chh011=String(nhh011);
chh011=(chh011.length()==1?"0"+chh011:chh011);
String cmm011=String(nmm011);
cmm011=(cmm011.length()==1?"0"+cmm011:cmm011);
String chh010=String(nhh010);
chh010=(chh010.length()==1?"0"+chh010:chh010);
String cmm010=String(nmm010);
cmm010=(cmm010.length()==1?"0"+cmm010:cmm010);
// Dev 0 S2
String chh021=String(nhh021);
chh021=(chh021.length()==1?"0"+chh021:chh021);
String cmm021=String(nmm021);
cmm021=(cmm021.length()==1?"0"+cmm021:cmm021);
String chh020=String(nhh020);
chh020=(chh020.length()==1?"0"+chh020:chh020);
String cmm020=String(nmm020);
cmm020=(cmm020.length()==1?"0"+cmm020:cmm020);
// Dev 1 S1
String chh111=String(nhh111);
chh111=(chh111.length()==1?"0"+chh111:chh111);
String cmm111=String(nmm111);
cmm111=(cmm111.length()==1?"0"+cmm111:cmm111);
String chh110=String(nhh110);
chh110=(chh110.length()==1?"0"+chh110:chh110);
String cmm110=String(nmm110);
cmm110=(cmm110.length()==1?"0"+cmm110:cmm110);
// Dev 1 S2
String chh121=String(nhh121);
chh121=(chh121.length()==1?"0"+chh121:chh121);
String cmm121=String(nmm121);
cmm121=(cmm121.length()==1?"0"+cmm121:cmm121);
String chh120=String(nhh120);
chh120=(chh120.length()==1?"0"+chh120:chh120);
String cmm120=String(nmm120);
cmm120=(cmm120.length()==1?"0"+cmm120:cmm120);
// Dev 2 S1
String chh211=String(nhh211);
chh211=(chh211.length()==1?"0"+chh211:chh211);
String cmm211=String(nmm211);
cmm211=(cmm211.length()==1?"0"+cmm211:cmm211);
String chh210=String(nhh210);
chh210=(chh210.length()==1?"0"+chh210:chh210);
String cmm210=String(nmm210);
cmm210=(cmm210.length()==1?"0"+cmm210:cmm210);
// Dev 2 S2
String chh221=String(nhh221);
chh221=(chh221.length()==1?"0"+chh221:chh221);
String cmm221=String(nmm221);
cmm221=(cmm221.length()==1?"0"+cmm221:cmm221);
String chh220=String(nhh220);
chh220=(chh220.length()==1?"0"+chh220:chh220);
String cmm220=String(nmm220);
cmm220=(cmm220.length()==1?"0"+cmm220:cmm220);
// Dev 3 S1
String chh311=String(nhh311);
chh311=(chh311.length()==1?"0"+chh311:chh311);
String cmm311=String(nmm311);
cmm311=(cmm311.length()==1?"0"+cmm311:cmm311);
String chh310=String(nhh310);
chh310=(chh310.length()==1?"0"+chh310:chh310);
String cmm310=String(nmm310);
cmm310=(cmm310.length()==1?"0"+cmm310:cmm310);
// Dev 3 S2
String chh321=String(nhh321);
chh321=(chh321.length()==1?"0"+chh321:chh321);
String cmm321=String(nmm321);
cmm321=(cmm321.length()==1?"0"+cmm321:cmm321);
String chh320=String(nhh320);
chh320=(chh320.length()==1?"0"+chh320:chh320);
String cmm320=String(nmm320);
cmm320=(cmm320.length()==1?"0"+cmm320:cmm320);
//dev0 s1
Serial.print("--Dev0 S1--");
Serial.print(" --Dev0 S2--");
Serial.print(" --Dev1 S1--");
Serial.print(" --Dev1 S2--");
Serial.print(" --Dev2 S1--");
Serial.print(" --Dev2 S2--");
Serial.print(" --Dev3 S1--");
Serial.println(" --Dev3 S2--");
Serial.print(chh011);
Serial.print(":");
Serial.print(cmm011);
Serial.print("-");
Serial.print(chh010);
Serial.print(":");
Serial.print(cmm010);
Serial.print(" ");
//dev0 s2
Serial.print(chh021);
Serial.print(":");
Serial.print(cmm021);
Serial.print("-");
Serial.print(chh020);
Serial.print(":");
Serial.print(cmm020);
Serial.print(" ");
//dev1 s1
Serial.print(chh111);
Serial.print(":");
Serial.print(cmm111);
Serial.print("-");
Serial.print(chh110);
Serial.print(":");
Serial.print(cmm110);
Serial.print(" ");
//dev1 s2
Serial.print(chh121);
Serial.print(":");
Serial.print(cmm121);
Serial.print("-");
Serial.print(chh120);
Serial.print(":");
Serial.print(cmm120);
Serial.print(" ");
//dev2 s1
Serial.print(chh211);
Serial.print(":");
Serial.print(cmm211);
Serial.print("-");
Serial.print(chh210);
Serial.print(":");
Serial.print(cmm210);
Serial.print(" ");
//dev2 s2
Serial.print(chh221);
Serial.print(":");
Serial.print(cmm221);
Serial.print("-");
Serial.print(chh220);
Serial.print(":");
Serial.print(cmm220);
Serial.print(" ");
//dev3 s1
Serial.print(chh311);
Serial.print(":");
Serial.print(cmm311);
Serial.print("-");
Serial.print(chh310);
Serial.print(":");
Serial.print(cmm310);
Serial.print(" ");
//dev3 s2
Serial.print(chh321);
Serial.print(":");
Serial.print(cmm321);
Serial.print("-");
Serial.print(chh320);
Serial.print(":");
Serial.println(cmm320);
}
//-----------------------
void loadnvram() {
// load the nvram values to variables
//-----------------------
enas=rtc.readnvram(1);
ena0=enas&&1==1;
ena1=enas&&2==2;
ena2=enas&&4==4;
ena3=enas&&8==8;
uint8_t dev0[8] = {0};
rtc.readnvram(dev0, 8, 2);
nhh011=dev0[0]; nmm011=dev0[1];nhh010=dev0[2];nmm010=dev0[3]; // dev0 s1
nhh021=dev0[4]; nmm021=dev0[5];nhh020=dev0[6];nmm020=dev0[7]; // dev0 s2
uint8_t dev1[8] = {0};
rtc.readnvram(dev1, 8, 10);
nhh111=dev1[0]; nmm111=dev1[1];nhh110=dev1[2]; nmm110=dev1[3]; // dev1 s1
nhh121=dev1[4]; nmm121=dev1[5];nhh120=dev1[6]; nmm120=dev1[7]; // dev1 s2
uint8_t dev2[8] = {0};
rtc.readnvram(dev2, 8, 18);
nhh211=dev2[0]; nmm211=dev2[1];nhh210=dev2[2]; nmm210=dev2[3]; // dev1 s1
nhh221=dev2[4]; nmm221=dev2[5];nhh220=dev2[6]; nmm220=dev2[7]; // dev1 s2
uint8_t dev3[8] = {0};
rtc.readnvram(dev3, 8, 26);
nhh311=dev3[0]; nmm311=dev3[1];nhh310=dev3[2]; nmm310=dev3[3]; // dev1 s1
nhh321=dev3[4]; nmm321=dev3[5];nhh320=dev3[6]; nmm320=dev3[7]; // dev1 s2
/*
Serial.print(nhh011);Serial.print(":");Serial.print(nmm011);
Serial.print(" ");
Serial.print(nhh010);Serial.print(":");Serial.print(nmm010);
Serial.print(",");
Serial.print(nhh021);Serial.print(":");Serial.print(nmm021);
Serial.print(" ");
Serial.print(nhh020);Serial.print(":");Serial.println(nmm020);
Serial.print(nhh111);Serial.print(":");Serial.print(nmm111);
Serial.print(" ");
Serial.print(nhh110);Serial.print(":");Serial.print(nmm110);
Serial.print(",");
Serial.print(nhh121);Serial.print(":");Serial.print(nmm121);
Serial.print(" ");
Serial.print(nhh120);Serial.print(":");Serial.println(nmm120);
Serial.print(nhh211);Serial.print(":");Serial.print(nmm211);
Serial.print(" ");
Serial.print(nhh210);Serial.print(":");Serial.print(nmm210);
Serial.print(",");
Serial.print(nhh221);Serial.print(":");Serial.print(nmm221);
Serial.print(" ");
Serial.print(nhh220);Serial.print(":");Serial.println(nmm220);
Serial.print(nhh311);Serial.print(":");Serial.print(nmm311);
Serial.print(" ");
Serial.print(nhh310);Serial.print(":");Serial.print(nmm310);
Serial.print(",");
Serial.print(nhh321);Serial.print(":");Serial.print(nmm321);
Serial.print(" ");
Serial.print(nhh320);Serial.print(":");Serial.println(nmm320);
Serial.println();
*/
}
//------------------------
void writeTimers(){
//------------------------
Serial.println("Set To Default");
byte enas=ena0+2*ena1+4*ena2+8*ena3;
rtc.writenvram(1, enas);
byte dev0[8] = { nhh011,nmm011,nhh010,nmm010, nhh021,nmm021,nhh020,nmm020};
rtc.writenvram(2, dev0, 8);
byte dev1[8] = { nhh111,nmm111,nhh110,nmm110, nhh121,nmm121,nhh120,nmm120};
rtc.writenvram(10, dev1, 8);
byte dev2[8] = { nhh211,nmm211,nhh210,nmm210, nhh221,nmm221,nhh220,nmm220};
rtc.writenvram(18, dev2, 8);
byte dev3[8] = { nhh311,nmm311,nhh310,nmm310, nhh321,nmm321,nhh320,nmm320};
rtc.writenvram(26, dev3, 8);
}