B4R Tutorial Connecting to Arduino with BLE (Bluetooth Low Energy)

In the previous example we broadcasted a single byte as part of the advertising data: https://www.b4x.com/android/forum/threads/ble-hm-10-module-broadcasting-a-single-byte.65785/

In this example the Android or iOS device (only one at a time) will connect to the BLE peripheral and read and write data to the Arduino board.

The phone controls the intensity of the two leds:

SS-2016-04-14_17.28.05.jpg


The steps required are:
1. Scan for BLE devices.
2. Connect to the BLE.
3. On iOS we need to call ReadData once to discover the characteristics.
4. Call SetNotify to be notified whenever the Arduino writes to the BLE.
5. Call WriteData to send data to the Arduino.

The B4R code:

B4X:
Sub Process_Globals
  Public Serial1 As Serial
  Private astream As AsyncStreams
  Private softserial As SoftwareSerial
  Private timer1 As Timer
  Private pins(2) As Pin
End Sub

Private Sub AppStart
   Serial1.Initialize(115200)
   Log("appstart")
   softserial.Initialize(9600, 2, 3)
   astream.Initialize(softserial.Stream, "astream_newdata", Null)
   timer1.Initialize("Timer1_Tick", 1000)
   timer1.Enabled = True
   'Must be pwm pins as we are using AnalogWrite
   pins(0).Initialize(5, pins(0).MODE_OUTPUT)
   pins(1).Initialize(6, pins(1).MODE_OUTPUT)
   pins(1).DigitalWrite(True)
End Sub

Sub Timer1_Tick
   astream.Write("Time: ".GetBytes)
   astream.Write(NumberFormat(Millis, 0, 0).GetBytes)
End Sub

Sub astream_NewData (Buffer() As Byte)
   For i = 0 To Buffer.Length - 2 Step 2
     Dim pinIndex As Byte = Buffer(i)
     Dim pinValue As Byte = Buffer(i + 1)
     'using constrain to avoid non allowed values
     pins(Constrain(pinIndex, 0, 1)).AnalogWrite(pinValue)
   Next
End Sub

The B4A and B4i projects are attached.
 

Attachments

  • B4i_BLE.zip
    3.1 KB · Views: 1,337
  • B4A_BLE.zip
    8.5 KB · Views: 2,185
Last edited:

dilettante

Active Member
Licensed User
Longtime User
In case anyone got this far down without moving on in frustration...

BLE = Bluetooth Low Energy, and requires some sort of (BLE) Shield.
 

tufanv

Expert
Licensed User
Longtime User
In the previous example we broadcasted a single byte as part of the advertising data: https://www.b4x.com/android/forum/threads/ble-hm-10-module-broadcasting-a-single-byte.65785/

In this example the Android or iOS device (only one at a time) will connect to the BLE peripheral and read and write data to the Arduino board.

The phone controls the intensity of the two leds:

SS-2016-04-14_17.28.05.jpg


The steps required are:
1. Scan for BLE devices.
2. Connect to the BLE.
3. On iOS we need to call ReadData once to discover the characteristics.
4. Call SetNotify to be notified whenever the Arduino writes to the BLE.
5. Call WriteData to send data to the Arduino.

The B4R code:

B4X:
Sub Process_Globals
  Public Serial1 As Serial
  Private astream As AsyncStreams
  Private softserial As SoftwareSerial
  Private timer1 As Timer
  Private pins(2) As Pin
End Sub

Private Sub AppStart
   Serial1.Initialize(115200)
   Log("appstart")
   softserial.Initialize(9600, 2, 3)
   astream.Initialize(softserial.Stream, "astream_newdata", Null)
   timer1.Initialize("Timer1_Tick", 1000)
   timer1.Enabled = True
   'Must be pwm pins as we are using AnalogWrite
   pins(0).Initialize(5, pins(0).MODE_OUTPUT)
   pins(1).Initialize(6, pins(1).MODE_OUTPUT)
   pins(1).DigitalWrite(True)
End Sub

Sub Timer1_Tick
   astream.Write("Time: ".GetBytes)
   astream.Write(NumberFormat(Millis, 0, 0).GetBytes)
End Sub

Sub astream_NewData (Buffer() As Byte)
   For i = 0 To Buffer.Length - 1 Step 2
     Dim pinIndex As Byte = Buffer(i)
     Dim pinValue As Byte = Buffer(i + 1)
     'using constrain to avoid non allowed values
     pins(Constrain(pinIndex, 0, 1)).AnalogWrite(pinValue)
   Next
End Sub

The B4A and B4i projects are attached.

Is it possible for you to post a tutorial like this but with a rgb led so we can control the color of the rgb led remotelyu with our phone ?
 

tufanv

Expert
Licensed User
Longtime User
There is no logical difference between regular leds and rgb leds. An rdb led is the same as three leds.
Yes but the regular leds have a color and you just switch them on , but rgb leds as i know we can set the color witg rgb values, ı want to know how can i set the rgb value for the led
 

tufanv

Expert
Licensed User
Longtime User
In the previous example we broadcasted a single byte as part of the advertising data: https://www.b4x.com/android/forum/threads/ble-hm-10-module-broadcasting-a-single-byte.65785/

In this example the Android or iOS device (only one at a time) will connect to the BLE peripheral and read and write data to the Arduino board.

The phone controls the intensity of the two leds:

SS-2016-04-14_17.28.05.jpg


The steps required are:
1. Scan for BLE devices.
2. Connect to the BLE.
3. On iOS we need to call ReadData once to discover the characteristics.
4. Call SetNotify to be notified whenever the Arduino writes to the BLE.
5. Call WriteData to send data to the Arduino.

The B4R code:

B4X:
Sub Process_Globals
  Public Serial1 As Serial
  Private astream As AsyncStreams
  Private softserial As SoftwareSerial
  Private timer1 As Timer
  Private pins(2) As Pin
End Sub

Private Sub AppStart
   Serial1.Initialize(115200)
   Log("appstart")
   softserial.Initialize(9600, 2, 3)
   astream.Initialize(softserial.Stream, "astream_newdata", Null)
   timer1.Initialize("Timer1_Tick", 1000)
   timer1.Enabled = True
   'Must be pwm pins as we are using AnalogWrite
   pins(0).Initialize(5, pins(0).MODE_OUTPUT)
   pins(1).Initialize(6, pins(1).MODE_OUTPUT)
   pins(1).DigitalWrite(True)
End Sub

Sub Timer1_Tick
   astream.Write("Time: ".GetBytes)
   astream.Write(NumberFormat(Millis, 0, 0).GetBytes)
End Sub

Sub astream_NewData (Buffer() As Byte)
   For i = 0 To Buffer.Length - 1 Step 2
     Dim pinIndex As Byte = Buffer(i)
     Dim pinValue As Byte = Buffer(i + 1)
     'using constrain to avoid non allowed values
     pins(Constrain(pinIndex, 0, 1)).AnalogWrite(pinValue)
   Next
End Sub

The B4A and B4i projects are attached.

I have a little problem. I have connected one of the leds to 5 an the other one to 6, with other legs connected to gnd via 330. i run the b4r app, green led is always on with full brightness and red is not. Later i run the b4i app, i get the connected msg. When i change one of the sliders red goes on but nothing changes when i slide it, nothing happens to green with the other slider. What did i make wrong any ideas ?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The first step in debugging this is to use ByteConverter to log the data received:
B4X:
Log(bc.HexFromBytes(Buffer))
Also test it without the BLE connection. Set the pins values directly and make sure that the leds react as expected.

Note that there is a bug in v1.00 beta 6 related to AsyncStreams. Beta 7 will be released today.
 

tufanv

Expert
Licensed User
Longtime User
The first step in debugging this is to use ByteConverter to log the data received:
B4X:
Log(bc.HexFromBytes(Buffer))
Also test it without the BLE connection. Set the pins values directly and make sure that the leds react as expected.

Note that there is a bug in v1.00 beta 6 related to AsyncStreams. Beta 7 will be released today.

i used the upper slider in b4i and the log on b4r was:

B4X:
appstart
003A0053005B30
003A0053005B30
003A0053005B30
Out of bounds error. Array length = 7, Index = 7

I get the same error for the other slider and time stops after this error and nothing happens.
 

tufanv

Expert
Licensed User
Longtime User
I've updated the for line to:
B4X:
For i = 0 To Buffer.Length - 2 Step 2
This will fix the out of bounds error. As I wrote you should wait for beta 7 before you further tests it.
ok I will change the code and wait for the beta7 =) thanks
 

tufanv

Expert
Licensed User
Longtime User
I've updated the for line to:
B4X:
For i = 0 To Buffer.Length - 2 Step 2
This will fix the out of bounds error. As I wrote you should wait for beta 7 before you further tests it.

by the way i just changed the code and wanted to test it, and it works fine now with beta 6.
 

stari

Active Member
Licensed User
Longtime User
Until now, I used another tool for Arduino (microBasic for AVR). Now I use only B4R.
Question: How can I communicate simultaneously with multiple HM-10 devices?
 

kostas3001

Member
Licensed User
Longtime User
What if there are 4 arduino equal teams, with HM-10 each and only 1 smartphone? I want to communicate only with one of them. How can I do it?
 

Beja

Expert
Licensed User
Longtime User
The B4A example was updated with the changes required when setting targetSdkVersion to 26.

Hi Erel
Can I use HC-31 instead of the ble shield with Arduino Uno?

Thanks.
 
Top