Software serial using ESP8266

1.8k views Asked by At

I am transmitting the CSV file saved on SD card using STM32F103 on UART line to ESP8266, The file reading and UART transmission snippet is as below. While loop of the STM32f103 for read/transmit file.

Using the Software Serial code on ESP8266 code I can read the transmitted string from the STM32F103 and the actual length of the transmitted string is 59 but I am getting following output on the Serial monitor ESP8266 serial monitor output

The code for Software is as follows


#include <SoftwareSerial.h>
#include <Arduino.h>

#include <stdio.h>

#include <string.h>

using namespace std;

SoftwareSerial s; //RX TX

String str= "";
String str_tx = "";
char char_array[60];
double buff[24];
int str_len = 0;
double val;
char* token; 
//char* rest = char_array;
int i=0;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  s.begin(115200, SWSERIAL_8N1, 13, 15, false, 128); //115200, SWSERIAL_8N1, 13, 15, false, 512
  pinMode(13, INPUT);
  pinMode(15, OUTPUT);

  Serial.println("in setup 1");
  Serial.println("in setup 2");
  Serial.println("in setup 3");
  
}

void loop() {
  // put your main code here, to run repeatedly:2
 while(s.available() >0)
 {

  char ch=s.read();
  if(ch != '\n')
    str.concat(ch);
  if( ch =='\n' )
  {
    /*Serial.println("------------------------------------");
    Serial.println(char_array);
    Serial.println("------------------------------------");*/
    Serial.println(str);
    str_len = str.length();
    Serial.println(str_len);
    str.toCharArray(char_array, str_len);
    Serial.println(char_array);
    //strcpy(char_array, str.c_str());
    memset(char_array, 0, sizeof(char_array));
    str="";
    //break;
  }
  /*
  str = s.readStringUntil('\n');
  Serial.println(str); 
  str_len = str.length();
  Serial.println(str_len);
  //Serial.println(str.c_str());
  //strcpy(char_array, str.c_str());
  
  //str.toCharArray(char_array, str_len); 
  //Serial.println(char_array);*/
}

 
}

I have used the above code for the reading but the length is showing different after the 1st string which goes around 125 and because of that first time its getting copied into char buffer but further its showing empty.

Would appreciate if anyone could throw some light on what's causing this issue and how to solve it, Thankyou!

1

There are 1 answers

0
Saadat On

Software serial does not work very well on ESP8266. Use lower bitrate. It may be helpful but does not completely solve the problem. I had the same problem using GSM modules.

I used the hardware serial (Serial pin 1 , 3) for this communication and used a software serial to print out the debug information. It make it really hard to work with; because the port is used for programming; you need to add 470R resistor to prevent any damage to your module. Every time you change the code, you need to connect the programmer, program the module then you need to remove the programmer so the board can communicate with your STM32. But using the hardware serial was very stable and solved my problem.