Example sketch for connecting Sparkfun Thing Plus ESP32-C6 to macOS via bluetooth?
ESP32-C6 BLE Example for Arduino Core
The user is asking for an example sketch for connecting a SparkFun Thing Plus ESP32-C6 to macOS via Bluetooth. This is a specific hardware + software question.
Let me think about what I know:
- SparkFun Thing Plus ESP32-C6 is a development board with ESP32-C6 chip
- ESP32-C6 has Bluetooth 5 (LE) support
- macOS has Bluetooth LE support built-in
- They likely want a simple BLE sketch that can be discovered and connected from macOS
I should search for specific examples or documentation for this board and BLE on ESP32-C6, or provide a standard ESP32 BLE example that would work with this board since the ESP32-C6 Arduino core supports BLE.
Actually, I know that the ESP32-C6 Arduino core supports BLE via the NimBLE or standard BLE library. A simple BLE server sketch would work. Let me provide a practical example.
Key components:
- Include BLEDevice.h
- Create a BLE server
- Create a service and characteristic
- Start advertising
- macOS can connect using any BLE terminal app or built-in tools
Let me provide a complete, working example sketch.
Here's a complete example sketch for the SparkFun Thing Plus ESP32-C6 that creates a BLE UART server (similar to a virtual serial port). macOS can connect to it using the built-in Bluetooth menu or any BLE terminal app (e.g., Serial Bluetooth Terminal, LightBlue, or the macOS Terminal with
ble-serial).cpp
Copy
/*
SparkFun Thing Plus ESP32-C6 BLE Example
Creates a BLE UART server that macOS can discover and connect to.
Board: "SparkFun ESP32-C6 Thing Plus" (or "ESP32C6 Dev Module")
Upload Speed: 921600
Partition Scheme: Default 4MB with spiffs
*/
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
// BLE UART Service UUIDs (standard Nordic UART Service)
#define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E"
#define CHARACTERISTIC_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
#define CHARACTERISTIC_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
BLEServer *pServer = NULL;
BLECharacteristic *pTxCharacteristic = NULL;
BLECharacteristic *pRxCharacteristic = NULL;
bool deviceConnected = false;
uint8_t txValue = 0;
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
Serial.println("Client connected");
};
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
Serial.println("Client disconnected");
// Restart advertising so macOS can reconnect
delay(500);
pServer->startAdvertising();
}
};
class MyCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
std::string rxValue = pCharacteristic->getValue();
if (rxValue.length() > 0) {
Serial.print("Received from macOS: ");
for (int i = 0; i < rxValue.length(); i++) {
Serial.print(rxValue[i]);
}
Serial.println();
// Echo back with prefix
std::string response = "Echo: " + rxValue;
pTxCharacteristic->setValue(response);
pTxCharacteristic->notify();
}
}
};
void setup() {
Serial.begin(115200);
while (!Serial) { ; } // Wait for native USB on C6
delay(1000);
Serial.println("Starting BLE on Thing Plus ESP32-C6...");
// Initialize BLE
BLEDevice::init("ThingPlus-C6"); // This name appears in macOS Bluetooth menu
// Create server
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
// Create UART service
BLEService *pService = pServer->createService(SERVICE_UUID);
// TX Characteristic (ESP32 -> macOS, notify)
pTxCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_TX,
BLECharacteristic::PROPERTY_NOTIFY
);
pTxCharacteristic->addDescriptor(new BLE2902());
// RX Characteristic (macOS -> ESP32, write)
pRxCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_RX,
BLECharacteristic::PROPERTY_WRITE
);
pRxCharacteristic->setCallbacks(new MyCallbacks());
// Start service and advertising
pService->start();
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->setScanResponse(true);
pAdvertising->setMinPreferred(0x06); // iOS/macOS connection stability
pAdvertising->setMinPreferred(0x12);
BLEDevice::startAdvertising();
Serial.println("Ready! Look for 'ThingPlus-C6' in macOS Bluetooth.");
Serial.println("On macOS: System Settings -> Bluetooth -> Connect");
}
void loop() {
if (deviceConnected) {
// Example: send a heartbeat message every 2 seconds
static unsigned long lastSend = 0;
if (millis() - lastSend > 2000) {
lastSend = millis();
String msg = "Heartbeat: " + String(millis() / 1000) + "s\n";
pTxCharacteristic->setValue(msg.c_str());
pTxCharacteristic->notify();
Serial.println("Sent: " + msg);
}
}
delay(10);
}macOS Connection Steps
- Upload the sketch to the Thing Plus C6 via USB-C.
- Open macOS System Settings → Bluetooth.
- Look for "ThingPlus-C6" in Nearby Devices and click Connect.
- To send/receive data, use a BLE terminal app from the App Store, or install
ble-serialvia Terminal:
bash
Copy
pip3 install ble-serial
ble-scan # Find the MAC address
ble-serial -d <MAC_ADDRESS>Key Notes for the C6
- The ESP32-C6 uses BLE 5.0 (not classic Bluetooth SPP). macOS handles BLE natively, but you cannot use it as a standard
/dev/tty.*serial port without a helper app like the one above. - If the board doesn't appear in the Arduino IDE, add the ESP32 board package:
File → Preferences → Additional Boards Manager URL:https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json - Then install "ESP32 by Espressif Systems" (v3.0+) and select "SparkFun ESP32-C6 Thing Plus".
If you need classic Bluetooth SPP (like a traditional COM port) instead of BLE, that requires an ESP32 classic (like the ESP32-WROOM), as the C6 only supports BLE.