#include <BluetoothSerial.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include<WiFi.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
BluetoothSerial SerialBT;
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
Serial.begin(115200);
Serial.println(WiFi.macAddress());
// Start Bluetooth with the name "ESP32_Device1"
if (SerialBT.begin("ESP32_Device1")) {
while (1); // Halt if Bluetooth fails to start
}
Serial.println("Device 1 Bluetooth Started. Waiting for connection...");
// Initialize the OLED display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // I2C address 0x3C
while (1); // Halt if display initialization fails
}
display.clearDisplay(); // Clear the buffer
display.setTextSize(1); // Set text size to 1
display.setTextColor(WHITE); // Set text color to white
display.setCursor(0, 0); // Set the cursor to top-left corner
display.println("Waiting for Device 2...");
display.display(); // Display the text
}
void loop() {
if (SerialBT.hasClient()) {
// Check if the client is connected
Serial.println("Device 1: Connected to a client!");
// Display the connection status on the OLED
display.clearDisplay();
display.setCursor(0, 0);
display.println("Connected to Client");
display.display();
// Read incoming data from the client
if (SerialBT.available()) {
String receivedData = SerialBT.readString();
Serial.println("Device 1 Received: " + receivedData);
// Display the received data on the OLED
display.clearDisplay();
display.setCursor(0, 0);
display.println("Received:");
display.setCursor(0, 10);
display.println(receivedData);
display.display();
// Send a response back to the client
SerialBT.println("Device 1 Acknowledged: " + receivedData);
}
} else {
// If no client is connected, display a message
display.clearDisplay();
display.setCursor(0, 0);
display.println("Waiting for Device 2...");
display.display();
}
delay(20); // Small delay for stability
}