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,619
Last edited:

tigleth

Member
Licensed User
Longtime User
Hello

i am using the hc 06 and arduino micro which has serial1 and the b4r code seems running when i connect with Bluetooth terminal i see the millis; the b4a side errors on opening line 30 ... attempt to invoke virtual method ... null object reference

thank you
 

Cableguy

Expert
Licensed User
Longtime User
You should create a new thread in the questions sub forum, and also post a bit more info, specially as it seems to be B4A related too... If possible, the problematic sub should also be posted so that we can try to help you better
 

emexes

Expert
Licensed User
How do I send strings?
Probably like this sample:
B4X:
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
from post #5 of this very thread.
 

rad

Member
Licensed User
Longtime 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.
Hi Erel...

I have try the code form post #5, and the result is I can turn on and off the led from android via bluetooth.
The question is, on the image/photo from post #1 we can see that there is a message received from device (Millis here : 1987095), but why this message is not appear on my android application, it still wrote "Device Message:"
 

rad

Member
Licensed User
Longtime User
Hi Erel...
Previously, I have changed pins 10 and 11 to 0 (rx) and 1 (txt).

Just now, I try to restore it as you suggested, using pins 10 and 11 instead of 0 (rx) and 1 (tx) pins.
And the results are as I expected, work like a charm ....

Thank you very much for your attention to my question.
 

vali khandangoll

Active Member
It means that the data wasn't sent properly. Maybe the BT module is not connected correctly.
Hi all of you.
i use your code and connect zs-40 to UNO and see this error.
B4R Version: 3.00
Parsing code. (0.00s)
Building folders structure. (0.01s)
Compiling code. (0.01s)
Building project (0.04s)
Compiling & deploying Ino project (Arduino/Genuino Uno - COM12) Error
Loading configuration...
Initializing packages...
Preparing boards...
Verifying...
Uploading...
avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x33
avrdude: stk500_getsync() attempt 2 of 10: not in sync: resp=0x33
avrdude: stk500_getsync() attempt 3 of 10: not in sync: resp=0x33
avrdude: stk500_getsync() attempt 4 of 10: not in sync: resp=0x0d
avrdude: stk500_getsync() attempt 5 of 10: not in sync: resp=0x0a
avrdude: stk500_getsync() attempt 6 of 10: not in sync: resp=0x33
avrdude: stk500_getsync() attempt 7 of 10: not in sync: resp=0x33
avrdude: stk500_getsync() attempt 8 of 10: not in sync: resp=0x31
avrdude: stk500_getsync() attempt 9 of 10: not in sync: resp=0x0d
avrdude: stk500_getsync() attempt 10 of 10: not in sync: resp=0x0a
An error occurred while uploading the sketch

there is not any different when connect
RX to 10 & TX to 11
or
revers.
and can not solve that.
 

tolisn

Member
Licensed User
Longtime User
Is there anyway to auto connect when the application opens without the need to press the "connect" button?
 

Beja

Expert
Licensed User
Longtime User
Hi Erel,
After many years I tried to run this app, but utterly failed. Here're the logs:

Looking for library 'user32'
Adding paths from jna.library.path: null
Trying user32.dll
Found library 'user32' at user32.dll
Looking for library 'shell32'
Adding paths from jna.library.path: null
Trying shell32.dll
Found library 'shell32' at shell32.dll
Looking for library 'Ole32'
Adding paths from jna.library.path: null
Trying Ole32.dll
Found library 'Ole32' at Ole32.dll
Loading configuration...
Initializing packages...
Preparing boards...
Verifying...
C:\Arduino\B4R\Arduino\arduino-builder -dump-prefs -logger=machine -hardware C:\Arduino\B4R\Arduino\hardware -tools C:\Arduino\B4R\Arduino\tools-builder -tools C:\Arduino\B4R\Arduino\hardware\tools\avr -built-in-libraries C:\Arduino\B4R\Arduino\libraries -libraries C:\Users\Control Acer\Documents\Arduino\libraries -fqbn=arduino:avr:uno -ide-version=10805 -build-path C:\Users\CONTRO~1\Desktop\APKs\ADRUIN~1\B4RSAM~1\Objects\bin -warnings=null -prefs=build.path=C:\Users\CONTRO~1\Desktop\APKs\ADRUIN~1\B4RSAM~1\Objects\bin -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.avrdude.path=C:\Arduino\B4R\Arduino\hardware\tools\avr -prefs=runtime.tools.avr-gcc.path=C:\Arduino\B4R\Arduino\hardware\tools\avr -prefs=runtime.tools.arduinoOTA.path=C:\Arduino\B4R\Arduino\hardware\tools\avr -verbose C:\Users\CONTRO~1\Desktop\APKs\ADRUIN~1\B4RSAM~1\Objects\src\src.ino
C:\Arduino\B4R\Arduino\arduino-builder -compile -logger=machine -hardware C:\Arduino\B4R\Arduino\hardware -tools C:\Arduino\B4R\Arduino\tools-builder -tools C:\Arduino\B4R\Arduino\hardware\tools\avr -built-in-libraries C:\Arduino\B4R\Arduino\libraries -libraries C:\Users\Control Acer\Documents\Arduino\libraries -fqbn=arduino:avr:uno -ide-version=10805 -build-path C:\Users\CONTRO~1\Desktop\APKs\ADRUIN~1\B4RSAM~1\Objects\bin -warnings=null -prefs=build.path=C:\Users\CONTRO~1\Desktop\APKs\ADRUIN~1\B4RSAM~1\Objects\bin -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.avrdude.path=C:\Arduino\B4R\Arduino\hardware\tools\avr -prefs=runtime.tools.avr-gcc.path=C:\Arduino\B4R\Arduino\hardware\tools\avr -prefs=runtime.tools.arduinoOTA.path=C:\Arduino\B4R\Arduino\hardware\tools\avr -verbose C:\Users\CONTRO~1\Desktop\APKs\ADRUIN~1\B4RSAM~1\Objects\src\src.ino
Using board 'uno' from platform in folder: C:\Arduino\B4R\Arduino\hardware\arduino\avr
Using core 'arduino' from platform in folder: C:\Arduino\B4R\Arduino\hardware\arduino\avr
Detecting libraries used...
"C:\Arduino\B4R\Arduino\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10805 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\Arduino\B4R\Arduino\hardware\arduino\avr\cores\arduino" "-IC:\Arduino\B4R\Arduino\hardware\arduino\avr\variants\standard" "C:\Users\CONTRO~1\Desktop\APKs\ADRUIN~1\B4RSAM~1\Objects\bin\sketch\src.ino.cpp" -o "nul"
Using cached library dependencies for file: C:\Users\CONTRO~1\Desktop\APKs\ADRUIN~1\B4RSAM~1\Objects\bin\sketch\AsyncStreams.cpp
Using cached library dependencies for file: C:\Users\CONTRO~1\Desktop\APKs\ADRUIN~1\B4RSAM~1\Objects\bin\sketch\B4RArduino.cpp
Using cached library dependencies for file: C:\Users\CONTRO~1\Desktop\APKs\ADRUIN~1\B4RSAM~1\Objects\bin\sketch\B4RCore.cpp
Using cached library dependencies for file: C:\Users\CONTRO~1\Desktop\APKs\ADRUIN~1\B4RSAM~1\Objects\bin\sketch\B4RSerializator.cpp
Using cached library dependencies for file: C:\Users\CONTRO~1\Desktop\APKs\ADRUIN~1\B4RSAM~1\Objects\bin\sketch\B4RStream.cpp
Using cached library dependencies for file: C:\Users\CONTRO~1\Desktop\APKs\ADRUIN~1\B4RSAM~1\Objects\bin\sketch\ByteConverter.cpp
Using cached library dependencies for file: C:\Users\CONTRO~1\Desktop\APKs\ADRUIN~1\B4RSAM~1\Objects\bin\sketch\RandomAccessFile.cpp
Using cached library dependencies for file: C:\Users\CONTRO~1\Desktop\APKs\ADRUIN~1\B4RSAM~1\Objects\bin\sketch\Scheduler.cpp
"C:\Arduino\B4R\Arduino\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10805 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\Arduino\B4R\Arduino\hardware\arduino\avr\cores\arduino" "-IC:\Arduino\B4R\Arduino\hardware\arduino\avr\variants\standard" "C:\Users\CONTRO~1\Desktop\APKs\ADRUIN~1\B4RSAM~1\Objects\bin\sketch\b4r_main.cpp" -o "nul"
Generating function prototypes...
"C:\Arduino\B4R\Arduino\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10805 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\Arduino\B4R\Arduino\hardware\arduino\avr\cores\arduino" "-IC:\Arduino\B4R\Arduino\hardware\arduino\avr\variants\standard" "C:\Users\CONTRO~1\Desktop\APKs\ADRUIN~1\B4RSAM~1\Objects\bin\sketch\src.ino.cpp" -o "C:\Users\CONTRO~1\Desktop\APKs\ADRUIN~1\B4RSAM~1\Objects\bin\preproc\ctags_target_for_gcc_minus_e.cpp"
"C:\Arduino\B4R\Arduino\tools-builder\ctags\5.8-arduino11/ctags" -u --language-force=c++ -f - --c++-kinds=svpf --fields=KSTtzns --line-directives "C:\Users\CONTRO~1\Desktop\APKs\ADRUIN~1\B4RSAM~1\Objects\bin\preproc\ctags_target_for_gcc_minus_e.cpp"
Compiling sketch...
Using previously compiled file: C:\Users\CONTRO~1\Desktop\APKs\ADRUIN~1\B4RSAM~1\Objects\bin\sketch\AsyncStreams.cpp.o
Using previously compiled file: C:\Users\CONTRO~1\Desktop\APKs\ADRUIN~1\B4RSAM~1\Objects\bin\sketch\B4RArduino.cpp.o
Using previously compiled file: C:\Users\CONTRO~1\Desktop\APKs\ADRUIN~1\B4RSAM~1\Objects\bin\sketch\B4RCore.cpp.o
Using previously compiled file: C:\Users\CONTRO~1\Desktop\APKs\ADRUIN~1\B4RSAM~1\Objects\bin\sketch\B4RSerializator.cpp.o
Using previously compiled file: C:\Users\CONTRO~1\Desktop\APKs\ADRUIN~1\B4RSAM~1\Objects\bin\sketch\B4RStream.cpp.o
Using previously compiled file: C:\Users\CONTRO~1\Desktop\APKs\ADRUIN~1\B4RSAM~1\Objects\bin\sketch\ByteConverter.cpp.o
Using previously compiled file: C:\Users\CONTRO~1\Desktop\APKs\ADRUIN~1\B4RSAM~1\Objects\bin\sketch\RandomAccessFile.cpp.o
Using previously compiled file: C:\Users\CONTRO~1\Desktop\APKs\ADRUIN~1\B4RSAM~1\Objects\bin\sketch\Scheduler.cpp.o
"C:\Arduino\B4R\Arduino\hardware\tools\avr/bin/avr-g++" -c -g -Os -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD -flto -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10805 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\Arduino\B4R\Arduino\hardware\arduino\avr\cores\arduino" "-IC:\Arduino\B4R\Arduino\hardware\arduino\avr\variants\standard" "C:\Users\CONTRO~1\Desktop\APKs\ADRUIN~1\B4RSAM~1\Objects\bin\sketch\b4r_main.cpp" -o "C:\Users\CONTRO~1\Desktop\APKs\ADRUIN~1\B4RSAM~1\Objects\bin\sketch\b4r_main.cpp.o"
C:\Users\CONTRO~1\Desktop\APKs\ADRUIN~1\B4RSAM~1\Objects\bin\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
 
Status
Not open for further replies.
Top