# Bluetooth communications between HC-05 and processing inconsistent?

**URL:** https://discourse.processing.org/t/bluetooth-communications-between-hc-05-and-processing-inconsistent/13601
**Category:** Electronics (Arduino, etc.)
**Created:** [August 26, 2019, 3:20am UTC](https://discourse.processing.org/t/bluetooth-communications-between-hc-05-and-processing-inconsistent/13601 "2019-08-26T03:20:20Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![blorglue](https://avatars.discourse-cdn.com/v4/letter/b/73ab20/32.png) [@blorglue](https://discourse.processing.org/u/blorglue)
#### Post date: [August 26, 2019, 3:20am UTC](https://discourse.processing.org/t/bluetooth-communications-between-hc-05-and-processing-inconsistent/13601/1 "2019-08-26T03:20:20Z")

</div>

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);
   }
}
 }
```

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [August 26, 2019, 3:28am UTC](https://discourse.processing.org/t/bluetooth-communications-between-hc-05-and-processing-inconsistent/13601/2 "2019-08-26T03:28:22Z")

</div>

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](https://processing.org/reference/trim_.html)  
first, then the compare `a.equals(b)` can work?  
hm, but the float thing should go wrong too??

---

<div class="post-metadata">

### Author: ![blorglue](https://avatars.discourse-cdn.com/v4/letter/b/73ab20/32.png) [@blorglue](https://discourse.processing.org/u/blorglue)
#### Post date: [August 26, 2019, 3:32am UTC](https://discourse.processing.org/t/bluetooth-communications-between-hc-05-and-processing-inconsistent/13601/3 "2019-08-26T03:32:14Z")

</div>

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

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [August 26, 2019, 3:38am UTC](https://discourse.processing.org/t/bluetooth-communications-between-hc-05-and-processing-inconsistent/13601/4 "2019-08-26T03:38:34Z")

</div>

-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

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [August 26, 2019, 3:48am UTC](https://discourse.processing.org/t/bluetooth-communications-between-hc-05-and-processing-inconsistent/13601/5 "2019-08-26T03:48:17Z")

</div>

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](https://processing.org/reference/libraries/serial/serialEvent_.html)

---

<div class="post-metadata">

### Author: ![blorglue](https://avatars.discourse-cdn.com/v4/letter/b/73ab20/32.png) [@blorglue](https://discourse.processing.org/u/blorglue)
#### Post date: [August 26, 2019, 3:49am UTC](https://discourse.processing.org/t/bluetooth-communications-between-hc-05-and-processing-inconsistent/13601/6 "2019-08-26T03:49:00Z")

</div>

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

---

<div class="post-metadata">

### Author: ![blorglue](https://avatars.discourse-cdn.com/v4/letter/b/73ab20/32.png) [@blorglue](https://discourse.processing.org/u/blorglue)
#### Post date: [August 26, 2019, 3:50am UTC](https://discourse.processing.org/t/bluetooth-communications-between-hc-05-and-processing-inconsistent/13601/7 "2019-08-26T03:50:59Z")

</div>

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)…

---

<div class="post-metadata">

### Author: ![blorglue](https://avatars.discourse-cdn.com/v4/letter/b/73ab20/32.png) [@blorglue](https://discourse.processing.org/u/blorglue)
#### Post date: [August 26, 2019, 3:53am UTC](https://discourse.processing.org/t/bluetooth-communications-between-hc-05-and-processing-inconsistent/13601/8 "2019-08-26T03:53:18Z")

</div>

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

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [August 26, 2019, 4:00am UTC](https://discourse.processing.org/t/bluetooth-communications-between-hc-05-and-processing-inconsistent/13601/9 "2019-08-26T04:00:27Z")

</div>

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](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 )

* * *

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [August 27, 2019, 1:33am UTC](https://discourse.processing.org/t/bluetooth-communications-between-hc-05-and-processing-inconsistent/13601/10 "2019-08-27T01:33:15Z")

</div>

[https://processing.org/reference/trim\_.html](https://processing.org/reference/trim_.html)

trim() example:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/1/13a5f88c51953da8efee93e20922212b3fb41c6d.png)

You are not using trim() correctly.

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

🙂

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [August 27, 2019, 2:15am UTC](https://discourse.processing.org/t/bluetooth-communications-between-hc-05-and-processing-inconsistent/13601/11 "2019-08-27T02:15:28Z")

</div>

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

> **[HC-05 Bluetooth Module interfacing with Arduino - Arduino Bluetooth Module...](https://electronicshobbyists.com/hc-05-bluetooth-module-interfacing-with-arduino-arduino-bluetooth-module-tutorial/)**
>
> In this tutorial, you will read about arduino Bluetooth module. HC-05 Bluetooth module works with the Arduino through the serial communication.

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

🙂

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [August 27, 2019, 8:34am UTC](https://discourse.processing.org/t/bluetooth-communications-between-hc-05-and-processing-inconsistent/13601/12 "2019-08-27T08:34:58Z")

</div>

( 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](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.
