/* * Web-Controlled LED - https://steve.fi/Hardware/ * * This is a simple program which uses WiFi to allow the on-board * LED to be controlled. * * http://192.168.10.51/LED=OFF * -> Turns off the on-board LED. * * http://192.168.10.51/LED=ON * -> Turns on the on-board LED. * */ #include // // WiFi details. // const char* ssid = "SCOTLAND"; const char* password = "highlander1"; // // The on-board status-LED pin, and starting state (on). // int ledPin = 2; int state = LOW; // // We'll be running a HTTP-server on port 80. // WiFiServer server(80); // // Called once to set things up. // void setup() { // Setup serial-output Serial.begin(115200); delay(10); // Configure the pin. pinMode(ledPin, OUTPUT); digitalWrite(ledPin, state); // Connect to WiFi network WiFi.mode(WIFI_STA); WiFi.hostname("web-led"); WiFi.begin(ssid, password); // Wait until we're connected. while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("WiFi connected"); // Start the server server.begin(); Serial.println("Server started"); // Print the IP address Serial.print("Use this URL to connect: "); Serial.print("http://"); Serial.print(WiFi.localIP()); Serial.println("/"); } // // Called constantly to respond to things. // void loop() { // Check if a client has connected WiFiClient client = server.available(); if (!client) return; // Wait until the client sends some data while (client.connected() && !client.available()) delay(1); // Read the first line of the request String request = client.readStringUntil('\r'); Serial.println(request); client.flush(); if (request.indexOf("/LED=ON") != -1) state = LOW; if (request.indexOf("/LED=OFF") != -1) state = HIGH; // Change the state. digitalWrite(ledPin, state); // Return the response client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println(""); // do not forget this one client.println(""); client.println("Web-LED"); if (state == LOW) client.print("

Led pin is now on.

"); if (state == HIGH) client.print("

Led pin is now off.

"); // Show the output client.println("

Turn OFF

"); client.println("

Turn ON

"); client.println(""); delay(1); Serial.println("Client disconnected"); }