Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This Garage Door Opener script has a main and a tab named "server"
- //credits to Arduino Forum member "nolasca" https://forum.arduino.cc/t/garage-door-opener-with-esp8266-web-server/1154364/41
- //and Rui Santos ESP8266 Garage door opener 2 relays - programming //https://www.codemacs.com/iaot/applications/esp8266-//garage-door-opener.4002131.htm
- // I edited this script to give a 12 second delay when the buttons are pressed and also to give a proper message statement on the buttons, when the web buttons are pressed, to give the user the Garage Door Status, and progress when the garage door is open and when the garage door is closed. It gives 12 seconds for the garage door to open and 12 seconds for the garage door to close.
- /*********
- Door opener
- https://forum.arduino.cc/t/garage-door-opener-with-esp8266-web-server/1154364/18
- *********/
- //This is bad practice.https://forum.arduino.cc/t/having-issues-adding-a-void-in-this-script/1147852/47
- //Output5State should be bool (if it can be only on or off) or uint8_t if there are more options or better: use an enum.
- //My Add/Edit global across tabs
- extern bool The_Door_Is_Now_Closed = false;
- extern bool The_Door_Is_Now_Open = false;
- extern bool State_Is_Off = false;
- extern bool State_Is_On = false;
- extern bool Make_Button_State_The_Door_Is_Open = false;
- extern bool Reverse_The_Door = false;
- extern bool FirstPass = false;
- static bool Door_Closed = false;
- //My Add/Edit
- //#77878A color Regent Grey
- //#FF0000 color Red
- //#195B6A Color Green
- // Load Wi-Fi library
- #include <ESP8266WiFi.h>
- #include <ESP8266WebServer.h>
- unsigned long LastTimeDoorWasActivated = 0;
- const unsigned long DoorDelay = 12000;
- #if true // true for stspringer
- // Replace with your network credentials
- const char* ssid = "Your SSID";
- const char* password = "Your Password";
- //My Add/ DEFINE STATIC IP xx.0.xx.xx WORKING! https://www.youtube.com/watch?v=B9jJI7p2Gw4
- IPAddress local_IP(xx.0.xx.xx);
- IPAddress gateway(xx.0.xx.xx);
- IPAddress subnet(255, 255, 255, 0);
- IPAddress primaryDNS(208, 67, 222, 222);
- IPAddress secondaryDNS(208, 67, 220, 220);
- #endif
- // Set web server port number to 80
- ESP8266WebServer server(80);
- // Variable to store the HTTP request
- //String header;
- // Auxiliar variables to store the current output state
- String output5State = "off";
- // Assign output variables to GPIO pins
- const int output5 = 5;
- // Current time
- unsigned long currentTime = millis();
- // Previous time
- unsigned long previousTime = 0;
- long interval = 100; // interval at which to blink (milliseconds)
- // Define timeout time in milliseconds (example: 2000ms = 2s)
- const long timeoutTime = 2000;
- // open door//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- void doorOpen() {
- output5State = "on";
- State_Is_On = true;
- State_Is_Off = false;
- The_Door_Is_Now_Open = true;
- The_Door_Is_Now_Closed = false;
- //Door_Closed = false;
- digitalWrite(output5, HIGH);//turn off the motor remeber the last time LastTimeDoorWasActivated
- LastTimeDoorWasActivated = millis();
- Serial.println(F("Door opened"));
- }
- // forced close///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- void doorClose() {
- output5State = "off"; /////////////////////turn on the motor keep it on for 12 seconds have passed then set output5State = "off"; and the button reads "The Garage Door Is Closed"
- State_Is_Off = true;
- State_Is_On = false;
- digitalWrite(output5, LOW);//turn on the motor
- Serial.println(F(" Door closed"));
- The_Door_Is_Now_Closed = true;
- The_Door_Is_Now_Open = false;
- //Door_Closed = true;
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // check if the door needs to be closed
- void doorTimer() {
- if (output5State == "on" && (millis() - LastTimeDoorWasActivated) >= DoorDelay) { /////////////////////after 12 seconds have passed output5State = "off"; and the button reads "The Garage Door Is Closed"
- doorClose();
- if (FirstPass)
- {
- Reverse_The_Door = true;
- FirstPass = false;
- }
- else
- {
- Reverse_The_Door = false;
- FirstPass = true;
- }
- Serial.println(F(" Door closed by time"));
- }
- }
- void setup() {
- Serial.begin(115200);
- // Initialize the output variables as outputs
- pinMode(output5, OUTPUT);
- // Set outputs to LOW //ORIGINAL
- //digitalWrite(output5, LOW);
- // My Add/Edit Set outputs to high put load in "NO" Slot
- digitalWrite(output5, HIGH);
- // Connect to Wi-Fi network with SSID and password
- Serial.print("Connecting to ");
- Serial.println(ssid);
- //My Add/ Define Static IP Setings working!
- if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
- Serial.println("STA Failed to configure");
- }
- ; WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- // Print local IP address and start web server
- Serial.println("");
- Serial.println("WiFi connected.");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- server.on("/", handlePage); // the home page
- server.on("/5/on", handle5on); // currently you are using hyperlinks to new pages - therefore you need additional handlers
- server.on("/5/off", handle5off);
- server.begin();
- }
- void loop() {
- server.handleClient(); // call the webserver
- doorTimer();
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- TAB server
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- /*
- Webserver Parts
- */
- // the main handle for the page
- void handlePage() {
- Serial.println(F(" D8 handle page"));
- String message;
- //My Add/Edit
- //#77878A color Regent Grey
- //#FF0000 color Red
- //#195B6A Color Green
- // todo: handle incoming parameters
- // Display the HTML web page
- message += F("<!DOCTYPE html><html>\n"
- "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n"
- "<link rel=\"icon\" href=\"data:,\">\n"
- "<meta http-equiv=\"refresh\" content=\"3\">\n"
- "<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}"
- ".button { background-color: #FF0000; border: none; color: white; padding: 16px 40px;"//red
- "text-decoration: none; font-size: 20px; margin: 2px; cursor: pointer;}"
- ".button2 {background-color: #195B6A;}</style>\n</head>\n"//green
- "<body><h1>John's Garage Door Opener</h1>\n");
- // Display current state, and ON/OFF buttons for GPIO 5
- message += F("<p>GPIO 5 - State ");
- message += output5State ;
- message += F("</p>");
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //My Add this time delay placement from kolaha Karma: on arduino forum https://forum.arduino.cc/t/garage-door-opener-with-esp8266-web-server/1154364
- if (output5State == "off")
- {
- if ((Make_Button_State_The_Door_Is_Open) && (!Reverse_The_Door) )
- {
- message += F("<p><a href=\"/5/on\" target='i'><button class=\"button\">The Garage Door is Open</button></a></p>\n");
- Door_Closed = false;
- }
- else if (Reverse_The_Door)
- {
- message += F("<p><a href=\"/5/on\" target='i'><button class=\"button\">The Garage Door is Closed</button></a></p>\n");
- Door_Closed = true;
- }
- else
- {
- message += F("<p><a href=\"/5/on\" target='i'><button class=\"button\">The Garage Door is Closed</button></a></p>\n");
- Door_Closed = true;
- }
- }//if else
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- if (State_Is_On)
- {
- Serial.print( " The_Door_Is_Now_Closed = "); + Serial.print( The_Door_Is_Now_Closed);
- Serial.print( " The_Door_Is_Now_Open = "); + Serial.print( The_Door_Is_Now_Open);
- Serial.print( " Door_Closed = "); + Serial.print( Door_Closed);
- if (Door_Closed)// the garage door is "CLOSED" open it
- {
- message += F("<p><a href=\"/5/on\" target='i'><button class=\"button\">The Garage Door is Active Opening</button></a></p>\n");
- Make_Button_State_The_Door_Is_Open = true;
- }
- else// the garage door is "OPEN" closed it
- {
- message += F("<p><a href=\"/5/on\" target='i'><button class=\"button\">The Garage Door is Active Closing</button></a></p>\n");
- Make_Button_State_The_Door_Is_Open = false;
- }//if else
- }
- message += F("<iframe name='i' style='display:none'></iframe>"); // hack to keep the URI on the main page
- message += F("</body>");
- message += F("</html>");
- server.send(200, "text/html", message);
- }
- void handle5on() {
- Serial.println(F(" handle5on"));
- doorOpen();
- handlePage();
- }
- void handle5off() {
- Serial.println(F(" handle5off"));
- doorClose();
- handlePage();
- }
Advertisement
Add Comment
Please, Sign In to add comment