//Libraries #include //https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/src/ESP8266WiFi.h //Constants #define NUM_SLAVES 1 #define LED D4 //Parameters String nom = "Master"; const char* ssid = "Fishnet"; const char* password = "123faith"; //Variables bool sendCmd = false; String slaveCmd = "0"; String slaveState = "0"; //Objects WiFiServer server(80); WiFiClient browser; IPAddress ip(192, 168, 1, 44); IPAddress gateway(192, 168, 1, 1); IPAddress subnet(255, 255, 255, 0); void setup() { //Init Serial USB Serial.begin(115200); Serial.println(F("Initialize System")); //Init ESP8266 Wifi WiFi.config(ip, gateway, subnet); // forces to use the fix IP WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print(F(".")); } server.begin(); Serial.print(nom); Serial.print(F(" connected to Wifi! IP address : http://")); Serial.println(WiFi.localIP()); // Print the IP address pinMode(LED, OUTPUT); } void loop() { clientRequest(); } void clientRequest( ) { /* function clientRequest */ ////Check if client connected WiFiClient client = server.available(); client.setTimeout(50); if (client) { if (client.connected()) { //Print client IP address Serial.print(" ->");Serial.println(client.remoteIP()); String request = client.readStringUntil('\r'); //receives the message from the client if (request.indexOf("Slave0") == 0) { //Handle slave request Serial.print("From "); Serial.println(request); int index = request.indexOf(":"); String slaveid = request.substring(0, index); slaveState = request.substring(request.indexOf("x") + 1, request.length()); Serial.print("state received: "); Serial.println(slaveState); client.print(nom); if (sendCmd) { sendCmd = false; client.println(": Ok " + slaveid + "! Set state to x" + String(slaveCmd) + "\r"); } else { client.println(": Hi " + slaveid + "!\r"); // sends the answer to the client } client.stop(); // terminates the connection with the client } else { Serial.print("From Browser : "); Serial.println(request); client.flush(); handleRequest(request); webpage(client); } } } } void handleRequest(String request) { /* function handleRequest */ ////Check if client connected if (request.indexOf("/light1on") > 0) { digitalWrite(LED, LOW); } if (request.indexOf("/light1off") > 0) { digitalWrite(LED, HIGH); } if (request.indexOf("/light2on") > 0) { sendCmd = true; slaveCmd = "1"; } if (request.indexOf("/light2off") > 0) { sendCmd = true; slaveCmd = "0"; } } void webpage(WiFiClient browser) { /* function webpage */ ////Send webpage to browser browser.println("HTTP/1.1 200 OK"); browser.println("Content-Type: text/html"); browser.println(""); // do not forget this one browser.println(""); browser.println(""); browser.println(""); browser.println(""); browser.println(""); browser.println(""); browser.println(""); browser.println("

"); browser.println("

Esp8266 Electrical Device Control

"); browser.println("

"); browser.println("

"); browser.println("

"); browser.println("

Commands

"); browser.println("
"); browser.println("Built-in LED"); browser.println(""); browser.println("
"); browser.println("
"); browser.println("

"); browser.println("
"); browser.println("Device 2"); browser.println(""); browser.println("
"); browser.println("
"); browser.println("

"); browser.println("

"); browser.println("

Data

"); browser.println("
"); browser.println(""); browser.println(""); if (digitalRead(LED)) { browser.print(""); } else { browser.print(""); } browser.println("
"); if (slaveState == "1") //(digitalRead(4)) { browser.print(""); } else { browser.print(""); } browser.println(""); browser.println("
LED is OFFLED is ONLight 2 is OFFLight 2 is ON
"); browser.println("
"); browser.println(""); delay(1); }