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.