From 13eb3129eaf83378e3cc05ad584bbd8c823203c6 Mon Sep 17 00:00:00 2001 From: IB <128748761+IBrahem-Elyamany@users.noreply.github.com> Date: Fri, 15 Sep 2023 23:46:16 +0300 Subject: [PATCH 1/4] Update README.md edited my username --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 128cde8..51b342a 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ This repository was established during the course of the RoboTech project as par ### I want to extend my gratitude to my amazing teammates for their dedication and hard work. [Ali Amgad](https://github.com/aliamgad) -[IBrahemelyamany](https://github.com/IBrahemelyamany) +[IBrahem Mohamed](https://github.com/IBrahem-Elyamany) [Shady Atef](https://github.com/shady-ateff) From 0813c5052dca45e16d583030c546033f762b0497 Mon Sep 17 00:00:00 2001 From: IB Date: Sat, 16 Sep 2023 15:33:22 +0200 Subject: [PATCH 2/4] 1st --- main_project/main_project.ino | 543 ++++++++++++++++++++++++++++++++++ 1 file changed, 543 insertions(+) create mode 100644 main_project/main_project.ino diff --git a/main_project/main_project.ino b/main_project/main_project.ino new file mode 100644 index 0000000..b828aea --- /dev/null +++ b/main_project/main_project.ino @@ -0,0 +1,543 @@ +#include +#include +#include //for led matrix led +#include //for led matrix led +#include +#include +#include +#include +#include +#define HARDWARE_TYPE MD_MAX72XX::FC16_HW +#define MAX_DEVICES 4 +#define CS_PIN 15 +#define Relay_Led_1 D0 +#define Relay_Led_2 D1 +#define Relay_Led_3 D2 +#define Relay_Led_4 D3 +#define Alarm 10 + +bool All_Relay_state = 1 ,Relay1_state = 1 , Relay2_state = 1, Relay3_state = 1, Relay4_state = 1 , Display_state = 0;//if Display_state=0 show time else show word + +unsigned long timel, curTime[4] , lastT; +WiFiClient espClient; +PubSubClient client(espClient); + +MD_Parola Display = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES); +WiFiUDP ntpUDP; +NTPClient timeClient(ntpUDP, "pool.ntp.org" , + 3*60*60); //3*60*60 cairo zone + +ESP8266WebServer server(80); + +String msgAr = "" ; +char * timerSTR; + +long long int timer , timer1 , timer2 , timer3 , timer4 , mm ; + +const char* mqtt_Server = "broker.mqtt-dashboard.com"; + +struct settings { + char ssid[30]; + char password[30]; +} user_wifi = {}; + +void setup() { + Serial.begin(115200); + + pinSetup(); //setup pinModes and its defult state + + EEPROM.begin(sizeof(struct settings) ); //enable reading from flash memory that integrated in Nodemcu + + EEPROM.get( 0, user_wifi ); //searching for saved WiFi settings in flash and store it in user_wifi struct + + Display.begin(); //turn on display + Display.setIntensity(4); //set light intensity for display + Display.displayClear(); //clear anything from the previous sessions + + WiFi.mode(WIFI_STA); // STA to convert mode to connect to WIFI + WiFi.begin(user_wifi.ssid, user_wifi.password); // start using stored wifi settings + + unsigned long Timeout = millis(); + + Serial.println("Connecting to WiFi."); + Display.displayScroll("Connecting to WiFi......................", PA_LEFT, PA_SCROLL_LEFT, 20); + + while (WiFi.status() != WL_CONNECTED) //try to connect + { + Serial.print("."); + if (Display.displayAnimate()) { + Display.displayReset(); + } + + if (millis() - Timeout > 10000) // wifi may be not found or wrong password + { + Display.displayClear(); + Display.displayScroll("Connection Failed ", PA_LEFT, PA_SCROLL_LEFT, 20); + long long waitForAnimation = millis(); + for (int i = millis(); i - waitForAnimation < 4500 ; i = millis() ){ + Serial.print(i - waitForAnimation); + if (Display.displayAnimate()) + { + Display.displayReset(); + } + } + Serial.println("Connection Failed "); + WiFi.mode(WIFI_AP); // start making access point to enter ssid and password + WiFi.softAP("SmartPlug", ""); + Serial.print(WiFi.softAPIP()); + Display.displayClear(); + Display.displayScroll("Please Connect To SmartPlug WiFi", PA_LEFT, PA_SCROLL_LEFT, 40); + + waitForAnimation = millis(); + for (int i = 0; WiFi.softAPgetStationNum() < 1 ; i++ ){ + + Serial.print(WiFi.softAPgetStationNum()); + + if (Display.displayAnimate()) + { + Display.displayReset(); + } + } + break; + } + } + + Serial.print("http server started"); + server.on("/", handleRoot); + server.begin(); + + if (WiFi.status() == WL_CONNECTED) // فى حالة وجود النت بس هيبدأ يستدعى السيرفرات + { + timeClient.begin(); + client.setServer(mqtt_Server, 1883); + client.setCallback(callback); + timel= millis(); + timer= millis(); + } + + Display.displayClear(); + + Display.displayScroll("Please Write ** 192.168.4.1 ** in Your Browser ", PA_LEFT, PA_SCROLL_LEFT, 30); + +} + +void loop() { + + if(WiFi.status() != WL_CONNECTED) + { + + if (Display.displayAnimate()) + { + Display.displayReset(); + } + + server.handleClient(); + } + else + { + + if (!client.connected()) + { + reconnect(); + } + client.loop(); + + set_the_timer(); + + unsigned long now= millis(); + + if(now-timel>2000) + { + timel= now; + + client.publish("all relay state",String(All_Relay_state).c_str()); + client.publish("relay1 State",String(Relay1_state).c_str()); + client.publish("relay2 State",String(Relay2_state).c_str()); + client.publish("relay3 State",String(Relay3_state).c_str()); + client.publish("relay4 State",String(Relay4_state).c_str()); + } + + + if (Display_state == 0 ) + { + Display.setTextAlignment(PA_CENTER); + Display.print(liveClock()); + Serial.println(liveClock()); + } + else { + if (Display.displayAnimate()) + { + Display.displayAnimate(); + Display.displayReset(); + } + } + } +} + +void pinSetup(){ + pinMode(Relay_Led_1, OUTPUT); + pinMode(Relay_Led_2, OUTPUT); + pinMode(Relay_Led_3, OUTPUT); + pinMode(Relay_Led_4, OUTPUT); + pinMode(Alarm , OUTPUT); + //During Starting all Relays should TURN On + digitalWrite(Relay_Led_1, HIGH); + digitalWrite(Relay_Led_2, HIGH); + digitalWrite(Relay_Led_3, HIGH); + digitalWrite(Relay_Led_4, HIGH); + digitalWrite(Alarm, LOW); +} + +void callback(char* topic, byte* payload, unsigned int length) { + Serial.print("Message arrived ["); + Serial.print(topic); + Serial.print("] "); + + if (strstr(topic, "sub1")) + { + for (int i = 0; i < length; i++) { + Serial.print((char)payload[i]); + } + Serial.println(); + // Switch on the LED if an 1 was received as first character + if ((char)payload[0] == '0') { + digitalWrite(Relay_Led_1, LOW); // Turn the LED on (Note that LOW is the voltage level + Relay1_state=0; + } else { + digitalWrite(Relay_Led_1, HIGH); // Turn the LED off by making the voltage HIGH + Relay1_state=1; + } + } + + else if ( strstr(topic, "sub2")) + { + for (int i = 0; i < length; i++) { + Serial.print((char)payload[i]); + } + Serial.println(); + // Switch on the LED if an 1 was received as first character + if ((char)payload[0] == '0') { + digitalWrite(Relay_Led_2, LOW); // Turn the LED on (Note that LOW is the voltage level + Relay2_state=0; + } else { + digitalWrite(Relay_Led_2, HIGH); // Turn the LED off by making the voltage HIGH + Relay2_state=1; + } + } + else if ( strstr(topic, "sub3")) + { + for (int i = 0; i < length; i++) { + Serial.print((char)payload[i]); + } + Serial.println(); + // Switch on the LED if an 1 was received as first character + if ((char)payload[0] == '0') { + digitalWrite(Relay_Led_3, LOW); // Turn the LED on (Note that LOW is the voltage level + Relay3_state=0; + } else { + digitalWrite(Relay_Led_3, HIGH); // Turn the LED off by making the voltage HIGH + Relay3_state=1; + } + } + else if ( strstr(topic, "sub4")) + { + for (int i = 0; i < length; i++) { + Serial.print((char)payload[i]); + } + Serial.println(); + // Switch on the LED if an 1 was received as first character + if ((char)payload[0] == '0') { + digitalWrite(Relay_Led_4, LOW); // Turn the LED on (Note that LOW is the voltage level + Relay4_state=0; + } else { + digitalWrite(Relay_Led_4, HIGH); // Turn the LED off by making the voltage HIGH + Relay4_state=1; + } + } + else if( strstr(topic, "sub5")){ + msgAr = "" ; + for (int i = 0; i < length; i++) { + msgAr+=(char)payload[i]; + Serial.print((char)payload[i]); + } + if ((char)payload[0] == '0') + { + Display_state = 0 ; + msgAr.clear() ; + } + else{ + Display.displayClear(); + Display.displayScroll(msgAr.c_str(), PA_LEFT, PA_SCROLL_LEFT, 30); + Display_state = 1 ; + //Serial.println(msgAr.c_str()); + Serial.println(msgAr); + } + + } + else if ( strstr(topic, "timer1")) + { + timerSTR = "" ; + for (int i = 0; i < length; i++) { + Serial.print((char)payload[i]); + timerSTR+=(char)payload[i]; + } + mm = atol(timerSTR); + Serial.println(); + if ((char)payload[0] == '0') { + timer1 = 0; + } else { + timer1 = timer + mm; + } + } + else if ( strstr(topic, "timer2")) + { + timerSTR = "" ; + for (int i = 0; i < length; i++) { + Serial.print((char)payload[i]); + timerSTR+=(char)payload[i]; + } + mm = atol(timerSTR); + Serial.println(); + if ((char)payload[0] == '0') { + timer2 = 0; + } else { + timer2 = timer + mm; + } + } + else if ( strstr(topic, "timer3")) + { + timerSTR = "" ; + for (int i = 0; i < length; i++) { + Serial.print((char)payload[i]); + timerSTR+=(char)payload[i]; + } + mm = atol(timerSTR); + Serial.println(); + if ((char)payload[0] == '0') { + timer3 = 0; + } else { + timer3 = timer + mm; + } + } + else if ( strstr(topic, "timer4")) + { + timerSTR = "" ; + for (int i = 0; i < length; i++) { + Serial.print((char)payload[i]); + timerSTR+=(char)payload[i]; + } + mm = atol(timerSTR); + Serial.println(); + if ((char)payload[0] == '0') { + timer4 = 0; + } else { + timer4 = timer + mm; + } + } + else + { + Serial.println("unsubscribed topic"); + } + +} + +void reconnect() { + while (!client.connected()) { + String clientId="ESP8266Client-"; + clientId +=String(random(0xffff),HEX); + if (client.connect(clientId.c_str())) + { + Serial.println("MQTT connected"); + + client.publish("all relay state","hello World"); + client.publish("relay1 State","hello World"); + client.publish("relay2 State","hello World"); + client.publish("relay3 State","hello World"); + client.publish("relay4 State","hello World"); + + client.subscribe("sub1"); + client.subscribe("sub2"); + client.subscribe("sub3"); + client.subscribe("sub4"); + client.subscribe("sub5"); + client.subscribe("timer1"); + client.subscribe("timer2"); + client.subscribe("timer3"); + client.subscribe("timer4"); + } + else { + Serial.print("failed, rc="); + Serial.print(client.state()); + Serial.println(" try again in 5 seconds"); + // Wait 5 seconds before retrying + delay(5000); + } + } +} + +void handleRoot() { + + if (server.method() == HTTP_POST) { + + strncpy(user_wifi.ssid, server.arg("ssid").c_str(), sizeof(user_wifi.ssid) ); + strncpy(user_wifi.password, server.arg("password").c_str(), sizeof(user_wifi.password) ); + user_wifi.ssid[server.arg("ssid").length()] = user_wifi.password[server.arg("password").length()] = '\0'; + EEPROM.put(0, user_wifi); + EEPROM.commit(); + server.send (200, "text/html", + "\n" + "\n" + "\n" + "\n" + "\n" + "Wifi Setup\n" + " \n" + " \n" + "\n" + "
\n" + "

Wifi Setup

\n" + "

Your settings have been saved successfully!
\n" + "Wait For restart the device.

" + ); + + delay(1000); + ESP.restart(); + + } else { + + server.send(200, "text/html", + "\n" + "\n" + "\n" + " \n" + " \n" + " SESB\n" + " \n" + "\n" + "\n" + "
\n" + "
\n" + "

SESB

\n" + " \n" + "
\n" + "
\n" + "

Welcome To
\n" + " Smart Electric Switch Board
Setup

\n" + "
\n" + "
\n" + "
\n" + "
\n" + "

Connect Smart Plug to your WiFi

\n" + "
\n" + "
\n" + " \n" + "
\n" + "
\n" + "
\n" + " \n" + "
\n" + "

\n" + " \n" + "
\n" + "
\n" + "
\n" + "

ABOUT US

\n" + "
\n" + "

This project is the graduation project of Team 5 of the Robotech Summer Training 2023 at
the Faculty of Computing and Information, Ain Shams University\n" + "

Team members : Shady atef , Ahmed Ibrahim , Ali Ashraf , Abdullah mohamed , Abdelrahman Mohamed , Ali Amgad , Omar Amgad , Ibrahim Mohammed

\n" + "
\n" + "
\n" + "\n" + "" ); + } +} + +String liveClock(){ + + timeClient.update(); + String time = "" ; + int hours = timeClient.getHours(); + if (hours > 12){ + hours-=12; + } + int minutes = timeClient.getMinutes(); + if(hours >= 10){ + if(minutes >= 10){ + time = String(hours) + ":" + String(minutes); + } + else + time = String(hours) + ":" + "0" +String(minutes); + } + else + { + if(minutes >= 10){ + time = "0" +String(hours) + ":" + String(minutes); + } + else + time = "0" +String(hours) + ":" + "0" +String(minutes); + } + return time; +} + +void set_the_timer(){ + if( timer >= timer1 && timer1 != 0 ){ + digitalWrite(Relay_Led_1, LOW); + Relay1_state = 0; + } + if( timer >= timer2 && timer2 != 0 ){ + digitalWrite(Relay_Led_2, LOW); + Relay2_state = 0; + } + if( timer >= timer3 && timer3 != 0 ){ + digitalWrite(Relay_Led_3, LOW); + Relay3_state = 0; + } + if( timer >= timer4 && timer4 != 0 ){ + digitalWrite(Relay_Led_4, LOW); + Relay4_state = 0; + } +} +/*String displayMsg (String newMsg) { + String Msg = ""; + Msg=newMsg.c_str(); + return Msg; + +}*/ \ No newline at end of file From 2a9c046deffbc817213f14818fe8437a5b17c3ea Mon Sep 17 00:00:00 2001 From: IB Date: Sat, 16 Sep 2023 17:02:15 +0200 Subject: [PATCH 3/4] add timer is done --- main_project.ino | 543 ----------------------------------------------- 1 file changed, 543 deletions(-) delete mode 100644 main_project.ino diff --git a/main_project.ino b/main_project.ino deleted file mode 100644 index b828aea..0000000 --- a/main_project.ino +++ /dev/null @@ -1,543 +0,0 @@ -#include -#include -#include //for led matrix led -#include //for led matrix led -#include -#include -#include -#include -#include -#define HARDWARE_TYPE MD_MAX72XX::FC16_HW -#define MAX_DEVICES 4 -#define CS_PIN 15 -#define Relay_Led_1 D0 -#define Relay_Led_2 D1 -#define Relay_Led_3 D2 -#define Relay_Led_4 D3 -#define Alarm 10 - -bool All_Relay_state = 1 ,Relay1_state = 1 , Relay2_state = 1, Relay3_state = 1, Relay4_state = 1 , Display_state = 0;//if Display_state=0 show time else show word - -unsigned long timel, curTime[4] , lastT; -WiFiClient espClient; -PubSubClient client(espClient); - -MD_Parola Display = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES); -WiFiUDP ntpUDP; -NTPClient timeClient(ntpUDP, "pool.ntp.org" , + 3*60*60); //3*60*60 cairo zone - -ESP8266WebServer server(80); - -String msgAr = "" ; -char * timerSTR; - -long long int timer , timer1 , timer2 , timer3 , timer4 , mm ; - -const char* mqtt_Server = "broker.mqtt-dashboard.com"; - -struct settings { - char ssid[30]; - char password[30]; -} user_wifi = {}; - -void setup() { - Serial.begin(115200); - - pinSetup(); //setup pinModes and its defult state - - EEPROM.begin(sizeof(struct settings) ); //enable reading from flash memory that integrated in Nodemcu - - EEPROM.get( 0, user_wifi ); //searching for saved WiFi settings in flash and store it in user_wifi struct - - Display.begin(); //turn on display - Display.setIntensity(4); //set light intensity for display - Display.displayClear(); //clear anything from the previous sessions - - WiFi.mode(WIFI_STA); // STA to convert mode to connect to WIFI - WiFi.begin(user_wifi.ssid, user_wifi.password); // start using stored wifi settings - - unsigned long Timeout = millis(); - - Serial.println("Connecting to WiFi."); - Display.displayScroll("Connecting to WiFi......................", PA_LEFT, PA_SCROLL_LEFT, 20); - - while (WiFi.status() != WL_CONNECTED) //try to connect - { - Serial.print("."); - if (Display.displayAnimate()) { - Display.displayReset(); - } - - if (millis() - Timeout > 10000) // wifi may be not found or wrong password - { - Display.displayClear(); - Display.displayScroll("Connection Failed ", PA_LEFT, PA_SCROLL_LEFT, 20); - long long waitForAnimation = millis(); - for (int i = millis(); i - waitForAnimation < 4500 ; i = millis() ){ - Serial.print(i - waitForAnimation); - if (Display.displayAnimate()) - { - Display.displayReset(); - } - } - Serial.println("Connection Failed "); - WiFi.mode(WIFI_AP); // start making access point to enter ssid and password - WiFi.softAP("SmartPlug", ""); - Serial.print(WiFi.softAPIP()); - Display.displayClear(); - Display.displayScroll("Please Connect To SmartPlug WiFi", PA_LEFT, PA_SCROLL_LEFT, 40); - - waitForAnimation = millis(); - for (int i = 0; WiFi.softAPgetStationNum() < 1 ; i++ ){ - - Serial.print(WiFi.softAPgetStationNum()); - - if (Display.displayAnimate()) - { - Display.displayReset(); - } - } - break; - } - } - - Serial.print("http server started"); - server.on("/", handleRoot); - server.begin(); - - if (WiFi.status() == WL_CONNECTED) // فى حالة وجود النت بس هيبدأ يستدعى السيرفرات - { - timeClient.begin(); - client.setServer(mqtt_Server, 1883); - client.setCallback(callback); - timel= millis(); - timer= millis(); - } - - Display.displayClear(); - - Display.displayScroll("Please Write ** 192.168.4.1 ** in Your Browser ", PA_LEFT, PA_SCROLL_LEFT, 30); - -} - -void loop() { - - if(WiFi.status() != WL_CONNECTED) - { - - if (Display.displayAnimate()) - { - Display.displayReset(); - } - - server.handleClient(); - } - else - { - - if (!client.connected()) - { - reconnect(); - } - client.loop(); - - set_the_timer(); - - unsigned long now= millis(); - - if(now-timel>2000) - { - timel= now; - - client.publish("all relay state",String(All_Relay_state).c_str()); - client.publish("relay1 State",String(Relay1_state).c_str()); - client.publish("relay2 State",String(Relay2_state).c_str()); - client.publish("relay3 State",String(Relay3_state).c_str()); - client.publish("relay4 State",String(Relay4_state).c_str()); - } - - - if (Display_state == 0 ) - { - Display.setTextAlignment(PA_CENTER); - Display.print(liveClock()); - Serial.println(liveClock()); - } - else { - if (Display.displayAnimate()) - { - Display.displayAnimate(); - Display.displayReset(); - } - } - } -} - -void pinSetup(){ - pinMode(Relay_Led_1, OUTPUT); - pinMode(Relay_Led_2, OUTPUT); - pinMode(Relay_Led_3, OUTPUT); - pinMode(Relay_Led_4, OUTPUT); - pinMode(Alarm , OUTPUT); - //During Starting all Relays should TURN On - digitalWrite(Relay_Led_1, HIGH); - digitalWrite(Relay_Led_2, HIGH); - digitalWrite(Relay_Led_3, HIGH); - digitalWrite(Relay_Led_4, HIGH); - digitalWrite(Alarm, LOW); -} - -void callback(char* topic, byte* payload, unsigned int length) { - Serial.print("Message arrived ["); - Serial.print(topic); - Serial.print("] "); - - if (strstr(topic, "sub1")) - { - for (int i = 0; i < length; i++) { - Serial.print((char)payload[i]); - } - Serial.println(); - // Switch on the LED if an 1 was received as first character - if ((char)payload[0] == '0') { - digitalWrite(Relay_Led_1, LOW); // Turn the LED on (Note that LOW is the voltage level - Relay1_state=0; - } else { - digitalWrite(Relay_Led_1, HIGH); // Turn the LED off by making the voltage HIGH - Relay1_state=1; - } - } - - else if ( strstr(topic, "sub2")) - { - for (int i = 0; i < length; i++) { - Serial.print((char)payload[i]); - } - Serial.println(); - // Switch on the LED if an 1 was received as first character - if ((char)payload[0] == '0') { - digitalWrite(Relay_Led_2, LOW); // Turn the LED on (Note that LOW is the voltage level - Relay2_state=0; - } else { - digitalWrite(Relay_Led_2, HIGH); // Turn the LED off by making the voltage HIGH - Relay2_state=1; - } - } - else if ( strstr(topic, "sub3")) - { - for (int i = 0; i < length; i++) { - Serial.print((char)payload[i]); - } - Serial.println(); - // Switch on the LED if an 1 was received as first character - if ((char)payload[0] == '0') { - digitalWrite(Relay_Led_3, LOW); // Turn the LED on (Note that LOW is the voltage level - Relay3_state=0; - } else { - digitalWrite(Relay_Led_3, HIGH); // Turn the LED off by making the voltage HIGH - Relay3_state=1; - } - } - else if ( strstr(topic, "sub4")) - { - for (int i = 0; i < length; i++) { - Serial.print((char)payload[i]); - } - Serial.println(); - // Switch on the LED if an 1 was received as first character - if ((char)payload[0] == '0') { - digitalWrite(Relay_Led_4, LOW); // Turn the LED on (Note that LOW is the voltage level - Relay4_state=0; - } else { - digitalWrite(Relay_Led_4, HIGH); // Turn the LED off by making the voltage HIGH - Relay4_state=1; - } - } - else if( strstr(topic, "sub5")){ - msgAr = "" ; - for (int i = 0; i < length; i++) { - msgAr+=(char)payload[i]; - Serial.print((char)payload[i]); - } - if ((char)payload[0] == '0') - { - Display_state = 0 ; - msgAr.clear() ; - } - else{ - Display.displayClear(); - Display.displayScroll(msgAr.c_str(), PA_LEFT, PA_SCROLL_LEFT, 30); - Display_state = 1 ; - //Serial.println(msgAr.c_str()); - Serial.println(msgAr); - } - - } - else if ( strstr(topic, "timer1")) - { - timerSTR = "" ; - for (int i = 0; i < length; i++) { - Serial.print((char)payload[i]); - timerSTR+=(char)payload[i]; - } - mm = atol(timerSTR); - Serial.println(); - if ((char)payload[0] == '0') { - timer1 = 0; - } else { - timer1 = timer + mm; - } - } - else if ( strstr(topic, "timer2")) - { - timerSTR = "" ; - for (int i = 0; i < length; i++) { - Serial.print((char)payload[i]); - timerSTR+=(char)payload[i]; - } - mm = atol(timerSTR); - Serial.println(); - if ((char)payload[0] == '0') { - timer2 = 0; - } else { - timer2 = timer + mm; - } - } - else if ( strstr(topic, "timer3")) - { - timerSTR = "" ; - for (int i = 0; i < length; i++) { - Serial.print((char)payload[i]); - timerSTR+=(char)payload[i]; - } - mm = atol(timerSTR); - Serial.println(); - if ((char)payload[0] == '0') { - timer3 = 0; - } else { - timer3 = timer + mm; - } - } - else if ( strstr(topic, "timer4")) - { - timerSTR = "" ; - for (int i = 0; i < length; i++) { - Serial.print((char)payload[i]); - timerSTR+=(char)payload[i]; - } - mm = atol(timerSTR); - Serial.println(); - if ((char)payload[0] == '0') { - timer4 = 0; - } else { - timer4 = timer + mm; - } - } - else - { - Serial.println("unsubscribed topic"); - } - -} - -void reconnect() { - while (!client.connected()) { - String clientId="ESP8266Client-"; - clientId +=String(random(0xffff),HEX); - if (client.connect(clientId.c_str())) - { - Serial.println("MQTT connected"); - - client.publish("all relay state","hello World"); - client.publish("relay1 State","hello World"); - client.publish("relay2 State","hello World"); - client.publish("relay3 State","hello World"); - client.publish("relay4 State","hello World"); - - client.subscribe("sub1"); - client.subscribe("sub2"); - client.subscribe("sub3"); - client.subscribe("sub4"); - client.subscribe("sub5"); - client.subscribe("timer1"); - client.subscribe("timer2"); - client.subscribe("timer3"); - client.subscribe("timer4"); - } - else { - Serial.print("failed, rc="); - Serial.print(client.state()); - Serial.println(" try again in 5 seconds"); - // Wait 5 seconds before retrying - delay(5000); - } - } -} - -void handleRoot() { - - if (server.method() == HTTP_POST) { - - strncpy(user_wifi.ssid, server.arg("ssid").c_str(), sizeof(user_wifi.ssid) ); - strncpy(user_wifi.password, server.arg("password").c_str(), sizeof(user_wifi.password) ); - user_wifi.ssid[server.arg("ssid").length()] = user_wifi.password[server.arg("password").length()] = '\0'; - EEPROM.put(0, user_wifi); - EEPROM.commit(); - server.send (200, "text/html", - "\n" - "\n" - "\n" - "\n" - "\n" - "Wifi Setup\n" - " \n" - " \n" - "\n" - "
\n" - "

Wifi Setup

\n" - "

Your settings have been saved successfully!
\n" - "Wait For restart the device.

" - ); - - delay(1000); - ESP.restart(); - - } else { - - server.send(200, "text/html", - "\n" - "\n" - "\n" - " \n" - " \n" - " SESB\n" - " \n" - "\n" - "\n" - "
\n" - "
\n" - "

SESB

\n" - " \n" - "
\n" - "
\n" - "

Welcome To
\n" - " Smart Electric Switch Board
Setup

\n" - "
\n" - "
\n" - "
\n" - "
\n" - "

Connect Smart Plug to your WiFi

\n" - "
\n" - "
\n" - " \n" - "
\n" - "
\n" - "
\n" - " \n" - "
\n" - "

\n" - " \n" - "
\n" - "
\n" - "
\n" - "

ABOUT US

\n" - "
\n" - "

This project is the graduation project of Team 5 of the Robotech Summer Training 2023 at
the Faculty of Computing and Information, Ain Shams University\n" - "

Team members : Shady atef , Ahmed Ibrahim , Ali Ashraf , Abdullah mohamed , Abdelrahman Mohamed , Ali Amgad , Omar Amgad , Ibrahim Mohammed

\n" - "
\n" - "
\n" - "\n" - "" ); - } -} - -String liveClock(){ - - timeClient.update(); - String time = "" ; - int hours = timeClient.getHours(); - if (hours > 12){ - hours-=12; - } - int minutes = timeClient.getMinutes(); - if(hours >= 10){ - if(minutes >= 10){ - time = String(hours) + ":" + String(minutes); - } - else - time = String(hours) + ":" + "0" +String(minutes); - } - else - { - if(minutes >= 10){ - time = "0" +String(hours) + ":" + String(minutes); - } - else - time = "0" +String(hours) + ":" + "0" +String(minutes); - } - return time; -} - -void set_the_timer(){ - if( timer >= timer1 && timer1 != 0 ){ - digitalWrite(Relay_Led_1, LOW); - Relay1_state = 0; - } - if( timer >= timer2 && timer2 != 0 ){ - digitalWrite(Relay_Led_2, LOW); - Relay2_state = 0; - } - if( timer >= timer3 && timer3 != 0 ){ - digitalWrite(Relay_Led_3, LOW); - Relay3_state = 0; - } - if( timer >= timer4 && timer4 != 0 ){ - digitalWrite(Relay_Led_4, LOW); - Relay4_state = 0; - } -} -/*String displayMsg (String newMsg) { - String Msg = ""; - Msg=newMsg.c_str(); - return Msg; - -}*/ \ No newline at end of file From 84dfa51cc2ad60ba8f0b13383e7ae659525043e9 Mon Sep 17 00:00:00 2001 From: IB Date: Sat, 16 Sep 2023 17:02:35 +0200 Subject: [PATCH 4/4] add timer is done --- main_project/main_project.ino | 49 +++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/main_project/main_project.ino b/main_project/main_project.ino index b828aea..3319351 100644 --- a/main_project/main_project.ino +++ b/main_project/main_project.ino @@ -29,9 +29,10 @@ NTPClient timeClient(ntpUDP, "pool.ntp.org" , + 3*60*60); //3*60*60 cairo zone ESP8266WebServer server(80); String msgAr = "" ; -char * timerSTR; +String timerSTR; -long long int timer , timer1 , timer2 , timer3 , timer4 , mm ; +unsigned long timer1 , timer2 , timer3 , timer4 , mm ; +bool timer1_state = 0, timer2_state = 0, timer3_state = 0, timer4_state = 0 ; const char* mqtt_Server = "broker.mqtt-dashboard.com"; @@ -111,7 +112,7 @@ void setup() { client.setServer(mqtt_Server, 1883); client.setCallback(callback); timel= millis(); - timer= millis(); + //timer= millis(); } Display.displayClear(); @@ -280,12 +281,15 @@ void callback(char* topic, byte* payload, unsigned int length) { Serial.print((char)payload[i]); timerSTR+=(char)payload[i]; } - mm = atol(timerSTR); + mm = timerSTR.toInt(); Serial.println(); + Serial.println(mm); if ((char)payload[0] == '0') { - timer1 = 0; + timer1_state = 0 ; } else { - timer1 = timer + mm; + timer1 = millis() + mm; + Serial.println(timer1); + timer1_state = 1 ; } } else if ( strstr(topic, "timer2")) @@ -295,12 +299,13 @@ void callback(char* topic, byte* payload, unsigned int length) { Serial.print((char)payload[i]); timerSTR+=(char)payload[i]; } - mm = atol(timerSTR); + mm = timerSTR.toInt(); Serial.println(); if ((char)payload[0] == '0') { - timer2 = 0; + timer2_state = 0 ; } else { - timer2 = timer + mm; + timer2 = millis() + mm; + timer2_state = 1 ; } } else if ( strstr(topic, "timer3")) @@ -310,12 +315,13 @@ void callback(char* topic, byte* payload, unsigned int length) { Serial.print((char)payload[i]); timerSTR+=(char)payload[i]; } - mm = atol(timerSTR); + mm = timerSTR.toInt(); Serial.println(); if ((char)payload[0] == '0') { - timer3 = 0; + timer3_state = 0 ; } else { - timer3 = timer + mm; + timer3 = millis() + mm; + timer3_state = 1 ; } } else if ( strstr(topic, "timer4")) @@ -325,12 +331,13 @@ void callback(char* topic, byte* payload, unsigned int length) { Serial.print((char)payload[i]); timerSTR+=(char)payload[i]; } - mm = atol(timerSTR); + mm = timerSTR.toInt(); Serial.println(); if ((char)payload[0] == '0') { - timer4 = 0; + timer4_state = 0 ; } else { - timer4 = timer + mm; + timer4 = millis() + mm; + timer4_state = 1 ; } } else @@ -518,20 +525,24 @@ String liveClock(){ } void set_the_timer(){ - if( timer >= timer1 && timer1 != 0 ){ + if( millis() >= timer1 && timer1_state == 1 ){ digitalWrite(Relay_Led_1, LOW); + timer1_state = 0; Relay1_state = 0; } - if( timer >= timer2 && timer2 != 0 ){ + if( millis() >= timer2 && timer2_state == 1 ){ digitalWrite(Relay_Led_2, LOW); + timer2_state = 0; Relay2_state = 0; } - if( timer >= timer3 && timer3 != 0 ){ + if( millis() >= timer3 && timer3_state == 1 ){ digitalWrite(Relay_Led_3, LOW); + timer3_state = 0; Relay3_state = 0; } - if( timer >= timer4 && timer4 != 0 ){ + if( millis() >= timer4 && timer4_state == 1 ){ digitalWrite(Relay_Led_4, LOW); + timer4_state = 0; Relay4_state = 0; } }