Blog
esp8266 nodeMCU
Overview About ESP8266
ESP8266 is a low-cost, highly integrated Wi-Fi chip developed by Espressif Systems in Shanghai, China. Thanks to its low price and rich features, it’s widely used in the IoT field.ESP8266 has greatly lowered the threshold for IoT devices to connect to the internet.
Introduction About WIFI
First off, let’s talk about WIFI technology.WIFI network is wireless communication network, also known as wireless broadband. Normally, WIFI can work within a 100-meter range, but due to factors like transmission power and the environment, the typical indoor range is around 10 meters, and outdoors it’s about 30 meters. What’s more, WIFI has a pretty high transmission rate, up to 600Mb/s.
WIFI technology is a way of transmitting data via electrical signals over radio waves. It sets up a local area network so that various devices can connect wirelessly, enabling wireless data transfer between them.
esp32 vs esp8266
ESP32 is more powerful than ESP8266. Its dual-core 240MHz processor is 10 times faster than ESP8266’s single-core 80MHz one. It supports dual-band WiFi and dual-mode Bluetooth, has more peripherals , and better power control. It’s great for complex, multi-functional scenarios and has more GPIOs. ESP8266, on the other hand, only has single-band WiFi, no Bluetooth, and basic performance. But it wins with its low cost, making it perfect for simple WiFi connection needs.
Pros and Cons
ESP32 is better than ESP8266 in terms of performance, wireless capabilities, peripheral expandability, and power control, so it’s good for complex scenarios. ESP8266, though, has the advantage of being cheap, which suits basic WiFi connection needs.
esp8266 pinout
nodeMCU esp8266
ESP8266 and NodeMCU are two terms often heard in IoT development. They’re closely related but not the same. Here’s how they differ and connect
Differences
ESP8266: It’s a specific Wi-Fi chip developed by Espressif. It’s an integrated circuit that includes core components like a processor and a Wi-Fi radio module, and it’s the key hardware for wireless network connectivity.
NodeMCU: It’s a development board with the ESP8266 chip at its core. It integrates the ESP8266 chip, power management circuits, a USB-to-serial chip, and other components on a circuit board, and brings out standardized GPIO interfaces to make it easy for developers to use directly.
Relationship
NodeMCU is a development board designed based on the ESP8266 chip, and its core functions depend on the performance of ESP8266 (like Wi-Fi connection, processor capability, etc.).
Simply say ESP8266 is the core chip, and NodeMCU is the development board based on that chip.
Features
ESP8266 | NodeMCU |
Small size, suitable for embedding | Larger size, with standardized interfaces and a USB debugging port |
Needs additional peripheral circuits to work | Directly use, no need for extra circuit design |
Suitable for integration in mass-produced products | Better for beginner |
Needs programming via serial port or special tools | Supports direct program burning via USB, more convenient |
How to choose?
If you’re learning IoT prototype development, the NodeMCU development board is a better choice because it simplifies hardware design and you can start using it directly.
If you’re mass-producing products or need a miniaturized design, you might use the ESP8266 chip directly, along with custom peripheral circuits, to reduce cost and size.
ESP8266 working modes
ESP8266 supports three working modes: STA, AP, and AP+STA.
STA mode
In this mode, module can connect to wireless networks provided by other devices. For example, it can connect to a router via a wireless network to access the internet, allowing phones or computers to remotely control the device over the internet.
Simply say, in this mode, the module is like a user. It needs to connect to an external WIFI (or your own hotspot) and communicate with other devices through that connected WIFI.
AP mode
AP mode is default mode. In this mode, the module acts as a hotspot for other devices to connect, let phones or computers communicate directly with the module and enable wireless control within a local area network.
in this mode, the module is like providing a hotspot, creating a small local area network. Any device that connects to it can communicate with it.
STA+AP mode
It’s a mode that combines two. It allows seamless switching for internet control, making operation easy. The module can both connect to wireless networks provided by other devices and act as a hotspot for other devices to connect, enable seamless switching between wide area networks and local area networks for convenient use.
Debugging methods
ESP8266 is a pretty mature module, and as mentioned earlier, it can communicate via serial port.Because can do serial communication, we can use the following three methods for debugging
USB to TTL tool
You’re probably familiar with this. We usually use it to print microcontroller logs.
We can also use it to debug some modules with the help of upper computers.
If esp8266 can’t start 3.3V, it might be because the 3.3V of the USB-to-TTL module isn’t really 3.3V. Just connect the 3.3V pin of ESP8266 to the 5V pin of the USB-to-TTL.
If the ESP8266 gets so hot that you can’t touch it, unplug it. If the temperature is bearable, it’s fine. ESP8266 isn’t that fragile.
After wiring it up, connect the USB end to the computer, open the serial port debugging assistant, and you can start debugging.
USB to ESP8266 WIFI module
The USB-to-TTL module above requires you to wire it up yourself, which is a bit of a hassle. And if you get one wire wrong, you can’t debug properly.
But this module is specifically designed for debugging ESP8266. You don’t need to wire up each pin one by one. Just plug the ESP8266 into it, and it’s super convenient. Similarly, connect the USB end to the computer, open the serial port debugging assistant, and you can debug.
Microcontroller debugging
This is the most commonly used method in projects. You need to connect the ESP8266 to any serial port of the microcontroller, then write code to drive the ESP8266.
esp8266 tutorial for beginners
Hardware preparation
1.Arduino
2.CH340 module driver
3.ESP8266 development board
esp8266 Arduino
- Download and install Arduino IDE
Arduino official website : https://docs.arduino.cc/
2.Add the ESP8266 board manager URL to Arduino IDE
Open Arduino IDE, click File -> Preferences , and add the following URL in the “Additional Boards Manager URLs”
http://arduino.esp8266.com/stable/package_esp8266com_index.json
3. Restart Arduino IDE
After adding the URL, close Arduino IDE and then reopen it.
4.Download and install the ESP8266 library
Click Tools -> Board -> Board Manager, find ESP8266 and install it.
After the download and installation are complete, you’ll be able to see the ESP8266 library. reset Arduino IDE before using it
ESP8266 Code
#include
#include // Include servo library
Servo myservo; // Create servo object
#define relayPin 16 // servo connected to GPIO2 of ESP8266
const char *ssid = "1405";// User WiFi, modify WiFi SSID when using
const char *password = "YX14051405";// WiFi Password you want to connect
const char *host = "192.168.1.214";// Modify IP address of your Server
WiFiClient client;
const int tcpPort = 8266;// Modify port number of your Server
void setup()
{
Serial.begin(9600);
pinMode(relayPin,OUTPUT);
delay(10);
Serial.println();
Serial.println();
Serial.print("Connecting to ");// Write some prompts
Serial.println(ssid);
WiFi.begin(ssid, password);
myservo.attach(15);
while (WiFi.status() != WL_CONNECTED)// WiFi.status() is the WiFi connection status, returns the connection status
{
delay(500);
Serial.print(".");
}// Send ..... to serial port if not connected
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP()); // WiFi.localIP() returns the IP address obtained by 8266
}
void loop()
{
while(!client.connected()) // Several exception handling for disconnection
{
if (!client.connect(host, tcpPort))
{
Serial.println("connection....");
//client.stop();
delay(500);
}
}
while(client.available())
{
char buff[99];
size_t counti = client.available();
client.readBytes(buff,counti);
if(buff[0]=='O')
if(buff[1]=='p')
if(buff[2]=='e')
if(buff[3]=='n')
{
digitalWrite(relayPin, LOW);
Serial.println("open");
myservo.write(0);
client.println("The LED is open!");
delay(10);
}
if(buff[0]=='C')
if(buff[1]=='l')
if(buff[2]=='o')
if(buff[3]=='s')
if(buff[4]=='e')
{
digitalWrite(relayPin, HIGH);
Serial.println("close");
myservo.write(180);
client.println("The LED is close!");
delay(10);
}
if(buff[0]=='R')
if(buff[1]=='o')
if(buff[2]=='t')
if(buff[3]=='a')
if(buff[4]=='t')
if(buff[5]=='e')
{
while(1){
for(int i=180;i>0;i-=5){
digitalWrite(relayPin, LOW);
myservo.write(i);
}
client.println("rotate!");
for(int i=0;i<180;i+=5){
digitalWrite(relayPin, HIGH);
Serial.println("rotate");
myservo.write(i);
}client.println("rotate!");
if(client.available())
break;
}
}
}
while(Serial.available())
{
size_t counti = Serial.available();
String data;
data=Serial.readStringUntil('\n');
client.println(data);
}
}
FAQs
Q: Why esp8266 connect fail to WiFi?
A: Common reasons include weak signal strength, router setting restrictions, outdated ESP8266 firmware, unstable power supply, firewall or router restrictions on connection protocols, etc. It might also be that the reconnection mechanism isn’t properly configured in the code or that DHCP failed to obtain an address.
Q: Why does packet loss happen during ESP8266 TCP transparent transmission?
A: Because hardware flow control isn’t set. The transparent transmission function uses the TCP protocol. If the network is poor or the interval between serial port data receptions exceeds about 20ms, some data might be lost.
Q: How many UART does ESP8266 have?
A: ESP8266 has two UART. UART0 has TX and RX pins and can be used for data transmission. UART1 RX pin is occupied by SPI-Flash, so only the TX pin can be used, which is often used for printing serial port debugging information.
Q: Why 8266 module doesn't respond when send AT commands via serial port.
A: First, check the hardware connection to make sure the serial port wires are connected correctly and not loose. Confirm that the module’s power supply is normal. Check if the baud rate setting matches the module. You can also try using other serial port debugging tools to rule out possible issues with the original tool.