NODEMCU is built on ESP8266 12E Module & is an excellent , economical IOT PLATFORM.

Generally it is shipped from factory with NODEMCU FIRMWARE which can be programmed with LUA Script.

In this post we shall install the ARDUINO CORE for ESP8266 on NODEMCU & start exploring how to use the ARDUINO IDE to code NODEMCU

nodemcu_pins1

 

To use the ARDUINO IDE , first thing is to install the ARDUINO CORE on to NODEMCU.

Open the ARDUINO IDE & under FILE –> PREFERENCES paste the link provided below :

Arduino Boards Manager file link is HERE

 

Image 1

under Tools –> BOARDS –> BOARDS MANAGER

search for ESP8266 & then click on it to install the latest version.

Image 2

 

Connect the NODEMCU to the USB of PC & open the DEVICE MANAGER to note the COM PORT allotted.If you see exclamation mark , then install the CP2102 driver for the NODEMCU.

Under Tools –>

select the board as NODEMCU V1.0 (ESP12E) , Port as the COM port alloted, Programmer as USBASP , UPLOAD SPEED as 115200.

Image 3

 

Under FILE –> EXAMPLES –> ESP8266 select the BLINK sketch

Image 4

 

Here the LED_BUILTIN is mapped to the inbuilt LED on board NODEMCU.

So , just click on the UPLOAD button.

Image 5

 

Once the sketch is compiled , the uploading starts , as seen at the bottom of the IDE.

It takes some time to upload ( i.e the reason to select upload speed 115200 , to make it fast)

as the code upload is like upgrading Firmware.

As the sketch is uploaded , the blue LED on the ESP chip (near WIFI antenna) flickers & finally the RED LED on board blinks.

In the next example let us connect an LED at pin D2 through a resistor.

D2 pin is GPIO4 of NODEMCU , but in the code we use the notation Dx to represent GPIO pin.

To control GPIO4 pin we use D2 in the code & not GPIO4 directly.

Image 7

 

In the blink code , the following line is added

#define LED_BUILTIN D2

Image 8

Upload the code to see the LED connected at DS2 blinking.

In the same way we can use ANALOG WRITE function to give a fade effect on the LED.

 

Now let us explore the NETWORKING capabilities of NODEMCU.

To start with we include the ESP8266WiFi.h header file.

The WIFI library is enhanced version of Arduino & is installed along with the Boards Manager.

This ESP8266WiFi  library has several classes, methods and properties  , which combines many powerful FUNCTIONS .

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.

After the header file  , we store the SSID name & Password as character arrays

#include <ESP8266WiFi.h>
char ssid[]=”SSID_NAME”;
char pass[]=”Password”;

Inside the setup function , Serial communication is started with 9600 baud & the name of SSID is printed.

void setup() {
Serial.begin(9600);
delay(500);
Serial.print(“Connecting to..”);
Serial.println(ssid);
delay(500);

WiFi.disconnect() function is called to disconnect any previous connections

While using the WiFi CLASS , there is no need to create WiFi Object.Simply call the function like

begin(),  SSID(), status(), disconnect(), scanNetworks(), localIP(), etc..

Actual connection to Wi-Fi is initialized by calling the begin() function.We pass on the 2 parameters to WiFi.begin() function to start the connection

  WiFi.disconnect();
WiFi.begin(ssid,pass);

We use a While loop to test the status of WiFi.status(). Here we check whether it returns WL_CONNECTED

IF not connected , a dot is printed every 500 msec.

while(WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.println(“.”);
}

Once connection is established , it jumps out of loop & prints the SSID name connected with.

if connection is established, and then lost for some reason, ESP will automatically reconnect to last used access point once it is again back on-line. This will be done automatically by Wi-Fi library, without any user intervention.

Serial.print(“SSID…”);
Serial.println (WiFi.SSID());
Serial.println(“Successfully connected!!!”);

 

Next the IP address allotted to the NODEMCU by the Router  (by DHCP method) is printed by calling the function WiFi.localIP()

The MAC address is printed using WiFi.macaddress()

 

Serial.print(“IP Address allotted to NodeMcu ESP..”);
Serial.println(WiFi.localIP());

Serial.print(“MAC Address of ESP…”);
Serial.println(WiFi.macAddress());

WiFi.printDiag(Serial) prints all the WiFi credentials like Channel , SSID name , Password , etc..

This specific function  prints out key Wi-Fi diagnostic information:

WiFi.printDiag(Serial);
}

void loop()
{
}

Upload the code & open the Serial monitor at baud 9600 to see the result.

 

Image 10

Next example is to scan the surrounding networks & print the name of SSIDs.

Here we use the function scanNetworks() which returns an Integer value on the number of surrounding SSIDs.

We assign this to an integer variable

int n = WiFi.scanNetworks();

& then in a For loop iterate to print out name of each surrounding SSIDs

 

for (int i=0; i<n; i++)

{

Serial.println(  WiFi.SSID (i) )

}

Image 11

 

Scan for available Wi-Fi networks in one run and return the number of networks that has been discovered.

WiFi.scanNetworks();

There is on Overload of this function that accepts two optional parameters to provide extended functionality of asynchronous scanning as well as looking for hidden networks

WiFi.scanNetworks(async,show_hidden);

Both function parameters are of boolean type. They provide the flowing functionality: * asysnc – if set to true then scanning will start in background and function will exit without waiting for result. To check for result use separate function scanComplete . * show_hidden – set it to true to include The in scan result networks with hidden SSID.

 

Image12

Download code samples HERE

Download ESP8266 Library document PDF HERE

VIDEO TUTORIALS :

 

 

cooltext753793315   cooltext753790696

Advertisements