B4R Tutorial HC 05 Classic Bluetooth

Discussion in 'B4R Tutorials' started by Erel, May 8, 2016.

  1. Erel

    Erel Administrator Staff Member Licensed User

    [​IMG]

    [​IMG]

    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:
    Code:
    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).
    Code:
    '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:
    Code:
    Sub Process_Globals
      
    Public Serial1 As Serial
      
    Private SerialNative1 As Stream
      
    Private astream As AsyncStreams
      
    Private leds(1As 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, 00).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
     

    Attached Files:

    Last edited: Apr 8, 2019
  2. jahswani

    jahswani Active Member Licensed User

    It should work with HC06 too right ?
     
  3. Erel

    Erel Administrator Staff Member Licensed User

    Yes.
     
  4. ingo.tw

    ingo.tw Member Licensed 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: May 10, 2016
  5. Erel

    Erel Administrator Staff Member Licensed 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:

    Code:
    Sub Process_Globals
      
    Public Serial1 As Serial
      
    Private SoftwareSerial1 As SoftwareSerial
      
    Private astream As AsyncStreams
      
    Private leds(1As 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(
    96001011'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, 00).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
     
    Johan Schoeman likes this.
  6. ingo.tw

    ingo.tw Member Licensed User

    Thank you. This issue has been resolved with your code
     
    Last edited: May 10, 2016
  7. Beja

    Beja Expert Licensed 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
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    Yes, it is a complete replacement.
     
    Beja likes this.
  9. Beja

    Beja Expert Licensed User

    He Erel,
    Can this example project run on Uno also?

    Thanks in advance.
     
  10. inakigarm

    inakigarm Well-Known Member Licensed User

    Code on Post #5 it's for Uno
     
    Beja likes this.
  11. Beja

    Beja Expert Licensed User

    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.
     
    jahswani likes this.
  12. Beja

    Beja Expert Licensed 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.
     
  13. Beja

    Beja Expert Licensed 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.
     
  14. Beja

    Beja Expert Licensed 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
     
  15. Erel

    Erel Administrator Staff Member Licensed User

    The code from post #1 will only work on a device with multiple hardware serial ports, like Arduino Due or Mega.

    The code from post #5 uses SoftwareSerial instead and will work on the Arduino Uno.
     
  16. Beja

    Beja Expert Licensed 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: Jun 27, 2016
  17. Erel

    Erel Administrator Staff Member Licensed User

    There are no commands. Check the B4A logs and see which devices are found.
     
    Beja likes this.
  18. Cableguy

    Cableguy Expert Licensed User

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

    positrom2 Active Member Licensed User

    Cableguy likes this.
  20. funker

    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
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice