Advertisement
Guest User

Untitled

a guest
Oct 18th, 2021
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include <WiFi.h>
  3.  
  4. // Replace with your network credentials
  5. const char* ssid = "********";
  6. const char* password = "**********";
  7.  
  8. // Set web server port number to 80
  9. WiFiServer server(80);
  10.  
  11. // Variable to store the HTTP request
  12. String header;
  13.  
  14.  
  15.  
  16. // Current time
  17. unsigned long currentTime = millis();
  18. // Previous time
  19. unsigned long previousTime = 0;
  20. // Define timeout time in milliseconds (example: 2000ms = 2s)
  21. const long timeoutTime = 2000;
  22.  
  23. void setup() {
  24. Serial.begin(9600);
  25.  
  26. // Connect to Wi-Fi network with SSID and password
  27. Serial.print("Connecting to ");
  28. Serial.println(ssid);
  29. WiFi.begin(ssid, password);
  30. while (WiFi.status() != WL_CONNECTED) {
  31. delay(500);
  32. Serial.print(".");
  33. }
  34. // Print local IP address and start web server
  35. Serial.println("");
  36. Serial.println("WiFi connected.");
  37. Serial.println("IP address: ");
  38. Serial.println(WiFi.localIP());
  39. server.begin();
  40. }
  41.  
  42. void loop(){
  43. WiFiClient client = server.available(); // Listen for incoming clients
  44.  
  45. if (client) { // If a new client connects,
  46. currentTime = millis();
  47. previousTime = currentTime;
  48. Serial.println("New Client."); // print a message out in the serial port
  49. String currentLine = ""; // make a String to hold incoming data from the client
  50. while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected
  51. currentTime = millis();
  52. if (client.available()) { // if there's bytes to read from the client,
  53. char c = client.read(); // read a byte, then
  54. Serial.write(c); // print it out the serial monitor
  55. header += c;
  56. if (c == '\n') { // if the byte is a newline character
  57. // if the current line is blank, you got two newline characters in a row.
  58. // that's the end of the client HTTP request, so send a response:
  59. if (currentLine.length() == 0) {
  60. // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
  61. // and a content-type so the client knows what's coming, then a blank line:
  62. client.println("HTTP/1.1 200 OK");
  63. client.println("Content-type:text/html");
  64. client.println("Connection: close");
  65. client.println();
  66.  
  67. client.println("<!DOCTYPE html>");
  68. client.println("<html lang=\"pt-br\">");
  69. client.println("<head>");
  70. client.println("<meta charset=\"UTF-8\">");
  71. client.println("<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">");
  72. client.println("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">");
  73. client.println("<title>Document</title>");
  74. client.println("</head>");
  75. client.println("<body>");
  76. client.println("Olá, mundo!<br/>");
  77. client.println("<a href=\"/dados.txt\" download=\"doc\">Clique Aqui para baixar o arquivo</a>");
  78. client.println("</body>");
  79. client.println("</html>");
  80.  
  81.  
  82. // The HTTP response ends with another blank line
  83. client.println();
  84. // Break out of the while loop
  85. break;
  86. } else { // if you got a newline, then clear currentLine
  87. currentLine = "";
  88. }
  89. } else if (c != '\r') { // if you got anything else but a carriage return character,
  90. currentLine += c; // add it to the end of the currentLine
  91. }
  92. }
  93. }
  94. // Clear the header variable
  95. header = "";
  96. // Close the connection
  97. client.stop();
  98. Serial.println("Client disconnected.");
  99. Serial.println("");
  100. }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement