Using ESP01 to ESP01 serial bridge

hey all,

I am a complete noob o some of this, but not having much luck and think I may have just got myself to the point where I am frustrated and need some help from others whom actually know what they're doing.

I have a screen that runs on serial, I can power this up and with the serial monitor see the Tx/RX commands clearly. (coming up for example "M87 S01 G223")

my goal is to pass this wirelessly through two esp01, however I cannot for the life of me workout how to do this, is there any guidance on how to get this to work, I have some code which currently allows me to have a AP and a client, they connect but all I get from the serial monitor is packets being sent back and forth, I just need to command lines to pass between them.

the original INO files are from another user online whom kindly released them but I am at a loss on how I can edit these to work or if I am working towards failure here.

sadly it will not allow me to upload the sketches as I am new. has anyone actually done this before?

any help would be greatly appreciated!

my client INO

//-- Libraries Included --------------------------------------------------------------
#include <ESP8266WiFi.h> // The Basic Function Of The ESP NOD MCU

//------------------------------------------------------------------------------------
// Define I/O Pins
#define LED0 2 // WIFI Module LED

//------------------------------------------------------------------------------------
// Authentication Variables
char* ssid; // Wifi Name
char* password; // Wifi Password
const String Devicename = "Device_1";

//------------------------------------------------------------------------------------
// WIFI Module Role & Port
IPAddress TCP_Server(192, 168, 4, 1);
IPAddress TCP_Gateway(192, 168, 4, 1);
IPAddress TCP_Subnet(255, 255, 255, 0);

unsigned int TCPPort = 2390;

WiFiClient TCP_Client;

//------------------------------------------------------------------------------------
// Some Variables
unsigned char buffer[80];
char result[10];

//====================================================================================

void setup(){
// setting the serial port ----------------------------------------------
Serial.begin(57600);

// setting the mode of pins ---------------------------------------------
pinMode(LED0, OUTPUT); // WIFI OnBoard LED Light
digitalWrite(LED0, !LOW); // Turn WiFi LED Off

// WiFi Connect ----------------------------------------------------
Check_WiFi_and_Connect_or_Reconnect(); // Checking For Connection
}

//====================================================================================

void loop(){

Send_Request_To_Server();

}

//====================================================================================

void Send_Request_To_Server() {
unsigned long tNow;

tNow=millis();
dtostrf(tNow, 8, 0, result); // create a char out of the tNow

TCP_Client.println(result); // Send Data

while(1){
int len = TCP_Client.available(); // Check For Reply
if (len > 0) {
if (len > 80){
len = 80;
}
String line = TCP_Client.readStringUntil('\r'); // if '\r' is found
Serial.print("received: "); // print the content
Serial.println(line);
break; // exit
}
if((millis()-tNow)>1000){ // if more then 1 Second No Reply -> exit
Serial.println("timeout");
break; // exit
}
}

TCP_Client.flush(); // Empty Bufffer
Check_WiFi_and_Connect_or_Reconnect();
}

//====================================================================================

void Check_WiFi_and_Connect_or_Reconnect(){
if (WiFi.status() != WL_CONNECTED){

TCP_Client.stop();                                  //Make Sure Everything Is Reset
WiFi.disconnect();
Serial.println("Not Connected...trying to connect...");
delay(50);
WiFi.mode(WIFI_STA);                                // station (Client) Only - to avoid broadcasting an SSID ??
WiFi.begin("DataTransfer");                         // the SSID that we want to connect to

while(WiFi.status() != WL_CONNECTED){
  for(int i=0; i < 10; i++){
    digitalWrite(LED0, !HIGH);
    delay(250);
    digitalWrite(LED0, !LOW);
    delay(250);
    Serial.print(".");
  }
  Serial.println("");
}

// stop blinking to indicate if connected -------------------------------
digitalWrite(LED0, !HIGH);
Serial.println("!-- Client Device Connected --!");

// Printing IP Address --------------------------------------------------
Serial.println("Connected To : " + String(WiFi.SSID()));
Serial.println("Signal Strenght : " + String(WiFi.RSSI()) + " dBm");
Serial.print ("Server IP Address : ");
Serial.println(TCP_Server);
Serial.print ("Device IP Address : ");
Serial.println(WiFi.localIP());

// conecting as a client -------------------------------------
Tell_Server_we_are_there();
}
}

//====================================================================================

void Tell_Server_we_are_there(){
// first make sure you got disconnected
TCP_Client.stop();

// if sucessfully connected send connection message
if(TCP_Client.connect(TCP_Server, TCPPort)){
Serial.println ("<" + Devicename + "-CONNECTED>");
TCP_Client.println ("<" + Devicename + "-CONNECTED>");
}
TCP_Client.setNoDelay(1); // allow fast communication?
}

//====================================================================================

It is not clear, to me, what you want to do.

You can use ESP-NOW to directly communicate between 2 ESP8266 modules. You do not need a WiFi network to use ESP-NOW.

WiFiClient and WiFiTelnetToSerial examples of the ESP8266 WiFi library

I have looked into espnow and its exactly what I am after, however I cannot under any way get the esp_now.h library installed.

where did you get the library? How are you trying to install the library? What problems are you having?

Here is information on how to install a library.

Thanks groundfungus

I am at my normal job today but should get some time again tonight to go through everything and give more detail.

I appreciate all the advice so far it has been a painfully steep learning curve

So I have finally found some time to look at this again,

so here's the goal.

I have a screen that currently talks between a 3d printer board and the touch screen, this send back and fourth commands in G code.

this is what I see when I monitor the serial wires from the screen the screen will send through commands back to the board, however the board also returns command data which is the same.

the goal was to run 3 ESP01

1: the host, this will connect to the 3d printer board and send data out to the two other esp.
2: client 1, this will need to send and receive the data.
3: client 2, will only need to send commands back no requirement to receive.

from a hardware perspective I have everything I need outside of the bridge.

the problem I am facing here is I do not know where to turn, espnow I have been very unsuccessful with erroneous libraries etc maybe this is just me not knowing what I am doing.

my biggest pain so far is I am completely unable to add the esp_now.h library
the code I was intending to use is below, however I feel half of it may be for purpose but I am clueless on how to pass the tx and rx lines between each other... this falls out of my knowledge range.

I really hope someone may have an answer for me here :frowning:

/*
  Rui Santos
  Complete project details at https://RandomNerdTutorials.com/esp-now-two-way-communication-esp8266-nodemcu/
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files.
  
  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.
*/

#include <ESP8266WiFi.h>
#include <espnow.h>

#include <Adafruit_Sensor.h>
#include <DHT.h>

// REPLACE WITH THE MAC Address of your receiver 
uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};

// Digital pin connected to the DHT sensor
#define DHTPIN 5    

// Uncomment the type of sensor in use:
//#define DHTTYPE    DHT11     // DHT 11
#define DHTTYPE    DHT22     // DHT 22 (AM2302)
//#define DHTTYPE    DHT21     // DHT 21 (AM2301)

DHT dht(DHTPIN, DHTTYPE);

// Define variables to store DHT readings to be sent
float temperature;
float humidity;

// Define variables to store incoming readings
float incomingTemp;
float incomingHum;

// Updates DHT readings every 10 seconds
const long interval = 10000; 
unsigned long previousMillis = 0;    // will store last time DHT was updated 

// Variable to store if sending data was successful
String success;

//Structure example to send data
//Must match the receiver structure
typedef struct struct_message {
    float temp;
    float hum;
} struct_message;

// Create a struct_message called DHTReadings to hold sensor readings
struct_message DHTReadings;

// Create a struct_message to hold incoming sensor readings
struct_message incomingReadings;

// Callback when data is sent
void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
  Serial.print("Last Packet Send Status: ");
  if (sendStatus == 0){
    Serial.println("Delivery success");
  }
  else{
    Serial.println("Delivery fail");
  }
}

// Callback when data is received
void OnDataRecv(uint8_t * mac, uint8_t *incomingData, uint8_t len) {
  memcpy(&incomingReadings, incomingData, sizeof(incomingReadings));
  Serial.print("Bytes received: ");
  Serial.println(len);
  incomingTemp = incomingReadings.temp;
  incomingHum = incomingReadings.hum;
}

void getReadings(){
  // Read Temperature
  temperature = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  //float t = dht.readTemperature(true);
  if (isnan(temperature)){
    Serial.println("Failed to read from DHT");
    temperature = 0.0;
  }
  humidity = dht.readHumidity();
  if (isnan(humidity)){
    Serial.println("Failed to read from DHT");
    humidity = 0.0;
  }
}

void printIncomingReadings(){
  // Display Readings in Serial Monitor
  Serial.println("INCOMING READINGS");
  Serial.print("Temperature: ");
  Serial.print(incomingTemp);
  Serial.println(" ºC");
  Serial.print("Humidity: ");
  Serial.print(incomingHum);
  Serial.println(" %");
}
 
void setup() {
  // Init Serial Monitor
  Serial.begin(115200);

  // Init DHT sensor
  dht.begin();
 
  // Set device as a Wi-Fi Station
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();

  // Init ESP-NOW
  if (esp_now_init() != 0) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }

  // Set ESP-NOW Role
  esp_now_set_self_role(ESP_NOW_ROLE_COMBO);

  // Once ESPNow is successfully Init, we will register for Send CB to
  // get the status of Trasnmitted packet
  esp_now_register_send_cb(OnDataSent);
  
  // Register peer
  esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_COMBO, 1, NULL, 0);
  
  // Register for a callback function that will be called when data is received
  esp_now_register_recv_cb(OnDataRecv);
}
 
void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    // save the last time you updated the DHT values
    previousMillis = currentMillis;

    //Get DHT readings
    getReadings();

    //Set values to send
    DHTReadings.temp = temperature;
    DHTReadings.hum = humidity;

    // Send message via ESP-NOW
    esp_now_send(broadcastAddress, (uint8_t *) &DHTReadings, sizeof(DHTReadings));

    // Print incoming readings
    printIncomingReadings();
  }
}