Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void loop() {
- WiFiClient client = server.available(); // Listen for incoming clients
- if (client)
- {
- String currentLine = "";
- while (client.connected())
- { // loop while the client's connected
- if (client.available())
- { // if there's bytes to read from the client,
- char c = client.read(); // read a byte, then
- // print it out the serial monitor
- header += c;
- if (c == '\n')
- { // if the byte is a newline character
- // if the current line is blank, you got two newline characters in a row.
- // that's the end of the client HTTP request, so send a response:
- if (currentLine.length() == 0)
- {
- if(header.indexOf("/api/") >= 0) {
- //TODO
- client.println("HTTP/1.1 404 Not Found");
- client.println("Connection: close");
- client.println();
- client.println("Not found");
- client.println();
- break;
- } else {
- String path = midString(header, "GET ", " HTTP");
- if(path == "/")
- path = "/index.htm";
- path ="/www" + path;
- Serial.println(path);
- if(!SPIFFS.exists(path)) {
- client.println("HTTP/1.1 404 Not Found");
- client.println("Connection: close");
- client.println();
- client.println("Not found");
- client.println();
- break;
- }
- File f = SPIFFS.open(path, "r");
- // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
- // and a content-type so the client knows what's coming, then a blank line:
- client.println("HTTP/1.1 200 OK");
- client.println("Content-type:text/html");
- client.println("Connection: close");
- client.println("Content-length: " + f.size());
- client.println();
- client.write(f);
- f.close();
- client.println();
- break;
- }
- }
- else
- { // if you got a newline, then clear currentLine
- currentLine = "";
- }
- }
- else if (c != '\r')
- { // if you got anything else but a carriage return character,
- currentLine += c; // add it to the end of the currentLine
- }
- }
- }
- // Clear the header variable
- header = "";
- // Close the connection
- client.stop();
- }
- //serialEvent();
- //_server.handleClient();
- //TODO: read temp sensor every 5s
- /*while(true) {
- readTempSensor();
- delay(5000);
- }*/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement