Problem transmiting data between two ESP8266mod

Hello,

I'm currently working on a project to read temperature, humidity, and CO2 data on an ESP8266, and then transmit it via LoRaWAN using another ESP8266 board. The issue is that all UART, I2C, and SPI pins are already in use on one of the boards, and the only available pins are a free GPIO on each board. Additionally, I can't use the RX and TX pins because it's essential to be able to view the serial monitor.

The goal is to transmit the following struct:

struct payload {
  uint8_t v0;    // Variable that stores ambient humidity
  uint8_t v1;    // Variable that stores ambient temperature
  int v2;        // Variable that stores CO2 ppm
  uint8_t v3;    // Variable that stores radiation value
  int v4;        // Variable that stores luminosity
  uint8_t v5;    // Variable that stores operating temperature
};

As the WiFi connection isn't available, I need to find a way to transmit this data through a cable between the two ESP8266 boards, using only the available free GPIO pins.

I appreciate any guidance or suggestions on how to achieve this wired communication between the ESP8266 boards. Thank you!

Why? You have WiFi enabled boards. Does the cable need to go through thick concrete or something?

1 Like

The problem is that there is no availability of a router to connect them together.

No router required. Have one of the ESP act as an Access Point. The other can join its network.

1 Like

I2C and SPI are busses. You can connect multiple devices to the same pins. Perhaps you don't need 2x ESP? If you are not using WIFI, why use ESP at all?

Oh, really? I thought it wasn't possible. Thank you very much, I will look into it.

I have to use the ESP because it's an upgrade of an old project that already has a PCB designed to use an ESP8266mod

That doesn't follow - just use a pin-compatible module?

But they're not intended for off-board connections, nor for long distances.

@juanezete how far apart are your two units?

Not much, just a few centimeters of separation

It is not possible for me, I am constrained to use ESP8266mod.

No problem for I2C or SPI. You need only one ESP I think!

1 Like

have a look at esp-now-esp8266-nodemcu-arduino-ide which enables wireless communication between ESP8266 boards (and ESP32)

Is there a practical example of this, simple data exchange without a webpage?
(I have done this with ESP-Now.)

I have used HTTP for data exchange before with ESP8266. The request can include small amounts of data as parameters to the request. The response can be simple text values, it does not have to be a web page.

server running on an access point

// ESP32 Access point - open server on port 10000 to receive data structure

#include <WiFi.h>

// Replace with your network credentials
const char* ssid = "ESP32_Access_point";
const char* password = "123456789";

// test structure
struct __attribute__((packed)) Data {
  int16_t seq;  // sequence number
  int32_t distance;
  float voltage;
  char text[50];
} data;

WiFiServer server(10000);  // server port to listen on

void setup() {
  Serial.begin(115200);
  // setup Wi-Fi network with SSID and password
  Serial.printf("Setting AP (Access Point)… %s\n", ssid);
  // Remove the password parameter, if you want the AP (Access Point) to be open
  WiFi.softAP(ssid, password);
  IPAddress IP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.print(IP);
  Serial.println(" listening on port 10000");
  server.begin();
}

boolean alreadyConnected = false;  // whether or not the client was connected previously

void loop() {
  static WiFiClient client;
  static int16_t seqExpected=0;
  if (!client)
    client = server.available();  // Listen for incoming clients
  if (client) {                   // if client connected
    if (!alreadyConnected) {
      // clead out the input buffer:
      client.flush();
      Serial.println("We have a new client");
      alreadyConnected = true;
    }
    // if data available from client read and display it
    int length;
    if ((length = client.available()) > 0) {
      //str = client.readStringUntil('\n');  // read entire response
      Serial.printf("Received length %d - ", length);
      // if data is correct length read and display it
      if (length == sizeof(data)) {
        client.readBytes((char*)&data, sizeof(data));
        Serial.printf("seq %d distaance %ld voltage %f text '%s'\n",
                      (int)data.seq, (long)data.distance, data.voltage, data.text);
        if (data.seq != seqExpected)        // check sequence number received
          Serial.printf("Error! seq expected %d received %d\n", seqExpected, data.seq);
        seqExpected = data.seq;   // set sequence number ready for next data
        seqExpected++;
      } else
        while (client.available()) client.read();  // discard corrupt packet
    }
  }
}

client transmitting data to server

// ESP32 client - connect to Access point server on port 10000 - transmit data structure

#include <WiFi.h>

// access point SSID and password
char ssid[] = "ESP32_Access_point";  // AP network SSID (name)
char password[] = "123456789";       // AP network password
IPAddress apserver(192, 168, 4, 1);  // AP IP address

// test structure
struct __attribute__((packed)) Data {
  int16_t seq;  // sequence number
  int32_t distance;
  float voltage;
  char text[50];
} data = { 0, 56, 3.14159, "hello test" };  // sample data

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(115200);
  Serial.printf("\nattempting to connect to WiFi network SSID '%s' password '%s' \n", ssid, password);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print('.');
    delay(500);
  }
  Serial.print("connected to SSID: ");
  Serial.println(ssid);
}

boolean alreadyConnected = false;  // whether or not the client was connected previously

void loop() {
  static WiFiClient client;
  if (!client) {  // connect to server on AP
    if (client.connect(apserver, 10000))
      Serial.println("Connected to AP server");
  }
  // if clength connected send data
  if (client) {
    if (!alreadyConnected) {
      client.flush();
      Serial.println("client connected to server OK");
      alreadyConnected = true;
    }
    // transmit data structure to server
    Serial.printf("seq %d distaance %ld voltage %f text '%s'\n",
                  (int)data.seq, (long)data.distance, data.voltage, data.text);
    client.write((char*)&data, sizeof(data));
    delay(1000);
    data.seq++;           // update data ready for next transmission
    data.distance += 10;
    data.voltage += 2.5;
    data.text[9]++;
  }
}

AP/server serial monitor output

Setting AP (Access Point)… ESP32_Access_point
AP IP address: 192.168.4.1 listening on port 10000
We have a new client
Received length 60 - seq 28 distaance 336 voltage 73.141586 text 'hello tes�'
Error! seq expected 0 received 28
Received length 60 - seq 29 distaance 346 voltage 75.641586 text 'hello tes�'
Received length 60 - seq 30 distaance 356 voltage 78.141586 text 'hello tes�'
Received length 60 - seq 31 distaance 366 voltage 80.641586 text 'hello tes�'
Received length 60 - seq 32 distaance 376 voltage 83.141586 text 'hello tes�'
Received length 60 - seq 33 distaance 386 voltage 85.641586 text 'hello tes�'
Received length 60 - seq 34 distaance 396 voltage 88.141586 text 'hello tes�'
Received length 60 - seq 35 distaance 406 voltage 90.641586 text 'hello tes�'

client serial monitor output

attempting to connect to WiFi network SSID 'ESP32_Access_point' password '123456789' 
............connected to SSID: ESP32_Access_point
Connected to AP server
client connected to server OK
seq 0 distaance 56 voltage 3.141590 text 'hello test'
seq 1 distaance 66 voltage 5.641590 text 'hello tesu'
seq 2 distaance 76 voltage 8.141590 text 'hello tesv'
seq 3 distaance 86 voltage 10.641590 text 'hello tesw'
seq 4 distaance 96 voltage 13.141590 text 'hello tesx'
seq 5 distaance 106 voltage 15.641590 text 'hello tesy'
seq 6 distaance 116 voltage 18.141590 text 'hello tesz'
seq 7 distaance 126 voltage 20.641590 text 'hello tes{'
seq 8 distaance 136 voltage 23.141590 text 'hello tes|'
seq 9 distaance 146 voltage 25.641590 text 'hello tes}'
seq 10 distaance 156 voltage 28.141590 text 'hello tes~'
seq 11 distaance 166 voltage 30.641590 text 'hello tes'
seq 12 distaance 176 voltage 33.141590 text 'hello tes�'
seq 13 distaance 186 voltage 35.641590 text 'hello tes�'

the structure carries a sequence number so the receiver can check for lost or duplicate packets (could request retransmission of lost packets)
the server indicated a sequence number error as it synchronised with the client

Edit: similar code could be implemented using ESP-NOW

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.