Internet of Things project – Communication between ESP8266 modules | ESP8266 Arduino Tutorial

ESP8266 talk with each other -two esp connected

What people don’t know is the real benefits of making two ESP8266 talk each other. While many of you are trying to find how to connect two ESP8266 modules, you actually don’t have a good image about what applications or projects you can do by making multiple ESP8266 modules communicate between them. In this article you will find everything you need to know about benefits of creating an independent self-sustained Wi-Fi ESP8266 network.

How to make two ESP8266 talk each other

Is it hard?

First of all, you don’t know what is an ESP8266 or how to use it you first need to read this complete ESP8266 Arduino tutorial and review and then come back.

Connecting two ESP8266 (or many) and make them communicate each other is not hard at all, you just need to know the ESP8266 working modes, and have a good knowledge about the software libraries.

Before you start, ask yourself this:

How will that really help you and how can you take advantage of having two Wi-Fi modules connected in your application or in your IoT project?

Hooking up two ESP8266 should be a solution to your problem not just a question. I will give you one of the most relevant application example that I found so far:

Powerful example: The Farmer story

A very talented farmer from Europe, who grows vegetables on large, square fields has managed to implement a fully automatic temperature and moisture Wi-Fi monitoring system over a 5 squares km field using the benefits of interconnecting ESP8266 modules without using any wires. Yes, 5km, its a huge area, but it can easily be covered if you have imagination.

How he did it?

So this farmer spliced his 5km field in 4 area parts, and in each part has build a sub-network of 9 ESP8266s. Also in the center of the field he placed a box containing another ESP8266 module, 1 Arduino NANO and a GPRS 3G Arduino compatible module. In each sub-network he arranged ESPs in a custom formation (a sort of combination between mesh and star) in order to use 3 of them as repeaters (the middleman) and the other 6 just for sensor readings (the workers). He choosed this topology in order to be able to cover all the field by using some of the ESPs as signal repeaters.

IoT project - Communication between two ESP8266 - Talk with Each Other

In the next image you can see a join between a mesh formation topology and a star topology into a Star Mesh hybrid.

IoT project - Communication between two ESP8266 - Talk with Each Other

There are many possible arrangements (like the one below) that you can design for your project, but you need to find the real advantage behind it. You can find here a good wireless infrastructure and topologies documentation.

IoT project - Communication between two ESP8266 - Talk with Each Other

Back to our farmer

While the 6 workers modules were most of the time in DEEP_SLEEP, waking up at 1 hour interval to read the sensors, the 3 middlemans were both in AP (Acces point) and ST (Station mode) having a web server started, waiting for workers requests to come.

How he programmed the modules?

Each worker ESP was programmed to wake up from DEEP_SLEEP, read the sensors and connect to the closest AP ESP and send an HTTP request to its server with the data. If the closest AP middleman was not available the workers ESP were programmed to keep up to 10 readings in the internal ESP826 RTC memory during the DEEP_SLEEP, and send them whenever the AP gets back. ESP workers were powered by 4 x Li-Ion 18650B Panasonic  3.3 A, hooked in parallel and 1-Wire sensors were also controlled (cutt-off) using transistors between measurements. Workers were raised 1m above the ground.

Every middleman ESP was programmed to listen and receive the readings from the workers, and then send it further to next middleman up until the ESP from the central box, being configured in both access point and station (AP_ST). Middlemans were continuous powered on by 3 x Li-Ion 18650B Panasonic  3.3 A hooked in parallel, and charged by 15v solar panels, and where 2.5m above the ground.

He configured workers (clients) to use static IP addresses and hard-coded Gateway and Mask, in order to reduce the time spent in connecting to the AP, and managed to send a complete measurement in less than 2.5 seconds.

The central box was configured to be the Main Cluster data manager, in charge with receiving and processing data from the closest middlemans, and send it in an MySQL database via the 3G GPRS Module in an JSON / XML format, once a day. All the data received hourly was stored in CSV format on an SD card module attached to the Arduino NANO module and processed at a specific hour in the night.

Also the central ESP8266, and the middlemans were programmed to behave like a Module Discovery Agent, making PING requests on the nearby AP to check the availability and send status to the database together with the measurements. This is a technique used in Services / Micro-services based software architectures to know which module / services is available.

What he accomplished?

So at the end of the day, our geek farmer was able to monitor the entire field from his cozzy lounge, using a very pretty web interface with maps and reports and make statistics or forecast based on the data gathered in the database.

How much he spent?

The complete solution including the 1-Wire sensors costed about 350 – 400$ using batteries from laptops cells and a simple phone card charged with 5$ / month, which for an industrial large scale monitoring system is extremely cheap. I saw people spending thousands dollars on a commercial Zig-Bee system,  monitoring a small 100 m deposit, having the same results.

This example should be inspiring for those of you who are looking to get the most from two ESP8266 modules, or even many, and not just play with them as little toys. They are pretty powerful modules, cheaper than other radio available solutions on the market, and you also can find plenty resources over the internet.

What I learned from the farmer?

I also did a project in my house using 4 x ESP8266 modules connected between each other after finding about this young farmer. I made a little monitoring system using a central AP web server and 3 clients in charge with sensing temperature, humidity and methane gas.

You can read a complete Arduino Gas sensor tutorial and review or check a comparison between the best Arduino temperature sensors to make an idea.

Off course I could easily use them independently, and make them send data via my router individually but then, few weeks ago, my internet provider cutout the internet service, and suddenly my 3 x ESP8266 clients were absolutely useless. So i made a proxy between them and the internet router using another ESP8266 in both AP and ST mode, hosting a web server too, and all my clients now send data to the proxy ESP, and then the proxy sends data to the thingspeak IoT platform trough my internet router, or keeps in memory until internet is available. I will soon add a video tutorial so that you can see how it works.

The theory – how it works?

IoT project - Communication between two ESP8266 - Talk with Each OtherThe working principle is quite simple. It works almost as with humans. In order to connect two ESP8266, at least one of them must be an configured as Access Point to give the other one possibility to discover it. Once you’ve connected them together you then need a way to make them talk each other.  As humans do, while one is talking, the other one needs to listen. The advantage of ESP8266 is that in the same time, can be both Client and Acces Point / Server.

“The same time” is actually more theoretically because this module has single threaded MCU, so it can only do a certain thing at 1 point, but the fact that when you receive a request while being in AP / Server mode, you can then immediately connect to next AP and make a request as a client is amazing.

Two ESP8266 communication Arduino code examples

Below you can see the code I uploaded in one of my ESP8266 client. It reads temperature and humidity from a DHT22 and sends it to the AP ESP, and then sleeps for 5 minutes. I also used a static network configuration in order to win some time by not waiting for DHCP. For request handling I’ve used ESP8266HTTPClient library which I found being much simpler and logic and I recommend it.

For the Access Point I used the following code:

As you can see, it starts in AP_ST mode and configures the web server and the main event listeners. On the main page listener (“/feed”), the  setupStMode() function is called, which connects to the main Internet router, takes the readings as parameters and send them via http request to the thinkspeak IoT platform. After that it continues to listen for other clients in AP mode. If you encounter problems after doing a complete send, use ESP.reset(); to quickly get back in the AP mode. Off course, I choosed the simplest way, using simple HTTP requests, but you can find more techniques to do the talking, like websockets (find morrissinger library for websockets server and client) or other UDP advanced protocols.

Final thoughts

If you are a hobbyist / geek you definitely need to give ESP8266 a chance before trowing your money on high end monitoring systems. But keep in mind that this kind of devices require thorough testing and patience since they are not coming embedded. First make a little prototype, test it and then if it works scale it to your needs. I’d choose ESP8266 just for the huge coverage area in straight line, and never the less for its price. Making two ESP8266 talk each other is an easy task for this powerful devices and with a bit imagination you can do impressive projects with low budget.

Hoping that this article inspired you, i kindly invite you share this articlesubscribe my YouTube channel and join the communities on social networks. Please feel free to comment or send suggestions / remarks so i can improve the content quality!

Save

Save

10 comments » Write a comment

  1. Nice work and great network topology. I just believe that have better options instead of using ESP8266. The main reason I’m saying that is because of the power consumption of a WiFi node. Even using sleep modes ot still consumes a lot of power to join the network and transmitt.

    In the described solution you need to provide some significant amount of energy and have solar panels, which can increase the cost, complexity and maintenance.

    If you pick options like subGhz modules you could probably use standard AA batteries for working nodes and also gett some better range might eliminating the need for intermediate/router nodes.

    Apart from that it’s a great article showing real life useful example instead of “how to turn on lights”. 😉

    • Btw I’m have good results with radil RFM69 and the Whispe Node board. I normally use a RPI as gateway to the Internet.

    • Yes, I totally agree with you, ESP8266 is so controversial when it comes to power consumption. But seem that some people gave it a chance. If you can reduce the timings and optimize your network, you can then organize a schedule to monitor and change or recharge batteries. An ordinary 200 mA solar panel is under 5$ and its enough to recharge a AA lithium between the 1 hour interval. The highest peek for ESP8266 is somewhere between 180-220 mA but it last only 1.5-3s. Anyway, thank you for your interest, glad that you liked !

      • I’m one of those people. 🙂

        Connecting to the wifi and transmitting data takes about 2 (3 sec. max) in my tests.
        Combined peak current of analog and radio part is about 500mA but should be about 80mA average according to this interesting FAQ : (Page 15)
        http://espressif.com/sites/default/files/documentation/espressif_faq_en.pdf

        DeepSleep current of a simple temp logger I’ve built is about 30µA. Can’t tell how long it will last because I just set it up but I’m expecting about 3 months at a 5min log interval using a 2000mAh lipo battery.

        Also built a prototype which sleeps at under 1µA using a external RTC + MOSFET to turn off the whole system but this is only practical if using longer sleep intervals(like >1 hour) and better batteries like eneloop.

        Anyway I also don’t think the ESP can compete with something like the RFM69.

        • Hello, I will make a demo video soon, my ESP-01 connects and sends data to the other ESP-01 AP in less than 2 seconds using static ip. about your 500mA, I really think that you’ve gone something wrong, ESP-01 can’t consume that much… even in full TCP using highest TX rates should stay under 300mA, at least the version with no LEDs. And the sensor should be under 50mA. Maybe using a high capacitor just near the ESP pinouts will cut those peaks. I really recommend to watch this video: https://www.youtube.com/watch?v=6SdyImetbp8

  2. Hi! Great article, very inspiring.

    Just a question about your sketch.
    I saw you are calling ESP.eraseConfig() on every boot but also WiFi.persistent(false).
    Aren’t they mutually exclusive?
    As far as I know persistent(false) tries to eliminate unneccassary flash writes while eraseConfig() is doing just that.

    • So while the eraseConfig should clear the static ip/network configurations, the persistance(false) should not allow remembering the connection credentials(ssid, pass, wps, etc.) Or at least this is what i’ve found about them

      • What I mean is that ESP.eraseConfig() explicit writes to the flash while WiFi.persistent(false) tries to prevent flash writes.
        This doesn’t makes sense to me.
        But maybe i misunderstood something.

  3. Pingback: How to monitor TV screen time for kids with Internet of Things | ESP8266

Leave a Reply

Required fields are marked *.