Why does ESP32S transmit the same TX value?

allrobot
Posts: 17
Joined: Wed Nov 03, 2021 7:15 am

Why does ESP32S transmit the same TX value?

Postby allrobot » Fri Mar 18, 2022 3:02 pm

I am a newbie, using the ESP32S example file to simply modify the TX value, resulting in garbled output?

The question is this:
The notification received by the mobile phone is the same as 00 01 01 00 C0 C8 FD 3F 54 C0 FD..... 3F 00 00 00 00
Image

If you cut back to UTF-8, it becomes garbled:
Image

The bluetooth code is changed from the ESP32 example file, which has been analyzed for a long time....I am a rookie...

example file is ESP32 BLE Arduino----->BLE_uart.ino

Code: Select all

#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
BLEServer *pServer = NULL;
BLECharacteristic *pTxCharacteristic;
bool deviceConnected = false;
bool oldDeviceConnected = false;
uint8_t txValue = 0;

// See the following for generating UUIDs:
// https://www.uuidgenerator.net/

#define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
#define CHARACTERISTIC_UUID_RX "f78ebbff-c8b7-4107-93de-889a6a06d408"
#define CHARACTERISTIC_UUID_TX "ca73b3ba-39f6-4ab3-91ae-186dc9577d99"

class MyServerCallbacks : public BLEServerCallbacks
{
  void onConnect(BLEServer *pServer)
  {
    deviceConnected = true;
  };

  void onDisconnect(BLEServer *pServer)
  {
    deviceConnected = false;
  }
};

class MyCallbacks : public BLECharacteristicCallbacks
{
  void onWrite(BLECharacteristic *pCharacteristic)
  {
    std::string rxValue = pCharacteristic->getValue();

    if (rxValue.length() > 0)
    {
      Serial2.println("*********");
      Serial2.print("Received Value: ");
      for (int i = 0; i < rxValue.length(); i++)
        Serial2.print(rxValue[i);

      Serial2.println();
      Serial2.println("*********");
    }
  }
};
void setup()
{
  Serial1.begin(250000, SERIAL_8N1, 3, 1);
  Serial2.begin(250000, SERIAL_8N1, 16, 17);

  // Create the BLE Device
  BLEDevice::init("UART Service");

  // Create the BLE Server
  pServer = BLEDevice::createServer();
  pServer->setCallbacks(new MyServerCallbacks());

  // Create the BLE Service
  BLEService *pService = pServer->createService(SERVICE_UUID);

  // Create a BLE Characteristic
  pTxCharacteristic = pService->createCharacteristic(
      CHARACTERISTIC_UUID_TX,
      BLECharacteristic::PROPERTY_NOTIFY);

  pTxCharacteristic->addDescriptor(new BLE2902());

  BLECharacteristic *pRxCharacteristic = pService->createCharacteristic(
      CHARACTERISTIC_UUID_RX,
      BLECharacteristic::PROPERTY_WRITE);

  pRxCharacteristic->setCallbacks(new MyCallbacks());

  // Start the service
  pService->start();

  // Start advertising
  pServer->getAdvertising()->start();

  Serial2.println("\nSet Serial1 ok!");
  Serial2.println("Waiting a client connection to notify...");
}

void loop()
{
  if (deviceConnected)
  {
    // long time=millis();
    // for(int i=0;i<1000;){
    if (Serial1.available() > 0)
    {
      // int x=0;
      // if(char(Serial1.peek())=='s'){i++;}
      //   if(Serial1.peek()!='\n'){x++;}else{x=0;}


      //tx传值,通过蓝牙notify不间断发送通知
      Serial2.print(char(Serial1.peek()));
      pTxCharacteristic->setValue(&txValue, Serial1.read());
      pTxCharacteristic->notify();
      // if(x>10){
      //   Serial2.println();
      //   Serial2.println("Error!!!");
      //   delay(10000000000);
      // }
      delay(10);
    }
    // // }
    //   float a=1000/((millis()-time)/1000);
    //   Serial2.printf("Frequency domain:%f /n",a);
    //   delay(1000000);
  }
  // disconnecting
  if (!deviceConnected && oldDeviceConnected)
  {
    delay(500);                  // 让蓝牙堆栈有机会做好准备
    pServer->startAdvertising(); // 重启广播
    Serial2.println("start advertising");
    oldDeviceConnected = deviceConnected;
  }
  // connecting
  if (deviceConnected && !oldDeviceConnected)
  {
    // do stuff here on connecting在连接上做点事情
    oldDeviceConnected = deviceConnected;
    Serial2.println("start advertising");
  }
}
The ideal Bluetooth scenario would be to send values (sEMG and 6 values) from other microcontrollers through the UART to ESP32, which then sends one-character current notifications from the UART buffer:

Image

allrobot
Posts: 17
Joined: Wed Nov 03, 2021 7:15 am

Re: Why does ESP32S transmit the same TX value?

Postby allrobot » Sat Mar 19, 2022 4:30 am

It's solved.

Replies from Others: :oops:

What data is actually incoming on Serial1.
Why do you want to echo it back out a byte at a time rather than line by line?

The UART service is designed to send text messages and it may be better to setValue to a text string.

I don't understand this

pTxCharacteristic->setValue(&txValue, Serial1.read());

First, I don't see where in the code you update the txValue. The fact that the txValue is never updated answers the question posed in the title of this thread.

The second parameter should be a number of bytes, and its not clear what the value of Serial1.read() is going to be

I am guessing at what you are trying to do, but perhaps you want

Code: Select all

if (Serial1.available() > 0)
{
  Serial2.print(char(Serial1.peek()));
  txValue = Serial1.read();
  pTxCharacteristic->setValue(&txValue,1);
  pTxCharacteristic->notify();

  delay(10);
}
In a typical BLE Service/ charactceristic you would setValue to a multi byte variable for example a uint32_t
xxxCharacteristic->setValue( ( uint8_t* )&pressure, 4 );

But as I said before, the UART service is a bit of a BLE anomaly and is best used where the rx and tx characteristics are terminated text strings.

Who is online

Users browsing this forum: Baidu [Spider] and 146 guests