// ESP32 Sender - Sends "hello" every 5 seconds and receives "ciao"
#include <Arduino.h>
// Define UART pins
#define RX_PIN 16 // GPIO16 for UART2 RX
#define TX_PIN 17 // GPIO17 for UART2 TX
// Timer variables
unsigned long previousMillis = 0;
const long interval = 5000; // 5 seconds interval
void setup() {
// Initialize Serial Monitor for debugging
Serial.begin(115200);
Serial.println("ESP32 Sender Initialized");
// Initialize UART2 for communication with the second ESP32
Serial2.begin(9600, SERIAL_8N1, RX_PIN, TX_PIN);
}
void loop() {
// Check if 5 seconds have passed
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// Save the last time a message was sent
previousMillis = currentMillis;
// Send "hello" to the other ESP32
Serial2.println("hello");
Serial.println("Sent: hello");
}
// Check if data is available from the other ESP32
if (Serial2.available()) {
// Read the incoming message
String message = Serial2.readStringUntil('\n');
message.trim(); // Remove any whitespace or newline characters
// Print received message
Serial.print("Received: ");
Serial.println(message);
}
}