Relay Control Using ESP866

I am trying to control Relay module using arduino uno & esp8266

I have taken example code from below linkhttps://www.electronicshub.org/control-a-relay-using-esp8266-wifi-based/

I am getting error . i have attached error file .

Can someone guide proper library <ESP8266WiFi.h> can be downloaded.

#include <ESP8266WiFi.h>

const char* ssid = "SSID";//type your ssid
const char* password = "PASSWORD";//type your password

int relayPin = 2; // GPIO2 of ESP8266
WiFiServer ESPserver(80);//Service Port

void setup() 
{
Serial.begin(115200);
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, HIGH);

Serial.println();
Serial.println();
Serial.print("Connecting to: ");
Serial.println(ssid);

WiFi.begin(ssid, password);
delay(5000);

/*
 The following four line of the 
 code will assign a Static IP Address to 
 the ESP Module. If you do not want this, 
 comment out the following four lines.  
 */

IPAddress ip(192,168,1,254);   
IPAddress gateway(192,168,1,1);   
IPAddress subnet(255,255,255,0);   
WiFi.config(ip, gateway, subnet);
delay(5000);

while (WiFi.status() != WL_CONNECTED) 
{
delay(100);
Serial.print("*");
}
Serial.println("");
Serial.println("WiFi connected");

// Start the server
ESPserver.begin();
Serial.println("Server started");

// Print the IP address
Serial.print("The URL to control ESP8266: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
}

void loop() 
{
// Check if a client has connected
WiFiClient client = ESPserver.available();
if (!client) 
{
return;
}

// Wait until the client sends some data
Serial.println("New Client");
while(!client.available())
{
delay(1);
}

// Read the first line of the request
String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();

// Match the request

int value = LOW;
if (request.indexOf("/RELAYON") != -1) 
{
Serial.println("LAMP is ON");
digitalWrite(relayPin, LOW);
value = LOW;
} 
if (request.indexOf("/RELAYOFF") != -1)
{
Serial.println("LAMP is OFF");
digitalWrite(relayPin, HIGH);
value = HIGH;
}

// Return the response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); //  IMPORTANT
client.println("<!DOCTYPE HTML>");
client.println("<html>");

client.print("Status of the Lamp: ");

if(value == LOW) 
{
client.print("ON");  
} 
else 
{
client.print("OFF");
}

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

the Uno is not used in the tutorial

Juraj:
the Uno is not used in the tutorial

Arduino uno is used

#include <ESP8266WiFi.h>

Where i can get proper working library function. I downloaded from lot of github link but can use like other library. can someone guide me link.

if someone using it let me which version of arduino IDE being used.
Some google example using with esp8266 Nodemcu & also on arduino Uno. That means it support both platform. for UNO i have attached link.

read again what I wrote in yout threads until now

please use the link i shared . where they showing example with arduino connected. But he is not mentioned ESPwifi.h library used. & demo video show how it works.

Mean while i could not able to get proper library function . i tested another library from github

Below code uses LED control using ESP8266 & arduino UNo using SoftwareSerial.

Here i could able to connected to wifi router but i am getting some errors .

  1. could not able to get IP address of ESP8266 connected to router from comport
  2. display show ip address 0.0.0.0
/*
 WiFiEsp example: WebServerLed
 
 A simple web server that lets you turn on and of an LED via a web page.
 This sketch will print the IP address of your ESP8266 module (once connected)
 to the Serial monitor. From there, you can open that address in a web browser
 to turn on and off the LED on pin 13.

 For more details see: http://yaab-arduino.blogspot.com/p/wifiesp.html
*/

#include "WiFiEsp.h"

// Emulate EspSerial on pins 6/7 if not present
#ifndef HAVE_HWEspSerial
#include "SoftwareSerial.h"
SoftwareSerial EspSerial(2, 3); // RX, TX
#endif

char ssid[] = "esp8266";            // your network SSID (name)
char pass[] = "Test123456";        // your network password
int status = WL_IDLE_STATUS;

int ledStatus = LOW;

WiFiEspServer server(80);

// use a ring buffer to increase speed and reduce memory allocation
RingBuffer buf(8);

void setup()
{
  pinMode(LED_BUILTIN, OUTPUT);	// initialize digital pin LED_BUILTIN as an output.
  Serial.begin(115200);   // initialize serial for debugging
  EspSerial.begin(115200);    // initialize serial for ESP module
  WiFi.init(&EspSerial);    // initialize ESP module

  // check for the presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue
    while (true);
  }

  // attempt to connect to WiFi network
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);
  }

  Serial.println("You're connected to the network");
  printWifiStatus();
  
  // start the web server on port 80
  server.begin();
}


void loop()
{
  WiFiEspClient client = server.available();  // listen for incoming clients

  if (client) {                               // if you get a client,
    Serial.println("New client");             // print a message out the serial port
    buf.init();                               // initialize the circular buffer
    while (client.connected()) {              // loop while the client's connected
      if (client.available()) {               // if there's bytes to read from the client,
        char c = client.read();               // read a byte, then
        buf.push(c);                          // push it to the ring buffer

        // printing the stream to the serial monitor will slow down
        // the receiving of data from the ESP filling the serial buffer
        //Serial.write(c);
        
        // you got two newline characters in a row
        // that's the end of the HTTP request, so send a response
        if (buf.endsWith("\r\n\r\n")) {
          sendHttpResponse(client);
          break;
        }

        // Check to see if the client request was "GET /H" or "GET /L":
        if (buf.endsWith("GET /H")) {
          Serial.println("Turn led ON");
          ledStatus = HIGH;
          digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
        }
        else if (buf.endsWith("GET /L")) {
          Serial.println("Turn led OFF");
          ledStatus = LOW;
          digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
        }
      }
    }
    
    // close the connection
    client.stop();
    Serial.println("Client disconnected");
  }
}


void sendHttpResponse(WiFiEspClient client)
{
  // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
  // and a content-type so the client knows what's coming, then a blank line:
  client.println("HTTP/1.1 200 OK");
  client.println("Content-type:text/html");
  client.println();
  
  // the content of the HTTP response follows the header:
  client.print("The LED is ");
  client.print(ledStatus);
  client.println("
");
  client.println("
");
  
  client.println("Click <a href=\"/H\">here</a> turn the LED on
");
  client.println("Click <a href=\"/L\">here</a> turn the LED off
");
  
  // The HTTP response ends with another blank line:
  client.println();
}

void printWifiStatus()
{
  // print the SSID of the network you're attached to
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print where to go in the browser
  Serial.println();
  Serial.print("To see this page in action, open a browser to http://");
  Serial.println(ip);
  Serial.println();
}

you still didn’t read the guide

I have tested code below. I have the serial out put like this.But cant open the ip address

How can i over come with this issue.

Yes i have gone through thread u shared

  1. Model NODE MCU : will come up with digital io, analog io, and Arduino IDE support inbuilt board supporting software and library .Based on which you can code and pop data to server without using arduino uno
    This unit i have already ordered

  2. I have lost cost mode ESP8266-12E version where before the Node MCU people are integrating with it . I have tested basic code where i can transmit AT command and get response from salve. It shows connected. But i am not getting proper thread explaining the complete library. i have attached lot of links /blogs where they could able to communicate with ESP8266 module.

In my testing ESP8266-12E i used to get below msgs.

Where i cant able to open webpages . but router is identifying the esp8266.

#include <SoftwareSerial.h>  //Including the software serial library
#define DEBUG true
SoftwareSerial esp8266(2,3); // This will make the Arduino pin 2 as the RX pin and Arduino pin 3 as the TX. Software UART
/* So you have to connect the TX of the esp8266 to the pin 2 of the Arduino and the TX of the esp8266 to the pin 3 of the Arduino. This means that you need to connect the TX line from the esp to the Arduino's pin 2 */

void setup() 
{
  Serial.begin(115200);   // Setting the baudrate to 9600
  esp8266.begin(115200);  // Set it according to your esp’s baudrate. Different esp’s have different baud rates.
  pinMode(11,OUTPUT);   // Setting the pin 11 as the output pin.
  digitalWrite(11,LOW); // Making it low.
  pinMode(12,OUTPUT);   // Setting the pin 12 as the output pin..
  digitalWrite(12,LOW); // Making pin 12 low.
  pinMode(13,OUTPUT);   // Setting the pin 13 as the output pin.
  digitalWrite(13,LOW); // Making pin 13 low.
  sendData("AT+RST\r\n",2000,DEBUG);            //This command will reset module to default
  sendData("AT+CWMODE=2\r\n",1000,DEBUG);       // This will configure the mode as access point
  sendData("AT+CIFSR\r\n",1000,DEBUG);          // This will get ip address and will show it
  sendData("AT+CIPMUX=1\r\n",1000,DEBUG);       // This will configure the ESP8266 for multiple connections
  sendData("AT+CIPSERVER=1,80\r\n",1000,DEBUG); // This will set the server on port 80
}
void loop() 
{
  if(esp8266.available()) // Checking that whether the esp8266 is sending a message or not (Software UART Data)
  { 
    if(esp8266.find("+IPD,"))
    { 
      delay(1000);        // Waiting for 1 sec
      int connectionId = esp8266.read()-48;   // Subtracting 48 from the character to get the number.
      esp8266.find("pin=");                   // Advancing the cursor to the "pin="
      int pinNumber = (esp8266.read()-48)*10; // Getting the first number which is pin 13
      pinNumber += (esp8266.read()-48);       // This will get the second number. For example, if the pin number is 13 then the 2nd number will be 3 and then add it to the first number
      digitalWrite(pinNumber, !digitalRead(pinNumber)); // This will toggle the pin
      // The following commands will close the connection 
      String closeCommand = "AT+CIPCLOSE="; 
      closeCommand+=connectionId; 
      closeCommand+="\r\n";
      sendData(closeCommand,1000,DEBUG);     // Sending the data to the ESP8266 to close the command
    } 
  } 
}
String sendData(String command, const int timeout, boolean debug) // Function to send the data to the esp8266
{
  String response = ""; 
  esp8266.print(command);           // Send the command to the ESP8266
  long int time = millis();
  while( (time+timeout) > millis()) // ESP8266 will wait for some time for the data to receive
  {
    while(esp8266.available())      // Checking whether ESP8266 has received the data or not
    {
      char c = esp8266.read();      // Read the next character.
      response+=c;                  // Storing the response from the ESP8266
    }
  }
  if(debug)
  { 
    Serial.print(response);         // Printing the response of the ESP8266 on the serial monitor.
  }
  return response;
}

to write a sketch for the esp8266 you should install the esp8266 arduino core package in IDE. the WiFi libraries are part of it.

to interact with module with AT firmware the best option is the WiFiEsp library. but it is not ideal.

the esp-12E, can be connected to Arduino over SPI like the Ethernet or WiFi shield are. the firmware and library for this type of connection is WiFiSpi. Until now there is no SPI connected shield with the esp8266

this is for esp-01 with 4 relay module.

works for me :smiley:

/*********
  Rui Santos
  Complete project details at http://randomnerdtutorials.com  
*********/

// Load Wi-Fi library
#include <ESP8266WiFi.h>

// Replace with your network credentials
const char* ssid     = "Guardians of the Gateway";
const char* password = "**********";

// Set static IP address
IPAddress ip(192, 168, 1, 222);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);

// Set web server port number to 80
WiFiServer server(80);

// Variable to store the HTTP request
String header;

// Auxiliar variables to store the current output state
String output0State = "off";
String output1State = "off";
String output2State = "off";
String output3State = "off";

// Assign output variables to GPIO pins
const int output0 = 0;
const int output1 = 1;
const int output2 = 2;
const int output3 = 3;

///////////////////////////////////////////////////////////////////////////////////////////////

void setup() {
  Serial.begin(115200);
  
  // Initialize the output variables as outputs
  pinMode(output0, OUTPUT);
  pinMode(output1, OUTPUT);
  pinMode(output2, OUTPUT);
  pinMode(output3, OUTPUT);
  
  // Set outputs to LOW
  digitalWrite(output0, LOW);
  digitalWrite(output1, LOW);
  digitalWrite(output2, LOW);
  digitalWrite(output3, LOW);

  // Connect to Wi-Fi network with SSID and password
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.config(ip, gateway, subnet);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  // Print local IP address and start web server
  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  server.begin();
}

////////////////////////////////////////////////////////////////////////////////////////////////

void loop(){
  WiFiClient client = server.available();   // Listen for incoming clients

  if (client) {                             // If a new client connects,
    Serial.println("New Client.");          // print a message out in the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        header += c;
        if (c == '\n') {                    // if the byte is a newline character
          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println("Connection: close");
            client.println();
            
            // turns the GPIOs on and off
            if (header.indexOf("GET /0/on") >= 0) {
              Serial.println("GPIO 0 on");
              output0State = "on";
              digitalWrite(output0, LOW);
            } else if (header.indexOf("GET /0/off") >= 0) {
              Serial.println("GPIO 0 off");
              output0State = "off";
              digitalWrite(output0, HIGH);
            } else if (header.indexOf("GET /1/on") >= 0) {
              Serial.println("GPIO 1 on");
              output1State = "on";
              digitalWrite(output1, LOW);
            } else if (header.indexOf("GET /1/off") >= 0) {
              Serial.println("GPIO 1 off");
              output1State = "off";
              digitalWrite(output1, HIGH);
            } else if (header.indexOf("GET /2/on") >= 0) {
              Serial.println("GPIO 2 on");
              output2State = "on";
              digitalWrite(output2, LOW);
            } else if (header.indexOf("GET /2/off") >= 0) {
              Serial.println("GPIO 2 off");
              output2State = "off";
              digitalWrite(output2, HIGH);
            } else if (header.indexOf("GET /3/on") >= 0) {
              Serial.println("GPIO 3 on");
              output3State = "on";
              digitalWrite(output3, LOW);
            } else if (header.indexOf("GET /3/off") >= 0) {
              Serial.println("GPIO 3 off");
              output3State = "off";
              digitalWrite(output3, HIGH);
            }
            
            // Display the HTML web page
            client.println("<!DOCTYPE html><html>");
            client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
            client.println("<link rel=\"icon\" href=\"data:,\">");
            
            // CSS to style the on/off buttons 
            client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
            client.println(".button { background-color: #00b22c; border: none; color: white; padding: 18px;");
            client.println("text-decoration: none; font-size: 45px; cursor: pointer;}");
            client.println(".button2 {background-color: #f44242;}");
            client.println("button.button {width: calc(100% - 20px);}</style></head>");
            
            // Web Page Heading
            client.println("<body><h1>ESP-01 Web Server</h1>");
            
            // Display current state, and ON/OFF buttons for GPIO 0  
            client.println("<p>Output 1 is " + output0State + "</p>");
            // If the output0State is off, it displays the ON button       
            if (output0State=="off") {
              client.println("<p><a href=\"/0/on\"><button class=\"button\">ON</button></a></p>");
            } else {
              client.println("<p><a href=\"/0/off\"><button class=\"button button2\">OFF</button></a></p>");
            } 
               
            // Display current state, and ON/OFF buttons for GPIO 1  
            client.println("<p>Output 2 is " + output1State + "</p>");
            // If the output1State is off, it displays the ON button       
            if (output1State=="off") {
              client.println("<p><a href=\"/1/on\"><button class=\"button\">ON</button></a></p>");
            } else {
              client.println("<p><a href=\"/1/off\"><button class=\"button button2\">OFF</button></a></p>");
            }

            // Display current state, and ON/OFF buttons for GPIO 2  
            client.println("<p>Output 3 is " + output2State + "</p>");
            // If the output2State is off, it displays the ON button       
            if (output2State=="off") {
              client.println("<p><a href=\"/2/on\"><button class=\"button\">ON</button></a></p>");
            } else {
              client.println("<p><a href=\"/2/off\"><button class=\"button button2\">OFF</button></a></p>");
            }

            // Display current state, and ON/OFF buttons for GPIO 3  
            client.println("<p>Output 4 is " + output3State + "</p>");
            // If the output3State is off, it displays the ON button       
            if (output3State=="off") {
              client.println("<p><a href=\"/3/on\"><button class=\"button\">ON</button></a></p>");
            } else {
              client.println("<p><a href=\"/3/off\"><button class=\"button button2\">OFF</button></a></p>");
            }

            // Finish web page
            client.println("</body></html>");
            
            // The HTTP response ends with another blank line
            client.println();
            // Break out of the while loop
            break;
          } else { // if you got a newline, then clear currentLine
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }
      }
    }
    // Clear the header variable
    header = "";
    // Close the connection
    client.stop();
    Serial.println("Client disconnected.");
    Serial.println("");
  }
}

sorry didnt look at the error pic

as juraj said u need to install the esp8266 boards

add this link in preferences/additional boards manager URL's

http://arduino.esp8266.com/stable/package_esp8266com_index.json

then goto boards manager and search and install 8266 boards

Weather the code for 4 relay module tested with arduino uno+ esp8266 e01 version or

node Mcu development board .

I have ordered node Mcu board.

but since these library are designed for esp8266 it should work without Node MCU controller. like wise gps,gsm etc i can connect any module but config pin are different.

Below code i am testing . Its results like here

  1. How can i change DHCP Ip adrress in Serial it showing.

Device is communicating but cant able to get IP address of Esp8266
ESP8266+Arduino UNo

#include <SoftwareSerial.h>  //Including the software serial library
#define DEBUG true
SoftwareSerial esp8266(2,3); // This will make the Arduino pin 2 as the RX pin and Arduino pin 3 as the TX. Software UART
/* So you have to connect the TX of the esp8266 to the pin 2 of the Arduino and the TX of the esp8266 to the pin 3 of the Arduino. This means that you need to connect the TX line from the esp to the Arduino's pin 2 */

void setup() 
{
  Serial.begin(115200);   // Setting the baudrate to 9600
  esp8266.begin(115200);  // Set it according to your esp’s baudrate. Different esp’s have different baud rates.
  pinMode(11,OUTPUT);   // Setting the pin 11 as the output pin.
  digitalWrite(11,LOW); // Making it low.
  pinMode(12,OUTPUT);   // Setting the pin 12 as the output pin..
  digitalWrite(12,LOW); // Making pin 12 low.
  pinMode(13,OUTPUT);   // Setting the pin 13 as the output pin.
  digitalWrite(13,LOW); // Making pin 13 low.
  sendData("AT+RST\r\n",2000,DEBUG);            //This command will reset module to default
  sendData("AT+CWMODE=2\r\n",1000,DEBUG);       // This will configure the mode as access point
  sendData("AT+CIFSR\r\n",1000,DEBUG);          // This will get ip address and will show it
  sendData("AT+CIPMUX=1\r\n",1000,DEBUG);       // This will configure the ESP8266 for multiple connections
  sendData("AT+CIPSERVER=1,80\r\n",1000,DEBUG); // This will set the server on port 80
}
void loop() 
{
  if(esp8266.available()) // Checking that whether the esp8266 is sending a message or not (Software UART Data)
  { 
    if(esp8266.find("+IPD,"))
    { 
      delay(1000);        // Waiting for 1 sec
      int connectionId = esp8266.read()-48;   // Subtracting 48 from the character to get the number.
      esp8266.find("pin=");                   // Advancing the cursor to the "pin="
      int pinNumber = (esp8266.read()-48)*10; // Getting the first number which is pin 13
      pinNumber += (esp8266.read()-48);       // This will get the second number. For example, if the pin number is 13 then the 2nd number will be 3 and then add it to the first number
      digitalWrite(pinNumber, !digitalRead(pinNumber)); // This will toggle the pin
      // The following commands will close the connection 
      String closeCommand = "AT+CIPCLOSE="; 
      closeCommand+=connectionId; 
      closeCommand+="\r\n";
      sendData(closeCommand,1000,DEBUG);     // Sending the data to the ESP8266 to close the command
    } 
  } 
}
String sendData(String command, const int timeout, boolean debug) // Function to send the data to the esp8266
{
  String response = ""; 
  esp8266.print(command);           // Send the command to the ESP8266
  long int time = millis();
  while( (time+timeout) > millis()) // ESP8266 will wait for some time for the data to receive
  {
    while(esp8266.available())      // Checking whether ESP8266 has received the data or not
    {
      char c = esp8266.read();      // Read the next character.
      response+=c;                  // Storing the response from the ESP8266
    }
  }
  if(debug)
  { 
    Serial.print(response);         // Printing the response of the ESP8266 on the serial monitor.
  }
  return response;
}

Html code

<html>
<head>
<title>Home Automation System</title>  <!-- This will be the page title -->
</head>
<body> <!-- All the data in it will be shown on the page -->
<button id="11" class="led">Toggle Pin 11</button> <!--  This will create the button for pin 11 -->
<button id="12" class="led">Toggle Pin 12</button> <!--  This will create the button for pin 12 -->
<button id="13" class="led">Toggle Pin 13</button> <!--  This will create the button for pin 13 -->

<script src="jquery.min.js"></script> <!-- This will read the script from the jquery -->
<script type="text/javascript">
$(document).ready(function(){
  $(".led").click(function(){
    var p = $(this).attr('id'); // Getting the id value that which pin to toggle, pin 13 or pin 13 or pin 11
    // Sending the request to the ip address of the server with the pin number to toggle the pin                                                                
    $.get("http://192.168.1.5:80/", {pin:p}); // Sending the get request
  });
});
</script>
</body>
</html>

you set the AT firmware to access point mode. to access some server, it must join your WiFi network in station mode (mode 1)

jasonlloyd:
this is for esp-01 with 4 relay module.

works for me :smiley:

/*********

Rui Santos
  Complete project details at http://randomnerdtutorials.com 
*********/

// Load Wi-Fi library
#include <ESP8266WiFi.h>

// Replace with your network credentials
const char* ssid    = “Guardians of the Gateway”;
const char* password = “**********”;

// Set static IP address
IPAddress ip(192, 168, 1, 222);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);

// Set web server port number to 80
WiFiServer server(80);

// Variable to store the HTTP request
String header;

// Auxiliar variables to store the current output state
String output0State = “off”;
String output1State = “off”;
String output2State = “off”;
String output3State = “off”;

// Assign output variables to GPIO pins
const int output0 = 0;
const int output1 = 1;
const int output2 = 2;
const int output3 = 3;

///////////////////////////////////////////////////////////////////////////////////////////////

void setup() {
  Serial.begin(115200);
 
  // Initialize the output variables as outputs
  pinMode(output0, OUTPUT);
  pinMode(output1, OUTPUT);
  pinMode(output2, OUTPUT);
  pinMode(output3, OUTPUT);
 
  // Set outputs to LOW
  digitalWrite(output0, LOW);
  digitalWrite(output1, LOW);
  digitalWrite(output2, LOW);
  digitalWrite(output3, LOW);

// Connect to Wi-Fi network with SSID and password
  Serial.print(“Connecting to “);
  Serial.println(ssid);
  WiFi.config(ip, gateway, subnet);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(”.”);
  }
  // Print local IP address and start web server
  Serial.println("");
  Serial.println(“WiFi connected.”);
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  server.begin();
}

////////////////////////////////////////////////////////////////////////////////////////////////

void loop(){
  WiFiClient client = server.available();  // Listen for incoming clients

if (client) {                            // If a new client connects,
    Serial.println(“New Client.”);          // print a message out in the serial port
    String currentLine = “”;                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client’s connected
      if (client.available()) {            // if there’s bytes to read from the client,
        char c = client.read();            // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        header += c;
        if (c == ‘\n’) {                    // if the byte is a newline character
          // if the current line is blank, you got two newline characters in a row.
          // that’s the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what’s coming, then a blank line:
            client.println(“HTTP/1.1 200 OK”);
            client.println(“Content-type:text/html”);
            client.println(“Connection: close”);
            client.println();
           
            // turns the GPIOs on and off
            if (header.indexOf(“GET /0/on”) >= 0) {
              Serial.println(“GPIO 0 on”);
              output0State = “on”;
              digitalWrite(output0, LOW);
            } else if (header.indexOf(“GET /0/off”) >= 0) {
              Serial.println(“GPIO 0 off”);
              output0State = “off”;
              digitalWrite(output0, HIGH);
            } else if (header.indexOf(“GET /1/on”) >= 0) {
              Serial.println(“GPIO 1 on”);
              output1State = “on”;
              digitalWrite(output1, LOW);
            } else if (header.indexOf(“GET /1/off”) >= 0) {
              Serial.println(“GPIO 1 off”);
              output1State = “off”;
              digitalWrite(output1, HIGH);
            } else if (header.indexOf(“GET /2/on”) >= 0) {
              Serial.println(“GPIO 2 on”);
              output2State = “on”;
              digitalWrite(output2, LOW);
            } else if (header.indexOf(“GET /2/off”) >= 0) {
              Serial.println(“GPIO 2 off”);
              output2State = “off”;
              digitalWrite(output2, HIGH);
            } else if (header.indexOf(“GET /3/on”) >= 0) {
              Serial.println(“GPIO 3 on”);
              output3State = “on”;
              digitalWrite(output3, LOW);
            } else if (header.indexOf(“GET /3/off”) >= 0) {
              Serial.println(“GPIO 3 off”);
              output3State = “off”;
              digitalWrite(output3, HIGH);
            }
           
            // Display the HTML web page
            client.println("");
            client.println("<meta name=“viewport” content=“width=device-width, initial-scale=1”>");
            client.println("<link rel=“icon” href=“data:,”>");
           
            // CSS to style the on/off buttons
            client.println(“html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}”);
            client.println(".button { background-color: #00b22c; border: none; color: white; padding: 18px;");
            client.println(“text-decoration: none; font-size: 45px; cursor: pointer;}”);
            client.println(".button2 {background-color: #f44242;}");
            client.println(“button.button {width: calc(100% - 20px);}”);
           
            // Web Page Heading
            client.println(“

ESP-01 Web Server

”);
           
            // Display current state, and ON/OFF buttons for GPIO 0 
            client.println(“

Output 1 is " + output0State + “

”);
            // If the output0State is off, it displays the ON button     
            if (output0State==“off”) {
              client.println(”

<a href="/0/on"><button class=“button”>ON

");
            } else {
              client.println("

<a href="/0/off"><button class=“button button2”>OFF

");
            }
             
            // Display current state, and ON/OFF buttons for GPIO 1 
            client.println(“

Output 2 is " + output1State + “

”);
            // If the output1State is off, it displays the ON button     
            if (output1State==“off”) {
              client.println(”

<a href="/1/on"><button class=“button”>ON

");
            } else {
              client.println("

<a href="/1/off"><button class=“button button2”>OFF

");
            }

// Display current state, and ON/OFF buttons for GPIO 2 
            client.println(“

Output 3 is " + output2State + “

”);
            // If the output2State is off, it displays the ON button     
            if (output2State==“off”) {
              client.println(”

<a href="/2/on"><button class=“button”>ON

");
            } else {
              client.println("

<a href="/2/off"><button class=“button button2”>OFF

");
            }

// Display current state, and ON/OFF buttons for GPIO 3 
            client.println(“

Output 4 is " + output3State + “

”);
            // If the output3State is off, it displays the ON button     
            if (output3State==“off”) {
              client.println(”

<a href="/3/on"><button class=“button”>ON

");
            } else {
              client.println("

<a href="/3/off"><button class=“button button2”>OFF

");
            }

// Finish web page
            client.println("");
           
            // The HTTP response ends with another blank line
            client.println();
            // Break out of the while loop
            break;
          } else { // if you got a newline, then clear currentLine
            currentLine = “”;
          }
        } else if (c != ‘\r’) {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }
      }
    }
    // Clear the header variable
    header = “”;
    // Close the connection
    client.stop();
    Serial.println(“Client disconnected.”);
    Serial.println("");
  }
}

Can anyone please help to create this code in the local wifi AP?
i want to connect to my ESP8266 and directly control the relay