I am sending text to my hc05 bluetooth module as string but when i see the text in arduino, it is all some random gibberish

When I send text to my HC-05 bluetooth module, the arduino is just interpreting it as some random gibberish.
This is my app inventor code:
image

Link of my app: MIT App Inventor Gallery

My arduino code:

#include <SoftwareSerial.h>
#include <LiquidCrystal_I2C.h>

// Define the TX and RX pins for the SoftwareSerial library
SoftwareSerial bluetooth(2, 3); // RX, TX

// Define LCD_I2C
LiquidCrystal_I2C lcd(0x3F, 16, 2);

void setup() {
// Start the serial communication with a baud rate of 9600
Serial.begin(9600);

// Start the Bluetooth communication with the HC-05 module
bluetooth.begin(9600);

// Start the lcd module
lcd.init();
lcd.backlight();
//Configure the button
pinMode(4, INPUT_PULLUP);
}
String comparison = "ffkggjhgjgkugkjghjgyug";
int buttonState = 0;
void loop() {
// Check if data is available to read from Bluetooth module
if (bluetooth.available()) {
String originalSpeech = bluetooth.readString();
Serial.println(originalSpeech);
String speech = removeRepeatedPart(originalSpeech);
char message[speech.length() + 1];
speech.toCharArray(message, sizeof(message));

  char* token = strtok(message, " ");

  while (token != NULL) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print(token);
    delay(1000);
    token = strtok(NULL, " ");
  }
  delay(1000);
  lcd.clear();

}
int pinValue = digitalRead(4);
if(buttonState == pinValue){
bluetooth.println("a");
}
else{
bluetooth.println("b");
}
}
String removeRepeatedPart(String input) {
int length = input.length();

// Iterate through the string to find the repeated part
for (int i = 1; i < length; i++) {
String prefix = input.substring(0, i);
String suffix = input.substring(i, 2 * i);

// Check if the prefix and suffix are the same
if (prefix.equals(suffix)) {
  // Remove the repeated part from the original string
  return input.substring(i);
}

}

// If no repeated part is found, return the original string
return input;
}

Here's what I have already tried:
I tried changing bluetooth.begin(9600) to bluetooth.begin(19200) and bluetooth.begin(4800). But that just makes more gibberish.

This my national level competition project which is due in 2 days. Please help urgently.

What about setting 38400 ? Sometimes some of them are factory set at that baudrate (but please be aware that the SoftwareSerial library works bad over 38400).
Depending on the version of chipset, the firmware of the HC05/06 has a different AT command set (with respect to older ones).
The annexed files helped me in the past.



Best wishes.

We can't read your Blocks!

Right-mouse in the Blocks work area and select "Download Blocks as image"

We also need precise information about your hardware:

  1. Smartphone: Make/Model/Android Version/Bluetooth Version
  2. Arduino: Model
  3. A photo, plan view, of your Arduino set-up, showing all the connections clearly

Edit: An example of the text your App is sending please

Instead of this:

String originalSpeech = bluetooth.readString();

Try (exactly as shown):

string originalSpeech = bluetooth.read();

1 Like

Hi, thanks for replying @ChrisWard . When I do as instructed, it does not bring about the desired string I want. This is why I used readString in the first place. But I am sure that is not causing the issue. Can you please suggest me some other solution...

Hi, thanks from replying @uskiara . But I am actually quite a beginner and am not able to understand anything. Can you please directly tell me the steps I need to do

@ChrisWard thanks for replying. Here is the pic:

Ok, I say it in other words: sometimes the HC05 or HC06 have a factory set baudrate of 38400 (not so often, but it can happen) . Therefore, since you've said that you've tried to set various baudrates on your Arduino board, my hint is to try also 38400 (i.e. bluetooth.begin(38400);).
Please also be sure to reverse the pins : Tx of Arduino (pin 3 according to your settings) shall be connected to the Rx pin of the HC05. Obviously pin 2 (Rx) to Tx of the HC05.

The two files I've annexed on my first answer are related to how to modify the baudrate on HC06 (HC05) side. If you didn't do anything on your side, just forget them.

Another hint is: try to send a fixed string from your app before sending a variable one.
That is: send just "Hello word" instead of what the SpeechRecognizer has understood. In this way you can be sure that the string sent is a "readable" one.

I have tried that... Doesn't work

i did everything...

Tried.. problem not with speech recognition

Also checked the pins ?

Yes

Based on all my googling, I think the problem is related to baud rate. Do you also think so?

Try this code
//=======================================

#include <SoftwareSerial.h>

// Define the TX and RX pins for the SoftwareSerial library
SoftwareSerial bluetooth(2, 3); // RX, TX

void setup() {
// Start the serial communication with a baud rate of 9600
Serial.begin(9600);

// Start the Bluetooth communication with the HC-05 module
bluetooth.begin(9600);

}

void loop()
{
char buf;
// Check if data is available to read from Bluetooth module
if (bluetooth.available())
{
buf = bluetooth.read();
Serial.print(buf);
delay(1);
}
}

With this super-simple code anything arrived from the app is shown on the Serial Monitor.

By the way: have you set the baudrate of the Serial monitor to 9600 (in the Arduino IDE) ?

In conjunction with the super-simple code on Arduino, you can use also this tool


that you can download free from playstore.
By running this tool on your device (i.e. where you are running the AI2 app) you can avoid the uncertainty of the app you developed. In other words: since this tool works for sure, you remain with only one unknown part (the Arduino code, and its hardware).

1 Like

Not that it matters right now, but it would be better to have the received bytes controlled by a separate clock - as-is, Clock1 keeps on sending (is that intended?) and Receive Bytes is going to get over-run.

You also should not use the flag 'Bytes Available to Receive' that's not the total, just an indication that bytes are there. Use '-1' instead, that signals that your app should receive all the bytes.

So, the Speech Recognizer is not producing random gibberish? It's famous for doing so........ Does the text stay in Label2 long enough for you to be sure?

I don't think so no - it is most likely an error in the code or in the circuit or Speech Recognizer is actually producing gibberish.

It's very hard to check your own work. For example, in your Arduino code, don't mix-up String (C++ string) and string(row of characters = C string), they are not the same and not easily interchangeable.

Post a photo of your board setup so that somebody here can check it for you. Have you checked your connections for continuity? Have you made sure that there is nothing damaging the Bluetooth signal?

https://www.professorcad.co.uk/appinventortips#BluetoothFailure

If the string the App sends uses more than one Bluetooth packet, the Arduino code will need to continue to read until all bytes are read:

bluetooth.readStringUntil('#'); Keep reading until '#' is reached (App should send a # after sending the string)