#include #include #include // Include the Wi-Fi-Multi library #include // Include the WebServer library #include // Include the mDNS library ESP8266WiFiMulti wifiMulti; // Create an instance of the server ESP8266WebServer server(80); const int led = D2; void handleRoot(); void handleLED(); void handleNotFound(); void setup() { Serial.begin(115200); delay(10); pinMode(led, OUTPUT); digitalWrite(led,1); // Connect to WiFi network Serial.println(); wifiMulti.addAP("", ""); // add Wi-Fi networks you want to connect to // You can add several of the above lines to enable multi-Ap scenarios Serial.println(); Serial.print("Connecting ..."); //WiFi.begin(ssid, password); while (wifiMulti.run() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected to "); Serial.println(WiFi.SSID()); Serial.println("IP address: "); Serial.println(WiFi.localIP()); if (MDNS.begin("iot")) { // Start the mDNS responder for esp8266.local Serial.println("mDNS responder started"); } else { Serial.println("Error setting up MDNS responder!"); } server.on("/", HTTP_GET, handleRoot); server.on("/LED", HTTP_POST, handleLED); server.onNotFound(handleNotFound); // Start the server server.begin(); Serial.println("Server started"); } void loop() { // Check if a client has connected server.handleClient(); } void handleRoot() { // When URI / is requested, send a web page with a button to toggle the LED server.send(200, "text/html", "Internet of Things - Demonstration\

Velkommen til denne WebServer

\

Internet of Things (IoT) er \"tingenes Internet\" - dagligdags ting kommer på nettet og får ny værdi. Det kan løse mange udfordringer.

\

Her kommunikerer du med en webserver på en lille microcontroller af typen Arduino, som i dette tilfælde styrer en digital udgang, som du så igen kan bruge til at styre en lampe, en ventilator, tænde for varmen eller hvad du lyster

\

Klik nedenstående knap for at tænde eller slukke LED port D2

\
\

Med en Arduino ESP8266 kan du lave et hav a sjove projekter

\ "); } void handleLED() { // If a POST request is made to URI /LED digitalWrite(led,!digitalRead(led)); // Change the state of the LED server.sendHeader("Location","/"); // Add a header to respond with a new location for the browser to go to the home page again server.send(303); // Send it back to the browser with an HTTP status 303 (See Other) to redirect } void handleNotFound(){ server.send(404, "text/plain", "404: Not found"); // Send HTTP status 404 (Not Found) when there's no handler for the URI in the request }