Bluetooth communications between HC-05 and processing inconsistent?

Hello everybody, I’m trying to make a motion sensor using a ultrasonic sensor and a HC-05 bluetooth module to send the data, it works perfectly on a wired connection on processing, the String i drew in the window also changes according to the received data(from “All clear” to “intruder alert”) but through Bluetooth and using the same code on my laptop, the received data is only being outputted in the console and nothing is happening in the window, please help.

here is a copy of my code

 import processing.serial.*;
import processing.sound.*;
SoundFile file;
Serial mySerial;
static String myString =null;
int nl =10;
static String alert="intruder alert!";
float myVal; //float for storing converted ascii serial data

void setup() {
  size (800, 800);

  String myPort =Serial.list()[1];
  mySerial = new Serial(this, myPort, 9600);
  //link processing to serial port (correct one)
  file = new SoundFile(this, "intruder.mp3");
  file.amp(0.5);
 }

void draw() {
  while (mySerial.available() >0) {
    myString = mySerial.readStringUntil(nl);//Strip data of serial port
    trim(myString);
    if (myString !=null) {

      background(0);
      myVal=float(myString);
      print(myString);
      if (myString==alert) {
        file.play(1);
        System.out.println("it works");
    } else {

    //file.pause();
  }
  textSize(50);
  text(myString, 300, 400);
  fill(255, 255, 255);
   }
}
 }

if you see in console
from the print(myString);
lines with that string then there could be a other byte ‘CR’
might need to clean string with trim
https://processing.org/reference/trim_.html
first, then the compare a.equals(b) can work?
hm, but the float thing should go wrong too??

the compare a.equals(b) does not work… I’m still working on fixing that will try the trim method though!

-a- your code is difficult to read, can you use a
[ctrl][t] for autoformat in the processing 3.5.3. IDE first
and later post it here.

-b- until the communication is working please lets ignore following problems like your audio job…

-c- again? did you check on myVal ? as i say that i expect that all ( convert to float AND compare with string ) might NOT work if the string is not cleaned??

-d- as you used print to console, why you not posted that output here too,
might have helped

also i worry that a multiple background() from inside the while loop from inside draw() does not work as expected ? not sure ?

better delete that while thing completely and use:
https://processing.org/reference/libraries/serial/serialEvent_.html

I have not checked on myVal, it’s not doing anything, its just outputting NaN in the console, so i commented it out, program is still up and running

I"ve used background() in processing in eclipse, all it does is to re-draw the background over unwanted objects on screen if i put it in the setup function you would be able to see all the times that the texts have changed from all clear to intruder alert(sort of like a shadow)…

I dont know if I wrote clearly but the communications is working, its just that the same piece of code is not doing the same thing on my laptop, the data is being showed correctly in the console but nothing is happening in the popup window like it is on my dekstop

i understand that same code Arduino and Processing should work same,
with and without Bluetooth ( sorry can not test )
but that is what i expected, that the Arduino serial lib for HC-05
does send something different ? LF ? CR ?..
BUT
if you see the same string in console in both cases
? https://processing.org/reference/String_length_.html
your program should react same.

so try to compare again serial USB and Bluetooth on LAPTOP only
just to be sure no other problems happened / processing version / OS / code copy /


still i wanted to mention that i see problems in your program structure ( ignore me if it works )


https://processing.org/reference/trim_.html

trim() example:

You are not using trim() correctly.

I also did a CTRL-A in the console so you could see the spaces.

:slight_smile:

1 Like

A voltage divider or logic level translator should be used to connect TX (Arduino) to RX (HC05).

I know of many that have “cooked” their HC05 because they failed to do this!

:slight_smile:

1 Like

( never used that HC-0x ) i also see that voltage divider drawing, but try to dig:
? not sure it is from the official manual?
https://translate.google.co.th/translate?sl=zh-CN&tl=en&u=http%3A%2F%2Fwww.wavesen.com%2Fprobig.asp%3Fid%3D35

i found

4, the interface level 3.3V, can be directly connected to a variety of single-chip (51, AVR, PIC, ARM, MSP430, etc.), 5V microcontroller It is best to connect a 1K resistor in series and connect directly to the module

so a pure current reducer resistor might suffice.