Serial event wont raised if connect directly to Rx Tx pins

Hello all,
I am trying to use serialEvent() because I need to capture strings that coming through serial from esp8266.

//constants
#define RX 10          
#define TX 11

#include <SoftwareSerial.h>
SoftwareSerial mySerial(RX, TX); // RX, TX
int flag = 0;
int val = 0;
int incomingByte = 0;
void setup() 
{  
  mySerial.begin(9600);
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH); 
  mySerial.println("Hello from arduino");
}

void loop() 
{ 
  
}
void serialEvent()
{
  String str;
  if (Serial.available() > 0)
  {
    str = Serial.readStringUntil('\n');
  }
  mySerial.println(str);

}

As you can see in the code above I am trying to capture the serial string and then print it back to software serial. At the moment esp is connected directly (through level shifters off course) to rx, tx (0,1) pin of arduino uno.
If i put serialEvent() into main loop its possible to read incoming data. But I dont want that i need to raise an event when there is data to capture.

Do you have any ideas?

But I dont want that i need to raise an event when there is data to capture.

The function serialEvent() is absolutely stupidly named. It is called, after loop() iterates, if there is serial data to be processed. It is NOT called when any event happens.

It is possible to modify the interrupt that is triggered when a byte arrives on the serial input pin, but it is HIGHLY unlikely that you need to know when ONE character arrives, so that you can take immediate action.

Especially given that loop() is doing nothing, and you are using blocking code to wait for serial data to arrive.

Do you have any ideas?

Get over it.

You should take a look at Serial Input Basics. It's a good summary. If you end up using other libraries that block, and you are receiving lots of data from the ESP8266, there are more advanced techniques: PaulS mentioned interrupt-driven character processing.

Before trying those advanced techniques, you need to get past the "wait until I get everything" mentality and learn how to handle one character at a time and keep going. No delay, no "readUntil"... no blocking.

Cheers,
/dev

Do you have any ideas?

Use the forum search box in the upper right of his page to find previous discussions of ESP8266 code.

Hello all,
Thank you for answering my questing (in general).

@PaulS:

  1. You are suggesting to modify the interrupt that is triggered when a byte arrives how can I do that?
  2. At the moment loop(){} is empty because I haven't write anything. But probably I will write some processing code in there (read input, process serial input etc) so I prefer to use interrupt logic for serial.
  3. "Get over it", yes this is an idea but I usually prefere to solve my problems than just to get over it.

@/dev:
Thank you for your reading suggestion, and I totally agree with your logic: "get past the "wait until I get everything" mentality". I will try change that.

@zoomkat:
I have already done this before posting my question. Finally I located the problem, was a loose wire on breadboard >:(

Again thank you all for your time and effort.

  1. You are suggesting to modify the interrupt that is triggered when a byte arrives how can I do that?

No, I am not suggesting that you do that. If that was a reasonable thing for you to do, you'd know how.

so I prefer to use interrupt logic for serial.

What you prefer, and what is reasonable, are not necessarily the same thing. Why do you think that you need to know the nanosecond that one byte of serial data arrives? Knowing that the end of a packet arrived is a different story. When that happens, you can parse the stored packet. But, storing the byte somewhere else the nanosecond it arrives does not make sense.

serialEvent() is one of those things like Strings that seems useful when you get into intermediate level programming but quickly gets discarded when you become more advanced.

I don't use it because it's not available on the Arduino Micro and therefore makes my code less portable. Is also a pretty blunt tool.

Interrupts are not appropriate for serial communications.

readStringUntil() should be discarded as soon as you can, to get out of the beginner level.