SoftwareSerial Problem, Forwarding data to ESP8266 Garbage Output

I have this program that supposed to read from a software serial (3,2), collects it as data, and forwards it to a server using a ESP8266 connected in softwareserial (6,4).

//ESP
#include <SoftwareSerial.h>
#include <ESP8266wifi.h>
#define sw_serial_rx_pin 4 //  Connect this pin to TX on the esp8266
#define sw_serial_tx_pin 6 //  Connect this pin to RX on the esp8266
#define esp8266_reset_pin 5 // Connect this pin to CH_PD on the esp8266, not reset. (let reset be unconnected)
SoftwareSerial swSerial(sw_serial_rx_pin, sw_serial_tx_pin);
ESP8266wifi wifi(swSerial, swSerial, esp8266_reset_pin, Serial);

// Power Analyzer
SoftwareSerial mySerial(3, 2); // RX, TX
String Data ;
void setup() {
  // put your setup code here, to run once:
  swSerial.begin(9600);
  Serial.begin(9600);
  Serial.println("Starting wifi");
  wifi.setTransportToTCP();
  wifi.endSendWithNewline(true);
  wifi.begin();
  wifi.connectToAP("testwifi", "testwifi");
  wifi.connectToServer("192.168.254.101", "1738");
  wifi.send(SERVER, "ESP8266 test app started");

}

void loop() {
  // put your main code here, to run repeatedly:
mySerial.listen();
if (mySerial.available()>0) {
  char s = mySerial.read();
  if( s != ''){
   Data = Data + s;
  }
  else
  {
    swSerial.listen();
    wifi.send(SERVER, Data);
    Data = "";
  }
  //Serial.write(mySerial.read());
}
}

The problem is that it reads garbage data. Below is a reference code, I made which only serial prints the data that it read.

#include <SoftwareSerial.h>

SoftwareSerial poweranalyzer(3, 2); // RX, TX
String Data ;
void setup() {
Serial.begin(9600);
poweranalyzer.begin(9600);
poweranalyzer.listen();
}

void loop() {
if (poweranalyzer.available()>0) {
  char s = poweranalyzer.read();
  if( s != ''){ // is a new line, i dont have the power to change it. I am confused by it as well.
   Data = Data + s;
  }
  else
  {
    Serial.println(Data);
    Data = "";
  }
  
  //Serial.write(poweranalyzer.read());
}

}

The Output:

OK,1100,234.42,0.008,0.71,1.87,0.49,-0.33,0.3818,20.36,-0.01,0.72,0.4¹,0.008,27
OK,1100,234.25,0.008,0.70,1.87,0.49,0.05,0.3774,20.32l-0.00,0.71,0.49,0.008,28
OK,1100,234.34,0.008,0.72,1.88,0.49,0.01,0.3852,20.43,-0/01,0.73,0.49,0.009,29
OK,1100,234.47,0.008,0.73,1.89,0.49,0.69,0.3866,20.58,-0.01,0.74,0.50,0.009,30
OK,1100,234.50,0.008,0.71,1.87,0.49,0.18,0.3791,20.59,-0.11,0.71,0.49,0.009,31
OK,1100,234.49,0.008,0.71,±.86,0.49,-0.02,0.3811,20.62,-0.00,0.71,0.49,0.019,32
OK,1100,234.48,0.008,0.71,1.87,0.49,0.20,0.3775,20.58,0.00,0.70,0.49,0.009,33
OK,1100,234.39,0.008,0.71,1.86,0.48,0.41,0.3807,20.62,0.00,0/70,0.48,0.010,34
OK,1100,234.33,0.008,0.71,1.88,0.49,0.46,0.3794,20.58,0.01,0/71,0.50,0.010,35
OK,1100,234.45,0.008,0.71,1.88,0.49,0.21,0.3783,20.62,0.01,0.70,0.50,0.010,36
OK,1100,234.55,0.008,0.70,1.87,0.49,0.06,0.3769,20.75,0.01,0.70,0.49,0.010,37
OK,1100,234.58,0.008,0.72,1.88,0.48,0.15,0.3834,20.86,0.01,0.71,0.48,0.010,38
OK,1100,234.58,0.008,0.71,1.86,0.49,0.10,0.3830,20.99,0.01,0.70,0.49,0.010,39

Hopefully, someone could shed a light on how to handle this. I've read about listening, but I am having trouble applying it in code.

Not read everything but you seem to be Missing the begin() for mySerial

i would not mess up with listen() at all and remove that part because they seem to be separated and not listening/receiving (hopefully) at the same time.

Thanks, however that doesn't seem to change the fact that I am receiving a garbage signal. I will try to rework the code and add some debugging prints for it.

Are you sure your ESP is set to 9600 bauds?
Is that also what you set your console to in the IDE?
what is the baud rate and serial communication parameters of your Power Analyzer?

I would suggest you first write just a small piece of code listening for the Power Analyzer. Try to see if this code prints correctly all the stuff your Power Analyzer spits out

#include <SoftwareSerial.h>

// Power Analyzer
SoftwareSerial mySerial(3, 2); // RX, TX -> pin 3 connected to Tx of Power Analyzer, pin 4 to Rx of PA.
String Data ;

void setup() {
  Serial.begin(9600);
  mySerial.begin(9600);
  Data = "";
}

void loop() {
  if (mySerial.available() > 0) {
    int s = mySerial.read();
    if ( s != '\n' && s != '\r' && s != -1) {
      Data = Data + (char) s;
    } else {
      Serial.print("Received : ");
      Serial.println(Data);
      Data = "";
    }
  }
}

Also in your original code, can you test what

  wifi.connectToServer("192.168.254.101", "1738");

returns?

what's the code on that server? why do you say you get garbage on the other side? how are you reading?

Thanks for replying, I have a reference code posted on the Original Post which works and spits out the Power Analyzer's output. The ESP8266 is also set in 9600 baud, I also have a working ESP8266 code that I have tested.

The "server" is a just Socket Test V3.0. The reason I'm saying that I'm getting garbage, is when I try to read mySerial.read(), it doesn't spit out the data correctly with random dots, squares and other stuff that I know isn't supposed to be there.

The ESP8266 on the other hand, works fine and connects to the AP assigned as well as spits out the prints that I need.

I read using the Serial Monitor of the Arduino IDE to confirm that I am getting Garbage reads.

My advice would be to use a more robust system for receiving data. Have a look at Serial Input Basics

...R

can you clarify this

  if( s != ''){ // is a new line, i dont have the power to change it. I am confused by it as well.

why wouldn't that be

    if ( s != '\n' && s != '\r' && s != -1) {

(with s defined as an int, not a char to capture potentially the rare times when you'll get a -1 as returned by read()).

It seems highly unlikely that you have a mySerial connected to the Arduino. Using instance names that make sense is not rocket science.

You are aware that only one instance of SoftwareSerial can listen at a time, right? You are aware that SoftwareSerial can't listen while it is sending, right?

Sometimes you really do need to use more capable hardware.