1

I want to both send some sensor data via Bluetooth (HC05) and Arduino to Java and I also want to send some command data via the same Bluetooth and serial port to Arduino.
I've managed to do the first part (arduino to java) successfully with this code:

hc05SerialEx.printf("^%d,%7.3f,%d,%7.3f,%d,%7.3f\n", s1, ss1, s2, ss2, s3, ss3);

^ is the character I devised in my simple protocol and it means the starting point of data.
@ means starting point of command packet.

The problem is whenever I want to write a char packet via the same HC05 connection to arduino, it sometimes works. In fact, whenever I click the button firing the write method, I see the packet received correctly but when I click the button again or click it rapidly, it sometimes outputs garbage in arduino side.
I do have delay in my main loop in arduino side and I'm using it to control the sampling rate of sensors. I've timed arduino side's calculation time (except delay of course) and it takes about 52 milliseconds.
I suspect whenever I fire the write method while arduino is busy in main loop, the received data becomes gibberish. So here are the questions:

1- What is the cause of this problem?
2- How can I achieve the goal I mentioned?

Here is java-side code sending the command packet:

String message = String.format("@%5.3f,%5.3f,%5.3f\n", rmR1, rmR2, rmR3);
System.out.println(message);
rightHC05Port.write(message);

Here is arduino-side code receiving and mirroring the received packet in arduino's own serial port (for me to see):

void loop() {
  serialEventHC05(); 
  //fetching sensor data and some math operations...
  hc05SerialEx.printf("^%d,%7.3f,%d,%7.3f,%d,%7.3f\n", s1, ss1, s2, ss2, s3, ss3);
  delay(loopTime);

int numOfReceivedBytes = 0;

void serialEventHC05() {
  while (hc05Serial.available() > 0) {
    numOfReceivedBytes = hc05Serial.available();
    String s = hc05Serial.readStringUntil('\n');
    arduinoSerialEx.println(numOfReceivedBytes);
    if (s.charAt(0) == '@') {
      arduinoSerialEx.println(s);
    }
  }
}

For example, here is the string I'm sending from Java via HC05 to Arduino:

@10.000,10.000,10.000

And here is the result of Arduino's behavior caused by me sending the same packet multiple times:
Arduino's mirrored received packet

Any help is appreciated.

  • could you please provide the complete code of your project, so I can check in detail? (Both java code and arduino code) – Tony Feb 15 '17 at 12:13
  • Thanks for reply. It seems the problem was with SoftwareSerial since when I used the 0,1 RXTX the code works flawlessly. Even with AltSoftSerial the code still works.... I wonder what are the other undocumented limitations of SoftwareSerial... – Zeta.Investigator Feb 15 '17 at 20:30
  • Unfortunately I know nothing about undocumented limitations of SoftwareSerial. But I found an example that send command from java side to Arduino that may help you.link But I also have problem when implementing such an issue. I can successfully receive data in arduino from java side, but I got stuck when receiving data from Arduino in java for a week of try. I would really appreciate that if you can share your experience or your code with me. – Tony Feb 16 '17 at 4:20
  • @Tony I've asked this very question here : forum.arduino.cc/index.php?topic=451673.0 The things I know as of yet is that SoftwareSerial can't handle "receive" in baudrates higher than 9600. You can use AltSoftSerial but RXTX pin is fixed for each Arduino. The relevant code for communication between Arduino/Java side is exactly as I mentioned above in my post, nothing more. – Zeta.Investigator Feb 16 '17 at 8:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.