I am sending a message to my MIT app via Bluetooth using an Arduino and a HC-06.
Most of the time the message is received intact. Occasionally though the message is received in separate parts. The message is always delivered but sometimes its in multiple parts instead of one. I am saving each message to a list so when this happens it causes issues because the data in the list isn't consistent.
Any guidance or help on how to fix this would be greatly appreciated!
The message being sent to the MIT app is structured as follows. Where the first number is a counter that increases with each message and 100 is a value randomly generated between 100-200.
1|100|100|100.
Here is what this look like in my serial monitor and MIT app. In this test it was the 5th message that got split.
Serial monitor
MIT emulated app
Arduino code
// 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)
// Variables for received/Sending Bluetooth messages
String AppCommand = ""; // Buffer to hold the incoming message from Bluetooth
String MessageToPhone; // 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!");
}
void loop() {
int HitRangeFromTag = random(100, 200);
if (Serial.available() > 0) {
// Read the incoming byte
char incomingByte = Serial.read();
// Check if the received byte is '0'
if (incomingByte == '0') {
HitTargetID++;
MessageToPhone = String(HitTargetID) + "|" + String(HitRangeFromTag) + "|" + String(HitRangeFromTag) + "|" + String(HitRangeFromTag);
Serial.print("Forward message to Phone - ");
Serial.println(MessageToPhone);
HC05.println(MessageToPhone);
}
//Reset counter
if (incomingByte == '1') {
HitTargetID = 0;
Serial.println("Counter is Cleared");
}
}
}
MIT App inventor blocks