ESP8266 to ESP8266 communication without a router
Being an IOT enthusiast and trying my hand in the field of IOT, I came across ESP8266 module. The ESP8266 is a Wi-Fi module great for IoT and Home Automation projects. I needed a two-way communication between two ESP8266 modules without the use of any external internet source or the extra load of creating web pages. Being a newbie in this field and completely new user of ESP8266 made this task of ESP8266 to ESP8266 communication without a router was a bit difficult for me.
It took me a some time to find a proper way for two-way communication between two ESP8266 modules. At first, it was very tiring and annoying as the sources available either used web pages or cloud and some even did not work properly in my system. But, learning about its uses and getting a functional code gave me immense pleasure. I learned how to use an ESP8266 as a hotspot and switching on another’s wifi and paving the way for communication between the two without any kind of router or web page.
Following are the easy steps to create two-way communication between two ESP8266 modules using Arduino IDE.
- We will begin with Installing the ESP8266 support for the Arduino.
- Making an ESP8266 module the access point(hotspot)
- Making another ESP8266 module the station point(Wifi)
Getting Started
1. Installing the Esp8266 support for the Arduino
Step 1: Firstly open the Arduino IDE
Step 2: Go to files and click on the preference in the Arduino IDE
Step 3: Copy the below code in the Additional boards Manager
http://arduino.esp8266.com/stable/package_esp8266com_index.json
Step 4: Click OK to close the preference Tab.
Step 5: After completing the above steps, go to Tools and board, and then select board Manager.
Step 6: Navigate to esp8266 by esp8266 community and install the software for Arduino.
Once the above process is completed, we are ready to program our esp8266 with Arduino IDE.
Step 7: Always change the board and port before using Esp8266 module, I used NodeMCU 1.0(ESP- 12E Module). Go to Tools followed by Board and select the NodeMCU 1.0(ESP- 12E Module) board and the Port accordingly.
2. Making an ESP8266 module the access point(hotspot)
In this section, we will learn how to configure ESP8266 to run in soft access point mode so Wi-Fi stations can connect to it. The Wi-Fi network established by the soft-AP will be identified with the SSID set during configuration. The network may be protected with a password. The network may be also open if no password is set during configuration.
The Sketch
Setting up soft-AP with ESP8266 can be done with just a few lines of code.
#include <ESP8266WiFi.h>void setup()
{
Serial.begin(115200);
Serial.println(); Serial.print("Setting soft-AP ... ");
boolean result = WiFi.softAP("ESPsoftAP_01", "pass-to-soft-AP");
if(result == true)
{
Serial.println("Ready");
}
else
{
Serial.println("Failed!");
}
}void loop()
{
Serial.printf("Stations connected = %d\n", WiFi.softAPgetStationNum());
delay(3000);
}
How to Use It?
In the line boolean result = WiFi.softAP("ESPsoftAP_01", "pass-to-soft-AP")
change pass-to-soft-AP
to some meaningful password and upload sketch. Open serial monitor and you should see:
Setting soft-AP ... Ready
Stations connected = 0
Stations connected = 0
...
Then take your mobile phone or a PC, open the list of available access points, find ESPsoftAP_01
and connect to it. This should be reflected on serial monitor as a new station connected:
Stations connected = 1
Stations connected = 1
...
If you have another Wi-Fi station available then connect it as well. Check serial monitor again where you should now see two stations reported.
How Does it Work?
Sketch is small so analysis shouldn’t be difficult. In first line we are including ESP8266WiFi
library:
#include <ESP8266WiFi.h>
Setting up of the access point ESPsoftAP_01
is done by executing:
cpp boolean result = WiFi.softAP("ESPsoftAP_01", "pass-to-soft-AP");
If this operation is successful then result
will be true
or false
if otherwise. Basing on that either Ready
or Failed!
will be printed by the following if - else
conditional statement.
ESP8266WiFi library makes it easy to turn ESP8266 into soft access point.
The code I used
Create a file named accesspoint.ino and then copy the code below and upload the code, keeping the ESP8266 module connected to your PC via the USB cable.
#include <ESP8266WiFi.h>WiFiServer server(80);
IPAddress IP(192,168,4,15);
IPAddress mask = (255, 255, 255, 0);byte ledPin = 2;void setup() {
Serial.begin(9600);
WiFi.mode(WIFI_AP);
WiFi.softAP(“Wemos_AP”, “Wemos_comm”);
WiFi.softAPConfig(IP, IP, mask);
server.begin();
pinMode(ledPin, OUTPUT);
Serial.println();
Serial.println(“accesspoint_bare_01.ino”);
Serial.println(“Server started.”);
Serial.print(“IP: “); Serial.println(WiFi.softAPIP());
Serial.print(“MAC:”); Serial.println(WiFi.softAPmacAddress());
}void loop() {
WiFiClient client = server.available();
if (!client) {return;}
digitalWrite(ledPin, LOW);
String request = client.readStringUntil(‘\r’);
Serial.println(“********************************”);
Serial.println(“From the station: “ + request);
client.flush();
Serial.print(“Byte sent to the station: “);
Serial.println(client.println(request + “ca” + “\r”));
digitalWrite(ledPin, HIGH);
}
3. Making another ESP8266 module the station point(Wifi)
To set up ESP module to Wi-Fi (like hooking up a mobile phone to a hotspot), you need just a few lines of code:
#include <ESP8266WiFi.h>void setup()
{
Serial.begin(115200);
Serial.println(); WiFi.begin("network-name", "pass-to-network"); Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println(); Serial.print("Connected, IP address: ");
Serial.println(WiFi.localIP());
}void loop() {}
In the line WiFi.begin("network-name", "pass-to-network")
replace network-name
and pass-to-network
with name and password to the Wi-Fi network you like to connect. Then upload this sketch to the ESP module and open the serial monitor. You should see something like:
How does it work? 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.
Actual connection to Wi-Fi is initialized by calling:
WiFi.begin("network-name", "pass-to-network");
Connection process can take couple of seconds and we are checking for this to complete in the following loop:
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
The while()
loop will keep looping while WiFi.status()
is other than WL_CONNECTED
. The loop will exit only if the status changes to WL_CONNECTED
.
The last line will then print out IP address assigned to the ESP module by DHCP:
Serial.println(WiFi.localIP());
If you don’t see the last line but just more and more dots .........
, then name or password to the Wi-Fi network in the sketch is entered incorrectly. Verify name and password by connecting from scratch to this Wi-Fi a PC or a mobile phone.
Note: if the 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. That’s all you need to connect ESP8266 to Wi-Fi.
The code I used
Create a file named stationpoint.ino and then copy the code below and upload the code, keeping the ESP8266 module connected to your PC via the USB cable.
#include <ESP8266WiFi.h>byte ledPin = 2;
char ssid[] = "Wemos_AP"; // SSID of your AP
char pass[] = "Wemos_comm"; // password of your APIPAddress server(192,168,4,15); // IP address of the AP
WiFiClient client;void setup() {
Serial.begin(9600);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass); // connects to the WiFi AP
Serial.println();
Serial.println("Connection to the AP");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println();
Serial.println("Connected");
Serial.println("station_bare_01.ino");
Serial.print("LocalIP:"); Serial.println(WiFi.localIP());
Serial.println("MAC:" + WiFi.macAddress());
Serial.print("Gateway:"); Serial.println(WiFi.gatewayIP());
Serial.print("AP MAC:"); Serial.println(WiFi.BSSIDstr());
pinMode(ledPin, OUTPUT);
}void loop() {
client.connect(server, 80);
digitalWrite(ledPin, LOW);
Serial.println("********************************");
Serial.print("Byte sent to the AP: ");
Serial.println(client.print("Anyo\r"));
String answer = client.readStringUntil('\r');
Serial.println("From the AP: " + answer);
client.flush();
digitalWrite(ledPin, HIGH);
client.stop();
delay(2000);
}
Conclusion
Always be careful about two things:
- Select the board and the port properly.
- Connect and load the program of the accesspoint.ino first then connect another ESP module, change the port available and upload the stationpoint.ino code to the ESP module.
When the code is uploaded the two-way communication will begin and the LED in both the ESP8266 modules will blink together when the connection is established. You can see the message of their communication in the serial monitor.
What are you waiting for? Go ahead, create two files, try out the code above and establish a ESP8266 to ESP8266 communication without a router.