SERIAL-BT data not showing correct on serial montor

Hey everyone :wave:,

I am making a project based on serial-bt

but the problem is when I assign serial.read to a variable and print it on the serial
it shows one by one digit every time :sweat_smile: :slightly_smiling_face:

I think there is any programming issue

please help!!!

more details are below with code and screenshots

#include "BluetoothSerial.h"

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

BluetoothSerial SerialBT;

char tc;

void setup() {

  Serial.begin(9600);
     SerialBT.begin("ESP32test"); //Bluetooth device name
  Serial.println("The device started, now you can pair it with bluetooth!");
 
  }

void loop() {
  if (SerialBT.available()) {

tc = (SerialBT.read());
}

    Serial.print("TC entered  =");
  Serial.println(tc);

  delay(1000);
}

.
.
.
The BELOW image is of the Bluetooth serial (where I enter data.)
.
.

.
.
.

screenshot of SERIAL MONITOR (where I see results)
.
.


.
.
.
.
.
I tried this with many Bluetooth serial monitor

#include "BluetoothSerial.h"

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

BluetoothSerial SerialBT;

char tc;

void setup() {

  Serial.begin(9600);
  SerialBT.begin("ESP32test"); //Bluetooth device name
  Serial.println("The device started, now you can pair it with bluetooth!");

}

void loop() {
  if (SerialBT.available()) {
    tc = (SerialBT.read());
    Serial.print("TC entered  =");
    Serial.println(tc);
  }
}
1 Like
#include "BluetoothSerial.h"

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

BluetoothSerial SerialBT;

char tc[50];
unsigned char tcIndex;

void setup() {

  Serial.begin(9600);
  SerialBT.begin("ESP32test"); //Bluetooth device name
  Serial.println("The device started, now you can pair it with bluetooth!");

}

void loop() {
  if (SerialBT.available()) {
    while (SerialBT.available() && tcIndex < 50) {
      tc[tcIndex] = (SerialBT.read());
      tcIndex++;
      tc[tcIndex] = '\0';
      delay(2UL);
    }
    tcIndex = 0;
    Serial.print("TC entered  =");
    Serial.println(tc);
  }
}

Hey Perehama, :wave:

thanks for your reply
but the solution also has a problem :sweat_smile:

It is not deleting the value it saved before... :thinking: :woozy_face:

when I enter a new value it gets after old value

my bad, there's a bug that tcIndex never gets set to zero...

#include "BluetoothSerial.h"

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

BluetoothSerial SerialBT;

char tc[50];
unsigned char tcIndex;

void setup() {

  Serial.begin(9600);
  SerialBT.begin("ESP32test"); //Bluetooth device name
  Serial.println("The device started, now you can pair it with bluetooth!");

}

void loop() {
  if (SerialBT.available()) {
    tcIndex = 0;
    while (SerialBT.available() && tcIndex < 50) {
      tc[tcIndex] = (SerialBT.read());
      tcIndex++;
      tc[tcIndex] = '\0';
      delay(2UL);
    }
    Serial.print("TC entered  =");
    Serial.println(tc);
  }
}

Nothing is seen on the serial monitor
everything is blank.

HEY, my work is done using this code example.....

// Example 2 - Receive with an end-marker

const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data

boolean newData = false;

void setup() {
    Serial.begin(9600);
    Serial.println("<Arduino is ready>");
}

void loop() {
    recvWithEndMarker();
    showNewData();
}

void recvWithEndMarker() {
    static byte ndx = 0;
    char endMarker = '\n';
    char rc;
    
    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (rc != endMarker) {
            receivedChars[ndx] = rc;
            ndx++;
            if (ndx >= numChars) {
                ndx = numChars - 1;
            }
        }
        else {
            receivedChars[ndx] = '\0'; // terminate the string
            ndx = 0;
            newData = true;
        }
    }
}

void showNewData() {
    if (newData == true) {
        Serial.print("This just in ... ");
        Serial.println(receivedChars);
        newData = false;
    }
}

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