Shop OBEX P1 Docs P2 Docs Learn Events
Android Control of Propeller using Joystick BT Commander App -- Spin Code — Parallax Forums

Android Control of Propeller using Joystick BT Commander App -- Spin Code

JonnyMacJonnyMac Posts: 8,912
edited 2015-05-08 21:22 in Propeller 1
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.


attachment.php?attachmentid=113010&d=1422686509


attachment.php?attachmentid=113011&d=1422686528


Updated: 31 JAN 2015

Comments

  • CRST1CRST1 Posts: 103
    edited 2015-01-31 03:58
    If you don't need the joystick for control I like using the Bluetooth Serial Controller and Bluetooth Serial Controller 16 apps for android. They allow programming buttons and also two way comm to the bluetooth module. Use buttons or type directly into terminal and recieve to terminal from bluetooth.
  • RoadsterRoadster Posts: 209
    edited 2015-01-31 05:00
    I'm not sure if you are interested in making your own bluetooth android apps, but basic for android makes it easy http://www.b4x.com/index.html, it's not free but really worth the cost if you consider time is money.
    I have a bluetooth app on my hard drive somewhere made with b4a, I will post when I find it if you are interested,
  • JonnyMacJonnyMac Posts: 8,912
    edited 2015-01-31 07:17
    I'm not sure if you are interested in making your own bluetooth android apps

    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.
  • Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
    edited 2015-01-31 08:59
    Very nice!

    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.)
  • twm47099twm47099 Posts: 867
    edited 2015-01-31 12:03
    JonnyMac wrote: »
    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.

    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
  • JonnyMacJonnyMac Posts: 8,912
    edited 2015-01-31 12:56
    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.)


    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! :)
  • JonnyMacJonnyMac Posts: 8,912
    edited 2015-01-31 23:33
    I updated the program a bit -- small things, but I think the changes are better long-term. I also added a program that echoes data between PST and the Bluetooth module. The program I attached is for RN-42 modules. I've used it with the Parallax module and the XB-socket-compatble RN-42 from Sparkfun.

    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.
  • KasKas Posts: 19
    edited 2015-02-19 09:41
    Great job JonnyMac, I really appreciate that the App is now available for Spin code users

    JonnyMac wrote: »
    As there is no generic "white paper" for JBTC, I had to wade through the Arduino code
    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
  • JonnyMacJonnyMac Posts: 8,912
    edited 2015-02-19 14:14
    [/quote]The communication protocol is fully described here
    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.

    Thanks again for the hard work

    No worries; you created a nice little app and I wanted Propeller-powered robot enthusiasts to be able to take advantage of it.
  • KasKas Posts: 19
    edited 2015-02-20 05:46
    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

    What about sending
    < STX , Buttons state , 0X01 , DataField#1 , 0x04, 0x05 , ETX >
    Will it raise problems with your MCU ??


    Perhaps my little demo will encourage you to explore the Propeller -- it's a lot of fun
    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
  • JonnyMacJonnyMac Posts: 8,912
    edited 2015-02-20 08:41
    What about sending
    < STX , Buttons state , 0X01 , DataField#1 , 0x04, 0x05 , ETX >
    Will it raise problems with your MCU ??

    I can do that without any problems -- will your app interpret the data correctly?

    reminds me the good ol' Apple vs PC contest

    It's not a contest. The Propeller destroys the Arduino in absolute horsepower.
  • KasKas Posts: 19
    edited 2015-02-20 09:21
    I can do that without any problems -- will your app interpret the data correctly?
    It will (just tested) ;)



    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
  • JonnyMacJonnyMac Posts: 8,912
    edited 2015-02-20 09:43
    It will (just tested)

    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.
  • KasKas Posts: 19
    edited 2015-02-20 09:51
    JonnyMac wrote: »
    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.
    Much appreciated :)
  • carl vianellicarl vianelli Posts: 11
    edited 2015-02-22 15:17
    Is there a chance you could help with a Pbasic template for this app.? I have been looking at the data on the debug terminal and I am trying to figure a way to use this. I do see the 200200 for the joystick and when I push a button it throughs a letter in the there ,one for on and one for off like you mentioned before,anyway, I am lost looking at the spin examples..... I will look at the(http://forum.arduino.cc/index.php?to...491#msg1766491) that kas posted. I'm not a arduino guy either,just a "Good'o BS2er"(I really do need to move up!) Anyway any Pbasic/basic help would be very appreciated from me and my BOE BOT and I 'm sure other old BOE BOTers as well. THANKS
  • JonnyMacJonnyMac Posts: 8,912
    edited 2015-02-22 15:50
    I still program the BS2 from time-to-time, but usually in the form of the EFX-TEK Prop-2 controller and usually for a museum display or haunted house attraction.

    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
  • NWCCTVNWCCTV Posts: 3,629
    edited 2015-02-22 16:48
    There's an App in Google Play for BoeBot control that uses a Basic Stamp. called BoeBot Controller. If you open the help file he has a link to the PBasic code. I did this several years ago using an EBT and the program that came with it.
  • vanmunchvanmunch Posts: 568
    edited 2015-05-08 08:18
    I just wanted to give JonnyMac a shout out for another excellent demo. I just tried his demo with a HC6 from ebay using the HC6's default baud setting of 9600 (I updated the demo code to 9600) loaded the code, connected the Joystick Commander app and it worked the first time.

    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
  • JonnyMacJonnyMac Posts: 8,912
    edited 2015-05-08 21:22
    Glad to be of service! :)
  • I'll echo vanmunch's thanks.
    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.
  • Hi there,
    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.
  • PublisonPublison Posts: 12,366
    edited 2015-12-22 16:25
    Welcome to the forums!

    Looks like baud may be incorrect. Are you using 115200 on both ends? You might want to try 960 on each side fore starters.
Sign In or Register to comment.