PlatformIO Community

serialEvent does not receive data over UART?

Hi, hope someone can help, please.

I can transmit data over a UART from an ESP32 to a terminal, but I cannot receive data sent from the terminal. I’m using a standard Arduino example. My intended use is to have the program notify (interrupt) main when a null or carriage returned string has been received. I do not want to poll the characters as they arrive. Code provided below. Many thanks. Greg

#include <arduino.h>
#include <HardwareSerial.h>

// #defines
#define LED_Blue 18
#define LED_POWER 19
#define Delay 1000                // Delay in milliseconds

String inputString = "";          // a String to hold incoming data
bool stringComplete = false;      // whether the string is complete

void setup() {
    
  Serial.begin(115200);           // get the serial port working at 115200 Baud.   
  inputString.reserve(200);       // reserve 200 bytes for the inputString:
  pinMode(LED_Blue, OUTPUT);
  pinMode(LED_POWER, OUTPUT);
  digitalWrite(LED_POWER, 1);
  digitalWrite(LED_Blue, 0);    //led On
  delay(1000);                  // wait for a second
  digitalWrite(LED_Blue, 1);    //led Off
  Serial.println("Waiting for a string");
  Serial.println();             // prints a new line
}

void loop() {
  if (stringComplete) {
    Serial.println(inputString);    // print the string when a newline arrives:
    inputString = "";               // clear the string:
    stringComplete = false;
  }
}

void serialEvent() {
  while (Serial.available()) {
    char inChar = (char)Serial.read();  // get the new byte:
    inputString += inChar;              // add char to the inputString:
    if (inChar == '\r') {               // if the incoming character is a carriage return, set flag
      stringComplete = true;
    }
  }
}

As far as I can tell, serialEvent() has not been implemented in the Arduino core for ESP32.

You can easily achieve what you want to do with:

void serialEvent();

void loop() {
    if (Serial.available())
        serialEvent();
}

void serialEvent() {
    char inChar = (char)Serial.read();
    inputString += inCar;
    if (inChar == '\r') {
        Serial.println(inputString);
        inputString = "";
    }
}

I haven’t tried it though.

1 Like

Yep seems broken for specfic serials, see PRs like https://github.com/espressif/arduino-esp32/pull/3733

But the code still says

So if you call your function serialEventRun you get a function which is always called after loop in which you can chek stuff. So it’s functionally the same as always doing it in loop() anyways.

Manuel, morning.

Your code works perfectly, once typo slip “inCar” is corrected, thanks.

As ever, there are always supplementary question, especially since I’m new to esp32, platformio etc.

In the project .ini file there are two options for framework; arduino and espidf.
As you have pointed out via serialEvent, not all Arduino library functions are available in the Arduino framework for ESP32/PlatformIO. I’ve also notice that there are similar library functions available in espidf. Who decides whats in or omitted?

So…
Q1: Is there a list of all the valid arduino functions available to ESP32 and platformio.

I’ve found this Espressif Arduino Libraries, but the libraries are listed by project, not by peripheral. I gues what I’m asking for is “where are the resources (libraries, functions, descriptions, examples etc) relating to a specific peripheral or feature?”; in this instance UART.

The library list for espressif is organised on their website.

Q2: What is the policy regarding using both arduino and espressif in the same project. Can it be done, should it be done, what are the strengths and weakness, if you wanted to do it “How?”. (I know this is an involved question, but I can only find opinion on the web. Since platformio accomodates both frameworks, I’m sure you can provide clarity?

Thanks

Arduino-ESP32 uses in many instances ESP-IDF functions to implement everything. As a base target, they want to implement all functionality of the Arduino API (https://github.com/espressif/arduino-esp32/issues/1148#issuecomment-368384375), so they redirect to the documentation of the general Arduino API. In some special cases like this (serialEvent) they however differ from the standard core, which can only be found in the code. To my best knowledge that’s pretty much the only point they differ… and then also only slightly in function name, as you can see in my post above.

Missing / empty link.

As above, in this issue they refer to the generael Arduino and ESP-IDF docs

See docs and official example. The obvious pro is that you get a ESP-IDF base-project with the Arduino-core as addon, so you can take full advantage of advanced ESP-IDF APIs. Cons may be that configuration might be more difficult (through the documented CMakeLists.txt commands) for beginners.