Accesspoint - Station Communication Between Two ESP8266 MCUs

5,830

45

21

Published

Introduction: Accesspoint - Station Communication Between Two ESP8266 MCUs

Hello Makers!

In my previous instructable I made a WiFi communication between two ESP8266 MCUs through a home WiFi router. As I saw from the comments there are Makers who would like to use ESP8266 MCUs far from the range of a router. So here is a bare minimum instructable about an accesspoint - station communication which don't needs WiFi network.

Step 1: What Do You Need.

Things you need:

  • 2 pcs ESP8266 based MCUs e.g. Wemos D1 mini
  • 2 pcs micro USB cables
  • and a PC with Arduino IDE

Step 2:

Everything you have to do:

  • Download the attached sketches
  • Open them in two independent Arduino IDEs
  • Upload them into two ESP8266 MCUs.

The AP has a fix IP address eliminating the IP conflict with your other projects.

The station uses this predefined IP to connect to the AP. The station triggers the AP with a message and the AP answers it.

Opening the serial monitors of each running Arduino IDEs you can see the flow of the messages and answers.

The built in LED flashes during the communication. It is only visualization you can eliminate flashing if you don't like it.

Step 3: How to Open Independent IDEs

How to start two independent Arduino IDEs?

  • At first click on the accesspoint_bare_01.ino. It will open the first Arduino IDE.
  • Connect the first MCU.
  • Set the new port appears in the Tools->Port menu to the first MCU.
  • Upload the sketch. It will be your accesspoint.
  • After that go back to the downloaded sketches and double click on station_bare_01.ino. It will open the second Arduino IDE.
  • Connect the second MCU.
  • Set the new port appears in the Tools->Port menu to the second MCU. (If your IDEs are independent then port will not change in the first IDE window.)
  • Uplod the sketch. It will be your station.

2 People Made This Project!

Recommendations

  • Planter Challenge

    Planter Challenge
  • Oil Contest

    Oil Contest
  • Woodworking Contest

    Woodworking Contest
user

We have a be nice policy.
Please be positive and constructive.

Tips

2 Questions

i have 200 slave (8266) and 1 master (8266) without internet or external router (offline network)

and slave have string data (for data logger string : 29;12.5;1&20 29= temp 12.5 = volte 1 state micro switch 20= id slave) (i write source in atmega328 and work)

and master most know which slave connect and then request data from slave then slave send string to master and master example print value string in serial

but it's not all i need but help

my need is very bigger than that

now i need your source have static ip

that source i config in your cod work with web browser in pc

s.jpgm.jpg

1

Hi majidmp97,

there is a limitation in ESP8266. Max. five clients can connect to one accesspoint. I made this trial. According to my experiences it works well with three clients. Above three clients the clients have to communicate with the AP in different time otherwise the AP can not serve them.

So I think you have to do a more layer network, in which ESPs in the internal layers have to be AP and station in same time.

A client from a lower layer connects to the AP, and the ESP which acts as AP, connects as a client to an AP of the upper layer.

esp_network.jpg

hello

how can i have static ip in client?

-----------------------------------------------------------

i set static ip in config-client connect to access point but data dont send or receive

source client

-----

#include <ESP8266WiFi.h>

#include <ESP8266WiFi.h>

IPAddress local_IP(192,168,4,20);

IPAddress gateway(192,168,4,255);

IPAddress subnet(255,255,255,0);

byte ledPin = 2;

char ssid[] = "Wemos_AP"; // SSID of your AP

char pass[] = "Wemos_comm"; // password of your AP

IPAddress server(192,168,4,255); // IP address of the AP

//IPAddress server2(192,168,4,255);

WiFiClient client;

void setup() {

Serial.begin(115200);

WiFi.mode(WIFI_STA);

WiFi.config(local_IP, gateway, subnet, gateway, gateway);

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() {

digitalWrite(ledPin, HIGH);

client.connect(server, 80);

digitalWrite(ledPin, LOW);

Serial.println("********************************");

Serial.print("it client : ");

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(500);

}

--------------------------------------------------------

#include

IPAddress local_IP(192,168,1,20);

IPAddress gateway(192,168,1,1);

IPAddress subnet(255,255,255,0);

byte ledPin = 2;

char ssid[] = "Wemos_AP"; // SSID of your AP

char pass[] = "Wemos_comm"; // password of your AP

IPAddress server(192,168,1,255); // IP address of the AP

WiFiClient client;

void setup() {

Serial.begin(115200);

WiFi.mode(WIFI_STA);

WiFi.config(local_IP, gateway, subnet, gateway, gateway);

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() {

digitalWrite(ledPin, HIGH);

client.connect(server, 80);

digitalWrite(ledPin, LOW);

Serial.println("********************************");

Serial.print("it client : ");

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);

}

---------------------------------------------------------------

and source accespoint

--------------------------------------------------------------------

#include <ESP8266WiFi.h>

WiFiServer server(80);

IPAddress local_IP(192,168,4,255);

IPAddress gateway(192,168,4,255);

IPAddress subnet(255,255,255,0);

byte ledPin = 2;

void setup() {

Serial.begin(115200);

WiFi.mode(WIFI_AP);

WiFi.softAP("Wemos_AP", "Wemos_comm");

//WiFi.softAPConfig(IP,gateway,mask);

WiFi.config(local_IP, gateway, subnet, gateway, gateway);

//WiFi.printDiag(Serial);

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());

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());

}

void loop() {

digitalWrite(ledPin, HIGH);

WiFiClient client = server.available();

if (!client) {return;}

String request = client.readStringUntil('\r');

Serial.println("********************************");

Serial.println("From the client : " + request);

client.flush();

Serial.print("Byte sent to the station: ");

Serial.println(client.println(request + "ca" + "\r"));

digitalWrite(ledPin, HIGH);

}

1

Hi majidmp,
Why do you need static IP for a client?
Clients usually get IP from the servers / accesspoints DHCP.
Usually only servers / APs need fix IP to be able to connect to them.

21 Comments

For some reason, when I run this code for a few minutes, the ESPs stop communicating with each other. Its like they get stuck. What could be causing this problem and how can it be fixed?

2 replies

Hi Jacob,

I downloaded my codes from above and uploaded them to two Wemos D1 mini. After more than 40 minute test run the station and the AP are talking without any problem.

Did you modify anything in the code?

Hi Gyalu,

Thank you so much for the excellent code example. I wouldn't have gotten my project to work without it.

I am using the Adafruit HUZZAH ESP8266 Breakout Board. I did modify the code but left all of the wifi connection code the same (I had the AP and station "control" each the other's LED by saying "turn on... turn off"). Most importantly, I changed the delay at the end of loop() to be 200 ms, so my code should run about 10x faster than yours (which is why your could last over 40 mins while mine only lasted a few).

I was doing some debugging and it seemed like the problem was with the station, because when I would hit the reset button on the AP, nothing would happen, but when I hit the reset button on the station, it would start working again.

I also noticed that the client.connect() method returns a boolean if it was successful or not. I printed the result of client.connect() out to Serial and noticed that after about 400 times (I got a different number each time I ran the experiment), the client.connect() method starts failing.

client.connect() starts to fail because the WiFiClient object can only connect to a server so many times before it stops working. I moved the construction of client to the beginning of loop(), so that it would construct a new WiFiClient object each time it tried to connect to the server, and this seemed to fix the bug! :) This is a reasonable solution since the WiFiClient object takes less that 1ms to construct (I timed it).

I wonder if an alternative solution to the problem would be to move "client.connect(server, 80);" to the the end of setup() and delete "client.stop();". I haven't tested that to see if its reliable since I'm happy with the current solution.

tl;dr, I was able to get the ESPs to communicate indefinitely by moving the code "WiFiClient client;" to the beginning of loop() in the station code.

On an unrelated note, in the access point code, I think you're supposed to add "client.stop();" at the end of loop(), but I don't really understand what this does, so I could be wrong.

Hello sir, i stuck in the below problem hope will get ans.

I just want make AP(192.168.4.1) and 3 STA(192.168.4.2/3/4) all should beaccess from http webpage,when the 4th client(my cell or laptop)connect to it.

I am able to connect only 1 STA but other 2 are not connecting.

please help.

Darshan

1 reply

Hi Darshan,

Theoretically you can connect max five stations to the AP. Of course your AP can not serve each stations in same time. You should let finish the communication between the AP and station "A" before your station "B" tries to ask the AP. You have to increase the time between two communication on each stations.

I tried it with very short messages (sensor readings). According to my experience it works stably with about three stations. But downloading a page costs much more time...

Hi Gyalu,

I just want to thank you in English.

Your article is so great :D

3 replies

Hi 潘宗轅,

You are welcome!

Did you use it in your project?

Yes, I try to use ESP-01 instead of arduino nano and HC-05.

But there are some problems.

I need to send data once by client in 2ms.

But it's working too slowly by ESP-01 Wi-Fi, from time to time will pause.

I still don't know the reason why...

I am afraid ESP and the WiFi communication wil be slow to send and receive messages in every 2ms.

good day bro,

I am trying to use your code with two clients(+) and one station. I have uploaded the access point code in two esp8266 12e and made some modification, but no success so far. have you tried such thing?

2 replies

Hi alharith,
I don't understand well what would you like to do, but if you want to connect more esp into one network, then you need only one accesspoint and you can connect more (max five) stations to it.
So I don't know why do you need two accesspoints.

Hey Gyalu1,

yeah you are right! I mixed up programs. I got it working now, thanks to you. I have noticed in many websites that 5 stations is your maximum. but I might try to wake some and put some in sleep to solve this problem.

Dear Mr Gyalu,

Hi , this is Vijay .

Thanks for the excellent tutorial.

I am working out to send the digital Input status of the accesspoint to client.

Ex D0 input status of accesspoint is to be sent to client on WiFi.

Client should read status of D0 ( High or low ) and should switch ON or OFF its D0 output.

Can you please guide on this code ?

2 replies

Hi Vijay,
try something similar in the AP program. Replace the existing "client.println...." command with the next:
client.println(String(digitalRead(D0))).
In the client:
String answer = client.readStringUntil('\r');
int APD0status = answer.toInt();
digitalWrite(D0, APD0status);
Write the results step by step to the serial moditor and you will see what you have to modify.

Hi Gyalu !

Thanks a lot .

It is working fine.

Only change I did , was output no D7 instead of D0 in client code.

Regards

Vijay

Hi Gyalu,

Excellent Tutorial , but your station side code

char ssid[] = "Wemos_AP"; // SSID of your home WiFi

char pass[] = "Wemos_comm"; // password of your home WiFi

requires wifi ( thru' some router) .

Is there any way we just communicate between two nodemcu ( esp8266) without usage of any type of router , wherein the station communicates directly with the access point.

1 reply

Hi naveen_gemini,
Thank you for your comment! The remarks are wrong in my station code. These are not the SSID and password of my home WiFi. The station connects using this SSID and pass to the accesspoint directly.
So the code does what you want, the two ESP communicate without the home WiFi network.
Gyalu

Thanks !!! worked form me.
Two node MCU's talking successfully.

Regards: ASIF IQBAL
Any one who needs help can contact me on asif5955iqbal@yahoo.com

Sir,

I want to communicate between two nodemcu's via same hotspot.

first node is connected to ir sensor and second one at the other end is also connected to different ir sensor. When the first ir sensor is trip and after that the second one at the other end in the same way. I want to calculate the difference

in time and display on the server or on the app. The distance between two end is approx 100 feet.