Why does ESP32S transmit the same TX value?

I am a newbie, using the ESP32 Dev Module 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
图片

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

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

代码: 全选

#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 ESP32S, which then sends one-character current notifications from the UART buffer:

Excuse me, how to explain this phenomenon?

Which ESP32 board are you selecting? I see "ESP32 Dev Module", "ESP32S2 Dev Module" and "ESP32C3 Dev Module" but no "ESP32S Dev Module".

Sorry, This is a slip of the pen. It is ESP32 Dev Module example


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

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.

1 Like

Thanks for pointing out my mistake👍

Serial1.read() reads the sEMG value passed by the microcontroller

The format is as follows:

sEMG

500

500

500

500

500

500

Serial1.read() returns a single integer, either -1 if there is no character in the buffer or the value of the first character in the buffer, a value from 0 to 255.

Did you mean to read multiple digits into a number? The would be "Serial1.parseInt()"

The problem has been solved. I still appreciate your reply😃

It should be:

while((Serial1.available()){
Serial2.print((char)Serial1.read());
}

If serial1.parseint () is used, packet loss occurs and six consecutive values cannot be read. Is detrimental to collecting data, and maybe I'm not familiar with Serial1.parselnt () in the appropriate scenario.

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