You are here
Home > IoT Experiments >

Experiment-8 : To configure nodeMCU ESP8266 12-E in station mode

AIM : To configure nodeMCU ESP8266 12-E in station mode, and indicate the connected status on the LED.

Objective:

  1. To configure ESP8266 as a station
  2. To establish communication between Access point and the station nodeMCU ESP8266 station.
  • To blink the LED while searching for SSID and continuously glow when connected

Requirement:

  1. Breadboard
  2. NodeMCU ESP8266 12-E
  3. One LED to indicate connected status
  4. One 220 Ohm Resistor

Theory:

Mode of Operation of ESP8266 –

nodeMCU ESP8266 can operate in the following modes:

ESP 8266 nodeMCU in station Mode
  1. Station (STA) Only
  2. Soft Access Point (Soft AP)
  3. Both the Soft Access Point and Station (Soft AP +STA)

To play around with the ESP8266 WiFi module, you can program it in two modes: Station (STA) or Access Point (AP).

Station (STA) Mode

In station mode, ESP8266 will act just like your smartphone or laptop. It will connect to an existing WiFi channel, or in most cases, the WiFi advertised by your router.

Once you have programmed ESP8266 in STA mode and you are successfully connected to stable WiFi, you can access any web page on the internet.

 Configuring ESP8266 in Station Mode

In station Mode, we need to assign our Access Point (AP) or mobile Hot-spot SSID and password to the ESP8266 code.   The station when uploaded with the code will search for the SSID of the hot-spot and if connected will show the status as connected.

ESP8266 AP station

/*

The code below when uploaded on the ESP8266 will search for the SSID of  */

/*

* This code will configure ESP8266 in station mode, once uploaded will connect with a WiFi access point / mobile hot-spot.

*/

#include <ESP8266WiFi.h>

//Here we specify the SSID and Password of the Local WiFi Network or hotspot

const char* ssid = “moto”; //“localNetwork_wifi_ssid”

const char* password = “12345678”; //”your_wifi_password”

uint8_t retries=0;

void setup()

{

//Start the serial communication with the computer

pinMode(D1, OUTPUT);

Serial.begin(115200);

delay(100);

Serial.println();

//Try and Connect to the Network

WiFi.begin(ssid,password);

Serial.print(“Connecting to “);

Serial.print(ssid);

Serial.println(“…”);

//Wait for WiFi to connect for a maximum timeout of 20 seconds

while(WiFi.status()!=WL_CONNECTED && retries<20)

{

Serial.print(“.”);

digitalWrite(D1, HIGH);               //Blink LED to indicate searching WiFi

delay(100);

digitalWrite(D1, LOW);

delay(100);

retries++;

delay(500);

}

Serial.println();

//Inform the user whether the timeout has occured,

if(retries= =20)                 //Timeout has occured

{

Serial.print(“Unable to Connect to “);

Serial.println(ssid);

digitalWrite(D2, HIGH);    // red LED to indicate network not available

}

//Check if ESP8266 is connected to the local network WiFi access point

}

void loop() {

// Maintain your WiFi connection by checking its status before performing any internet related task

if (WiFi.status()==WL_CONNECTED)

{

//EP8266 is connected to WiFi Access Point. You can access Internet or any Web Server

Serial.println(“Connected…”);

digitalWrite(D1, HIGH);                        // status Green LED ON

delay(1000);

}

else

{

//ESP8266 is not connected to any WiFi network.

//You need to wait for the connection before you start interacting with any web server

Serial.print(“Trying to connect with “);

Serial.print(ssid);

while(WiFi.status()!=WL_CONNECTED)

{

Serial.print(“.”);

digitalWrite(D2, HIGH);                //Red LED on to indicate no network

delay(1000);

}

Serial.println();

Serial.print(“Sucessfully Connected to “);

Serial.println(ssid);

Serial.print(“IP Address: “);

Serial.println(WiFi.localIP());

}

}

Output:

Observe the status LED green and red to indicate network status. The green LED blinks during network SSID search and then turns continuously ON after establishing connection. The red LED will turn on if no network established. Status of connection is also displayed on the serial Monitor.

Summary :

In this experiment we learnt how to configure ESP8266 nodeMCU in station mode and the working of a station when connecting to a hotspot(Access point) by blinking the RED and GREEN LEDs

Viva Questions:

  1. Briefly explain three mode of operation of the ESP8266

 

 

  1. Write the steps involved in writing code for ESP8266 as a station.

 

 

  1. Explain the use of the conditional statements

while(WiFi.status() !=WL_CONNECTED){…..}

 

Leave a Reply

Top
error: Content is protected !!