Advertisement
stspringer

Hoffman_Rui_Santos_Tutorial_Testing_Static_IP_1

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