Share My Creation Bluetooth Microcontroller communication

Hello everyone, i thought i'd share this project with everyone who's interested, i've seen a few posts of a few people trying to work on some projects that involve communication with a micro-controller via bluetooth.
Well i've been working on and off on this project and here's what i have so far, please feel free to check it out and if you have any questions feel free to message me and i'll try to answer your questions.

the project consists of a modified version of the chat program posted by erel, and the micro-controller side was written also in Basic, with Bascom AVR, the micro-controller being used is an Atmega168.

So far i'm able to transmit the voltage being read by the microcontroller on one of the Analog to Digital converters, and control a few LEDs on and off, also dim one led up and down.

So far this project works as expected, my next step will be to connect this interface to a fixture i built at work to control 5 different air cylinders to test the buttons on a product!

here is the source code for the microchip
View attachment Wireless DC Meter.zip

and here is the source code for the android side, obviously written in Basic4Android
View attachment Bluetooth.zip

The bluetooth module i'm using is an RN-42 module by Rover Networks
Roving Networks | RN-42

The software used for the microncontroller can be found here, note that it is very easy to use as it is based on Basic programming language.
http://www.mcselec.com/index.php?option=com_content&task=view&id=14&Itemid=41

Hope this helps someone, again feel free to ask questions!

I will try to post a video of the whole setup when i get a chance to!

Cheers,
Walter
 

tolisn

Member
Licensed User
Longtime User
So I don't need to change the other parameters like s.substring(4) and buffer.lenght ?
 

walterf25

Expert
Licensed User
Longtime User
Bluetooth and Microcontroller

just change the 4 to 2

B4X:
s.substring(2)

since your string only has 2 characters at the begining, that line is basically removing the first two characters in your string received.
 

tolisn

Member
Licensed User
Longtime User
Hi
I back again with yet a few questions.

My micro continiously sends information regarding the status of the battery, the compass info and the the sonar reading.
The battery status is send using the header $b_data and the compass info is sent using the header $h_data

I can read the battery info using this code

B4X:
Sub AStreams_NewData (Buffer() As Byte)

 If Buffer.Length >= 4 Then
   s =  BytesToString(Buffer, 0, Buffer.Length, "UTF8")
   
   If s.IndexOf("$b") <> -1 Then
   test = s.SubString(2)
   If IsNumber(test) Then
   voltage = test
   Log(NumberFormat(voltage, 0, 4))
battery.Text = NumberFormat(voltage, 0, 5)

End If
End If
End If   'readings.Text = BytesToString(Buffer, 4, Buffer.Length - 4, "UTF8"
End Sub

The question is, what code is needed so I can read the battery data and the compass data and display them in a different section on the screen?


My other question is how can I make the program force connect to a specific bluetooth device if it finds it without needing to always search for devices and then select the correct one ?
 
Last edited:

positrom2

Active Member
Licensed User
Longtime User
What I always wanted to ask:
B4X:
Sub AStreams_NewData (Buffer() As Byte)
Is Buffer() a reserved "word"? That appears also in other B4A statements. Could one take
B4X:
Buffer5()
instead, for instance?
(I never got asyncstreams to run properly, so I cannot try myself so far).
Thanks, positrom2
 

walterf25

Expert
Licensed User
Longtime User
Bluetooth and Microcontroller

Hi Tolisn, you can something like this to read both strings of data.

B4X:
Sub AStreams_NewData (Buffer() As Byte)

 If Buffer.Length >= 4 Then
    s =  BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    
    If s.IndexOf("$b") <> -1 Then
    test = s.SubString(2)
    If IsNumber(test) Then
    voltage = test
    Log(NumberFormat(voltage, 0, 4))
     battery.Text = NumberFormat(voltage, 0, 5)
      else if s.IndexOf("&h") <> -1 then    'this looks for the other compass data
      test = s.SubString(2)
      If IsNumber(test) then
      voltage = test
      Log(NumberFormat(voltage, 0, 4))
      compass.text = NumberFormat(voltage, 0, 5)
End If
End If
End If    'readings.Text = BytesToString(Buffer, 4, Buffer.Length - 4, "UTF8"
End Sub

hope this helps!

Cheers,
Walter
 

Tom Christman

Active Member
Licensed User
Longtime User
Walter, I hope I'm not violating the "New Thread" requirements, but I have enjoyed your very useful posts re: " Bluetooth Microcontroller Communication" and the use of Bascom for microcontroller programming. I have used Bascom for many years now and have also used the Atmel devices. I found them to be highly useful with one exception....the 10 bit A/D's. More recently, I have used the line of 8052 clones by Analog Devices starting with the ADuC812 ( with 12 bit A/D's, 24k flash memory), the ADuc831( 12 bit A/D's, with 61K flash), and the ADuC845 (with 24 bit A/D's, with 61k flash). An additional quality of these devices is the ease of programming via RS232 (using Analogs "Windows Serial Downloader" Program). In the event that you require more "bit-power A/D's", these devices might fill your needs. Thanks for the great posts.
Tom
 

tolisn

Member
Licensed User
Longtime User
Hi Tolisn, you can something like this to read both strings of data.

B4X:
Sub AStreams_NewData (Buffer() As Byte)

 If Buffer.Length >= 4 Then
    s =  BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    
    If s.IndexOf("$b") <> -1 Then
    test = s.SubString(2)
    If IsNumber(test) Then
    voltage = test
    Log(NumberFormat(voltage, 0, 4))
     battery.Text = NumberFormat(voltage, 0, 5)
      else if s.IndexOf("&h") <> -1 then    'this looks for the other compass data
      test = s.SubString(2)
      If IsNumber(test) then
      voltage = test
      Log(NumberFormat(voltage, 0, 4))
      compass.text = NumberFormat(voltage, 0, 5)
End If
End If
End If    'readings.Text = BytesToString(Buffer, 4, Buffer.Length - 4, "UTF8"
End Sub

hope this helps!

Cheers,
Walter


Thanks for the suggestion, I'll try it out and see how it goes. I tried somethig like this but I kept getting only the battery data and not the compass data.


What about my question regarding the force connecting of the bluetooth device?
 

tolisn

Member
Licensed User
Longtime User
Walter I just tried your suggestion but I keep getting only the battery data and not the compass data. If I try to receive only the compass data or only the battery data then it is ok.
 

walterf25

Expert
Licensed User
Longtime User
Bluetooth and Microcontroller

Hi Toslin i made a mistake in the code i gave you instead of putting
B4X:
else if s.IndexOf("&h") <> -1 then

use this

B4X:
else if s.IndexOf("$h") <> -1 then

If you notice i placed an
instead of

hope this helps!
 

tolisn

Member
Licensed User
Longtime User
I just tried it, but still nothing. If I comment out the line used for the battery data then the compass data is shown
 

walterf25

Expert
Licensed User
Longtime User
Bluetooth and Microcontroller

Ok try this then
B4X:
Sub AStreams_NewData (Buffer() As Byte)

 If Buffer.Length >= 4 Then
    s =  BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    
    If s.IndexOf("$b") <> -1 Then
    test = s.SubString(2)
    If IsNumber(test) Then
    voltage = test
    Log(NumberFormat(voltage, 0, 4))
     battery.Text = NumberFormat(voltage, 0, 5)
end if
end if

 if s.IndexOf("$h") <> -1 then    'this looks for the other compass data
      test = s.SubString(2)
      If IsNumber(test) then
      voltage = test
      Log(NumberFormat(voltage, 0, 4))
      compass.text = NumberFormat(voltage, 0, 5)
End If
End If
End If

This should work!
 

tolisn

Member
Licensed User
Longtime User
Yes, that did the trick.

Now one more question. How is it possible to force connect to a known bluetooth module without going through the search and select procedure ?
 

walterf25

Expert
Licensed User
Longtime User

tolisn

Member
Licensed User
Longtime User
B4X:
Sub AStreams_NewData (Buffer() As Byte)

 If Buffer.Length >= 4 Then
    s =  BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    
    If s.IndexOf("$b") <> -1 Then  'this looks for the battery data
    test = s.SubString(2)
    If IsNumber(test) Then
    voltage = test
    Log(NumberFormat(voltage, 0, 4))
     battery.Text = NumberFormat(voltage, 0, 5)
End If
End If

 If s.IndexOf("$h") <> -1 Then    'this looks for the compass data
      test = s.SubString(2)
      If IsNumber(test) Then
      voltage = test
      Log(NumberFormat(voltage, 0, 4))
      heading.text = NumberFormat(voltage, 0, 5)
End If
End If

End If

End Sub


The above code works OK and data come smoothly from my MCU to the android device.
When I add one more check so I can add one more data input like the code below, then I get the following in the log

B4X:
sending message to waiting queue (astreams_newdata)
Ignoring event (too many queued events: astreams_newdata)

and the data does not come smoothly from the MCU.
Why is this happening when I add the additional check ?


B4X:
Sub AStreams_NewData (Buffer() As Byte)

 If Buffer.Length >= 4 Then
    s =  BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    
    If s.IndexOf("$b") <> -1 Then  'this looks for the battery data
    test = s.SubString(2)
    If IsNumber(test) Then
    voltage = test
    Log(NumberFormat(voltage, 0, 4))
     battery.Text = NumberFormat(voltage, 0, 5)
End If
End If

 If s.IndexOf("$h") <> -1 Then    'this looks for the compass data
      test = s.SubString(2)
      If IsNumber(test) Then
      voltage = test
      Log(NumberFormat(voltage, 0, 4))
      heading.text = NumberFormat(voltage, 0, 5)
End If
End If

 If s.IndexOf("$s") <> -1 Then 'this looks for the sonar data
    test = s.SubString(2)
    If IsNumber(test) Then
    voltage = test
    Log(NumberFormat(voltage, 0, 4))
     sonar.Text = NumberFormat(voltage, 0, 5)
End If
End If


End If

End Sub

And one more question. I have seen elsewhere in the forum the you are doing home automation. How can I connect to multiple bluetooth modules from one android device at the same time? This sound like a very good project.
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Top