Created
March 11, 2024 12:12
-
-
Save Stobf1985/b03cd87aa50860667312b51a58c8ffbf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <BLEDevice.h> | |
#include <BLEUtils.h> | |
#include <BLEServer.h> | |
#include <ESP32Servo.h> | |
#include <driver/ledc.h> | |
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b" | |
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8" | |
#define GENERIC_ACCESS_UUID "00001800-0000-1000-8000-00805f9b34fb" | |
#define GENERIC_CHARACTERISTIC_UUID "00002aa6-0000-1000-8000-00805f9b34fb" | |
Servo servoTurn; | |
Servo servoDrive; | |
int pinServoTurn = 4; | |
int pinServoDrive = 3; | |
int pinLED = 8; | |
bool parkingMode = false; | |
bool neutralMode = false; | |
bool reverseMode = false; | |
class CarCallbacks: public BLECharacteristicCallbacks { | |
void onWrite(BLECharacteristic *pCharacteristic) { | |
std::string rxValue = pCharacteristic->getValue(); | |
Serial.print("Received Value: "); | |
Serial.println(rxValue.c_str()); | |
if (rxValue.length() > 0) { | |
Serial.println("Processing command..."); | |
if (rxValue.length() < 2 || !isalpha(rxValue[0])) { | |
Serial.println("Invalid command format!"); | |
return; | |
} | |
char commandType = rxValue[0]; | |
int commandValue = atoi(rxValue.substr(1).c_str()); | |
Serial.print("Command Type: "); | |
Serial.println(commandType); | |
Serial.print("Command Value: "); | |
Serial.println(commandValue); | |
switch (commandType) { | |
case 'T': | |
Serial.print("Turning to angle: "); | |
Serial.println(commandValue); | |
if (commandValue >= 0 && commandValue <= 180) { | |
servoTurn.write(commandValue); | |
} else { | |
Serial.println("Angle value out of range!"); | |
} | |
break; | |
case 'S': | |
Serial.print("Setting speed: "); | |
Serial.println(commandValue); | |
if (!parkingMode && !neutralMode) { | |
if (commandValue >= 0 && commandValue <= 180) { | |
if (reverseMode) { | |
servoDrive.write(180 - commandValue); // Reverse mode | |
} else { | |
servoDrive.write(commandValue); // Forward mode | |
} | |
} else { | |
Serial.println("Speed value out of range!"); | |
} | |
} | |
break; | |
case 'L': | |
Serial.print("Setting LED state to: "); | |
Serial.println(commandValue); | |
if (commandValue == 0 || commandValue == 1) { | |
digitalWrite(pinLED, commandValue); | |
} else { | |
Serial.println("LED state value out of range!"); | |
} | |
break; | |
case 'P': | |
parkingMode = (commandValue == 1); | |
break; | |
case 'N': | |
neutralMode = (commandValue == 1); | |
break; | |
case 'R': | |
reverseMode = (commandValue == 1); | |
break; | |
default: | |
Serial.println("Unknown command type!"); | |
} | |
} | |
} | |
}; | |
void setup() { | |
Serial.begin(115200); | |
Serial.println("Initializing..."); | |
ledc_timer_config_t ledc_timer = { | |
.speed_mode = LEDC_LOW_SPEED_MODE, | |
.duty_resolution = LEDC_TIMER_10_BIT, | |
.timer_num = LEDC_TIMER_0, | |
.freq_hz = 5000 | |
}; | |
ledc_timer_config(&ledc_timer); | |
servoTurn.attach(pinServoTurn); | |
servoDrive.attach(pinServoDrive); | |
pinMode(pinLED, OUTPUT); | |
int middlePosition = 90; // Серединне значення для діапазону 0-180 градусів | |
servoTurn.write(middlePosition); | |
BLEDevice::init("RC Car Controller"); | |
BLEServer *pServer = BLEDevice::createServer(); | |
BLEService *pCustomService = pServer->createService(GENERIC_ACCESS_UUID); | |
BLECharacteristic *pCustomCharacteristic = pCustomService->createCharacteristic( | |
GENERIC_CHARACTERISTIC_UUID, | |
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | |
); | |
BLEService *pService = pServer->createService(SERVICE_UUID); | |
BLECharacteristic *pCharacteristic = pService->createCharacteristic( | |
CHARACTERISTIC_UUID, | |
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | |
); | |
pCharacteristic->setCallbacks(new CarCallbacks()); | |
pService->start(); | |
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising(); | |
pAdvertising->addServiceUUID(SERVICE_UUID); | |
pAdvertising->setScanResponse(true); | |
pAdvertising->setMinPreferred(0x06); | |
pAdvertising->setMinPreferred(0x12); | |
BLEDevice::startAdvertising(); | |
Serial.println("Waiting for a client connection to notify..."); | |
} | |
void loop() { | |
} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment