Android Control of Propeller using Joystick BT Commander App -- Spin Code
JonnyMac
Posts: 9,105
I've started to explore controlling some apps via my Android phone through the Bluetooth connection. In another thread, I was pointed to Joystick BT Commander. The creator is an Arduino user, so that's the existing code base. As I do my work in Spin, I created a template app for myself and others to use when JBTC is the remote. The attached code is setup as a framework for testing. I used the Activity Board for my test. Two of the buttons on the program control the P26 and P27 LEDs. Button B6 on the remote clears the other buttons. The mainline code synthesizes a simple RTC that is sent back to the remote.
I tested the program with the Parallax RN-42 module, and an $8 HC-06 that I bought from eBay.
Updated: 31 JAN 2015
I tested the program with the Parallax RN-42 module, and an $8 HC-06 that I bought from eBay.
Updated: 31 JAN 2015
Comments
I have a bluetooth app on my hard drive somewhere made with b4a, I will post when I find it if you are interested,
In fact I am -- that's where this all started. I created a simple app using MIT App Inventor 2 as the front end for a project. Another forum member pointed out JBTC and lamented that the original code was in C and he couldn't translate it to Spin. This saddens me a bit, as I find Spin far easier and more elegant than C when it comes to the Propeller. I translated the core behavior of the Arduino JBTC application as an exercise and service to those that would like to use that phone app with Spin on the Propeller.
My goal is custom solutions on both ends. I'll have a look at B4A. Thanks.
I too am starting to look at how we can integrate those cheap android tablets into microcontroller controls and windows.
Presently you can buy a 7 inch android for about the same price as a 7" composite/vga screen.
For completeness to this thread, there is also another free alternative for Android developement called RFOBASIC which is a simple BASIC programming language that runs directly on the Android itself.
BTW, Jon would you mind sharing the breakdown of your transmitted packets so that we can adapt this app for other microcontroller applications?
(and I'd like to understand your method a little better for personal educational reasons.)
Jon,
That was me. Thanks for writing a SPIN version. I learn best when having examples to compare. Since I wrote a version in C having a SPIN version will help a lot. I've also just got the Propeller manual in book form, which I find more useful for learning & note taking compared with a pdf.
Tom
As there is no generic "white paper" for JBTC, I had to wade through the Arduino code -- it's pretty easy in the end.
-- command will be 8 bytes for joystick values
-- command will be 3 bytes for button change event
-- all valid command packets begin with <STX> ($02) and end with <ETX> ($03)
Joystick:
-- The JBTC joystick values range from -100 to 100; these are values are transmitted in ASCII format after an offset of 200 is added to each axis
-- offset is added so that values can be sent as three ASCII chars: hundreds digit, tens digit, ones digit without sign indicator
-- With the joystick at 0, 0 the command packet is: <STX> <"2"> <"0"> <"0"> <"2"> <"0"> <"0"> <ETX>
-- my program has a method called get_joystick that converts the respective strings to numbers and removes the offset (values are signed)
Buttons:
-- Button events send a single character in a 3-byte packet
-- B1 uses "A" for changed to on, "B" for changed to off -- command packet when B1 is click on is <STX> <"A"> <ETX>
-- B2 uses "C" for changed to on, "D" for changed to off
-- B3..B6 follow in order; valid button even characters are "A".."L"
If sending a response to JBTC the packet will contain four strings with additional separators: <STX> <buttons> <$01> <Data1> <$04> <Data2> <$05> <Data3> <ETX>
-- button status is binary formatted string (no indicator)
-- data fields sent as strings
-- send empty string to unused field (not sure if short response packet is allowed without additional testing)
I'd like to think my commented Spin listing is a little easy to work through than those offered in C -- but that could my big Hollywood ego talking!
BTW... I've also test the JBTC with an $8 HC-06 Bluetooth adapter from eBay. It works, too. It's more finicky to update so I'm working of a terminal program for that as well.
The communication protocol is fully described here
http://forum.arduino.cc/index.php?topic=173246.msg1766491#msg1766491
Should you need additional information or specific modifications for the Propeller environment, just let me know
Thanks again for the hard work
http://forum.arduino.cc/index.php?to...491#msg1766491[/quote]
I really dislike wading through the muddy waters of the Arduino pool, so I never saw that.
Still, as you can see in my post above (#7), it's not hard to spell out the protocol in a platform independent fashion.
Question: Can the response packet be shortened to a single field? I haven't had time to test. In my little demo I'm sending an RTC string and two empty fields -- would be nice to stop after the RTC string.
Perhaps my little demo will encourage you to explore the Propeller -- it's a lot of fun.
No worries; you created a nice little app and I wanted Propeller-powered robot enthusiasts to be able to take advantage of it.
What about sending
< STX , Buttons state , 0X01 , DataField#1 , 0x04, 0x05 , ETX >
Will it raise problems with your MCU ??
No doubt, thanks but I really enjoy swimming in muddy water
reminds me the good ol' Apple vs PC contest
BTW, I learnt a lot from Basic Stamp some years ago
http://forums.parallax.com/showthread.php/103850-Yet-another-balancing-robot?p=728541#post728541
I can do that without any problems -- will your app interpret the data correctly?
It's not a contest. The Propeller destroys the Arduino in absolute horsepower.
I am not here for promoting or bashing any MCU, let's drop this kind of discussion
Excellent. If you don't mind, I may mention your app in a future Nuts & Volts column, or another writing project that is in the works.
Challenges:
-- There are two packet types, and they have different sizes
-- the BS2 cannot buffer serial data; you must RX on-the-fly
-- if you're going to control servos, too, timing is going to be very tricky
Suggestion:
-- Setup the BT module to output 38.4K -- any faster will be a problem
-- if your BT module has RTS and CTS, use them (PBASIC syntax supports this; see the manual)
-- as you're receiving bytes you need to keep track of the count; if you get a frame ender, jump out and deal with the packet based on count
Thank you Kas for making and sharing the Joystick Commander App. I thought that I was going to need to change the baud rate in the joystick app, but it worked without a problem (plus, there doesn't seem to be a way to change it. magic?)
It's taken me longer to write this thank you than it did to get the demo working. Thank you!
Dave
Someone gave me an old smartphone and I got Bluetooth Commander up and running using Jon's code.
I also changed the Bluetooth baud to 9600.
I had to ground the enable (aka "key") pin on my HC-05 board.
The protocol is pretty easy to use. I'm wondering about making a fork of the Eddie firmware which would allow the one to use Bluetooth Commander as a remote. I don't think it would be very hard to make the modification to the code.
I was trying to use the code that you have provided and I am having problems with reading the coordinates on the serial monitor from the BT commander. The serial monitor is displaying junk characters.
Looks like baud may be incorrect. Are you using 115200 on both ends? You might want to try 960 on each side fore starters.