Overview: ESP8266 to ESP8266 communication
In this article, we are going to learn about ESP8266 to ESP8266 Communication using Ad hoc Networking. We will be sending data from one ESP8266 to another ESP8266 over Wi-Fi using an ad-hoc, device to device network, without using any WiFi Router. The concept is similar to the ESP8266 Mesh Networking.
On the Sender/Transmitter Side, we will interface DS18B20 Waterproof Temperature Sensor with NodeMCU ESP8266 12E Board. We will measure the surrounding temperature and send it to the Receiver Side. The Receiver Side consists of NodeMCU ESP8266 Board along with SSD1306 0.96″ I2C OLED Display. The OLED Display will display the received temperature on OLED Screen.
To run an Ad hoc Network we need a Web Server Library. The ESP8266WebServer library allows us to run an ESP8266 as a web server and also an access point. The webserver can process data received from a remote sensor over Wi-Fi without connecting the devices to a network or router. Thus using WeServer we can have ESP8266 to ESP8266 Communication.
Required Components
Following are the components required for making this project. All the components can be easily purchased from Amazon. The component purchase link is given below.
| S.N. | Components Name | Quantity | Purchase Links |
|---|---|---|---|
| 1 | NodeMCU ESP8266 | 2 | Amazon | AliExpress |
| 2 | DS18B20 Temperature Sensor | 1 | Amazon | AliExpress |
| 3 | 0.96" I2C OLED Display | 1 | Amazon | AliExpress |
| 4 | Resistor 4.7K | 1 | Amazon | AliExpress |
| 5 | Connecting Wires | 10 | Amazon | AliExpress |
| 6 | Breadboard | 2 | Amazon | AliExpress |
What is Ad hoc Networking?
An ad hoc networking is a network that is composed of individual devices (ESP8266 in our cases) communicating with each other directly. Ad hoc networks are created between two or more wireless Nodemcu ESP8266 together, without the use of a wireless router or an access point. The multiple ESP8266 communicate directly with each other.
Ad hoc networks can be very helpful during meetings or in any location where a network doesn’t exist and where people need to share files. An ad hoc network can also be useful in situations where only one PC has Internet access and that access needs to be shared.
Ciruit Diagram & Connections
The ESP8266 to ESP8266 communication requires two or more circuits. In this example, we will use only two circuits. One circuit will be the sender or transmitter Circuit whereas the other circuit will be the Receiver Circuit.
Sender Circuit
On the sender side we have connected a DS18B20 Waterproof Temperature Sensor to NodeMCU ESP8266 Board. Connect its VCC to 3.3V & GND to GND. Similarly, connect its digital pin to the NodeMCU D2 pin. Connect a 4.7K Pull-up Resistor between digital pin & VCC Pin as shown in the image below.
Receiver Circuit
On the receiver side we have just connected 0.96″ SSD1306 I2C OLED Display with NodeMCU ESP8266 Board. Connect its VCC to 3.3V & GND to GND. Connect its SDA & SCL pin to D2 & D1 of NodeMCU.
Source Code/Program: ESP8266 to ESP8266 communication
Let us see the code for both the sender and receiver part. We will work on esp8266 mesh networking by creating ESP8266 access point and client.
First the code will require few Libraries. So download the libraries from the following links:
1. Adafruit_SSD1306 : https://github.com/adafruit/Adafruit_SSD1306
2. Adafruit_GFX : https://github.com/adafruit/Adafruit-GFX-Library
3. One Wire Library: https://github.com/PaulStoffregen/OneWire
4. Dallas Temperature Library: https://github.com/milesburton/Arduino-Temperature-Control-Library
Sender Code
|
1 2 |
const char *ssid = "Alexahome"; const char *password = "loranthus"; |
Make sure to change the WiFi SSID & Password in the above line.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
#include <ESP8266WiFi.h> #include <OneWire.h> #include <DallasTemperature.h> #define DS18B20 D2 OneWire ourWire(DS18B20); DallasTemperature sensor(&ourWire); const char *ssid = "Alexahome"; const char *password = "loranthus"; int temp = 0; // value read from the DS18B20 void setup() { Serial.begin(115200); delay(10); // We start by connecting to a WiFi network Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } void loop() { // read the temperature value: sensor.requestTemperatures(); temp = sensor.getTempCByIndex(0); Serial.print("Temperature: "); Serial.print(temp); Serial.println(" °C"); char intToPrint[5]; itoa(temp, intToPrint, 10); //integer to string conversion for OLED library // Use WiFiClient class to create TCP connections WiFiClient client; const char * host = "192.168.4.1"; const int httpPort = 80; if (!client.connect(host, httpPort)) { Serial.println("connection failed"); return; } // We now create a URI for the request String url = "/data/"; url += "?sensor_reading="; url += intToPrint; Serial.print("Requesting URL: "); Serial.println(url); // This will send the request to the server client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n"); unsigned long timeout = millis(); while (client.available() == 0) { if (millis() - timeout > 5000) { Serial.println(">>> Client Timeout !"); client.stop(); return; } } Serial.println(); Serial.println("Closing connection"); Serial.println(); Serial.println(); Serial.println(); delay(500); } |
Receiver Code
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
#include <ESP8266WiFi.h> #include <ESP8266WebServer.h> #include <U8g2lib.h> U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ 5, /* data=*/ 4); const char *ssid = "Alexahome"; const char *password = "loranthus"; ESP8266WebServer server(80); void handleSentVar() { Serial.println("handleSentVar function called..."); if (server.hasArg("sensor_reading")) { // this is the variable sent from the client Serial.println("Sensor reading received..."); int readingInt = server.arg("sensor_reading").toInt(); char readingToPrint[5]; itoa(readingInt, readingToPrint, 10); //integer to string conversion for OLED library u8g2.firstPage(); u8g2.drawUTF8(0, 64, readingToPrint); u8g2.nextPage(); Serial.print("Reading: "); Serial.println(readingToPrint); Serial.println(); server.send(200, "text/html", "Data received"); } } void setup() { delay(1000); Serial.begin(115200); Serial.println(); Serial.print("Configuring access point..."); u8g2.begin(); u8g2.setFont(u8g2_font_logisoso62_tn); u8g2.setFontMode(0); // enable transparent mode, which is faster WiFi.softAP(ssid, password); IPAddress myIP = WiFi.softAPIP(); Serial.print("AP IP address: "); Serial.println(myIP); server.on("/data/", HTTP_GET, handleSentVar); server.begin(); Serial.println("HTTP server started"); } void loop() { server.handleClient(); } |
Result & Observations
Once the code is uploaded on the both sender and receiver circuit, press the rest button on both the ESP8266. Once rest button is pressed the Serial Monitor will show something like this.
On both the section the ESP8266 will connect to the wifi Network & creates Access Point. The data is then transferred wirelessly via the same Web Server.
Now the temperature data is sent from one device to another device using ESP8266 Ad hoc Networking. You can check the temperature data on OLED Display.













6 Comments
What is the range between transmitter and receiver?
Hello, I have one problem with the OLED. I do not see the temp on the OLED. I used yout libs.
Sender and receiver works great. I controlled this with each serial monitor.
so, one node act as access point and one node act as client connected to it. what if there is another node as intermediate node that can forward data from sender to server? is it possible?
so, one node act as access point or web server, and one node act as client/sender. what if there is another node that act as intermediate node that forward data sent from client to server, is it possible?
Yes it is possible.
I believe that this isn’t ad-hoc mode. Since one device is operating as AP and other as STA, it is clear that they are connected in infrastructure mode.