// GETメソッドに対応 void printDirectory() { if(!server.hasArg("dir")) return returnFail("BAD ARGS"); String path = server.arg("dir"); if(path != "/" && !SD.exists((char *)path.c_str())) return returnFail("BAD PATH"); File dir = SD.open((char *)path.c_str()); path = String(); if(!dir.isDirectory()){ dir.close(); return returnFail("NOT DIR"); } dir.rewindDirectory(); server.setContentLength(CONTENT_LENGTH_UNKNOWN); server.send(200, "text/json", ""); WiFiClient client = server.client(); server.sendContent("["); for (int cnt = 0; true; ++cnt) { File entry = dir.openNextFile(); if (!entry) break; String output; if (cnt > 0) output = ','; output += "{\"type\":\""; output += (entry.isDirectory()) ? "dir" : "file"; output += "\",\"name\":\""; output += entry.name(); output += "\""; output += "}"; server.sendContent(output); entry.close(); } server.sendContent("]"); dir.close(); }
void setup() { Serial.begin(115200); Serial.println(); Serial.println("Starting WiFi..."); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); if (WiFi.waitForConnectResult() == WL_CONNECTED) { Serial.printf("OK\n"); Serial.printf("Starting UPnP..."); UPnPDevice device; device.setFriendlyName("Gateway LUPS"); device.setDeviceVersion("1.0"); device.setDeviceType("GatewayNativo"); device.setManufacturer("LUPS"); device.setManufacturerURL("http://lups.inf.ufpel.edu.br/"); device.setModelDescription("versao 1"); device.setModelName("Gateway Nativo"); device.setModelNumber("1.0"); device.setModelURL("http://lups.inf.ufpel.edu.br/"); device.setSerialNumber("12002983"); device.setPresentationURL("http://lups.inf.ufpel.edu.br/"); // device.addDevice(UPnPDevice inputDevice); // device.addService(UPnPService inputService); // device.addIcon(UPnPIcon inputIcon); HTTP.on("/index.html", HTTP_GET, []() { HTTP.send( 200, "text/plain", "ESP8266 - Gateway EXEHDA-IoT \n\n -> " "IP/temp?sensor=1 a 7 para " "ler temperatura DS18B20; \n -> IP/temp2438?sensor=1 a 7 para " "ler temperatura DS2438; \n -> IP/umidade?sensor=1 a 7 para ler " "umidade com HIH4000 e DS2438 \n -> IP/luz?sensor=1 a 7 para ler " "presenca de luz com LDR e DS2438."); }); HTTP.on("/description.xml", HTTP_GET, []() { UPnP.schema(HTTP.client()); }); HTTP.begin(); UPnP.setDevice(device); UPnP.setSchemaURL("description.xml"); UPnP.setHTTPPort(80); UPnP.begin(); Serial.printf("OK"); Serial.printf("Starting HTTP Server..."); Serial.printf("OK\n"); } else { Serial.printf("Failed\n"); while (1) delay(100); } }
bool MyFunctionRequestHandler::handle(ESP8266WebServer& server, HTTPMethod requestMethod, String requestUri){ server.client().setNoDelay(true); requestUri.toLowerCase(); Serial.println(String(F("handle(")) + String(requestMethod) + String(F(", ")) + requestUri + String(F(")"))); if (requestMethod == HTTP_GET){ if (requestUri.equals(F("/")) || requestUri.equals(F("/index.html"))){ server.sendHeader(F("cache-control"), F("private, max-age=0, no-cache, no-store")); server.sendHeader(F("Content-Encoding"), F("gzip")); server.send_P(200, String(F("text/html")).c_str(), index_html_gz, sizeof(index_html_gz)); return true; } if (requestUri.equals(F("/font.woff"))){ server.send_P(200, String(F("text/html")).c_str(), font_woff, sizeof(font_woff)); return true; } /*if (handleStatic(server, requestUri, F("/"), F("/index_comb.html.gz"), F("text/html"), false)) return true; if (handleStatic(server, requestUri, F("/"), F("/index.html"), F("text/html"), false)) return true; if (handleStatic(server, requestUri, F("/index.html"), F("/index_comb.html.gz"), F("text/html"), false)) return true; if (handleStatic(server, requestUri, F("/index.html"), F("/index.html"), F("text/html"), false)) return true; if (handleStatic(server, requestUri, F("/phonon.min.css"), F("/phonon.min.css"), F("text/css"), true)) return true; if (handleStatic(server, requestUri, F("/phonon.min.js"), F("/phonon.min.js"), F("application/javascript"), true)) return true; if (handleStatic(server, requestUri, F("/forms.min.css"), F("/forms.min.css"), F("text/css"), true)) return true; if (handleStatic(server, requestUri, F("/forms.min.js"), F("/forms.min.js"), F("application/javascript"), true)) return true; if (handleStatic(server, requestUri, F("/icons.min.css"), F("/icons.min.css"), F("text/css"), true)) return true; if (handleStatic(server, requestUri, F("/lists.min.css"), F("/lists.min.css"), F("text/css"), true)) return true; if (handleStatic(server, requestUri, F("/phonon-base.min.css"), F("/phonon-base.min.css"), F("text/css"), true)) return true; if (handleStatic(server, requestUri, F("/phonon-core.min.js"), F("/phonon-core.min.js"), F("application/javascript"), true)) return true; if (handleStatic(server, requestUri, F("/font.woff"), F("/font.woff"), F("application/font.woff"), true)) return true; if (handleStatic(server, requestUri, F("/font.eot"), F("/font.eot"), F("application/font.eot"), true)) return true; if (handleStatic(server, requestUri, F("/font.svg"), F("/font.svg"), F("application/font.svg"), true)) return true; if (handleStatic(server, requestUri, F("/font.ttf"), F("/font.ttf"), F("application/font.ttf"), true)) return true; */ if (requestUri.startsWith(F("/api"))){ apiHandler(server); return true; } if (requestUri.startsWith(F("/info"))){ infoHandler(server); return true; } if (requestUri.startsWith(F("/dtintinfo"))){ dtIntegrationHandler(server); return true; } } else if (requestMethod == HTTP_POST){ if (requestUri.startsWith(F("/update"))){ StreamString error; if (Update.hasError()) { Update.printError(error); } if (mbRebootRequired) httpReboot(server, (Update.hasError()) ? error : String(F("Upload succeeded! ")) + String(miUploadSize) + String(F(" bytes transferred<p>"))); else{ server.sendHeader(F("location"), F("/")); server.send(302); return true; } } if (requestUri.equals(F("/dynatrace"))){ dynatracePostHandler(server); return true; } } //Now we really need to respond with 404 String message = F("File Not Found\n\n"); message += F("URI: "); message += server.uri(); message += F("\nMethod: "); message += (server.method() == HTTP_GET ) ? F("GET") : F("POST"); message += F("\nArguments: "); message += server.args(); message += F("\n"); for ( uint8_t i = 0; i < server.args(); i++ ) { message += " " + server.argName ( i ) + ": " + server.arg ( i ) + "\n"; } server.sendHeader(F("cache-control"), F("private, max-age=0, no-cache, no-store")); server.send (404, F("text/plain"), message); return true; }