/************ Store in Brazil: www.masterwalkershop.com.br Blog: http://blogmasterwalkershop.com.br/ Facebook: https://www.facebook.com/masterwalkershop/ Youtube: https://www.youtube.com/channel/UC95NobPgWj7L2yFM3IVf_fw/videos ************/ #include "WiFiEsp.h" //INCLUSION OF THE LIBRARY NECESSARY FOR THE OPERATION OF THE CODE char ssid[] = "NAME OF YOUR WIFI NETWORK"; //VARIABLE THAT STORIES THE NAME OF THE WIRELESS NETWORK char pass[] = "PASSWORD OF YOUR WIFI NETWORK";//VARIABLE THAT STORMS THE WIRELESS NETWORK PASSWORD int status = WL_IDLE_STATUS; //TEMPORARY STATUS ATTRIBUTED WHEN WIFI IS INITIALIZED AND REMAINS ACTIVE //UNTIL THE NUMBER OF ATTEMPTS EXPIRE (RESULTING IN WL_NO_SHIELD) OR A CONNECTION IS ESTABLISHED //(RESULTS IN WL_CONNECTED) WiFiEspServer server(80); //CONNECTION MADE IN PORT 80 RingBuffer buf(8); //BUFFER TO INCREASE SPEED AND REDUCE MEMORY ALLOCATION int statusLed = LOW; //VARIABLE STORING CURRENT LED STATUS (ON / OFF) void setup(){ pinMode(LED_BUILTIN, OUTPUT); //DEFINE PIN AS OUTPUT (LED_BUILTIN = PIN 13) digitalWrite(LED_BUILTIN, LOW); //PIN 13 STARTED OFF Serial.begin(9600); //INICIALIZA A SERIAL WiFi.init(&Serial); //INITIALIZES SERIAL COMMUNICATION WITH SHIELD WIFI ESP8266 WiFi.config(IPAddress(192,168,0,110)); //CHANGE TO IP RANGE AVAILABLE FROM YOUR NETWORK //BEGINNING - CHECK IF THE SHIELD IS COUPLED TO THE ARDUINO, CONNECT THE WIRELESS NETWORK AND START THE WEB SERVER if(WiFi.status() == WL_NO_SHIELD){ while (true); } while(status != WL_CONNECTED){ status = WiFi.begin(ssid, pass); } server.begin(); //END - CHECK IF THE SHIELD IS COUPLED TO THE ARDUINO, CONNECT THE WIRELESS NETWORK AND START THE WEB SERVER } void loop(){ WiFiEspClient client = server.available(); //RESPONSE CUSTOMER REQUESTS if (client) { //IF CLIENT ATTEMPTS TO CONNECT, DOES buf.init(); //INICIALIZA O BUFFER while (client.connected()){ //WHILE THE CLIENT IS CONNECTED, DOES if(client.available()){ //IF CUSTOMER IS REQUESTED, DOES char c = client.read(); //READ THE CLIENT REQUIREMENT buf.push(c); //BUFFER RECORDS REQUIREMENT //IDENTIFIES THE END OF HTTP REQUEST AND SENDS ANSWER if(buf.endsWith("\r\n\r\n")) { sendHttpResponse(client); break; } if(buf.endsWith("GET /H")){ //IF THE PARAMETER OF THE REQUIREMENT COMING BY GET EQUALS THE "H", DOES digitalWrite(LED_BUILTIN, HIGH); //LED TURN ON statusLed = 1; //VARIABLE RECEIVES VALUE 1 (MEANS THE LED IS ON) } else{ //ELSE, DOES if (buf.endsWith("GET /L")) { //IF THE PARAMETER OF THE REQUIREMENT COMING BY GET EQUALS THE "L", DOES digitalWrite(LED_BUILTIN, LOW); //LED TURN OFF statusLed = 0; //VARIABLE RECEIVES VALUE 0 (MEANS THE LED IS OFF) } } } } client.stop(); //END HTTP REQUEST AND DISCONNECT CUSTOMER } } //METHOD OF RESPONSE TO CUSTOMER HTTP REQUIREMENT void sendHttpResponse(WiFiEspClient client){ client.println("HTTP/1.1 200 OK"); //WRITE TO CUSTOMER THE HTTP VERSION client.println("Content-Type: text/html"); //WRITE TO THE CLIENT THE TYPE OF CONTENT (text/html) client.println(""); client.println(""); //BROWSER TO HTML SPECIFICATION client.println(""); //OPEN TAG "html" client.println(""); //OPEN TAG "head" client.println(""); //ICON PAGE DEFINITION client.println(""); //REFERENCE TO CSS FILE (STYLE SHEETS) client.println("MasterWalker Shop - Shield WiFi ESP8266 for Arduino"); //WRITE THE TEXT ON THE PAGE client.println(""); //CLOSE TAG "head" //LINES BELOW CREATE HTML PAGE client.println(""); //OPEN TAG "body" client.println("masterwalkershop"); //LOGO OF MASTERWALKER SHOP client.println("

Shield WiFi ESP8266 for Arduino

"); //WRITE THE TEXT ON THE PAGE client.println("

(ESP8266 ESP-12E UART Wireless WIFI Shield TTL Converter)

"); //WRITE THE TEXT ON THE PAGE client.println("CURRENT STATUS OF LED"); //WRITE THE TEXT ON THE PAGE if (statusLed == HIGH){ //IF VARIABLE IS EQUAL TO HIGH (1), DOES client.println("

ON

"); //WRITE "ON" ON PAGE client.println("TURN OFF"); //COMMAND TO TURN OFF THE LED (PASS THE PARAMETER /L) }else{ //ELSE, DOES if (statusLed == LOW){ //IF VARIABLE IS EQUAL TO LOW (0), DOES client.println("

OFF

"); //WRITE "OFF" ON PAGE client.println("TURN ON"); //COMMAND TO TURN ON THE LED (PASS THE PARAMETER /H) } } client.println("
"); //HTML TAG THAT CREATES A LINE ON THE PAGE client.println("
"); //HTML TAG THAT CREATES A LINE ON THE PAGE client.println(""); //CLOSE TAG "body" client.println(""); //CLOSE TAG "html" delay(1); //INTERVAL 1ms }