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.
7 People Made This Project!
- nikospax made it!
- hausofpayne17 made it!
- niq_ro made it!
- HoangNP made it!
See 3 More
53 Comments
2 years ago
Why the connection is so slow????, Is it possible to send pack of 20 bytes every 10 Hz? How we can increase speed?
3 years ago
I want to add the ultrasonic sensor to the server. I sell data that the distance is less than 10. Run the LED on the client.
Reply 3 years ago
Hello alafshok.ly,
let's go. Your project will be a good use the communication between esp mcu-s.
Would you please share your project when it is running?
Gyalu1
Question 3 years ago
Hello.
Your sketch works well with me. I have a question.
How can I send more text lines?
For example.
---------------------
Hello world.
Hello friend.
---------------------
I tried it but am too much a beginner (73).
Thanks in advance.
Bert Heideveld.
Answer 3 years ago
Hi Bert,
There are many ways to send more text.
Merge the short strings to a longer one separated with a special character. Send the long string to the other ESP, and cut the long string to parts at the separator.
The other way can be to change the value of the string in every cycle before the client.print("... command.
Gyalu
Reply 3 years ago
Oke i work on it. Do you have a exampel ?
Bert
4 years ago
Hi, NIce project. Thank you. Is there a way AP starts the communication?
Reply 4 years ago
Hi Petr,
There may be reversed action I think, but I do not know it.
Gyalu
Reply 4 years ago
The reverse action and is very slow in sending messages.
there's a solution?
4 years 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?
Reply 4 years 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?
Reply 4 years ago
see my solution: http://www.arduinotehniq.com/index.php/2019/01/14/accesspoint-station-communication-without-broken-link-between-two-esp8266-mcus/
Reply 4 years 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.
4 years ago
can any one help me in the problem of the station nodemcu stuck and not send data after a while ?
Reply 4 years ago
see my solution: http://www.arduinotehniq.com/index.php/2019/01/14/accesspoint-station-communication-without-broken-link-between-two-esp8266-mcus/
Reply 4 years ago
Hi eslam46,
I think Jabob solved this problem. See the comments below.
Reply 4 years ago
thanks you Gyalu for ur reply
4 years 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
Reply 4 years ago
see my solution: http://www.arduinotehniq.com/index.php/2019/01/14/accesspoint-station-communication-without-broken-link-between-two-esp8266-mcus/
Tip 4 years ago
I put hard-reset after exceeding the number of messages (maxim 700, in my sketch 20), lost comunication, lost data.. see http://www.arduinotehniq.com/index.php/2019/01/14/...