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
Hi

The problem was finally solved with the help of a fellow member (walterf25). The problem was located in the autoconnect routine that I had in the main process. That was causing the lag with the data values.
After reverting back to the original manual connect process and modifying a few lines in the Admin_DiscoveryFinished sub, I got it to work with no lags whatsoever.

Thanks for the help


B4X:
Sub Admin_DiscoveryFinished
ProgressDialogHide
If foundDevices.Size = 0 Then
ToastMessageShow("No device found.", True)
Else
Dim l As List
l.Initialize
For i = 0 To foundDevices.Size - 1
Dim nm As NameAndMac
nm = foundDevices.Get(i)
l.Add(nm.Name)
If nm.Name.StartsWith("R") Then    'Here i basically look for my device's name which starts with the letter "R"
Log("Connecting.....")
serial1.ConnectInsecure(admin, nm.Mac, 1)       'Also try using ConnectInsecure, is a better way to connect since there are devices that have some issues when trying to connect with Connect, or Connect2 or Connect3
Exit 
End If
Next
End If
End Sub
 

Atul_savla

New Member
Nice example. I'm new in programming so excuse me.i have some application. i am try to run simulate the code but get some
ERROR
ERROR.JPG

ERROR.JPG
Like
i user b4a trial virsion

thanks
 

Mitesh_Shah

Member
Licensed User
Longtime User
I'm also try to use code for my micro cntroller application. try to test with USB blue tooth dongle on PC and Check With Com Terminal Software. but nothing to communication. can u help ?. i new in Android programming. i am working in Micro cntroller and VB6, or VB.net.
 

walterf25

Expert
Licensed User
Longtime User
I'm also try to use code for my micro cntroller application. try to test with USB blue tooth dongle on PC and Check With Com Terminal Software. but nothing to communication. can u help ?. i new in Android programming. i am working in Micro cntroller and VB6, or VB.net.

What exactly do you need help with, can you be more specific?
 

Mitesh_Shah

Member
Licensed User
Longtime User
HI thanks for reply.
i want to test the code to communicate with BT module using RS232 Terminal Software. it's need other configuration in BT module ? . my BT module on 9600 baud rate. and it's RS232 interface.
 

dein

New Member
hello all, I have a question and it has nothing to do with the bluetooth communication microcontroller, but still related to bluetooth. I want to make a voice command with Bluetooth communication in android, not by the microcontroller but the raspberry pi, but I am still having trouble with his code. how to sending voice data through bluetooth. I was a newbie to this basic4android, I wish everything on this forum can help me. Thank you so much. I'm sorry if my english is less understandable, because my English is bad.
 

Mitesh_Shah

Member
Licensed User
Longtime User
HI all

i transmit some data from android, (to operate relay) it's work perfectly. but my receiving part not working properly. some time it's receive and some time not received.

i send u string = AT12345678 (10 characters) from my Micro ocntroller with CHR 13

i have some quires about receiving part

1] what is max or minimum receiving length ?
2] after sending string, it's required CHR-13 ?

here my code

Sub AStreams_NewData (Buffer() As Byte)

If Buffer.Length >= 8 Then
test = ""
s = ""
s = BytesToString(Buffer, 0, Buffer.Length, "UTF8")

If s.IndexOf("AT") <> -1 Then
test = s.SubString(2)
EditText1.text = " "
EditText1.text = test
End If
End If
End Sub

thanks
 

Opa

New Member
Licensed User
Longtime User
Your b4a code seems ok.
However, as often said in this forum, for a reliable bluetooth transmission prefix mode is necessary.
The microcontroller code should be in pseudo C something like this:
B4X:
putc(0);     putc(0);   putc(0);  putc(10);     //   prefix=10 as integer, this will not be visible on the receiver side 
putc(65);  putc(84);      //   A T                 // 2 bytes
putc(1);   putc(2); ...   putc(8);                // the 8 remaining bytes, not followed with anything like 10 or 13
In the b4a code after
Sub Activity_Create(FirstTime As Boolean)
enter
AStreams.InitializePrefix(Main.Serial1.InputStream, True, Main.Serial1.OutputStream, "AStreams") ' little Endian True

The maximum receiving lenght: i did succesfully with 120 bytes, as close as possible after each other packed
Good luck !

HI all

i transmit some data from android, (to operate relay) it's work perfectly. but my receiving part not working properly. some time it's receive and some time not received.

i send u string = AT12345678 (10 characters) from my Micro ocntroller with CHR 13

i have some quires about receiving part

1] what is max or minimum receiving length ?
2] after sending string, it's required CHR-13 ?

here my code

Sub AStreams_NewData (Buffer() As Byte)

If Buffer.Length >= 8 Then
test = ""
s = ""
s = BytesToString(Buffer, 0, Buffer.Length, "UTF8")

If s.IndexOf("AT") <> -1 Then
test = s.SubString(2)
EditText1.text = " "
EditText1.text = test
End If
End If
End Sub

thanks
 

Mitesh_Shah

Member
Licensed User
Longtime User
HI All
i make Android apps to control Relay's via BT Module its' working perfectly in android 4.0.4 (Samsung) but i test in other phone OS 4.4.2 kitkat(Micro Max) it's not connect with BT Module. i used B4A 2.71

Regard
Mitesh
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
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 15146

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

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
Is the rn42 all that's needed or is there another microcontroller involved? Cause that's $13 a unit in high quantity and that's awesome
 

walterf25

Expert
Licensed User
Longtime User
Is the rn42 all that's needed or is there another microcontroller involved? Cause that's $13 a unit in high quantity and that's awesome
You need a micro-controller as well, the one used in this example is an Atmega168p which costs around $1.60, you can use any other micro-controller. The source code for the micro-controller is attached on the first post, the compiler used is called Bascom AVR which uses a syntax similar to B4A.
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
Thank you. I can find a simple chip of the Atmega168p, but where's one with a USB port, battery slot, etc? Or can it plug right into the rn42?
 

walterf25

Expert
Licensed User
Longtime User
Thank you. I can find a simple chip of the Atmega168p, but where's one with a USB port, battery slot, etc? Or can it plug right into the rn42?
I think it would be better if you did a little more research about microcontrollers if you are new to them. There are tons of different programmers you can build or buy as far as power you need to provide an external power supply, typically it needs a +5VDC power source, you would need to check out the data sheet to find out what pin is Ground and which pin is +vcc. If you're not too interested in learning all of this then I would suggest to buy a development board such as the arduino which already comes with a boot loader and power supply, all arduino boards are based on the Atmega Atmel chips.

Good luck.
 
Top