Share My Creation Bluetooth Microcontroller communication

walterf25

Expert
Licensed 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 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
 

positrom2

Active Member
Licensed User
Nice example. I will analyse it to see how the appraised asyncstream is to be set up. I didn't get that to work, using Textreader instead but can't transmit binary (e.g. printbin won't work). Why do you use the software Uart at the atmega128? Baudrate is rather low. With BTM222a or better b I use 920000 (or so). Works flawlessly.
Regards, positrom2
 

walterf25

Expert
Licensed User
Bluetooth and Microcontroller

Hi Positron, i use software uart because the Atmega168 has only one hardware uart and i'm using two serial ports, one to communicate with the bluetooth and the other to print the received command from the bluetooth to hyperterminal this is just for debugging purpposes you can remove it if you like.

I use 9600 baud rate the RN-42 bluetooth module only has 2 modes you can use it, one is at 9600 Baud Rate and the other one is 115200, you can use either one, i prefer a slower one for my purposes.

Cheers,
Walter
 

YIM bunchhat

Active Member
Licensed User
Hi walterf25,
Sorry now I am really stuck on it, my question may be out of topic, but it is related to bluetooth project too. I can use the bluetooth for my project by follow Erel example smoothly but my problem is Script how to make my template can use with all screen size (phone and tablet). I have attach my template with for you too, can you show me an example to make it or can you do it for me ask example? Note: I already read forum about script by Erel :sign0013:View attachment worlde.bal
 

dddvvv

Member
Licensed User
Hi Walter

Im trying to figure out how you used asyncstream without the prefix, in your code. Im trying to do the exact same thing, but this time, with a pic micro. how are you sending the bytes from the avr to the android?

thanks
 

walterf25

Expert
Licensed User
Bluetooth and Microcontroller

HI there, is actually very simple, all i'm doing is sending for example i'm reading the voltage from one of the ADCs in the Atmega168 in this case i send the word "volt" followed by the reading of the ADC for example "volt" & 2.5 on the android side i look for the word volt once i receive that part then i can parse the voltage and display on the screen, i just added an LCD and a temperature sensor the the circuit and i can read both the temperature and the voltage on my phone, i've also added a small motor which i can control the speed through my phone.

Let me know if this helps.

Cheers,
Walter
 

tolisn

Member
Licensed 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
Hi.
I'm new in programming so excuse me.
How Can I see the buttons layout that you use to control the leds within your program ? I cannot see any graphical form that contains these buttons
 

walterf25

Expert
Licensed User
Bluetooth and Microcontroller

Hi there, if you download attachment 15147 you can see the full code, i just took the example created by Erel, and made it fit my application, you can see the layout files there.

Let me know if this answers your questions.
 

tolisn

Member
Licensed User
I have loaded your program but I can only see 2 forms. "Chatactivity" and "Main"
I cannot see any form that contains the buttons:sign0085:
 

walterf25

Expert
Licensed User
Bluetooth and Microcontroller

The Main layout contains two buttons one to search for devices and the other to make your device discoverable.

The ChatActivity, opens up once you pair and make a connection with your phone and another bluetooth device, you should see an EditText, two buttons one to turn on an LED and another one to turn if OFF, also you should see a slider bar to dim up or dim down the LED.

hope this helps!
 

tolisn

Member
Licensed User
So, if I undestand correctly, the buttons are in the code and are not in a seperate graphical form (something like visual basic) ?
The next question is how do you control where they are placed in the screen and how big they are ?
 

walterf25

Expert
Licensed User
Bluetooth and Microcontroller

Ok, I think i understand your question now, there should be two layout files, named
and
the first one contains the graphical layout with the buttons, and the second layout contains the graphical objects for the CharActivity screen.

in order to place the buttons, labels, etc.. i used the Designer Script window.
You can read about it here http://www.basic4ppc.com/forum/basic4android-getting-started-tutorials/16120-designer-scripts-tutorial.html#post91582

with the designer script you can set your views so that no matter what device you use, Tablet, phone with different resolutions the views will get resized to fit any screen.

hope this helps!
 

tolisn

Member
Licensed User
Hi
is there anyway to add a CR character on the end of the string that you send when the "light on" and "light off" button is pressed ?
 

tolisn

Member
Licensed User
OK, I got the buttons to control my robot.
Now, how can I read a value from my robot.
The robot sends the value $b10.8. This is the battery voltage.
How can I display this on the tablet screen ?
 

walterf25

Expert
Licensed User
Bluetooth and Microcontroller

Hi there, make sure you have this sub

B4X:
Sub AStreams_NewData (Buffer() As Byte)

'Log(BytesToString(Buffer, 0, Buffer.Length, "UTF8"))
 If Buffer.Length >= 8 Then
   s =  BytesToString(Buffer, 0, Buffer.Length, "UTF8")
   
   If s.IndexOf("volt") <> -1 Then
   test = s.SubString(4)
   If IsNumber(test) Then
   voltage = test
   'Log(NumberFormat(voltage, 0, 4))
readings.Text = NumberFormat(voltage, 0, 2)
End If
End If


   If s.IndexOf("Temp") <> -1 Then
   tempreading = s.SubString(4)
   If IsNumber(tempreading) Then
   tempvoltage = tempreading
   'Log(NumberFormat(tempvoltage, 0, 2))
   temp.Text = tempvoltage & " " & "°F"
   End If
   End If
End If
   
End Sub
anything sent to your bluetooth will arrive and seen at this sub, just make sure you extract only the data you want, take a look at my example, in my case i look for two different values, one is the voltage from an analog to digital converter and another one is the temperature value, in both cases i first look for the starting string as you can see for the voltage i look for the string "volt" and then i just parse the rest of the string, same for the temperature values.

Hope this helps!
 

tolisn

Member
Licensed User
Hi
If I undestood correctly, I just substite the
If s.IndexOf("volt") <> -1 Then
with
If s.IndexOf("$b") <> -1 Then

and that is it or do I need to change some other parameters like s.substring(4) and buffer.lenght too ?
 
Top