#include //Here you can set your ssid and password #ifndef STASSID #define STASSID "your-ssid" #define STAPSK "your-password" #endif const char* ssid = STASSID; const char* password = STAPSK; int status1 = 0; int status2 = 0; // Create an instance of the server // specify the port to listen on as an argument WiFiServer server(80); void setup() { //begin the serial connection make sure to adept your speed also in the serial monitor Serial.begin(115200); //define the pins D5 to D8 as an output pinMode(D5, OUTPUT); digitalWrite(D5, 0); pinMode(D6, OUTPUT); digitalWrite(D6, 0); pinMode(D7, OUTPUT); digitalWrite(D7, 0); pinMode(D8, OUTPUT); digitalWrite(D8, 0); // Connect to WiFi network Serial.println(); Serial.println(); Serial.print(F("Connecting to ")); Serial.println(ssid); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); //print a . until you're connected to wifi while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print(F(".")); } //if you are connected print it to the serial port 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() { // Check if a client has connected WiFiClient client = server.available(); if (!client) { return; } Serial.println(F("new client")); client.setTimeout(5000); // default is 1000 // Read the first line of the request String msgRes = client.readStringUntil('\r'); Serial.println(F("request: ")); Serial.println(msgRes); if (msgRes.indexOf(F("/sol1/0")) != -1) { digitalWrite(D7,LOW); delay(1000); digitalWrite(D8,HIGH); status1 = 0; } if (msgRes.indexOf(F("/sol1/1")) != -1) { digitalWrite(D8,LOW); delay(1000); digitalWrite(D7,HIGH); status1 = 1; } if (msgRes.indexOf(F("/sol2/0")) != -1) { digitalWrite(D5,LOW); delay(1000); digitalWrite(D6,HIGH); status2 = 0; } if (msgRes.indexOf(F("/sol2/1")) != -1) { digitalWrite(D6,LOW); delay(1000); digitalWrite(D5,HIGH); status2 = 1; } // read/ignore the rest of the request // do not client.flush(): it is for output only, see below while (client.available()) { // byte by byte is not very efficient client.read(); } // Send the response to the client // it is OK for multiple small client.print/write, // because nagle algorithm will group them into one single packet client.print(F("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n\r\n\r\nSolenoid 1 is now ")); client.print((status1) ? F("on") : F("off")); client.print(F("
Solenoid 2 is now ")); client.print((status2) ? F("on") : F("off")); client.print(F("

Solenoid 1 off
Solenoid 1 on")); client.print(F("

Solenoid 2 off
Solenoid 2 on")); // The client will actually be *flushed* then disconnected // when the function returns and 'client' object is destroyed (out-of-scope) // flush = ensure written data are received by the other side Serial.println(F("Disconnecting from client")); }