ESP8266 Sending Data Over Wi-Fi to another ESP8266

P1010244-e1524762751881.jpg

A simple guide to sending data from one ESP8266 to another over Wi-Fi using an ad-hoc, device to device network, without using a wifi router.

The ESP8266WebServer library allows you run an ESP8266 as a basic webserver and access point. This can process data received from a remote sensor over Wi-Fi without connecting the devices to a network or router.

For this tutorial I’m using two NodeMCU boards from eBay but you can do this with any ESP8266 based board. To simulate the output from a sensor I’m using a trim pot potentiometer like these from eBay. I’m also using two small OLED screens from AliExpress so you can see the data easily but you don’t need these if you want to see the results in the serial monitor.

As you can see in the video below, when the potentiometer pot is adjusted the value shown on the transmitting module OLED changes to reflect the change in voltage to pin A0. On the receiver, the value on the OLED is updated as the data is received from the transmitter.

If you don’t have the Arduino IDE set up for the ESP8266 range yet you can find a tutorial here – https://robotzero.one/heltec-wifi-kit-8/ under Setting Up the Arduino IDE for the ESP8266 Range.

I’m using these settings in the IDE (Tools menu) ..

NodeMCU IDE Settings

The wiring is identical for both the transmitter and receiver except the transmitting device has the potentiometer connected to the power and analogRead(A0) pins. You might need to connect the A0 pin via a resistor on some boards as they only read up to 1v on this pin.

ESP8266 to ESP8266 Wifi Wiring

ESP8266 to ESP8266 Wi-Fi Wiring Diagram

ESP8266 to ESP8266 Wifi Breadboard

ESP8266 to ESP8266 Wi-Fi Breadboard Layout

If it’s not installed already you will need to install the U8g2 display library (for the OLED) It can be installed using the Arduino IDE library manager – open Sketch > Include Library > Manage Libraries and search for and then install U8g2.

Here’s the sketch for the transmitter. There’s a more verbose version if you want to see the output in the serial monitor or need to debug here.

#include <ESP8266WiFi.h>
#include <U8g2lib.h>

//U8g2 Constructor List - https://github.com/olikraus/u8g2/wiki/u8g2setupcpp#introduction
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ 5, /* data=*/ 4);

const char *ssid = "poopssid";
const char *password = "pingu4prez";

const int analogInPin = 0;  // Analog input pin that the potentiometer is attached to
int sensorValue = 0;        // value read from the potentiometer
int outputValue = 0;        // value sent to server

void setup() {
  Serial.begin(115200);
  delay(10);

  // Explicitly set the ESP8266 to be a WiFi-client
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }

  u8g2.begin();
  u8g2.setFont(u8g2_font_logisoso62_tn);
  u8g2.setFontMode(0);    // enable transparent mode, which is faster
}

void loop() {
  // read the analog in value:
  sensorValue = analogRead(A0);
  // map to range. The pot goes from about 3 to 1023. This makes the sent value be between 0 and 999 to fit on the OLED
  outputValue = map(sensorValue, 3, 1023, 0, 999);

  char intToPrint[5];
  itoa(outputValue, intToPrint, 10); //integer to string conversion for OLED library
  u8g2.firstPage();
  u8g2.drawUTF8(0, 64, intToPrint);
  u8g2.nextPage();

  // 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. Something like /data/?sensor_reading=123
  String url = "/data/";
  url += "?sensor_reading=";
  url += intToPrint;

  // 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;
    }
  }

  delay(500);
}

On the server (receiver) the sketch looks like this. Again, if you want a version with serial outputs to see more details you can download that here.

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <U8g2lib.h>

//U8g2 Constructor List - https://github.com/olikraus/u8g2/wiki/u8g2setupcpp#introduction
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ 5, /* data=*/ 4);

const char *ssid = "poopssid";
const char *password = "pingu4prez";

ESP8266WebServer server(80);

void handleSentVar() {
  if (server.hasArg("sensor_reading")) { // this is the variable sent from the client
    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();
    server.send(200, "text/html", "Data received");
  }
}

void setup() {
  delay(1000);

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

  server.on("/data/", HTTP_GET, handleSentVar); // when the server receives a request with /data/ in the string then run the handleSentVar function
  server.begin();
}

void loop() {
  server.handleClient();
}

Hopefully this tutorial helps you start off in the right direction with using Wi-Fi on these devices. I’ve seen other tutorials that made things a lot more complicated than they need to be.

Leave a Reply

Your email address will not be published. Required fields are marked *

scroll to top