SETUP WiFi CONNECTION IN NODEMCU

After successful installation of ESP boards on Arduino IDE, We need to connect our NodeMCU to internet so that we can send our data to web server. For this we need to connect our NodeMCU to WiFi. To setup WiFi connection, we need to write a couple of lines of code and upload it in NodeMCU.

You can connect your NodeMCU to either broadband connection or Mobile hotspot but NodeMCU supports 802.11n (2.4GHz) as in data sheet.

QUICK START

  1. As you installed ESP8266 Board package from Board Manager you don’t need to include basic library for NodeMCU
  2. Select board you are using under Tools → Boards, here I am using NodeMCU 0.9 (ESP-12 Module).
  3. Same as for Arduino, select communication port (COM) under Tools → Port, on which NodeMCU connected.

PROGRAM CODE

To hook up ESP module to Wi-Fi (like hooking up a mobile phone to a hot spot), you need just couple of lines of code.

Note: If your ESP8266 and ESP8266HTTPClient library colour doesn’t change it means didn’t selected correct board

#include<ESP8266WiFi.h>
#include<ESP8266HTTPClient.h>

HTTPClient http;

const char* WIFINAME = "YOUR_WIFINAME/SSID";
const char* PASS = "YOUR_SSID_PASSWORD";
//int led = D0;
WiFiServer server(80);

void setup() {
  Serial.begin(115200);
  delay(10);
  Serial.println("");
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.print("Connecting To ");
  Serial.println(WIFINAME);


  WiFi.begin(WIFINAME, PASS);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi Connected");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop(){
  
}

CODE EXPLANATION

WiFiServer server(80)

This function creates a server that listens to the specified port for the incoming connections.

WiFiServer server(80);

The default HTTP port is 80, here Web server is on port 80, you can also use other ports, you need to enter port number after IP address to open the web page, with different port number. E.g. You must type 192.168.2.2.2:81 in your browser for port number 81.

ESP8266WiFi LIBRARY

ESP8266 is all about Wi-Fi. If you are eager to connect your new ESP8266 module to Wi-Fi network to start sending and receiving data, this is a good place to start.

In the first line of sketch

#include<ESP8266WiFi.h>

We are including ESP8266WiFi library. This library provides ESP8266 specific Wi-Fi routines we are calling to connect to network.

ESP8266HTTPClient LIBRARY

Then, we need the ESP8266HTTPClient library, which provides methods to send HTTP requests.

WiFi.begin()

Actual connection to Wi-Fi is initialized by calling

  WiFi.begin(WIFINAME, PASS);

In the line WiFi.begin(“network-name”, “pass-to-network”)
replace network-name and pass-to-network with name and password to the Wi-Fi network you like to connect. Then upload this sketch to ESP module and open serial monitor.
You should see something like:

Wifi begin

WiFi CONNECTION PROCESS

Connection process can take couple of seconds and we are checking for this to complete in the following loop:

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

WiFi.status()

Some functions provide more than just a binary status information. A good example is WiFi.status()

wifi status

WiFi.localIP()

The last line will then print out IP address assigned to ESP module

  Serial.println(WiFi.localIP());

 

 

4.3 3 votes
Article Rating
Subscribe
Notify of
guest
2 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
sagar
sagar
4 years ago

useful

Pedro Moraes
Pedro Moraes
9 days ago

Interessante o que acabei de ler aqui, existe muitos artigos em seu blog relacionado a este que acebei de ler gostei de seu blog. Cine vision filmes e series

2
0
Would love your thoughts, please comment.x
()
x