Advertisement
Guest User

Untitled

a guest
Nov 9th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void loop() {
  2.     WiFiClient client = server.available(); // Listen for incoming clients
  3.  
  4.     if (client)
  5.     {                  
  6.         String currentLine = "";
  7.         while (client.connected())
  8.         { // loop while the client's connected
  9.             if (client.available())
  10.             {                           // if there's bytes to read from the client,
  11.                 char c = client.read(); // read a byte, then
  12.                         // print it out the serial monitor
  13.                 header += c;
  14.                 if (c == '\n')
  15.                 { // if the byte is a newline character
  16.                     // if the current line is blank, you got two newline characters in a row.
  17.                     // that's the end of the client HTTP request, so send a response:
  18.                     if (currentLine.length() == 0)
  19.                     {
  20.                         if(header.indexOf("/api/") >= 0) {
  21.                             //TODO
  22.                             client.println("HTTP/1.1 404 Not Found");
  23.                             client.println("Connection: close");
  24.                             client.println();
  25.                             client.println("Not found");
  26.                             client.println();
  27.                             break;
  28.                         } else {
  29.                             String path = midString(header, "GET ", " HTTP");
  30.                             if(path == "/")
  31.                                 path = "/index.htm";
  32.                             path  ="/www" + path;
  33.                             Serial.println(path);
  34.  
  35.                             if(!SPIFFS.exists(path)) {
  36.                                 client.println("HTTP/1.1 404 Not Found");
  37.                                 client.println("Connection: close");
  38.                                 client.println();
  39.                                 client.println("Not found");
  40.                                 client.println();
  41.                                 break;
  42.                             }
  43.  
  44.                             File f = SPIFFS.open(path, "r");
  45.  
  46.                             // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
  47.                             // and a content-type so the client knows what's coming, then a blank line:
  48.                             client.println("HTTP/1.1 200 OK");
  49.                             client.println("Content-type:text/html");
  50.                             client.println("Connection: close");
  51.                             client.println("Content-length: " + f.size());
  52.                             client.println();
  53.  
  54.                             client.write(f);
  55.                             f.close();
  56.  
  57.                             client.println();
  58.                             break;
  59.                         }
  60.                     }
  61.                     else
  62.                     { // if you got a newline, then clear currentLine
  63.                         currentLine = "";
  64.                     }
  65.                 }
  66.                 else if (c != '\r')
  67.                 {                     // if you got anything else but a carriage return character,
  68.                     currentLine += c; // add it to the end of the currentLine
  69.                 }
  70.             }
  71.         }
  72.         // Clear the header variable
  73.         header = "";
  74.         // Close the connection
  75.         client.stop();
  76.     }
  77.  
  78.     //serialEvent();
  79.     //_server.handleClient();
  80.  
  81.     //TODO: read temp sensor every 5s
  82.     /*while(true) {
  83.         readTempSensor();
  84.         delay(5000);
  85.     }*/
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement