GPIO Control by ESP as Web Server-IOT

Hello there folks, welcome to the second part of the home automation ESP 8266 tutorial. In the previous post, we are able to set up our ESP and burned our Arduino firmware into that chip. Well in this post, I will show you how to control the GPIO through Wifi. Let’s get started.

Things we need

If you have followed my previous post, then you pretty much know the requirements of this project. But if you don’t, here are the things required in this project.

Let’s build it

Let’s just cut through the crap and start building it. First setup the connection. I am not going to tell you how to do this as we have covered this in the previous post. Open your Arduino IDE and select the right board and COM port.

We will start the project by creating an object of Server and Client. The parameter in the object of the server is the port number of the server.

WiFiClient client;
WiFiServer server(80);

Next up is the configuration of Access Point. Create two constant variables to store the name of SSID and the password of your choice and pass these two as an argument of a function. The return type of this function is Boolean and it returns “True” or “False” depending on the result of setting the Access Point.

boolean result = WiFi.softAP(ssid, password);

If this set up is successful, then call a function to return the IP address of the Access Point.

IPAddress myIP = WiFi.softAPIP();

Now I am starting the server.


Connecting a client and Reading the value

Now, here I am waiting for the client to connect. The function server.avaliable() does this for us. It returns false if there is no client and returns an object if there is a client connected.  If there is a connection, I have run a while loop because you don’t want to lose the connection. The function inside the while loop returns true if there is a connection.


If there is a client available till now, we want to read the data is sending. The function client.readStringUntil(‘\r’) does this for us. It reads the data until “enter”. And here I am comparing and toggling the value of the LED.

String req = client.readStringUntil('\r');

Full code

#include < ESP8266WiFi.h > 
#include < ESP8266WebServer.h >

const char * ssid = "ESP8266";
const char * password = "ESP8266Test";// wifi name and password
int z = 2;

WiFiClient client;
WiFiServer server(80);  // client and server object
void wificonnection() {
  boolean result = WiFi.softAP(ssid, password);
  if (result == true) {
    IPAddress myIP = WiFi.softAPIP();

    Serial.print("WiFi network name: ");
    Serial.print("WiFi network password: ");
    Serial.print("Host IP Address: ");
  } //Print the local IP
  else {
    Serial.println("error! Something went wrong...");
  Serial.println("Server started");
void readdata(){
  client = server.available();
  if (!client) {
  if (client) {
    while (client.connected()) {  // don't want to break the connection
        if (client.available()) {
            String req = client.readStringUntil('\r');

            if (req[0] == '0') {
                digitalWrite(1, !digitalRead(1)); //toggling the pin everytime
void setup() {
  pinMode(1, OUTPUT); / the blue LED of ESP is mapped to 1st pin
void loop() {

I think I have explained everything in this code. I have also commented the code for your assistance. If there is still any problem, you can ask me in the comment section down below.

Executing the Program

I have opened a terminal software named Hercules. You can use Putty too. There I have selected a TCP-IP Protocol, selected a right IP address and port number. There in the “message-box”, send ‘0’, wait for a sec or two and see the state of the LED gets changed.

sending ‘0’ to toggle the blue led.

Please note that it’s taking two or three seconds to execute your command. i.e., to toggle the LED.

Home autonation with ESP Home autonation with ESP

Share This
Scroll to Top