Some Bluetooth messages result in error and others work

I have a ESP32 which is sending messages to a MIT app via Bluetooth using a HC-05. There are three different messages listed below.

  1. $start (sent on button press)
  2. $end (sent on button press)
  3. The third message containers a series of numbers delineated by a "|" symbol. The numbers are not the same and each item separated by the symbol is saved to variables.

The third message always works as desired.

Here is where the problem occurs.

First I connect to the hc-05 with the MIT app. If I trigger Message 1 and/or 2 first, I get the below error. This happens no matter how many times I trigger them. If however, I trigger message 3 first, then I can follow up with messages 1 and 2 as desired and everything functions correctly.

Error: Segment: Start (1) + length (1) - 1 exceeds text length (0)

If anyone could tell me why this is happening, it would be much appreciated! I have attached a image of my blocks. Thanks very much for the assistance, its much appreciated!

We need to verify two more things regarding your message Delimiters:

  • How do you transmit the messages? (Show the code)
  • What do you use in the app for the BlueTooth Message Delimiter?
1 Like

Here is the Arduino code related to my original post.

I also created a dumbed down version that sends the messages when certain numbers are input into the serial monitor. I have included that below and it works as desired which makes we think there is something in my original code above that's causing this issue.

// HC-05 is connected via hardware serial pins
#define HC05_RX_PIN 16  // Example: Connect HC-05 TX to GPIO16 (RX)
#define HC05_TX_PIN 17  // Example: Connect HC-05 RX to GPIO17 (TX)

HardwareSerial HC05(1);  // Use Serial1 for HC-05 (1 refers to UART1)

String MessageToPhone1;  // Message to send to the phone
String MessageToPhone2;  // Message to send to the phone
String MessageToPhone3;  // Message to send to the phone

int HitTargetID = 0;

void setup() {
  Serial.begin(115200);                                    // For debugging
  HC05.begin(9600, SERIAL_8N1, HC05_RX_PIN, HC05_TX_PIN);  // HC-05 operates at 9600 baud rate

  Serial.println("HC-05 initialized. You can now pair it with your device!");
}

// the loop function runs over and over again forever
void loop() {
  if (Serial.available() > 0) {
    // Read the incoming byte
    char incomingByte = Serial.read();

    // Reset counter
    if (incomingByte == '1') {
      MessageToPhone1 = "$start";
      for (int i = 0; i < MessageToPhone1.length(); i++) {
        HC05.print(MessageToPhone1[i]);  // Send one character at a time
      }
      HC05.println();
      Serial.println(MessageToPhone1);
      delay(200);
    }
  
  if (incomingByte == '2') {
    MessageToPhone2 = "$end";
    for (int i = 0; i < MessageToPhone2.length(); i++) {
      HC05.print(MessageToPhone2[i]);  // Send one character at a time
    }
    HC05.println();
    Serial.println(MessageToPhone2);
    delay(200);
  }


if (incomingByte == '3') {
  int item1 = random(10, 20);
  int item2 = random(10, 20);
  int item3 = random(10, 20);
  int item4 = random(10, 20);

  HitTargetID++;
  MessageToPhone3 = String(item1) + "|" + String(item2) + "|" + String(item3) + "|" + String(item4);

  for (int i = 0; i < MessageToPhone3.length(); i++) {
    HC05.print(MessageToPhone3[i]);  // Send one character at a time
  }
  HC05.println();
  Serial.println(MessageToPhone3);
  delay(200);
}
}
}

Okay it seems I'm a idiot... Thanks you very much for your help and and apologies for wasting your time. I didn't have the variable correct. Its MessageToPhone in one area and MessageToPhone2 in another whereas these should be the same.
image

1 Like

Hello Salmon

Tip - don't send "$start", just send a single character! Ditto with "$end". Also, any value sent that is smaller in size than the Bluetooth packet size can be sent as a single sting, you don't have to send char-by-char. However, the String library is greedy, it produces 'byte heavy' strings. We mostly use the C string library instead

char myStr[] = "abc"; string size self-determined
char buffer[20]; //store a signal value of no more than 21 characters (0 to 20)
const char* password = "fortknox"; //temp store in memory

Hey @ChrisWard thanks for the tip, i'll look into that!

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