ESP32C3 Arduino Bluetooth keine Reaktion

#7413050
Lesenswert?

Hallo Zusammen, ich bin komplett neu in diesem Thema versuche aber mein bestes.

Ich habe nun das Problem das ich zwei ESP32C3 verbinden will. Einen als Sender den anderen als Empfänger. Der Sender funktioniert und sendet sein Signal.(kann ich mit dem Handy auslesen) Das Problem ist nun allerdings das mein Empfänger das Signal nicht verarbeitet bzw. nicht bekommt. Ich habe die Vermutung das der Empfänger das Signal nicht lesen kann. Da ich generell wenig zu dem Thema gefunden habe und mich mit dem Problem nun 2 Tage im Kreis drehe bitte ich hier mal um hilfe.

Sender:

1
#include <BLEDevice.h>
2
#include <BLEUtils.h>
3
#include <BLEServer.h>
4
#include <BLE2902.h>
5

6

7
BLECharacteristic *pCharacteristic;
8

9
#define BUTTON_PIN 10
10
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
11
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
12

13
BLEServer *pServer = NULL;
14
 
15

16
class MyServerCallbacks: public BLEServerCallbacks {
17
    void onConnect(BLEServer* pServer) {
18
        Serial.println("Connected");
19
    }
20

21
    void onDisconnect(BLEServer* pServer) {
22
        Serial.println("Disconnected");
23
    }
24
};
25

26
void setup() {
27
  Serial.begin(115200);
28
  
29
    pinMode(BUTTON_PIN, INPUT_PULLUP);
30

31
    BLEDevice::init("Sender");
32
    pServer = BLEDevice::createServer();
33
    pServer->setCallbacks(new MyServerCallbacks());
34

35
    BLEService *pService = pServer->createService(BLEUUID(SERVICE_UUID));
36
    pCharacteristic = pService->createCharacteristic(
37
                        BLEUUID(CHARACTERISTIC_UUID),
38
                        BLECharacteristic::PROPERTY_NOTIFY
39
                      );
40
    pCharacteristic->addDescriptor(new BLE2902());
41

42
    pService->start();
43

44
    BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
45
    pAdvertising->addServiceUUID(BLEUUID(SERVICE_UUID));
46
    pAdvertising->setScanResponse(false);
47
    pAdvertising->setMinPreferred(0x0);  // set value to 0x00 to not advertise this parameter
48
    BLEDevice::startAdvertising();
49
}
50

51
void loop() {
52
    int buttonState = digitalRead(BUTTON_PIN);
53
    if (buttonState == LOW) {
54
        uint8_t value = 1;
55
        pCharacteristic->setValue(&value, 1);
56
        pCharacteristic->notify();
57
        Serial.println("Button pressed, LED on");
58
    } else {
59
        uint8_t value = 0;
60
        pCharacteristic->setValue(&value, 1);
61
        pCharacteristic->notify();
62
        Serial.println("Button released, LED off");
63
    }
64
    delay(100);
65
}

Empfänger:

1
#include <BLEDevice.h>
2
#include <BLEUtils.h>
3
#include <BLEServer.h>
4

5
BLECharacteristic *pCharacteristic;
6

7
#define LED_PIN 10
8
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
9
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
10

11

12

13
class MyClientCallback : public BLEClientCallbacks {
14
    void onConnect(BLEClient* pClient) {
15
        Serial.println("Connected");
16
    }
17

18
    void onDisconnect(BLEClient* pClient) {
19
        Serial.println("Disconnected");
20
    }
21
};
22

23
void setup() {
24
    Serial.begin(115200);
25
    pinMode(LED_PIN, OUTPUT);
26
    digitalWrite(LED_PIN, HIGH);
27
  delay(1000);
28
  digitalWrite(LED_PIN, LOW);
29

30
    BLEDevice::init("Receiver");
31
    BLEClient* pClient = BLEDevice::createClient();
32
    pClient->setClientCallbacks(new MyClientCallback());
33
    
34
    BLEScan *pScan = BLEDevice::getScan();
35
    pScan->setActiveScan(true);
36
    pScan->start(5);
37

38
   
39

40

41
    BLERemoteService* pRemoteService = pClient->getService(BLEUUID(SERVICE_UUID));
42
    BLERemoteCharacteristic* pRemoteCharacteristic = pRemoteService->getCharacteristic(BLEUUID(CHARACTERISTIC_UUID));
43
   pRemoteCharacteristic->registerForNotify([](BLERemoteCharacteristic* pBLERemoteCharacteristic, uint8_t* pData, size_t length, bool isNotify) {
44
    if (pData[0] == 1) {
45
        digitalWrite(LED_PIN, HIGH);
46
        Serial.println("LED on");
47
    } else {
48
        digitalWrite(LED_PIN, LOW);
49
        Serial.println("LED off");
50
    }
51
});
52

53

54
}
55

56
void loop() {
57
}

Freu mich über alle Tipps und Lösungen.

#7415686
Lesenswert?

Hallo,

schau mal meinen Testclient an. Der funkt mit einer Bluetti Powerstation.

Was mir aufgefallen ist:

. Muss der Notify-Callback so inline sein?

Ist schwer zu debuggen.

. vielleicht solltest die return values der Aufrufe auswerten?!

Bsp.:

1
/* Obtain a reference to the characteristic in the service of the remote BLE server */
2
  pRemoteNotifyCharacteristic = pRemoteService->getCharacteristic(NOTIFY_UUID);
3
  if (pRemoteNotifyCharacteristic == nullptr)
4
  {
5
    Serial.print("Failed to find our characteristic UUID: ");
6
    Serial.println(NOTIFY_UUID.toString().c_str());
7
    pClient->disconnect();
8
    return false;
9
  }
10
  Serial.println(" - Found our characteristic");
11

12
  /* Read the value of the characteristic */
13
  if(pRemoteNotifyCharacteristic->canRead())
14
  {
15
    std::string value = pRemoteNotifyCharacteristic->readValue();
16
    Serial.print("The characteristic value was: ");
17
    Serial.println(value.c_str());
18
  }
19

20
  if(pRemoteNotifyCharacteristic->canNotify())
21
  {
22
    Serial.println("Notifikation allowed!");
23
    pRemoteNotifyCharacteristic->registerForNotify(notifyCallback);
24
  }
Angehängte Dateien:

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren