stspringer

ESP8266 Web Server Arduino Script

Jul 16th, 2023
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.80 KB | None | 0 0
  1. I found this script for the ESP8266 wireless and Arduino script and mane my modifications for my needs, works great
  2.  
  3.  
  4.  
  5.  
  6. /*********
  7. Rui Santos https://randomnerdtutorials.com/esp8266-web-server-with-arduino-ide/
  8. Complete project details at http://randomnerdtutorials.com
  9. *********/
  10.  
  11. //My Add/Edit
  12. //#77878A color Regent Grey
  13. //#FF0000 color Red
  14. //#195B6A Color Green
  15. // Load Wi-Fi library
  16. #include <ESP8266WiFi.h>
  17. //My Add/Edit
  18. #include "ESPAsyncWebServer.h"
  19.  
  20. // Replace with your network credentials
  21. const char* ssid = "XXXXXXXXXX";
  22. const char* password = "XXXXXXXXXX";
  23.  
  24. // Set web server port number to 80
  25. WiFiServer server(80);
  26.  
  27. // Variable to store the HTTP request
  28. String header;
  29.  
  30. // Auxiliar variables to store the current output state
  31. String output5State = "off";
  32. String output4State = "off";
  33.  
  34. //My Add/Edit ADD ANOTHER RELAY GPIO 3
  35. String output3State = "off";
  36.  
  37. // Assign output variables to GPIO pins
  38. const int output5 = 5;
  39. const int output4 = 4;
  40.  
  41. //My Add/Edit ADD ANOTHER RELAY GPIO 3
  42. const int output3 = 3;
  43.  
  44. // Current time
  45. unsigned long currentTime = millis();
  46. // Previous time
  47. unsigned long previousTime = 0;
  48. // Define timeout time in milliseconds (example: 2000ms = 2s)
  49. const long timeoutTime = 2000;
  50.  
  51. void setup() {
  52. Serial.begin(115200);
  53. // Initialize the output variables as outputs
  54.  
  55. pinMode(output5, OUTPUT);
  56. pinMode(output4, OUTPUT);
  57.  
  58. //My Add/Edit ADD ANOTHER RELAY GPIO 3
  59. pinMode(output3, OUTPUT);
  60.  
  61.  
  62.  
  63. // Set outputs to LOW
  64. digitalWrite(output5, LOW);
  65. digitalWrite(output4, LOW);
  66.  
  67. //My Add/Edit ADD ANOTHER RELAY GPIO 3
  68. digitalWrite(output3, LOW);
  69.  
  70. // Connect to Wi-Fi network with SSID and password
  71. Serial.print("Connecting to ");
  72. Serial.println(ssid);
  73. WiFi.begin(ssid, password);
  74. while (WiFi.status() != WL_CONNECTED) {
  75. delay(500);
  76. Serial.print(".");
  77. }
  78. // Print local IP address and start web server
  79. Serial.println("");
  80. Serial.println("WiFi connected.");
  81. Serial.println("IP address: ");
  82. Serial.println(WiFi.localIP());
  83. server.begin();
  84. }
  85.  
  86. void loop() {
  87. WiFiClient client = server.available(); // Listen for incoming clients
  88.  
  89. if (client) { // If a new client connects,
  90. Serial.println("New Client."); // print a message out in the serial port
  91. String currentLine = ""; // make a String to hold incoming data from the client
  92. currentTime = millis();
  93. previousTime = currentTime;
  94. while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected
  95. currentTime = millis();
  96. if (client.available()) { // if there's bytes to read from the client,
  97. char c = client.read(); // read a byte, then
  98. Serial.write(c); // print it out the serial monitor
  99. header += c;
  100. if (c == '\n') { // if the byte is a newline character
  101. // if the current line is blank, you got two newline characters in a row.
  102. // that's the end of the client HTTP request, so send a response:
  103. if (currentLine.length() == 0) {
  104. // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
  105. // and a content-type so the client knows what's coming, then a blank line:
  106. client.println("HTTP/1.1 200 OK");
  107. client.println("Content-type:text/html");
  108. client.println("Connection: close");
  109. client.println();
  110.  
  111. // turns the GPIOs on and off
  112. if (header.indexOf("GET /5/on") >= 0) {
  113. Serial.println("GPIO 5 on");
  114. output5State = "on";
  115. digitalWrite(output5, HIGH);
  116. } else if (header.indexOf("GET /5/off") >= 0) {
  117. Serial.println("GPIO 5 off");
  118. output5State = "off";
  119. digitalWrite(output5, LOW);
  120.  
  121. } else if (header.indexOf("GET /4/on") >= 0) {
  122. Serial.println("GPIO 4 on");
  123. output4State = "on";
  124. digitalWrite(output4, HIGH);
  125. } else if (header.indexOf("GET /4/off") >= 0) {
  126. Serial.println("GPIO 4 off");
  127. output4State = "off";
  128. digitalWrite(output4, LOW);
  129.  
  130. //My Add/Edit ADD ANOTHER RELAY GPIO 3
  131. } else if (header.indexOf("GET /3/on") >= 0) {
  132. Serial.println("GPIO 3 on");
  133. output3State = "on";
  134. digitalWrite(output3, HIGH);
  135. } else if (header.indexOf("GET /3/off") >= 0) {
  136. Serial.println("GPIO 3 off");
  137. output3State = "off";
  138. digitalWrite(output3, LOW);
  139. }
  140.  
  141. // Display the HTML web page
  142. client.println("<!DOCTYPE html><html>");
  143. client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
  144. client.println("<link rel=\"icon\" href=\"data:,\">");
  145. // CSS to style the on/off buttons
  146. // Feel free to change the background-color and font-size attributes to fit your preferences
  147. client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
  148. //client.println(".button { background-color: #195B6A; border: none; color: white; padding: 16px 40px;");//Original Color Green
  149.  
  150. //BACKGROUND COLOR RED
  151. client.println(".button { background-color: #FF0000; border: none; color: white; padding: 16px 40px;");//My Edit Color Red
  152.  
  153. //client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");//Original
  154.  
  155. //FONT SIZE
  156. client.println("text-decoration: none; font-size: 20px; margin: 2px; cursor: pointer;}");//My Edit Font Size
  157. //client.println(".button2 {background-color: #77878A;}</style></head>");//Original Color Regent Grey
  158.  
  159. //BACKGROUND COLOR GREEN
  160. client.println(".button2 {background-color: #195B6A;}</style></head>");//My Edit Color Green
  161.  
  162. // Web Page Heading
  163. //client.println("<body><h1>ESP8266 Web Server</h1>");//Original
  164.  
  165. //BODY HEADING HOFFMAN COLLISION
  166. client.println("<body><h1>Hoffman Collision ESP8266 Web Server</h1>"); //My Add/Edit
  167.  
  168. // Display current state, and ON/OFF buttons for GPIO 5
  169. client.println("<p>GPIO 5 - State " + output5State + "</p>");
  170. // If the output5State is off, it displays the ON button
  171. if (output5State == "off") {
  172. client.println("<p><a href=\"/5/on\"><button class=\"button\">Turn Paint Room Lights ON</button></a></p>");// My Edit For Hoffman Collision Paint Room Lights
  173. } else {
  174. client.println("<p><a href=\"/5/off\"><button class=\"button button2\">Turn Paint Room Lights OFF</button></a></p>");// My Edit For Hoffman Collision Paint Room Lights
  175. }
  176.  
  177. // Display current state, and ON/OFF buttons for GPIO 4
  178. client.println("<p>GPIO 4 - State " + output4State + "</p>");
  179. // If the output4State is off, it displays the ON button
  180. if (output4State == "off") {
  181. client.println("<p><a href=\"/4/on\"><button class=\"button\">Turn ON</button></a></p>");// For other rooms and devices
  182. } else {
  183. client.println("<p><a href=\"/4/off\"><button class=\"button button2\">Turn OFF</button></a></p>");// For other rooms and devices
  184. }
  185.  
  186. //My Add/Edit ADD ANOTHER RELAY GPIO 3
  187. // Display current state, and ON/OFF buttons for GPIO 3
  188. client.println("<p>GPIO 3 - State " + output3State + "</p>");
  189. // If the output3State is off, it displays the ON button
  190. if (output3State == "off") {
  191. client.println("<p><a href=\"/3/on\"><button class=\"button\">Turn ON</button></a></p>");// For other rooms and devices
  192. } else {
  193. client.println("<p><a href=\"/3/off\"><button class=\"button button2\">Turn OFF</button></a></p>");// For other rooms and devices
  194. }
  195.  
  196.  
  197. client.println("</body></html>");
  198.  
  199. // The HTTP response ends with another blank line
  200. client.println();
  201. // Break out of the while loop
  202. break;
  203. } else { // if you got a newline, then clear currentLine
  204. currentLine = "";
  205. }
  206. } else if (c != '\r') { // if you got anything else but a carriage return character,
  207. currentLine += c; // add it to the end of the currentLine
  208. }
  209. }
  210. }
  211. // Clear the header variable
  212. header = "";
  213. // Close the connection
  214. client.stop();
  215. Serial.println("Client disconnected.");
  216. Serial.println("");
  217. }
  218. }
Advertisement
Add Comment
Please, Sign In to add comment