// WiFi AccessPoint mit Webseite // LED ein und ausschalten mit der Webseite // Arduino IDE 1.6.5 // // Eine Abwandlung des Programms von dieser Seite // https://blog.thesen.eu/http-1-1-webserver-fuer-esp8266-als-accesspoint/ // // Matthias Busse 22.11.2015 Version 0.1 #include const char* ssid = "Shelvin AP"; const char* password = "123"; unsigned long zaehler=0; WiFiServer server(80); void setup() { pinMode(BUILTIN_LED, OUTPUT); // LED festlegen digitalWrite(BUILTIN_LED, 1); // ausschalten (LED ist an bei 0) delay(1); WiFi.mode(WIFI_AP); // AccessPoint aufmachen WiFi.softAP(ssid, password); server.begin(); } void loop() { WiFiClient client = server.available(); // Check ob Client verbunden ist if (!client) return; unsigned long ultimeout = millis()+250; // warten bis Daten rein kommen while(!client.available() && (millis()ultimeout) return; String sRequest = client.readStringUntil('\r'); client.flush(); if(sRequest=="") client.stop(); String sPath="",sParam="", sCmd=""; String sGetstart="GET "; int iStart,iEndSpace,iEndQuest; iStart = sRequest.indexOf(sGetstart); if (iStart>=0){ iStart+=+sGetstart.length(); iEndSpace = sRequest.indexOf(" ",iStart); iEndQuest = sRequest.indexOf("?",iStart); if(iEndSpace>0) { // Sind da Parameter ? if(iEndQuest>0) { sPath = sRequest.substring(iStart,iEndQuest); // Parameter vorhanden sParam = sRequest.substring(iEndQuest,iEndSpace); } else { sPath = sRequest.substring(iStart,iEndSpace); // keine Parameter } } } if(sParam.length()>0) { int iEqu=sParam.indexOf("="); if(iEqu>=0) sCmd = sParam.substring(iEqu+1,sParam.length()); } String sResponse,sHeader; if(sPath!="/") { // 404 Fehler sResponse="404 Not Found

Not Found

The requested URL was not found on this server.

"; sHeader = "HTTP/1.1 404 Not found\r\n"; sHeader += "Content-Length: "; sHeader += sResponse.length(); sHeader += "\r\n"; sHeader += "Content-Type: text/html\r\n"; sHeader += "Connection: close\r\n"; sHeader += "\r\n"; } else { // Seite ausgeben zaehler++; sResponse = "Shelvin AccessPoint"; sResponse += ""; sResponse += ""; sResponse += "

Shelvin AccessPoint

"; sResponse += "LED ein- und ausschalten.
"; sResponse += ""; sResponse += "

LED  

"; if (sCmd.length()>0) { sResponse += "Parameter:" + sCmd + "
"; if(sCmd.indexOf("LEDEIN")>=0) { // LED einschalten digitalWrite(BUILTIN_LED, 0); } if(sCmd.indexOf("LEDAUS")>=0){ // LED ausschalten digitalWrite(BUILTIN_LED, 1); } } sResponse += ""; sResponse += "
Zähler="; sResponse += zaehler; sResponse += "
"; sResponse += "von Matthias Busse 11/2015
"; sResponse += ""; sHeader = "HTTP/1.1 200 OK\r\n"; sHeader += "Content-Length: "; sHeader += sResponse.length(); sHeader += "\r\n"; sHeader += "Content-Type: text/html\r\n"; sHeader += "Connection: close\r\n"; sHeader += "\r\n"; } client.print(sHeader); client.print(sResponse); client.stop(); }