stspringer

John's LED Garage Lights with ESP8266 and Current Seneor

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