Advertisement
zdenekpetrzd

web

Mar 22nd, 2019
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.03 KB | None | 0 0
  1. /*********
  2. Rui Santos
  3. Complete project details at https://randomnerdtutorials.com
  4. *********/
  5.  
  6. // Load Wi-Fi library
  7. #include <ESP8266WiFi.h>
  8.  
  9. // Replace with your network credentials
  10. const char* ssid = "ZdendaAP";
  11. const char* password = "gt-M286.044";
  12.  
  13. // Set web server port number to 80
  14. WiFiServer server(80);
  15.  
  16. // Variable to store the HTTP request
  17. String header;
  18.  
  19. // Auxiliar variables to store the current output state
  20. String output5State = "off";
  21. String output4State = "off";
  22.  
  23. // Assign output variables to GPIO pins
  24. const int output5 = D0;
  25. const int output4 = D1;
  26.  
  27. void setup() {
  28. Serial.begin(115200);
  29. // Initialize the output variables as outputs
  30. pinMode(output5, OUTPUT);
  31. pinMode(output4, OUTPUT);
  32. // Set outputs to LOW
  33. digitalWrite(output5, LOW);
  34. digitalWrite(output4, LOW);
  35.  
  36. // Connect to Wi-Fi network with SSID and password
  37. Serial.print("Connecting to ");
  38. Serial.println(ssid);
  39. WiFi.begin(ssid, password);
  40. while (WiFi.status() != WL_CONNECTED) {
  41. delay(500);
  42. Serial.print(".");
  43. }
  44. // Print local IP address and start web server
  45. Serial.println("");
  46. Serial.println("WiFi connected.");
  47. Serial.println("IP address: ");
  48. Serial.println(WiFi.localIP());
  49. server.begin();
  50. }
  51.  
  52. void loop(){
  53. WiFiClient client = server.available(); // Listen for incoming clients
  54.  
  55. if (client) { // If a new client connects,
  56. Serial.println("New Client."); // print a message out in the serial port
  57. String currentLine = ""; // make a String to hold incoming data from the client
  58. while (client.connected()) { // loop while the client's connected
  59. if (client.available()) { // if there's bytes to read from the client,
  60. char c = client.read(); // read a byte, then
  61. Serial.write(c); // print it out the serial monitor
  62. header += c;
  63. if (c == '\n') { // if the byte is a newline character
  64. // if the current line is blank, you got two newline characters in a row.
  65. // that's the end of the client HTTP request, so send a response:
  66. if (currentLine.length() == 0) {
  67. // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
  68. // and a content-type so the client knows what's coming, then a blank line:
  69. client.println("HTTP/1.1 200 OK");
  70. client.println("Content-type:text/html");
  71. client.println("Connection: close");
  72. client.println();
  73.  
  74. // turns the GPIOs on and off
  75. if (header.indexOf("GET /5/on") >= 0) {
  76. Serial.println("GPIO 5 on");
  77. output5State = "on";
  78. digitalWrite(output5, HIGH);
  79. } else if (header.indexOf("GET /5/off") >= 0) {
  80. Serial.println("GPIO 5 off");
  81. output5State = "off";
  82. digitalWrite(output5, LOW);
  83. } else if (header.indexOf("GET /4/on") >= 0) {
  84. Serial.println("GPIO 4 on");
  85. output4State = "on";
  86. digitalWrite(output4, HIGH);
  87. } else if (header.indexOf("GET /4/off") >= 0) {
  88. Serial.println("GPIO 4 off");
  89. output4State = "off";
  90. digitalWrite(output4, LOW);
  91. }
  92.  
  93. // Display the HTML web page
  94. client.println("<!DOCTYPE html><html>");
  95. client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
  96. client.println("<link rel=\"icon\" href=\"data:,\">");
  97. // CSS to style the on/off buttons
  98. // Feel free to change the background-color and font-size attributes to fit your preferences
  99. client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
  100. client.println(".button { background-color: #195B6A; border: none; color: white; padding: 16px 40px;");
  101. client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
  102. client.println(".button2 {background-color: #77878A;}</style></head>");
  103.  
  104. // Web Page Heading
  105. client.println("<body><h1>ESP8266 Web Server</h1>");
  106.  
  107. // Display current state, and ON/OFF buttons for GPIO 5
  108. client.println("<p>GPIO 5 - State " + output5State + "</p>");
  109. // If the output5State is off, it displays the ON button
  110. if (output5State=="off") {
  111. client.println("<p><a href=\"/5/on\"><button class=\"button\">ON</button></a></p>");
  112. } else {
  113. client.println("<p><a href=\"/5/off\"><button class=\"button button2\">OFF</button></a></p>");
  114. }
  115.  
  116. // Display current state, and ON/OFF buttons for GPIO 4
  117. client.println("<p>GPIO 4 - State " + output4State + "</p>");
  118. // If the output4State is off, it displays the ON button
  119. if (output4State=="off") {
  120. client.println("<p><a href=\"/4/on\"><button class=\"button\">ON</button></a></p>");
  121. } else {
  122. client.println("<p><a href=\"/4/off\"><button class=\"button button2\">OFF</button></a></p>");
  123. }
  124. client.println("</body></html>");
  125.  
  126. // The HTTP response ends with another blank line
  127. client.println();
  128. // Break out of the while loop
  129. break;
  130. } else { // if you got a newline, then clear currentLine
  131. currentLine = "";
  132. }
  133. } else if (c != '\r') { // if you got anything else but a carriage return character,
  134. currentLine += c; // add it to the end of the currentLine
  135. }
  136. }
  137. }
  138. // Clear the header variable
  139. header = "";
  140. // Close the connection
  141. client.stop();
  142. Serial.println("Client disconnected.");
  143. Serial.println("");
  144. }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement