Creating a distributed control system based on microcontrollers is a logical step for any enthusiast. Internet of Things, which has outgrown simple blinking LEDs. When a single controller is no longer enough, it becomes necessary to connect multiple devices into a single network, and wireless connection seems the most elegant solution. Using classic boards Arduino Uno Directly for WiFi is impossible without additional modules, however modern boards of the series ESP8266 And ESP32 completely solve this problem, having a built-in radio module.
You don't need to be an expert in network protocols to implement basic data exchange between two pieces of hardware. All the magic lies in understanding the architecture correctly. client-server, where one device acts as an access point or server, and the other connects to it as a client. In this article, we'll look at how to turn two boards into a fully-fledged network that transmits telemetry and commands without a router.
There are several scenarios for implementing such a connection, and the choice of a specific method depends on your requirements for communication range and power consumption. A direct connection can be established WiFi Direct (SoftAP mode), create a local network through a router or even use technologies like ESP-NOW for ultra-fast packet transmission. We'll focus on the most universal and understandable method, which is based on standard TCP/IP sockets.
Selecting hardware and preparing the development environment
The first step to a successful project is choosing the right hardware. To implement WiFi connectivity, you'll need boards with a built-in wireless module, as the classic Arduino Uno or Nano They don't know how to operate WiFi independently. Chip-based boards are the optimal choice. ESP8266 (for example, NodeMCU or Wemos D1 Mini) or more powerful ESP32, which support dual-band WiFi and Bluetooth.
To program these devices we will use the environment Arduino IDE, which has long since learned to work with the non-standard ESP architecture. You will need to install additional support packages through the settings menu so that the compiler understands code intended for WiFi controllers. Without this step, uploading sketches will be impossible, and the environment will return compilation errors.
- 📡 Two ESP8266 or ESP32 boards (you can mix different models).
- 💻 A computer with Arduino IDE version 1.8 or later installed.
- 🔌 Micro-USB or Type-C cables for firmware and power.
- 🔋 Stable 5V power supply (PC USB port may not be sufficient when WiFi is active).
It's important to understand that when the radio module is actively operating, current consumption increases sharply. If you plan to power your devices with batteries, make sure they can supply up to 300-500 mA of current during peak data transfer periods. Otherwise, the microcontroller may reboot or become unstable.
⚠️ Note: Some cheap USB cables are designed for charging only and do not transfer data. If the computer doesn't detect the card when connected, try replacing the cable with a high-quality one that includes data lines.
How WiFi works in SoftAP and Station mode
To connect two Arduinos, you need to clearly understand how they will interact physically and logically. In the world of WiFi, there are two main operating modes we'll need: Station (Client) and SoftAP (Access Point). In our configuration, one board will act as a server (SoftAP), creating its own network, while the other will search for this network and connect to it as a client (Station).
This topology is called Ad-hoc network, as it doesn't require an external router. The server board generates its own IP address (usually 192.168.4.1) and assigns addresses to connecting clients from a pool. This is ideal for creating standalone systems where the internet is not required, and only local communication between devices is essential.
The connection establishment process is as follows: the server board initializes the access point with a specified name (SSID) and password. The client board scans the air, finds the desired network by name, and sends an association request. After a successful handshake, a data channel is opened between the devices via sockets.
Up to 5 at a time
| Parameter | Server Board (SoftAP) | Payment-Client (Station) |
|---|---|---|
| Opening hours | Creates a network | Connects to the network |
| IP address | Static (eg 192.168.4.1) | Dynamic (DHCP) |
| Consumption | Above (continuous broadcast) | Below (sleep mode possible) |
| Number of clients | 1 (connects to one network) |
Setting up the server part (Access Point)
Let's start programming with the device that will act as the center of our mini-network. In the sketch for this board, we need to include the library. ESP8266WiFi.h (or WiFi.h (for ESP32) and declare a server object. Our goal is to start an access point and listen on a specific port for incoming connections.
In function setup() We enter the network parameters: name (SSID) and password. It is advisable to use WPA2 security to prevent unauthorized devices within range from connecting to your network. After launching WiFi.softAP() The board will assign itself an IP address, which we will need to know to connect the client.
#include <ESP8266WiFi.h>
const char* ssid = "Arduino_Server";
const char* password = "12345678";
WiFiServer server(80); // Create a server on port 80
void setup() {
Serial.begin(115200);
WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
server.begin();
}
In the body of the cycle loop() The server constantly checks for a new client. If a connection is established, the server should read the incoming data and, if necessary, send a response. It's important not to block code execution with long delays. delay(), otherwise the network will work slowly and with timeouts.
Buffering can be used to process incoming data. The server reads the byte stream until the client terminates the connection or runs out of data. After processing the request (for example, receiving the "LED_ON" command), the server performs the action and closes the connection, freeing up resources for the next request.
Programming the client board (Station)
Now we move on to the second board, which will search for the created network and send data. Unlike the server, this board acts as WiFi ClientIts job is to know the SSID and password of the server network, as well as the server's IP address, so it knows where to knock.
In the client sketch we also include the WiFi library, but we use the method WiFi.begin() to connect to an external network. After a successful connection (status WL_CONNECTED) the board receives its IP address from the server. Next, we create a client object and use the method client.connect(), specifying the server IP address and port.
- 🔍 Scanning networks: the client is looking for the SSID "Arduino_Server".
- 🔑 Authorization: transmitting a password to gain access.
- 🤝 Connection: Establishing a TCP session with the server on port 80.
- 📤 Transmit: Send a string of data (e.g. sensor readings).
Particular attention should be paid to handling connection errors. The WiFi signal may be unstable, or the server may be temporarily unavailable. It's considered good programming practice to implement a connection retry mechanism with exponential backoff to avoid flooding the network with endless requests.
☑️ Check before launching the client
Organizing data exchange via sockets
The most interesting part is actually transmitting the payload. Once a TCP connection is established, both devices have a channel through which bytes can be transferred. You can send simple text strings, JSON objects, or binary data, depending on your protocol requirements.
For simplicity, a comma-separated string format is often used. For example, a client might send a packet like TEMP:25.5;HUM:60The server receives this string, parses it, extracts the values, and uses them to control actuators or display them.
⚠️ Note: TCP guarantees data delivery, but adds overhead. If you need maximum speed and are willing to tolerate possible packet loss, consider using UDP, although TCP is easier to debug for beginners.
When transferring large amounts of data, it's important to consider the buffer size. If you send more data than the socket buffer can handle, the transfer will block or the data will be truncated. Always check the client's availability using client.available() before reading and client.connected() before recording.
Traffic optimization
To reduce the size of transmitted packets, you can use binary format instead of ASCII text. For example, instead of the string "255" (3 bytes), transmit one byte with the value 0xFF. This is especially important when working with slow WiFi or to save power.
Network debugging and troubleshooting
Like any wireless technology, WiFi communication between Arduinos can encounter problems. The most common cause of failures is an unstable power supply. When the WiFi module begins transmitting, the current consumption fluctuates, and if the power supply is weak, the voltage drops, causing the chip to reboot.
The second common problem is IP address desynchronization. If the server reboots, it may receive a new IP address, and the client will no longer see it. Industrial solutions use DNS or static address assignment, but in amateur projects, simply rebooting the entire system is sufficient.
For diagnostics, use the built-in Serial Port Monitor. Display each connection stage: "Searching for SSID," "Connected," "Server started." This will help you understand at what stage the connection is being lost. It's also helpful to display the value WiFi.RSSI(), which will show the signal level in dBm.
- 📉 Weak signal: Make sure the antennas are not shielded by metal.
- 🔌 Voltage sags: Use a separate power supply for powerful loads.
- 🔄 Freezes: Add a watchdog timer for automatic restart.
If you're using a router as an intermediary (both cards are connected to your home WiFi), make sure Client Isolation isn't enabled on the router. This feature prevents devices on the same network from communicating directly with each other.
Is it possible to connect an Arduino Uno without a WiFi module?
The Arduino Uno board itself doesn't have WiFi. However, you can connect an external ESP-01 or ESP8266 module to it via UART (serial port). In this case, the Arduino will only collect data, and the ESP module will handle all WiFi communication, operating in transparent bridge mode (AT commands).
What is the maximum communication range between two ESPs?
In open, unobstructed spaces, the range can reach 100-200 meters. In an apartment or office with concrete walls, the range is typically 15-30 meters. Using an external antenna can significantly increase this range.
How much data can be transferred at one time?
Theoretically, the data volume is limited only by the socket buffer size and the board's RAM (approximately 80 KB for the ESP8266). In practice, it is recommended to break the transfer into small packets of 500-1000 bytes to ensure stability and fast system response.
Is internet required for such a network to operate?
No, an internet connection is not required. Using SoftAP mode creates a local network that exists only between your devices. Data is transferred directly from one board to another without accessing the external WAN.