Establishing wireless data exchange between microcontrollers is a fundamental task when creating smart home systems or distributed sensors. To implement communication between two boards ESP8266 There are several proven methods, each with its own advantages depending on range and power consumption requirements. The most common and easiest to get started with is creating a local network, with one card acting as an access point and the other connecting to it.
Unlike Bluetooth, Wi-Fi connection Provides significantly greater range and allows for easy integration of devices into existing infrastructure. However, it's important to note that power consumption in this mode is higher, which can be critical for standalone devices. It's important to understand the basic logic of interaction: devices must be on the same subnet and have correctly configured IP addresses to successfully exchange data packets.
The setup process does not require complex equipment, just two boards and a computer with the installed Arduino IDE and basic knowledge of C++ programming. In this guide, we'll cover creating a connection in "Access Point - Client" (AP-Station) mode, which is the most versatile solution for transmitting data without an external router.
Choosing a Network Architecture for the ESP8266
Before programming, it's important to determine the network topology, as it affects the code and port configuration. The simplest option is one in which one board creates the network (Access Point), and the other connects to it as a client (Station). In this configuration, the master device assigns IP addresses, and the slave device requests a connection using a known network name (SSID) and password.
An alternative is to use a protocol ESP-NOW, which operates over Wi-Fi but doesn't require a full-fledged network or connection to a router. This method provides minimal latency and low power consumption, making it ideal for battery-powered systems. However, for beginners, the classic approach with UDP or TCP sockets in AP-Station mode is more intuitive and easier to debug.
⚠️ Note: When using access point mode, the network range is limited by the ESP8266 antenna power. In built-up areas or near metal structures, the range may be as little as 10-15 meters, so ensure devices are positioned with a clear line of sight.
The protocol most often used for data transfer in a local network is UDP, as it doesn't require a persistent connection and is faster than TCP. This is especially important for telemetry transmission, where the loss of one packet out of hundreds isn't critical, and data update speed is a priority.
Necessary equipment and environment preparation
To implement the project, you will need two boards based on the ESP8266 chip, for example, popular models Nodemcu or Wemos D1 MiniThese devices feature a built-in USB interface for flashing firmware, simplifying the development process. Also, make sure you have the CH340 or CP2102 drivers installed to ensure proper operation of the ports in the operating system.
In the environment Arduino IDE You need to add support for ESP8266 boards via the File → Settings menu and paste the link to the board manager into the Additional links for the board manager field. After that, find and install the package in the board manager. esp8266 by ESP8266 Community.
For debugging and visualization of transmitted data, it is highly recommended to use a serial port monitor or specialized terminals such as Termite or the built-in IDE monitor. This will allow you to monitor the connection process and see transmitted lines in real time.
☑️ Equipment preparation
Setting up the first board in access point mode
The first board will act as a server and network coordinator. The library must be included in the sketch. ESP8266WiFi.h and initialize the mode WIFI_APThis will force the microcontroller to start its own wireless network with the given name.
The key is setting the IP address. By default, the access point receives an address, but for stable operation, it's better to set a static IP, for example, 192.168.4.1This will make it easier to connect the second device, as it won't have to search for a server on the network.
void setup {Serial.begin(115200);
WiFi.softAP("ESP8266_Master","password123");
IPAddress myIP = WiFi.softAPIP;
Serial.println(myIP);
UDP.begin(8080);
}
After running the sketch, you'll see the IP address assigned to the access point interface in the log. At this point, the board is ready to accept connections, but doesn't yet have the logic to process incoming messages. For full functionality, you need to create a UDP object that will listen on a specific port.
Why port 8080?
Port 8080 is often used as an alternative to the standard web port 80. It's not reserved by system services in most operating systems, reducing the risk of conflicts during debugging. However, you can use any free port in the range 1024-65535.
Configuring the second board in client mode
The second board will act as a client, finding and connecting to the network created by the first board. The code must specify the SSID and password of the network created by the master board, as well as the IP address of the server for sending data.
The connection process takes some time, so in the loop loop or functions setup It is necessary to implement a wait for a successful connection. The connection status can be checked using the function WiFi.status, which will return WL_CONNECTED if successful.
The method used to transfer data is beginPacket UDP object followed by a data entry and a final packet endPacketThis is the standard procedure for sending datagrams on the network.
void loop {if (WiFi.status == WL_CONNECTED) {
UDP.beginPacket(IPAddress(192, 168, 4, 1), 8080);
UDP.print("Hello from Client");
UDP.endPacket;
delay(1000);
}
}
⚠️ Important: Make sure the SSID and password in the client code exactly match the access point settings, including case. Any mismatch will result in an infinite connection attempt loop.
Implementation of two-way data exchange
For a full-fledged dialogue between the devices, both boards must be able to both send and receive data. This is achieved by adding a check for the presence of packets in the buffer using the function UDP.parsePacketIf the packet is received, we read its contents through UDP.read.
It's important to understand the difference between blocking and non-blocking operations. Using delays delay in a loop can lead to packet loss, since the device is not processing incoming traffic during this time. For professional solutions, it is better to use timers based on millis.
Below is a table showing the correspondence of functions for the server and client when organizing exchange:
| Action | Function (Master/AP) | Function (Station/Client) |
|---|---|---|
| Initializing WiFi | WiFi.softAP |
WiFi.begin |
| Obtaining an IP | WiFi.softAPIP |
WiFi.localIP |
| Sending data | UDP.beginPacket |
UDP.beginPacket |
| Receiving data | UDP.parsePacket |
UDP.parsePacket |
Debugging and troubleshooting
The most common problem is the client's inability to connect to the access point. In this case, first check the serial port logs. If the client reports "Connection failed," the password or network name is incorrect. If the connection is established but no data is available, the problem lies with the firewall (on the PC) or the recipient's IP address.
Another consideration concerns power supply. When actively transmitting via Wi-Fi, the ESP8266's current consumption can briefly reach 300 mA. A weak USB port or long cable can cause voltage drops, leading to reboots and disconnections. Use high-quality cables and power supplies with sufficient current.
To analyze traffic in complex cases, you can use third-party utilities such as Socket Test on a smartphone or Wireshark On a computer connected to the same network (if the AP has LAN access). This will help you see if the packets are reaching their destination.
Is it possible to connect ESP8266 without a router?
Yes, this is the AP-Station's primary operating mode, described in the article. One board creates the network, while the other connects. An external router is not required.
What is the maximum communication range between two ESP8266?
In open spaces, the range with standard antennas is 30-50 meters. In a room with walls, the range decreases to 10-20 meters, depending on the materials.
What if the IP addresses don't match?
In AP-Station mode, the client automatically obtains an IP from the access point. Make sure you use the IP assigned by the access point (this can be output in Serial), or manually set a static IP in the client code.
Does ESP8266 support WPA2 encryption?
Yes, when creating an access point through WiFi.softAP(ssid, password) By default, WPA2 encryption is used, which protects the channel from eavesdropping.