B4R Tutorial HC 05 Classic Bluetooth

Status
Not open for further replies.
SS-2016-05-08_17.39.25.jpg


SS-2016-05-08_17.39.57.png


In this example we will connect an Android device to an Arduino using class Bluetooth with the HC 05 module.

Working with this module is simple. Connect the power and gnd pins and connect the HC05 RX pin to an Arduino TX pin and the HC05 TX pin to an Arduino RX pin.

The HC05 RX pin expects 3.3 volt. In this case I'm using the Arduino Due which is a 3.3v board. However most boards are 5v so you will need to add two resistors as explained here: http://www.martyncurrey.com/arduino-with-hc-05-bluetooth-module-in-slave-mode/

You can use the rSoftwareSerial library to turn two regular pins to a serial port. If you are using a board such as Mega or Due then you can use one of the additional hardware serial ports.

This is done with the following code:
B4X:
 RunNative("SerialNative1", Null)
#if C
void SerialNative1(B4R::Object* unused) {
   ::Serial1.begin(9600);
  b4r_main::_serialnative1->wrappedStream = &::Serial1;
}
#end if

The Arduino will send the current millis value every second. It will send it as a string. In most cases it is simpler and better to work with binary data instead of strings. However for this example I chose to send a string and to read it in the Android side with AsyncStreamsText. AsyncStreamsText takes care of collecting the received data and parsing it based on the end of line characters.

The Android will send commands to turn on or off the led(s).
B4X:
'b4r
Sub AStream_NewData (Buffer() As Byte)
   For i = 0 To Buffer.Length - 2 Step 2
     Dim ledNumber As Byte = Buffer(i)
     Dim value As Boolean = Buffer(i + 1) = 1
     leds(ledNumber).DigitalWrite(value)
   Next
End Sub

'b4a
Sub Switch1_CheckedChange(Checked As Boolean)
   Dim b As Byte
   If Checked Then b = 1 Else b = 0
   CallSub2(Starter, "SendMessage", Array As Byte(0, b))
End Sub

Complete B4R code:
B4X:
Sub Process_Globals
  Public Serial1 As Serial
  Private SerialNative1 As Stream
  Private astream As AsyncStreams
  Private leds(1) As Pin
  Private timer1 As Timer
End Sub

Private Sub AppStart
  Serial1.Initialize(115200)
  Log("AppStart")
  leds(0).Initialize(leds(0).A0, leds(0).MODE_OUTPUT)
  'Using the hardware serial named Serial1 (Arduino Due)
  'A SoftwareSerial will also work.
  RunNative("SerialNative1", Null)
  astream.Initialize(SerialNative1, "astream_NewData", Null)
  timer1.Initialize("timer1_Tick", 1000)
  timer1.Enabled = True
End Sub

#if C
void SerialNative1(B4R::Object* unused) {
   ::Serial1.begin(9600);
  b4r_main::_serialnative1->wrappedStream = &::Serial1;
}
#end if

Sub Timer1_Tick
   astream.Write("Millis here: ".GetBytes)
   astream.Write(NumberFormat(Millis, 0, 0).GetBytes)
   astream.Write(Array As Byte(10)) 'end of line character. AsyncStreamsText will cut the message here
End Sub

Sub AStream_NewData (Buffer() As Byte)
   For i = 0 To Buffer.Length - 2 Step 2
     Dim ledNumber As Byte = Buffer(i)
     Dim value As Boolean = Buffer(i + 1) = 1
     leds(ledNumber).DigitalWrite(value)
   Next
End Sub
 

Attachments

  • Adruino_Bluetooth_B4A.zip
    9.7 KB · Views: 2,827
Last edited:

ingo.tw

Member
Licensed User
Longtime User
Hi Erel,
i want to upload a example
I get this error:
sketch\b4r_main.cpp: In function 'void SerialNative1(B4R::Object*)':
b4r_main.cpp:18: error: '::Serial1' has not been declared
::Serial1.begin(9600);
^
b4r_main.cpp:19: error: '::Serial1' has not been declared
b4r_main::_serialnative1->wrappedStream = &::Serial1;
^
exit status 1
'::Serial1' has not been declared
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
The code I posted will only compile with boards that have multiple hardware serial ports (such as Mega and Due).

You need to delete this code and use SoftwareSerial instead:

B4X:
Sub Process_Globals
  Public Serial1 As Serial
  Private SoftwareSerial1 As SoftwareSerial
  Private astream As AsyncStreams
  Private leds(1) As Pin
  Private timer1 As Timer
End Sub

Private Sub AppStart
  Serial1.Initialize(115200)
  Log("AppStart")
  leds(0).Initialize(leds(0).A0, leds(0).MODE_OUTPUT)
  SoftwareSerial1.Initialize(9600, 10, 11) 'software serial port on pins 10 and 11
  astream.Initialize(SoftwareSerial1.Stream, "astream_NewData", Null)
  timer1.Initialize("timer1_Tick", 1000)
  timer1.Enabled = True
End Sub


Sub Timer1_Tick
  astream.Write("Millis here: ".GetBytes)
  astream.Write(NumberFormat(Millis, 0, 0).GetBytes)
  astream.Write(Array As Byte(10)) 'end of line character. AsyncStreamsText will cut the message here
End Sub

Sub AStream_NewData (Buffer() As Byte)
  For i = 0 To Buffer.Length - 2 Step 2
  Dim ledNumber As Byte = Buffer(i)
  Dim value As Boolean = Buffer(i + 1) = 1
  leds(ledNumber).DigitalWrite(value)
  Next
End Sub
 

ingo.tw

Member
Licensed User
Longtime User
Thank you. This issue has been resolved with your code
 
Last edited:

Beja

Expert
Licensed User
Longtime User
Hi Erel,
Sorry a little confused.. is the code in #5 above a complete replacement for the sample project? My board is Uno

best
 

Beja

Expert
Licensed User
Longtime User
He Erel,
Can this example project run on Uno also?

Thanks in advance.
 

Beja

Expert
Licensed User
Longtime User
It should work with HC06 too right ?
HC-06 is conigured a slave from factory.. So if you want arduino to initiate the communican then you MUST use HC-05. If your Android drvice will discover and connect the BT of Arduino thrn you can use HC-06.
 

Beja

Expert
Licensed User
Longtime User
Edit #11
Erel was right (as always) when he said yes you can use the HC-05.. unlike 06, the 05 can be configured as slave or master by AT commands, while the 06 is a slave and can not be configured.
 

Beja

Expert
Licensed User
Longtime User
Alright and finally I ran the project.. the LED of the Bluetooth is blinking but can't connect.. pairing was ok.
The log says "Illegal first word in line" but didn't specify the line number or the word.
 

Beja

Expert
Licensed User
Longtime User
Hi Erel,
I tried to install your "Complete B4R Code) in #1 above, but I get the following error message.. please help with any information.. thanks in advance.

B4R version: 1.00
Parsing code. (0.00s)
Compiling code. (0.03s)
Building project (0.05s)
Compiling & deploying Ino project (Arduino/Genuino Uno - COM4) Error
sketch\b4r_main.cpp: In function 'void SerialNative1(B4R::Object*)':
b4r_main.cpp:18: error: '::Serial1' has not been declared
::Serial1.begin(9600);
^
b4r_main.cpp:19: error: '::Serial1' has not been declared
b4r_main::_serialnative1->wrappedStream = &::Serial1;
^
exit status 1
'::Serial1' has not been declared
 

Beja

Expert
Licensed User
Longtime User
Thanks Erel
Yes it compiled and worked fine.. But the Android side is not connecting and locked in endless search loop. I could connect with older bt example but didn't know what commands should I send. Thanks.
 
Last edited:

Cableguy

Expert
Licensed User
Longtime User
The code from post #5 uses SoftwareSerial instead and will work on the Arduino Uno.

With the UNO or NANO, do we need the voltage divider?
 

funker

Member
Licensed User
Hello, my Error :

---------------------------------------------
B4A version: 6.30
Parsing code. (0.00s)
Compiling code. (0.08s)
Compiling layouts code. (0.00s)
Organizing libraries. (0.00s)
Generating R file. Error
res\values-v20\theme.xml:3: error: Error retrieving parent for item: No resource found that matches the given name '@android:style/Theme.Material.Light'.
-----------------------------------------------

Thanks
 
Status
Not open for further replies.
Top