stspringer

Garage Lights Control ESP8266

Aug 16th, 2024 (edited)
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.78 KB | Source Code | 0 0
  1. //===========================================================================================
  2. //BEGIN # MAIN TAB
  3. //===========================================================================================
  4. #include <ESP8266WiFi.h>
  5. #include <ESP8266WebServer.h>
  6. #include "webpage.h" // Include the HTML code from the second tab
  7.  
  8. //My Add/
  9. #if true // true
  10. // Replace with your network credentials
  11. const char* ssid = "Your SSID"; //In quotes as shown
  12. const char* password = "your password";//In quotes as shown
  13.  
  14. //My Add/
  15. // DEFINE STATIC IP 192.168.1.X WORKING! https://www.youtube.com/watch?v=B9jJI7p2Gw4
  16.  
  17. //STATIC IP ADDRESS
  18.  
  19.  
  20. IPAddress local_IP(192.168.1.X); // X for Garage Lights ESP8266 Module
  21. IPAddress gateway(192.168.1.X);
  22. IPAddress subnet(255, 255, 255, 0);
  23. IPAddress primaryDNS(208, 67, 222, 222); //you could use google DNS 8.8.8.8
  24. IPAddress secondaryDNS(208, 67, 220, 220); //you could use google DNS 8.8.4.4
  25. #endif
  26.  
  27.  
  28. // Web server on port 80
  29. ESP8266WebServer server(80);
  30.  
  31. // Define states
  32. enum State {
  33. IDLE,
  34. CHECK_SENSOR,
  35. LIGHT_ON,
  36. LIGHT_OFF
  37. };
  38.  
  39. State currentState = IDLE;
  40.  
  41. // Define pins
  42. const int sensorPin = A0; // Analog pin connected to the current sensor
  43. const int relayPin = 5; // Digital pin connected to the relay
  44.  
  45. // Current sensor threshold
  46. int threshold = 512;
  47.  
  48. void setup() {
  49. Serial.begin(115200);
  50. pinMode(sensorPin, INPUT);
  51. pinMode(relayPin, OUTPUT);
  52.  
  53. // Connect to WiFi
  54. WiFi.begin(ssid, password);
  55. while (WiFi.status() != WL_CONNECTED) {
  56. delay(1000);
  57. Serial.println("Connecting to WiFi...");
  58. }
  59. Serial.println("Connected to WiFi");
  60.  
  61. // Define server routes
  62. server.on("/", HTTP_GET, handleRoot);
  63. server.on("/toggle", HTTP_POST, handleToggle);
  64. server.begin();
  65. }
  66.  
  67. void loop() {
  68. server.handleClient();
  69. checkSensor();
  70. }
  71.  
  72. void checkSensor() {
  73. int sensorValue = analogRead(sensorPin);
  74. Serial.println("Sensor Value: " + String(sensorValue));
  75.  
  76. switch (currentState) {
  77. case IDLE:
  78. currentState = CHECK_SENSOR;
  79. break;
  80.  
  81. case CHECK_SENSOR:
  82. if (sensorValue > threshold) {
  83. currentState = LIGHT_ON;
  84. } else {
  85. currentState = LIGHT_OFF;
  86. }
  87. break;
  88.  
  89. case LIGHT_ON:
  90. digitalWrite(relayPin, HIGH);
  91. Serial.println("Light is ON");
  92. currentState = IDLE;
  93. break;
  94.  
  95. case LIGHT_OFF:
  96. digitalWrite(relayPin, LOW);
  97. Serial.println("Light is OFF");
  98. currentState = IDLE;
  99. break;
  100. }
  101. }
  102.  
  103. void handleRoot() {
  104. server.send(200, "text/html", webpage); // Send HTML page
  105. }
  106.  
  107. void handleToggle() {
  108. String state = server.arg("state");
  109. if (state == "on") {
  110. currentState = LIGHT_ON;
  111. } else if (state == "off") {
  112. currentState = LIGHT_OFF;
  113. }
  114. server.send(200, "text/plain", "OK");
  115. }
  116.  
  117. //===========================================================================================
  118. //END # MAIN TAB
  119. //===========================================================================================
  120.  
  121.  
  122.  
  123.  
  124.  
  125. WEBPAGE TAB
  126.  
  127. //===========================================================================================
  128. //BEGIN # WEBPAGE TAB
  129. //===========================================================================================
  130.  
  131. const char webpage[] PROGMEM = R"=====(
  132. <!DOCTYPE html>
  133. <html>
  134. <head>
  135. <title>Garage Light Control</title>
  136. <style>
  137. body { font-family: Arial, sans-serif; text-align: center; margin-top: 50px; }
  138. .button { padding: 20px; font-size: 20px; margin: 10px; cursor: pointer; }
  139. .on { background-color: green; color: white; }
  140. .off { background-color: red; color: white; }
  141. </style>
  142. </head>
  143. <body>
  144. <h1>Garage Light Control</h1>
  145. <button id="onButton" class="button on" onclick="toggleLight('on')">Turn On</button>
  146. <button id="offButton" class="button off" onclick="toggleLight('off')">Turn Off</button>
  147.  
  148. <script>
  149. function toggleLight(state) {
  150. fetch('/toggle', {
  151. method: 'POST',
  152. headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
  153. body: 'state=' + state
  154. })
  155. .then(response => response.text())
  156. .then(data => {
  157. if (state === 'on') {
  158. document.getElementById('onButton').style.backgroundColor = 'darkgreen';
  159. document.getElementById('offButton').style.backgroundColor = 'red';
  160. } else {
  161. document.getElementById('onButton').style.backgroundColor = 'green';
  162. document.getElementById('offButton').style.backgroundColor = 'darkred';
  163. }
  164. });
  165. }
  166. </script>
  167. </body>
  168. </html>
  169. )=====";
  170.  
  171. //===========================================================================================
  172. //END # WEBPAGE TAB
  173. //===========================================================================================
  174.  
Advertisement
Add Comment
Please, Sign In to add comment