Shop OBEX P1 Docs P2 Docs Learn Events
HC-06 BLUETOOTH SOMETHING SIMPLE — Parallax Forums

HC-06 BLUETOOTH SOMETHING SIMPLE

Good day all,
I purchased a HC-06 BT module. I created an app on "MIT inventor 2". Kinda weird getting used to blocky programming. Anyway, the BT module works great with Johnnymacs "configurator". It also works great with the Arduino UNO R3. Each time I would push a button (in the app), on my Android, it would echo on the serial monitor (using the UNO).

So, I wrote a very simple spin program to display the button pushed on the app, to the PST. However, it does not seem to work. I know that it's got to be something simple. Any help would be greatly appreciated. Thanks.
Al

Comments

  • JonnyMacJonnyMac Posts: 9,298
    edited 2025-05-12 23:20

    Since my name was invoked (albeit misspelled), let me chime in....

    Your program is waiting on a byte but then treating it like a string. If you want to use bytes, I suggest this

      repeat
        c := bt.rxtime(1000)
        if (c => 0)
          term.str(string("BT input: "))
          term.tx(c)
          term.tx(13)
    

    This protects you from the -1 that is returned when .rxtime() times out.

    One of my own BT projects that made a lot of people smile was a mini "Stranger Things" wall made for a Halloween convention. When guests approached the booth I would ask for a name, enter it into a terminal program that would send the string via BT to the P1. The P1 would then spell out the person's name. People loved it.

    I like to use strings because they can be sent from any terminal. That said, I have also used MIT AI2 to create simple apps, and in those cases an event transmits a string. This makes things easy to troubleshoot on the Propeller end -- and there's no guessing what a byte means when the string spells it out.

    Warning: the attached program may be a little advanced (I don't know your level), but I think you may benefit from it.

  • Thanks for your post JonnyMac.
    Sorry about the misspelling; wife was talking to me while I was typing my post. Anyway, tried your snippet, still no joy. I get a "Y"... with two dots on top of the "Y"... from (term.tx(c)). I get this weather I push a button or not, on my app, every second. It repeats without pressing a button... "BT input: Y(with two dots)" every second (with a CR).
    My app has 7 buttons; Top two buttons are for "connecting/disconnecting" BT. The other 5 are... Forward, Reverse, Left, Right, Stop. They output a 1, 2, 3, 4, 5 respectively.

    Anyway, I have not had a chance to look at your "Stranger Things" code. I will try and find some time tomorrow. By the way, I have seen pic's of this... it looks fantastic. I can see why it was a Knock-Out. Have a good one.
    Al

  • JonnyMacJonnyMac Posts: 9,298

    They output a 1, 2, 3, 4, 5 respectively.

    Are you sending them as ASCII characters "1", "2", "3", etc., or plain binary values? I've attached a simple program that should help you; it receives the byte from the Bluetooth and prints the ASCII code (as 2-character hex). If you see "[01]" on the screen of this program when you press the 1 button, that will explain the issue. Consider changing the values your send to letters. "F" (forward), "R" (reverse), "L" (left), "R" (right), and "S" (stop). In a future P1 app you can make code that is very easy to understand and follow, for example

      case b
        "F" : move_forward
        "R" : move_reverse
        "L" : turn_left
        "R" : turn_right
        "S" : robot_stop
    

    It's just as easy to use these letters versus the numbers you have now, but it does make the code easy to read and maintain.

    I looked at your original code that uses .rxtime(). The problem is your variable is a byte which means a timeout (-1) will be truncated to $FF; when you print this it will give you the Y with two dots. You're seeing this every second because your .rxtime() timeout value is 1000ms. What you're seeing matches your code.

    Again, for simple command programs like this uses ASCII characters or strings. If you have to send a value, then you can use regular bytes -- but I don't. I send EVERYTHING as strings. The attached image is a program I created with MIT AI2 to control an add-on I made for the DEF CON 22 badge (that I co-designed and coded). Even values from the color strip are sent as a string; the Propeller converts the string to a value to be put onto the pixels.

  • Thanks again. After running your "jm bluetooth test" program, I got the following hex values... 31 - 35... that corresponds to 1 - 5. I will make modifications to the MIT app, to send F, R, L, R, S (in place of 1 - 5). I will have more time this weekend... seems a few pesky things keep getting in the way of my hobby time... family and work (but I would not have it any other way). Thanks again Jon.

  • JonnyMacJonnyMac Posts: 9,298
    edited 2025-05-15 02:26

    Glad I could help. Seems like your program is sending ASCII codes -- I think it was the timeout that was getting in the way. If you're waiting for something to happen you don't need to use rxtime -- just use rx and you'll get a value when something shows up.

      repeat
        c := bt.rx
        term.str(string("BT input: "))
        term.tx(c)
        term.tx(13)
    
  • Got a chance to work on this again. Everything works now... even rxtime() works. Put the BT module on the robot... that works.

    Prior to getting the BT module, I got the robot to go to a GPS spot, that I coded into the spin program (the compass helps). Now I can use the MIT app to send GPS coordinates to the robot. Attached a pic of the robot. You can see the GPS/compass (BE-880) module on top of the PVC pipe. I did some reading on the compass... it suggested to mount the compass away from any electronics.

    Although the GPS module works, I am finding that it's not real accurate. I am using the robot upstairs, in my house. That might have something to do with the accuracy... although, after a few minutes, I am getting 12 satellites.

    Thanks Jon for all your assistance. Have a good one.
    Al

Sign In or Register to comment.