Trouble with SoftwareSerial. Alternative recommendations?

mitchellclaxton
Posts: 5
Joined: Tue Jan 29, 2019 12:36 am

Trouble with SoftwareSerial. Alternative recommendations?

Postby mitchellclaxton » Fri Mar 08, 2019 12:10 am

Hi all.

I'm using an ESP32-WROVER-KIT in Arduino IDE

I'm trying to get an XBee wireless receiver working with my WROVER using the arduino breakout board and just wiring the digital pins 2, and 3 to GPIOs 2 and 4 on the ESP32 and using the SoftwareSerial library that has been floating around (the one the supposedly doesn't work above 54K baud or whatever) and running it at 38400 baud.

I've been trying this, and while the LED indicators on the XBee breakout board show successful transmission from the transmitter XBee, the ESP32 doesn't seem to be receiving them. I try feeding the received values into the main serial port (the USB) and am getting no results.

I'm wondering if there's a simpler way using HardwareSerial instead but i don't know which IO pins on the development board correspond with the other two UARTs (i'd rather keep the RXD,TXD0 and TXD, RXD0 bridged)

Any suggestions?


Here are the snippets of the code involving the softwareserial

Code: Select all


SoftwareSerial XBee(2,4, false, 256); //Rx, Tx

...
void setup(){
 XBee.begin(38400);
 
 ...
 }
 
 
 void checkSerial(){
  	int intBuffer[3];
  	byte dataCommand = 0;

	while(XBee.available() > 0)
	{
  		Serial.write(XBee.read()); //for testing
	}
 	if(XBee.available() == 3){
   	 	intBuffer[0] = XBee.read(); //when the above serial.write is removed
   		intBuffer[1] = XBee.read();
    		intBuffer[2] = XBee.read();
		blah blah blah
		...
	}
	blah
	...

}


Thank you!

gdsports
Posts: 15
Joined: Wed Aug 02, 2017 12:17 am

Re: Trouble with SoftwareSerial. Alternative recommendations?

Postby gdsports » Fri Mar 08, 2019 9:30 pm

I use hardware Serial2 pins RX=16,TX=17 on a SparkFun Thing ESP32.

Code: Select all

Serial2.begin(115200);
...
while (Serial2.available() > 0) {
	Serial2.read();
}

idahowalker
Posts: 166
Joined: Wed Aug 01, 2018 12:06 pm

Re: Trouble with SoftwareSerial. Alternative recommendations?

Postby idahowalker » Fri Mar 08, 2019 11:15 pm

You got 3 Serial ports you can use but, for now stay away from 0, till you learn a few things.

To start with you'd want to include this library: #include <HardwareSerial.h>

Then you want to declare the ports you'll be using like so:

HardwareSerial SerialTFMini( 1 );
HardwareSerial SerialController( 2 );
// serial(1) = pin27=RX green wire, pin26=TX white wire

Now you can use about any pins for rx and tx you want but I tend to stick with GPIO16, rx, and GPIO17 for (2).

You'll find GPIO2 is for the built in LED on for the ESP32Dev, you might want to consider using another pin.

I have: #define SerialDataBits 115200

In setup I have:
Serial.begin( SerialDataBits );
SerialController.begin( SerialDataBits );
SerialTFMini.begin( SerialDataBits, SERIAL_8N1, 27, 26 );

Notice for the SerialTFMini, I defined the pins, for (1) when the baud rate was declared.

After that, just use em like a serial port but referring to them with the names you assigned to them.

Good luck

mitchellclaxton
Posts: 5
Joined: Tue Jan 29, 2019 12:36 am

Re: Trouble with SoftwareSerial. Alternative recommendations?

Postby mitchellclaxton » Wed Mar 13, 2019 1:21 am

OK so i went with UART 2 instead of UART 1 and i used SERIAL_8N1 because i'm using no parity.

I'm getting some readings back now but it's the same thing. 0, 28 over and over whenever i try to pass in a serial command.

Does this mean anything to you?

idahowalker
Posts: 166
Joined: Wed Aug 01, 2018 12:06 pm

Re: Trouble with SoftwareSerial. Alternative recommendations?

Postby idahowalker » Wed Mar 13, 2019 8:18 pm

mitchellclaxton wrote:
Wed Mar 13, 2019 1:21 am
OK so i went with UART 2 instead of UART 1 and i used SERIAL_8N1 because i'm using no parity.

I'm getting some readings back now but it's the same thing. 0, 28 over and over whenever i try to pass in a serial command.

Does this mean anything to you?
That does not mean anything to me.

Perhaps posting your code you are using for reading the serial port?

Here is the code I have to read the serial port on the ESP32:

In the declaration section, other code for other tasks and other declarations will be missing

Code: Select all

#define evtReceiveSerial_LIDAR       ( 1 << 0 ) //1
TaskHandle_t xHandle_ReceiveSerial_LIDAR = NULL; 
hw_timer_t * timer = NULL;
/* create event group */
EventGroupHandle_t eg;
SemaphoreHandle_t sema_ReceiveSerial_LIDAR;
#define TimerDivider 80
#define TaskCore1 1
#define TaskCore0 0
#define ReceiveSerialStackSize 20000
#define SerialDataBits 115200
HardwareSerial LIDARSerial ( 2 );
// pin 26=RX, pin 25=TX
String sSerial = "";
String sSerialSentence = "";
void IRAM_ATTR onTimer()
{
  BaseType_t xHigherPriorityTaskWoken;
  iTicCount++;

  if ( (iTicCount % 2) == 0 )
  {
    if ( (xSemaphoreTakeFromISR(sema_ReceiveSerial_LIDAR, &xHigherPriorityTaskWoken)) == pdTRUE ) // grab semaphore, no wait
    {
      xEventGroupSetBitsFromISR(eg, evtReceiveSerial_LIDAR, &xHigherPriorityTaskWoken); // trigger every 2mS, if not already processing
    }
  }
  if ( iTicCount == OneK )
  {
    iTicCount = 0;
  }
} // void IRAM_ATTR onTimer()
Code in setup

Code: Select all

void setup()
{
  LIDARSerial.begin ( SerialDataBits, SERIAL_8N1, 26, 25 );
 eg = xEventGroupCreate();
timer = timerBegin( TIMER_FOUR, TimerDivider, true );
  timerAttachInterrupt( timer, &onTimer, true );
  ///
  /// ??? set call back to 500 for .5mS ?????
  ///
  timerAlarmWrite(timer, OneK, true);
  timerAlarmEnable(timer);
  ///////////////
 xTaskCreatePinnedToCore ( fReceiveSerial_LIDAR, "fReceiveSerial_LIDAR", ReceiveSerialStackSize, NULL, Priority5, &xHandle_ReceiveSerial_LIDAR, TaskCore1 ); // assigned to core
  sema_ReceiveSerial_LIDAR = xSemaphoreCreateMutex();
  xSemaphoreGive ( sema_ReceiveSerial_LIDAR );
}
The code for the receive serial task:

Code: Select all

void fReceiveSerial_LIDAR( void * parameters  )
{
  bool BeginSentence = false;
  sSerial.reserve ( StringBufferSize300 );
  char OneChar;
  for ( ;; )
  {
    EventBits_t xbit = xEventGroupWaitBits (eg, evtReceiveSerial_LIDAR, pdTRUE, pdTRUE, portMAX_DELAY);
    if ( LIDARSerial.available() >= 1 )
    {
      while ( LIDARSerial.available() )
      {
        OneChar = LIDARSerial.read();
        if ( BeginSentence )
        {
          if ( OneChar == '>')
          {
            if ( xSemaphoreTake( sema_ParseLIDAR_ReceivedSerial, xSemaphoreTicksToWait10 ) == pdTRUE )
            {
              xQueueOverwrite( xQ_LIDAR_Display_INFO, ( void * ) &sSerial );
              xEventGroupSetBits( eg, evtParseLIDAR_ReceivedSerial );
              //
            }
            BeginSentence = false;
            break;
          }
          sSerial.concat ( OneChar );
        }
        else
        {
          if ( OneChar == '<' )
          {
            sSerial = ""; // clear string buffer
            BeginSentence = true; // found begining of sentence
          }
        }
      } //  while ( LIDARSerial.available() )
    } //if ( LIDARSerial.available() >= 1 )
    xSemaphoreGive( sema_ReceiveSerial_LIDAR );
  }
  vTaskDelete( NULL );
} //void fParseSerial( void * parameters  )
I send sentences that begin with a "<" and end with a ">". If a "<" is not received no recording of received serial is done till a "<" is received. Recording is done. of incoming serial until the end of a sentence ">" is received. At which time the received serial is placed into a queue and sent to a serial parser. In this way the serial receiver can get back to work.

Who is online

Users browsing this forum: No registered users and 57 guests