Accesspoint - Station Communication Between Two ESP8266 MCUs

11,361

48

37

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.

3 People Made This Project!

Recommendations

  • Toys Contest

    Toys Contest
  • Make it Glow Contest 2018

    Make it Glow Contest 2018
  • PCB Contest

    PCB Contest

37 Discussions

0
None
Vlado66

7 weeks ago

Well thank you. That helped me.
But I do not know how to send numbers from the station to the server.
How to do it?
The commands: "client.read ()" and "client.write ()" do not work.

I have two Adafruit father Huzzah ESP8266 with WiFi

1 reply
1
None
Gyalu1Vlado66

Reply 7 weeks ago

Hi Vlado66,
convert the numbers to strings, send strings through WiFi and after receiving the message convert them back to number on demand.
See String(), string.toFloat() and string.toInt() functions.

0
None
eslam46

Question 4 months ago

is there any way to know which station whose send the trigger to access point in case multi-stations ?

can the access point distinguish between the stations ?

3 more answers
0
None
Gyalu1eslam46

Answer 4 months ago

This way AP answers to station only which treggered it.

If you have multiple stations, the AP sends answer to the station only which triggered it last time. If you can time the stations to ask the AP in different time, then each station will receive its unique answer from the AP.

I followed this way because my stations are usually in deepsleep to save the battery power. When they wake up, they read their sensors, they send and receive answers (if they need answer) and they go to deep sleep again.

That is the reason why I do not have experiences in long time "uninterrupted" communication. I have not needed it yet.

0
None
eslam46Gyalu1

Reply 4 months ago

it is the same case for me

what i need is that

when as an example PIR sensor_node send data ........ I need to know ..... that it is the PIR_node

0
None
eslam46eslam46

Reply 4 months ago

IF u can help , it will be a big favor for me

0
None
eslam46

4 months ago

can any one help me in the problem of the station nodemcu stuck and not send data after a while ?

2 replies
0
None
Gyalu1eslam46

Reply 4 months ago

Hi eslam46,

I think Jabob solved this problem. See the comments below.

0
None
eslam46Gyalu1

Reply 4 months ago

thanks you Gyalu for ur reply

0
None
eslam46

4 months ago

thank you for that great code

but .... after a small time the station stop to trigger the access point

nothing is being sent from the station side

0
None
PhonS

5 months ago

Reply Upvote


Hi Gyalu,

Thank you very much for such great project. It is workable, however, it sometimes sends null from AccessPoint to Station. May you explain a bit more on this part.

0
None
majidmp97

Question 6 months ago

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 more answer
1
None
Gyalu1majidmp97

Answer 6 months ago

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
0
None
majidmp97

Question 6 months ago

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 more answer
1
None
Gyalu1majidmp97

Answer 6 months ago

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.

0
None
JacobM205

8 months ago

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
0
None
Gyalu1JacobM205

Reply 8 months ago

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?

0
None
JacobM205Gyalu1

Reply 8 months ago

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.

0
None
DarshanP31

8 months ago

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