Arduino RP 2040 Bluetooth

Hello, I am currently trying to connect my mobile phone to the soldered Bluetooth module on the RP2040. The input on the serial monitor and output on the mobile phone work perfectly. Only the other way around, unfortunately only to a limited extent (data arrives at the serial monitor but unfortunately only incorrect values). What's wrong with the code?

#include <ArduinoBLE.h>

//byte = uint8_t
byte senden = 0;
byte lesen = 0;

BLEService MessageService("19B10000-E8F2-537E-4F6C-D104768A1214"); 
// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristic messageCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify | BLEWrite);
void setup() {
  Serial.begin(9600);
  // begin initialization
  if (!BLE.begin()) {
    Serial.println("starting BLE failed!");
  }

  // set advertised local name and service UUID:
  BLE.setLocalName("Arduino Nano RP2040");
  BLE.setAdvertisedService(MessageService);
  // add the characteristic to the service
  MessageService.addCharacteristic(messageCharacteristic);
  // add service
  BLE.addService(MessageService);
  // start advertising
  BLE.advertise();
  Serial.println("Arduino, waiting for connections...");
}

void loop() {
  // listen for BLE peripherals to connect:
  BLEDevice central = BLE.central();
  // if a central is connected to peripheral:
  if (central) {
    Serial.print("Connected to phone: ");
    // print the central's MAC address:
    Serial.println(central.address());
    while (central.connected()) {
    
      //Handy empfängt => eingabe via serial monitor
      if(Serial.available() > 0) {
        senden = (byte) Serial.read();
        messageCharacteristic.writeValue(senden);
      }
      
      //Handy sendet => ausgabe via serial monitor
      if (messageCharacteristic.written()) {      //valueUpdated
        messageCharacteristic.readValue(lesen);
        Serial.println(lesen);
      }
    }
    // when the central disconnects, print it out:
    Serial.print(F("Disconnected from phone: "));
    Serial.println(central.address());
  }
}

bluetooth_test.ino (1.8 KB)

Welcome to the forum.

Can you please modify your original post to ensure the source code is in a single box? It should look like this.

// Your example source code

You need three ' at the beginning and end in the edit window? When you click on this icon </> you get.

```
type or paste code here
```

This ensures we can get all the source code. Then you do not need to attach a file.

About your code:

  • using one characteristic for reading and writing in your case is likely not a good idea
  • your variable lesen is a 32-bit type for a byte size characteristic, if you have a good reason why you should comment it in your code
  • sending individual bytes is wasteful, BLE sends packets with an overhead of ~10bytes

Can you provide an example? The values I sent were exactly what I would expect. What app did you use? When you send characters, you will print the byte value of the ASCII code. e.g., A = 65 (0x41). When you send 1 it depends on whether your app sends it as a character or value.

I recommend you use only two languages while programming. C++ and English are enough. That will make your life easier in the long run. It easier to share and to ask for help. :slight_smile:

Using GATT to create a serial pipe will waste the good ideas about BLE. BLE does not have a Serial Port Profile (SPP) which was available in Bluetooth Classic.
With GATT you can create services and characteristics that will provide a modern interface, which is more power efficient and makes the application layer development easier and more scalable. e.g.,

  • clients can read only values they want
  • the format of the data can be known by the UUID or using descriptors
  • clients can use notifications
  • clients can read sensor data much slower then you intended so save power by polling instead
  • you can add new characteristics to the peripheral that old clients can ignore but new clients make use of, the same can be applied to low cost and premium clients, they can both use the same sensor (This can be done with serial streams but you have to think of that ahead of time).

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.