14A.- Send a message longer than 20 characters. MTU.
p110i_esp32_ble_mtu.aia (203.7 KB)
-
The BLE extension sends the messages in 20-byte packets, this is called MTU (Minimum Transmission Unit). The size of these packets can be changed using the RequestMTU block, but this size change would have to be changed in the Block code and on the device code.
-
We are going to see a code to send a long text in 20-byte packets that will be concatenated in the ESP32 code, that is, 20-byte packets will be sent and they will be joined in the code until the complete text is obtained.
-
We will send this text:
La cigüeña voló a su nido.\nEl "Niño" provocó grandes inundaciones.\nEl señor Sánchez no es de Cádiz.\nThe stork flew to its nest.\nThe "Niño" caused great floods.\nMr. Sánchez is not from Cádiz.\n -
We will split the text into 15-character, this is because language characters such as ü, ñ, ó, á, need two bytes, if we send:
La cigüeña voló
-
we have cut 15 characters, but we are sending 18 bytes, since ü, ñ, ó need 2 bytes. We are actually sending a 20-byte packet, the indicated 18 bytes and 2 more bytes to complete the 20.
-
Those 15 characters will reach the ESP32 and will be displayed on the Serial Monitor:
Serial.print (valor);
-
As these packages arrive, they will be shown.
-
In addition, in another variable called valor_return, all the characters that arrive will be added.
-
When the "#" character arrives, the valor_return variable will be reset.
// Juan A. Villalpando
// http://kio4.com/arduino/160_Wemos_ESP32_BLE.htm
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
String valor;
String valor_return = "";
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
class MyCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
std::string value = pCharacteristic->getValue();
if (value.length() > 0) {
valor = "";
for (int i = 0; i < value.length(); i++){
valor = valor + value[i];
}
Serial.print(valor); // Presenta valor.
valor_return = valor_return + valor;
}
pCharacteristic->setValue(valor_return.c_str()); // Valor return.
if(valor_return.indexOf("#") != -1){valor_return = "";}
}
};
void setup() {
Serial.begin(115200);
BLEDevice::init("MyESP32");
BLEServer *pServer = BLEDevice::createServer();
BLEService *pService = pServer->createService(SERVICE_UUID);
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);
pCharacteristic->setCallbacks(new MyCallbacks());
pService->start();
BLEAdvertising *pAdvertising = pServer->getAdvertising();
pAdvertising->start();
}
void loop() {
// put your main code here, to run repeatedly:
}
oooooooooooooooooOOOOOOOOOOOOOOooooooooooooooooo
14B.- Send a message longer than 20 characters. MTU.
Simplified code.
p110i_esp32_ble_mtu2.aia (205.0 KB)
- By means of this code the characters are sent one by one.
// Juan A. Villalpando
// http://kio4.com/arduino/160_Wemos_ESP32_BLE.htm
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
String valor;
String valor_return = "";
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
class MyCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
std::string value = pCharacteristic->getValue();
if (value.length() > 0) {
valor = "";
for (int i = 0; i < value.length(); i++){
valor = valor + value[i];
}
valor_return = valor_return + valor;
}
pCharacteristic->setValue(valor_return.c_str()); // Valor return.
if(valor_return.indexOf("#") != -1){Serial.print(valor_return); valor_return = "";}
}
};
void setup() {
Serial.begin(115200);
BLEDevice::init("MyESP32");
BLEServer *pServer = BLEDevice::createServer();
BLEService *pService = pServer->createService(SERVICE_UUID);
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);
pCharacteristic->setCallbacks(new MyCallbacks());
pService->start();
BLEAdvertising *pAdvertising = pServer->getAdvertising();
pAdvertising->start();
}
void loop() {
// put your main code here, to run repeatedly:
}



