Hallo alex
ein prog als BLE server mit Quitierung
// Blaue Led an Pin 8 zur Meldung
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#include <SoftwareSerial.h>
String Test="";
#define LED_PIN 8
BLECharacteristic *pCharacteristic;
bool deviceConnected = false;
// UART Service UUIDs (Nordic UART Service)
#define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E"
#define CHARACTERISTIC_RX_UUID "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
#define CHARACTERISTIC_TX_UUID "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
class ServerCallbacks : public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
Serial.println("Verbunden");
}
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
Serial.println("Getrennt");
BLEDevice::startAdvertising();
}
};
class RXCallbacks : public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pChar) {
String received = pChar->getValue().c_str();
1 |
received.trim();
|
2 |
Test=received; //wird in loop bearbeitet
|
3 |
if (received == "ON") {
|
4 |
digitalWrite(LED_PIN, HIGH);
|
5 |
}
|
6 |
else if (received== "OFF") {
|
7 |
digitalWrite(LED_PIN, LOW);
|
8 |
}
|
Serial.print("Empfangen: ");
Serial.println(received);
String reply = "Echo: " + received; //der server hängt das dran zur kontrolle
pCharacteristic->setValue(reply.c_str());
pCharacteristic->notify();
}
};
void setup() {
Serial.begin(115200);
//setup_();
delay(100);
pinMode(LED_PIN, OUTPUT);
BLEDevice::init("ESP32C3_BLE");
BLEServer *pServer = BLEDevice::createServer();
pServer->setCallbacks(new ServerCallbacks());
BLEService *pService = pServer->createService(SERVICE_UUID);
// TX (ESP32 -> Handy)
pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_TX_UUID,
BLECharacteristic::PROPERTY_NOTIFY
);
pCharacteristic->addDescriptor(new BLE2902());
// RX (Handy -> ESP32)
BLECharacteristic *pRX = pService->createCharacteristic(
CHARACTERISTIC_RX_UUID,
BLECharacteristic::PROPERTY_WRITE
);
pRX->setCallbacks(new RXCallbacks());
pService->start();
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->start();
}//setup
void aktion(int nr)
{
if( nr==1)
{
digitalWrite(LED_PIN, LOW);
delay(500);
digitalWrite(LED_PIN, HIGH);
delay(500);
digitalWrite(LED_PIN, LOW);
delay(500);
digitalWrite(LED_PIN, HIGH);
}
}
void Auswertung_Test()
{
if (Test=="")
{}
else
{
String s = String(Test);
int Be_zahl = s.toInt();
if (Be_zahl!=0) {aktion(Be_zahl);}//Bluetooth empfang und Aktion/ledpos,255);
Test="";
delay(50);
}
}
void loop() {
Auswertung_Test();
Test="";
}
Alexander schrieb:
Den Namen gibt's zweimal, einmal als Geräte-Name und einmal im
Advertising
wo wird das eingefügt das der name immer übernommen wird
Der 81 jährige
p.s.
ach ja, es ist ein esp32c3 super mini