Getting Started With ESP8266(LiLon NodeMCU V3) Complete Guide for IoT Startup With Example(as Server)

273,173

276

75

Introduction: Getting Started With ESP8266(LiLon NodeMCU V3) Complete Guide for IoT Startup With Example(as Server)

Things You Need:


  • NodeMCU Flasher Master (Flasher)
  • NodeMCU Firmware (Firmware)
  • Latest Arduino IDE (Arduino)
  • NodeMCU V3
  • Micro USB Cable
  • LED

Features

  • Open-source, Interactive, Programmable, Low cost, Simple, Smart, WI-FI enabled
  • Arduino-like hardware IO
  • Lowest cost WI-FI


Step 1: Installing the Firmware

In NodeMCU Boards the first thing you need is to install the Firmware to the board

the following method works for all NodeMCU Boards

  1. Open the NodeMCU flasher master folder than open the win32/win64 folder as your computer. now open the folder Release than double click ESP8266Flasher.
  2. Select the COM Port.
  3. Goto config tab
  4. click on the small gear and open up the firmware which you have downloaded
  5. go to the advenced tab and select the desired Baudrate
  6. Goto the Operation tab and click on Flash Button.

Step 2: Preparing the Arduino IDE

After Installing the firmware you are ready to do the programming with the ESP8266

  1. Install the Arduino IDE
  2. open the Arduino IDE from the desktop icon
  3. Click on File tab and than open preferences
  4. In the additional Boards Manager URLs add the following link (http://arduino.esp8266.com/stable/package_esp8266com_index.json) and click OK
  5. Goto Tools>Borads>Boards Manager
  6. In the search field type esp8266 click the esp8266 by ESP8266 Community option and click Install

Step 3: Code...

Now you can do whatever you want with your NodeMCU board
Following is an example for led blinking with NodeMCU board via webserver

  • In arduino IDE goto tools>Boards>select NODEMCU 1.0 (ESP - 12E Module)
  • again goto tools and select port.
  • Change the Wifi name and password from the following code.
  • Now click on Upload button to upload the following code.
  • Connect the led's positive leg on D9 pin of board and negative to the ground of the code.
  • Power up the board and open the serial monitor from arduino IDE
  • after connecting to the wifi it will show you the IP address.
  • type that IP address on the web browser(Edge, Chrome, Firefox etc..)
  • A webpage will open you can change the status of LED by turning it ON or OFF.
#include<ESP8266WiFi> 

const char* ssid = "Tenda"; //your WiFi Name
const char* password = "12345678";  //Your Wifi Password
int ledPin = 03; 
WiFiServer server(80);
void setup() {
  Serial.begin(115200);
  delay(10); 
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW); 
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid); 
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected"); 
  server.begin();
  Serial.println("Server started");
  Serial.print("Use this URL to connect: ");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/"); 
}

void loop() {
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  Serial.println("new client");
  while(!client.available()){
    delay(1);
  } 
  String request = client.readStringUntil('\r');
  Serial.println(request);
  client.flush(); 
  int value = LOW;
  if (request.indexOf("/LED=ON") != -1)  {
    digitalWrite(ledPin, HIGH);
    value = HIGH;
  }
  if (request.indexOf("/LED=OFF") != -1)  {
    digitalWrite(ledPin, LOW);
    value = LOW;
  }
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println(""); 
  client.println("");
  client.println("");
  client.print("Led is : "); 
  if(value == HIGH) {
    client.print("On");
  } else {
    client.print("Off");
  }  client.println("");  
client.println(" On ");
   client.println(" Off ");  
  client.println(" ");
     delay(1);
   Serial.println("Client disonnected");
  Serial.println("");   }
//code copied from link

If you encounter any problem comment it down

Make it Glow Contest 2016

Participated in the
Make it Glow Contest 2016

Arduino Contest 2016

Participated in the
Arduino Contest 2016

IoT Builders Contest

Participated in the
IoT Builders Contest

4 People Made This Project!

Recommendations

  • Chocolate Challenge

    Chocolate Challenge
  • Electronics Contest

    Electronics Contest
  • Stone Concrete Cement Contest

    Stone Concrete Cement Contest

75 Comments

0
DaveJ37
DaveJ37

3 years ago

Hey all, corrected code which works for a LoLin NodeMCU v3 with the LED connected between D0 (which is GPIO 16 in software) and then in series with a 330 ohm resistor to ground. Hope this assists:

#include <ESP8266WiFi.h>
const char* ssid = "your_network_ssid";
const char* password = "your_network_pass";
int ledPin = 16; // GPIO16
WiFiServer server(80);
void setup() {
Serial.begin(115200);
delay(10);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.print("Use this URL to connect: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
}
void loop() {
// Check if a client has connected
WiFiClient client = server.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("/LED=ON") != -1) {
digitalWrite(ledPin, HIGH);
value = HIGH;
}
if (request.indexOf("/LED=OFF") != -1) {
digitalWrite(ledPin, LOW);
value = LOW;
}
// Set ledPin according to the request
//digitalWrite(ledPin, value);
// Return the response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // do not forget this one
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.print("Led pin is now: ");
if(value == HIGH) {
client.print("On");
} else {
client.print("Off");
}
// HTML for buttons to work LED
client.println("<br><br>");
client.println("<a href=\"/LED=ON\"\"><button>Turn On </button></a>");
client.println("<a href=\"/LED=OFF\"\"><button>Turn Off </button></a><br />");
client.println("</html>");
delay(1);
Serial.println("Client disconnected");
Serial.println("");
}

0
Nunya7399
Nunya7399

Reply 5 weeks ago

Still working 3 years later, thank you!

0
MiguelC72
MiguelC72

Reply 7 months ago

Excellent! it works :)
Thanks a lot!!!

0
faester.paranormal
faester.paranormal

Reply 1 year ago

Your editing works with reversed buttons and no LED turning on, since it has to be pin 2 and not 16.

The ESP8266 has a builtin led, that is attached to D4 as labeled on LoLin boards, which maps to GPIO2. One thing to Note, is that the led is active low. In other words ... setting PIN 2 to '0' will turn the LED ON and setting PIN 2 to '1' will turn the LED OFF
0
rolandas3
rolandas3

Reply 2 years ago

Thanks, it was great!

0
Nurul Hazwani
Nurul Hazwani

Question 2 months ago

I had followed all the instructions given.. But, why it state that no such file or directory?

Screenshot 2022-03-05 164155.jpg
0
benmeulenbeld2007
benmeulenbeld2007

Question 6 months ago

Executable segment sizes:
ICACHE : 32768 - flash instruction cache
IROM : 247172 - code in flash (default or ICACHE_FLASH_ATTR)
IRAM : 27273 / 32768 - code in IRAM (IRAM_ATTR, ISRs...)
DATA : 1508 ) - initialized variables (global, static) in RAM/HEAP
RODATA : 1144 ) / 81920 - constants (global, static) in RAM/HEAP
BSS : 25784 ) - zeroed variables (global, static) in RAM/HEAP
De schets gebruikt 277097 bytes (26%) programma-opslagruimte. Maximum is 1044464 bytes.
Globale variabelen gebruiken 28436 bytes (34%) van het dynamisch geheugen. Resteren 53484 bytes voor lokale variabelen. Maximum is 81920 bytes.
esptool.py v3.0
Serial port COM1
Connecting........_____....._____....._____....._____....._____....._____....._____
Traceback (most recent call last):
File "C:\Users\myname\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.0.2/tools/upload.py", line 66, in <module>
esptool.main(cmdline)
File "C:/Users/myname/AppData/Local/Arduino15/packages/esp8266/hardware/esp8266/3.0.2/tools/esptool\esptool.py", line 3552, in main
esp.connect(args.before, args.connect_attempts)
File "C:/Users/Ben Meulenbeld/AppData/Local/Arduino15/packages/esp8266/hardware/esp8266/3.0.2/tools/esptool\esptool.py", line 529, in connect
raise FatalError('Failed to connect to %s: %s' % (self.CHIP_NAME, last_error))
esptool.FatalError: Failed to connect to ESP8266: Timed out waiting for packet header
esptool.FatalError: Failed to connect to ESP8266: Timed out waiting for packet header
Ongeldige bibliotheek gevonden in C:\Users\Myname\Documents\Arduino\libraries\ESP8266WiFi: no headers files (.h) found in C:\Users\myname\Documents\Arduino\libraries\ESP8266WiFi






0
dynamo766
dynamo766

7 months ago on Step 3

Uh ok, got to the web page and all it says is LED is ON OFF ON. How are u supposed to control it?????? Why wouldn't this info be in the article c'mon.

0
Saimak
Saimak

Question 9 months ago on Step 3

I am facing error issue in installation of esp8266 plz help me by sending links of 3.0.1 or 3.0.2 as I solve this error by puting any link in reference.

0
srinaath.ucal
srinaath.ucal

3 years ago

When I upload it gives an error
Arduino: 1.8.9 (Windows Store 1.8.21.0) (Windows 10), Board: "NodeMCU 1.0 (ESP-12E Module), 80 MHz, Flash, Disabled, 4M (no SPIFFS), v2 Lower Memory, Disabled, None, Only Sketch, 115200"
sketch_apr16a:4:1: error: 'WiFiServer' does not name a type
WiFiServer server(80);
^
D:\Automation\New development\sketch_apr16a\sketch_apr16a.ino: In function 'void setup()':
sketch_apr16a:14:3: error: 'WiFi' was not declared in this scope
WiFi.begin(ssid, password);
^
sketch_apr16a:15:27: error: 'WL_CONNECTED' was not declared in this scope
while (WiFi.status() != WL_CONNECTED) {
^
sketch_apr16a:21:3: error: 'server' was not declared in this scope
server.begin();
^
D:\Automation\New development\sketch_apr16a\sketch_apr16a.ino: In function 'void loop()':
sketch_apr16a:30:3: error: 'WiFiClient' was not declared in this scope
WiFiClient client = server.available();
^
sketch_apr16a:30:14: error: expected ';' before 'client'
WiFiClient client = server.available();
^
sketch_apr16a:31:8: error: 'client' was not declared in this scope
if (!client) {
^
sketch_apr16a:35:10: error: 'client' was not declared in this scope
while(!client.available()){
^
sketch_apr16a:38:20: error: 'client' was not declared in this scope
String request = client.readStringUntil('\r');
^
exit status 1
'WiFiServer' does not name a type
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

error.jpg
0
DaveJ37
DaveJ37

Reply 3 years ago

Hi Srinath,
your code above does not show the include statement for the library at the start. Not having included the ESP8266WiFi.h file in a preprocessor command at the start of your code would produce errors like what you are seeing. If it is not already there, try putting the line
#include <ESP8266WiFi.h>
at the beginning of your code.

0
hajar200842
hajar200842

Reply 1 year ago

Excuse me but I did write the library directory at the top of the code but I still get the same result as him.

0
hajar200842
hajar200842

1 year ago

sketch_may19a:6:1: error: 'WiFiServer' does not name a type
6 | WiFiServer server(80);
| ^~~~~~~~~~
C:\Users\ASUS\Desktop\sketch_may19a\sketch_may19a.ino: In function 'void setup()':
sketch_may19a:16:3: error: 'WiFi' was not declared in this scope
16 | WiFi.begin(ssid, password);
| ^~~~
sketch_may19a:17:27: error: 'WL_CONNECTED' was not declared in this scope
17 | while (WiFi.status() != WL_CONNECTED) {
| ^~~~~~~~~~~~
sketch_may19a:23:3: error: 'server' was not declared in this scope
23 | server.begin();
| ^~~~~~
C:\Users\ASUS\Desktop\sketch_may19a\sketch_may19a.ino: In function 'void loop()':
sketch_may19a:32:3: error: 'WiFiClient' was not declared in this scope
32 | WiFiClient client = server.available();
| ^~~~~~~~~~
sketch_may19a:33:8: error: 'client' was not declared in this scope
33 | if (!client) {
| ^~~~~~
sketch_may19a:37:10: error: 'client' was not declared in this scope
37 | while(!client.available()){
| ^~~~~~
sketch_may19a:40:20: error: 'client' was not declared in this scope
40 | String request = client.readStringUntil('\r');
| ^~~~~~


When I tried to compile, it give me this. How can I fix it so that I can compile it without error?

0
sasmadar
sasmadar

Tip 1 year ago

Great description, thank You very, it helped me a lot!

Just one remark: It did not work for the first completely: it connected to my WiFi router, but did not respond to any browser requests. Solution: the DNSServer library was missing. If you have similar problem, add this line above your code:
#include <DNSServer.h>

0
Metaripley
Metaripley

Reply 1 year ago

I added the #include"DNSServer.h" but still get the error of "This site can’t be reached".
Any more tips?

0
TalhaF3
TalhaF3

1 year ago

HI, CAN ANYONE RESOLVE THIS ISSUE?

Arduino: 1.8.9 (Windows 10), Board: "Generic ESP8266 Module, 80 MHz, Flash, Legacy (new can return nullptr), All SSL ciphers (most compatible), dtr (aka nodemcu), 26 MHz, 40MHz, DOUT (compatible), 1MB (FS:64KB OTA:~470KB), 2, nonos-sdk 2.2.1+100 (190703), v2 Lower Memory, Disabled, None, Only Sketch, 115200"

In file included from c:\programdata\matlab\supportpackages\r2018a\3p.instrset\arduinoide.instrset\idepkgs\packages\esp8266\tools\xtensa-lx106-elf-gcc\2.5.0-4-b40a506\xtensa-lx106-elf\include\c++\4.8.2\algorithm:60:0,

from C:\ProgramData\MATLAB\SupportPackages\R2018a\3P.instrset\arduinoide.instrset\idepkgs\packages\esp8266\hardware\esp8266\2.7.4\cores\esp8266/Arduino.h:238,

from sketch\sketch_sep18a.ino.cpp:1:

c:\programdata\matlab\supportpackages\r2018a\3p.instrset\arduinoide.instrset\idepkgs\packages\esp8266\tools\xtensa-lx106-elf-gcc\2.5.0-4-b40a506\xtensa-lx106-elf\include\c++\4.8.2\utility:68:28: fatal error: bits/c++config.h: No such file or directory

#include <bits/c++config.h>

^

compilation terminated.

exit status 1
Error compiling for board Generic ESP8266 Module.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

0
Lahiru Madushanka
Lahiru Madushanka

1 year ago

its worked.but when i push off button led is ON.when i push on button led is OFF.how ever its work.thank you.

0
95NJ
95NJ

Question 1 year ago on Step 3

Hello
After uploading the code successfully, the serial monitor just keeps printing "." which it does according to the code when connected if I understand this part of the code correctly: while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");

I tried connecting the pin to ground, and also tried connecting DI0 to ground since I have a V3. From my understanding the network should show up, when I scan for WiFi networks, though nothing comes up.

I don't know if I'm supposed to change the code or what the problem is...

0
marcelruas
marcelruas

Answer 1 year ago

Hi,

don't know if you've corrected the problems on the code above; I made the following changes and it worked here:
- add the ".h" at the end of the name of the ESP8266.WiFi library, as pointed out in some comments below;
- change the number of the pin; in the sketch above is "03", it should be D3;
- check the baud rate in the serial monitor and chage it accordingly (I've changed the code from to "Serial.begin(9600)" here).

Also added a part of a cool code posted bellow to add buttons to the page (in the end of the void loop, just before the last "}"):

// HTML for buttons to work LED
client.println("

");
client.println("Turn On ");
client.println("Turn Off
");
client.println("");
delay(1);
Serial.println("Client disconnected");
Serial.println("");
// end of insert

Good luck over there