bouton_ouverture_V2/src/Program.cpp
2023-10-21 22:47:13 +02:00

105 lines
3.0 KiB
C++

#include "Program.h"
Program::Program() {
// Startup Serial
Serial.begin(MONITOR_SPEED);
// Startup WiFi
WiFi.begin(WSSID, PASS);
Serial.print("Connecting to ");
Serial.print(WSSID); Serial.println(" ...");
int i = 0;
while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
delay(1000);
Serial.print(++i);
Serial.print(' ');
}
Serial.println('\n');
Serial.println("Connection established!");
Serial.print("IP address:\t");
Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer
// Startup Discord API
this->discord = new DiscordAPI(DISCORD_HOOK);
// startup NTP
this->ntpUDP = new WiFiUDP();
this->timeClient = new NTPClient(*this->ntpUDP, "pool.ntp.org", 3600*2);//*2 = gnt+2
this->timeClient->update();
//this->sendTime("18h12");
}
void Program::sendTime(String timeEnd){
String start = (String)this->timeClient->getHours() + "h";
int startQuater = this->timeClient->getMinutes()/15;
if (startQuater != 0){
start += (String)(startQuater * 15);
}
this->discord->sendHeure(start, timeEnd);
}
void Program::loop() {
ulong currentMillis = millis();
if (currentMillis + 1000 > this->lastMillis) {
this->lastMillis = currentMillis;
this->encoder->resetMenu();
this->encoder->resetValue();
this->ledLib->actLed(0);
}
if(this->encoder->update()){
switch (this->encoder->getMenu()) {
case 1:
this->encoder->resetValue();
this->timeClient->update();
int day = this->timeClient->getDay();
int hour = this->timeClient->getHours();
int halfHourNb = hour > 12 ? hour - 12 : hour;
halfHourNb *= 2;
int minute = this->timeClient->getMinutes();
if(minute > 30){
halfHourNb++;
}
if(day == 2 || day == 5){
//TODO: mettre l'heure d'envoie par defaut a 22h
}
int openTime = this->encoder->getValue();
this->ledLib->actLed(openTime,halfHourNb);
break;
case 2:
this->encoder->resetValue();
//this->sendTime();
break;
}
}
if(this->encoder->update()){
Serial.print(this->encoder->getValue());
this->ledLib->actLed(this->encoder->getValue());
Serial.print(" ");
Serial.println(this->encoder->getMenu());
}
delay(1000);
this->timeClient->update();
int currentHour = this->timeClient->getHours();
Serial.print("Hour: ");
Serial.println(currentHour);
int currentMinute = this->timeClient->getMinutes();
Serial.print("Minutes: ");
Serial.println(currentMinute);
int currentSecond = this->timeClient->getSeconds();
Serial.print("Seconds: ");
Serial.println(currentSecond);
int currentDayOfWeek = this->timeClient->getDay();
Serial.print("Day of week: ");
Serial.println(currentDayOfWeek);
}