Create a networking project using the ESP32C3
I created a simple Bluetooth project that lets me control an LED from my phone. When connected via Bluetooth, I can turn the LED on and off remotely.
I used the Xiao ESP32C3 for this project because of its built-in Bluetooth capabilities (and also because this is what I was using for my FP) The steps were:
Here's the code for the LED control:
#include
#include
#include
#define LED_PIN 2
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
BLECharacteristic* characteristic;
bool deviceConnected = false;
class MyServerCallbacks : public BLEServerCallbacks {
void onConnect(BLEServer* server) override {
deviceConnected = true;
Serial.println("Device connected!");
}
void onDisconnect(BLEServer* server) override {
deviceConnected = false;
Serial.println("Device disconnected!");
}
};
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
BLEDevice::init("LED_Control");
BLEServer* server = BLEDevice::createServer();
server->setCallbacks(new MyServerCallbacks());
BLEService* service = server->createService(SERVICE_UUID);
characteristic = service->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE
);
service->start();
BLEAdvertising* advertising = BLEDevice::getAdvertising();
advertising->addServiceUUID(SERVICE_UUID);
advertising->start();
}
void loop() {
if (deviceConnected) {
std::string value = characteristic->getValue();
if (value == "ON") {
digitalWrite(LED_PIN, HIGH);
} else if (value == "OFF") {
digitalWrite(LED_PIN, LOW);
}
}
delay(20);
}