stspringer

Garage Door Opener With ESP8266 Wireless Web Server

Aug 10th, 2023 (edited)
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.37 KB | None | 0 0
  1. // This Garage Door Opener script has a main and a tab named "server"
  2. //credits to Arduino Forum member "nolasca" https://forum.arduino.cc/t/garage-door-opener-with-esp8266-web-server/1154364/41
  3. //and Rui Santos ESP8266 Garage door opener 2 relays - programming //https://www.codemacs.com/iaot/applications/esp8266-//garage-door-opener.4002131.htm
  4. // 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.
  5.  
  6. /*********
  7. Door opener
  8. https://forum.arduino.cc/t/garage-door-opener-with-esp8266-web-server/1154364/18
  9. *********/
  10. //This is bad practice.https://forum.arduino.cc/t/having-issues-adding-a-void-in-this-script/1147852/47
  11. //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.
  12.  
  13.  
  14. //My Add/Edit global across tabs
  15. extern bool The_Door_Is_Now_Closed = false;
  16. extern bool The_Door_Is_Now_Open = false;
  17. extern bool State_Is_Off = false;
  18. extern bool State_Is_On = false;
  19. extern bool Make_Button_State_The_Door_Is_Open = false;
  20. extern bool Reverse_The_Door = false;
  21. extern bool FirstPass = false;
  22. static bool Door_Closed = false;
  23.  
  24.  
  25. //My Add/Edit
  26. //#77878A color Regent Grey
  27. //#FF0000 color Red
  28. //#195B6A Color Green
  29.  
  30.  
  31. // Load Wi-Fi library
  32. #include <ESP8266WiFi.h>
  33. #include <ESP8266WebServer.h>
  34.  
  35. unsigned long LastTimeDoorWasActivated = 0;
  36. const unsigned long DoorDelay = 12000;
  37.  
  38. #if true // true for stspringer
  39. // Replace with your network credentials
  40. const char* ssid = "Your SSID";
  41. const char* password = "Your Password";
  42. //My Add/ DEFINE STATIC IP xx.0.xx.xx WORKING! https://www.youtube.com/watch?v=B9jJI7p2Gw4
  43. IPAddress local_IP(xx.0.xx.xx);
  44. IPAddress gateway(xx.0.xx.xx);
  45. IPAddress subnet(255, 255, 255, 0);
  46. IPAddress primaryDNS(208, 67, 222, 222);
  47. IPAddress secondaryDNS(208, 67, 220, 220);
  48. #endif
  49.  
  50. // Set web server port number to 80
  51. ESP8266WebServer server(80);
  52.  
  53. // Variable to store the HTTP request
  54. //String header;
  55.  
  56. // Auxiliar variables to store the current output state
  57. String output5State = "off";
  58.  
  59.  
  60.  
  61. // Assign output variables to GPIO pins
  62. const int output5 = 5;
  63.  
  64.  
  65.  
  66. // Current time
  67. unsigned long currentTime = millis();
  68. // Previous time
  69. unsigned long previousTime = 0;
  70. long interval = 100; // interval at which to blink (milliseconds)
  71.  
  72.  
  73. // Define timeout time in milliseconds (example: 2000ms = 2s)
  74. const long timeoutTime = 2000;
  75.  
  76. // open door//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  77. void doorOpen() {
  78. output5State = "on";
  79. State_Is_On = true;
  80. State_Is_Off = false;
  81. The_Door_Is_Now_Open = true;
  82. The_Door_Is_Now_Closed = false;
  83. //Door_Closed = false;
  84. digitalWrite(output5, HIGH);//turn off the motor remeber the last time LastTimeDoorWasActivated
  85. LastTimeDoorWasActivated = millis();
  86. Serial.println(F("Door opened"));
  87. }
  88.  
  89. // forced close///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  90. void doorClose() {
  91. 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"
  92. State_Is_Off = true;
  93. State_Is_On = false;
  94. digitalWrite(output5, LOW);//turn on the motor
  95. Serial.println(F(" Door closed"));
  96. The_Door_Is_Now_Closed = true;
  97. The_Door_Is_Now_Open = false;
  98. //Door_Closed = true;
  99. }
  100. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  101.  
  102.  
  103. // check if the door needs to be closed
  104. void doorTimer() {
  105. if (output5State == "on" && (millis() - LastTimeDoorWasActivated) >= DoorDelay) { /////////////////////after 12 seconds have passed output5State = "off"; and the button reads "The Garage Door Is Closed"
  106. doorClose();
  107. if (FirstPass)
  108. {
  109. Reverse_The_Door = true;
  110. FirstPass = false;
  111. }
  112. else
  113. {
  114. Reverse_The_Door = false;
  115. FirstPass = true;
  116. }
  117.  
  118. Serial.println(F(" Door closed by time"));
  119. }
  120. }
  121.  
  122. void setup() {
  123. Serial.begin(115200);
  124. // Initialize the output variables as outputs
  125.  
  126. pinMode(output5, OUTPUT);
  127.  
  128.  
  129. // Set outputs to LOW //ORIGINAL
  130. //digitalWrite(output5, LOW);
  131.  
  132.  
  133. // My Add/Edit Set outputs to high put load in "NO" Slot
  134. digitalWrite(output5, HIGH);
  135.  
  136.  
  137.  
  138. // Connect to Wi-Fi network with SSID and password
  139. Serial.print("Connecting to ");
  140. Serial.println(ssid);
  141.  
  142. //My Add/ Define Static IP Setings working!
  143. if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
  144. Serial.println("STA Failed to configure");
  145. }
  146.  
  147. ; WiFi.begin(ssid, password);
  148. while (WiFi.status() != WL_CONNECTED) {
  149. delay(500);
  150. Serial.print(".");
  151. }
  152. // Print local IP address and start web server
  153. Serial.println("");
  154. Serial.println("WiFi connected.");
  155. Serial.println("IP address: ");
  156. Serial.println(WiFi.localIP());
  157.  
  158. server.on("/", handlePage); // the home page
  159. server.on("/5/on", handle5on); // currently you are using hyperlinks to new pages - therefore you need additional handlers
  160. server.on("/5/off", handle5off);
  161. server.begin();
  162. }
  163.  
  164. void loop() {
  165. server.handleClient(); // call the webserver
  166. doorTimer();
  167. }
  168.  
  169. ///////////////////////////////////////////////////////////////////////////////////////////////////
  170. TAB server
  171. ///////////////////////////////////////////////////////////////////////////////////////////////////
  172. /*
  173. Webserver Parts
  174.  
  175. */
  176.  
  177. // the main handle for the page
  178. void handlePage() {
  179. Serial.println(F(" D8 handle page"));
  180. String message;
  181.  
  182. //My Add/Edit
  183. //#77878A color Regent Grey
  184. //#FF0000 color Red
  185. //#195B6A Color Green
  186. // todo: handle incoming parameters
  187.  
  188. // Display the HTML web page
  189. message += F("<!DOCTYPE html><html>\n"
  190. "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n"
  191. "<link rel=\"icon\" href=\"data:,\">\n"
  192. "<meta http-equiv=\"refresh\" content=\"3\">\n"
  193. "<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}"
  194. ".button { background-color: #FF0000; border: none; color: white; padding: 16px 40px;"//red
  195. "text-decoration: none; font-size: 20px; margin: 2px; cursor: pointer;}"
  196. ".button2 {background-color: #195B6A;}</style>\n</head>\n"//green
  197. "<body><h1>John's Garage Door Opener</h1>\n");
  198.  
  199. // Display current state, and ON/OFF buttons for GPIO 5
  200. message += F("<p>GPIO 5 - State ");
  201. message += output5State ;
  202. message += F("</p>");
  203.  
  204.  
  205.  
  206. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  207. //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
  208. if (output5State == "off")
  209.  
  210. {
  211.  
  212. if ((Make_Button_State_The_Door_Is_Open) && (!Reverse_The_Door) )
  213. {
  214. message += F("<p><a href=\"/5/on\" target='i'><button class=\"button\">The Garage Door is Open</button></a></p>\n");
  215. Door_Closed = false;
  216. }
  217.  
  218. else if (Reverse_The_Door)
  219. {
  220. message += F("<p><a href=\"/5/on\" target='i'><button class=\"button\">The Garage Door is Closed</button></a></p>\n");
  221. Door_Closed = true;
  222. }
  223. else
  224. {
  225. message += F("<p><a href=\"/5/on\" target='i'><button class=\"button\">The Garage Door is Closed</button></a></p>\n");
  226. Door_Closed = true;
  227. }
  228.  
  229. }//if else
  230. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  231. if (State_Is_On)
  232. {
  233. Serial.print( " The_Door_Is_Now_Closed = "); + Serial.print( The_Door_Is_Now_Closed);
  234. Serial.print( " The_Door_Is_Now_Open = "); + Serial.print( The_Door_Is_Now_Open);
  235. Serial.print( " Door_Closed = "); + Serial.print( Door_Closed);
  236.  
  237. if (Door_Closed)// the garage door is "CLOSED" open it
  238. {
  239. message += F("<p><a href=\"/5/on\" target='i'><button class=\"button\">The Garage Door is Active Opening</button></a></p>\n");
  240. Make_Button_State_The_Door_Is_Open = true;
  241. }
  242. else// the garage door is "OPEN" closed it
  243. {
  244. message += F("<p><a href=\"/5/on\" target='i'><button class=\"button\">The Garage Door is Active Closing</button></a></p>\n");
  245. Make_Button_State_The_Door_Is_Open = false;
  246. }//if else
  247. }
  248.  
  249. message += F("<iframe name='i' style='display:none'></iframe>"); // hack to keep the URI on the main page
  250.  
  251. message += F("</body>");
  252. message += F("</html>");
  253.  
  254. server.send(200, "text/html", message);
  255. }
  256.  
  257. void handle5on() {
  258. Serial.println(F(" handle5on"));
  259. doorOpen();
  260. handlePage();
  261. }
  262.  
  263. void handle5off() {
  264. Serial.println(F(" handle5off"));
  265. doorClose();
  266. handlePage();
  267. }
  268.  
  269.  
  270.  
Advertisement
Add Comment
Please, Sign In to add comment