#include #ifndef STASSID #define STASSID "SSID" #define STAPSK "Password" #endif #define led 2 #define button 5 int ledState = HIGH; int buttonCurrent; int buttonPrevious = LOW; const char* ssid = STASSID; const char* password = STAPSK; // Create an instance of the server // specify the port to listen on as an argument WiFiServer server(80); void setup() { Serial.begin(115200); // prepare LED pinMode(led, OUTPUT); pinMode(button, INPUT_PULLUP); // Connect to WiFi network Serial.println(); Serial.println(); Serial.print(F("Connecting to ")); Serial.println(ssid); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print(F(".")); } Serial.println(); Serial.println(F("WiFi connected")); // Start the server server.begin(); Serial.println(F("Server started")); // Print the IP address Serial.println(WiFi.localIP()); } void loop() { buttonToggle(); // Check if a client has connected WiFiClient client = server.available(); if (!client) { return; } Serial.println(F("new client")); client.setTimeout(300); // default is 1000 // Read the first line of the request String req = client.readStringUntil('\r'); Serial.println(F("request: ")); Serial.println(req); // Match the request if (req.indexOf(F("/gpio/0")) != -1) { ledState = LOW; client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/plain"); client.println(); client.println("LED is now off"); } else if (req.indexOf(F("/gpio/1")) != -1) { ledState = HIGH; client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/plain"); client.println(); client.println("LED is now on"); } else if (req.indexOf(F("/gpio/status")) != -1) { client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/plain"); client.println(); if (digitalRead(led) == HIGH){ client.println("On"); //you can set here 1 instead of On if needed } else { client.println("Off"); //you can set here 0 instead of Off if needed } } else if (req.indexOf(F("/gpio/toggle")) != -1) { ledState = !ledState; client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/plain"); client.println(); if (ledState == HIGH){ client.println("LED is now on"); } else { client.println("LED is now off"); } } else { Serial.println(F("invalid request")); ledState = digitalRead(led); } // Set LED according to the request digitalWrite(led, ledState); while (client.available()) { client.read(); } Serial.println(F("Disconnecting from client")); } void buttonToggle(){ buttonCurrent = digitalRead(button); if(buttonCurrent == HIGH && buttonPrevious == LOW){ if(ledState == HIGH){ ledState = LOW; } else { ledState = HIGH; } } digitalWrite(led, ledState); buttonPrevious = buttonCurrent; }