A lightweight, thread-safe Serial communication library for ESP32 with FreeRTOS support.
- Thread-Safe: Uses mutex protection for multi-core ESP32 applications
- Lightweight: Minimal overhead with simple mutex implementation
- Easy to Use: Drop-in replacement for standard Serial functions
- FreeRTOS Compatible: Safe for use across multiple FreeRTOS tasks
- Open Arduino IDE
- Go to Sketch → Include Library → Manage Libraries
- Search for "Esp32-RTOS-Serial"
- Click Install
- Download this repository as ZIP
- In Arduino IDE: Sketch → Include Library → Add .ZIP Library
- Select the downloaded ZIP file
#include <rtosSerial.h>
void setup() {
Serial.begin(115200);
rtosSerialInit(128); // Initialize the thread-safe serial interface, 128byte buffer each task
}
void loop() {
// Thread-safe serial operations
rtosPrintln("Hello World");
rtosPrint("Hello ");
rtosPrintf("Value: %d", 42);
// Thread-safe read
String input = rtosRead();
if (input.length() > 0) {
rtosPrintln("Received: " + input);
}
delay(1000);
}Initialize the thread-safe serial interface. Must be called once in setup().
Thread-safe print without newline.
Thread-safe print with newline.
Thread-safe printf-style printing with automatic newline.
Thread-safe read until newline. Returns empty string if no data available. Note: Use only in one task to avoid conflicts.
#include <rtosSerial.h>
void task1(void* parameter) {
while (true) {
rtosPrintln("Task 1 running");
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
void task2(void* parameter) {
while (true) {
rtosPrintf("Task 2 counter: %d", millis());
vTaskDelay(pdMS_TO_TICKS(1500));
}
}
void setup() {
Serial.begin(115200);
rtosSerialInit();
xTaskCreate(task1, "Task1", 2048, NULL, 1, NULL);
xTaskCreate(task2, "Task2", 2048, NULL, 1, NULL);
}
void loop() {
// Main loop can also use serial safely
rtosPrintln("Main loop");
delay(2000);
}- ESP32 board
- Arduino IDE with ESP32 board package
- FreeRTOS (included with ESP32 Arduino Core)
This project is licensed under the MIT License - see the LICENSE file for details.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Hamza Yesilmen
- GitHub: @HamzaYslmn
- Email: resmiyslmn@gmail.com