Shop OBEX P1 Docs P2 Docs Learn Events
rn-42 bluetooth to propeller code example — Parallax Forums

rn-42 bluetooth to propeller code example

brockomarksbrockomarks Posts: 6
edited 2015-04-22 13:45 in Propeller 1
I noticed in the documentation for the rn-42 bluetooth there is a code snippet to help connect to basic stamp... here...
https://drive.google.com/viewerng/viewer?url=http://www.parallax.com/sites/default/files/downloads/30086-RN-42-Bluetooth-Module-Guide-v1.0.pdf


My question is there something similar for the Propeller Activity Board? or is there a good tutorial on this set up I am trying to connect my phone to the propeller board via bluetooth and this module.

and yes I am a noob but thanks for any advice you can give ;)

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2014-12-23 21:59
    Check out the Learn tutorials, particularly the one on full-duplex serial connections since that's what the RN-42 uses. Although you can use handshaking (RTS/CTS) with Spin and the 4-port serial driver in the Propeller Object Exchange, the Propeller doesn't need it and the Full-Duplex-Serial driver in the C library and the Spin default object doesn't support handshaking.

    I haven't been able to find a sample program for the Propeller like the one for the Basic Stamp, but you should be able to translate it into C or Spin. Note that the RN-42 won't work with an iPhone, but should work with others like Android phones.
  • twm47099twm47099 Posts: 867
    edited 2014-12-24 10:43
    I've posted a couple of threads on my experience and C code for using an RN-42 with my ActivityBot and an android app called "Joystick Bluetooth Commander (JBTC) found on googleplay at:
    https://play.google.com/store/apps/details?id=org.projectproto.btjoystick&hl=en

    The first thread describes my trial and error methods of programming and the last post in the thread has the simple version of a program for using the JBTC with the ActivityBot (The earlier versions won't work because the app author changed the protocol he used for the data sent by the app.) The thread is at:

    http://forums.parallax.com/showthread.php/154687-EZ-Bluetooth-in-***C***-and-a-nice-App?p=1282432&highlight=twm47099#post1282432

    The other thread is more complicated in that I use more of the soft buttons in the app to change the mode of what the ActivityBot does, (turning a ping)) distance measurement on and off, using the Pixy color tracking device and selecting the color to be tracked and the method used to track the color). It shows more of how to use the buttons and how to send data back to the android device. That thread is at:

    http://forums.parallax.com/showthread.php/156970-Bluetooth-App-to-control-ActivityBot?p=1292974&highlight=bluetooth#post1292974

    Posts 14 and 15 of that thread have the detailed comments and actual program code.

    The code in both threads is extensively comments and explains the hardware setup. I used the Sparkfun RN-42 device that fits in the ActivityBot XBee socket, but since it only uses Tx and Rx any RN42 should work.

    hope this helps

    Tom
  • brockomarksbrockomarks Posts: 6
    edited 2014-12-30 19:59
    thanks for the advice all... I am still unable to connect to my android so far... I think the issue is I don't know what the MAC address is for the RN-42... do either of you know how to find the MAC address? thanks
  • TumblerTumbler Posts: 323
    edited 2014-12-30 23:09
    No sticker with mac address on the bt module?
  • twm47099twm47099 Posts: 867
    edited 2014-12-31 09:55
    With my Samsung Tablets (Tab 2 and Tab 4) all I did was turn on Bluetooth on one tablet and with the RN-42 turned on have the tablet scan for bluetooth devices. Then I chose the one I wanted (in my house it found a BT keyboard, and another BT device. I selected the non keyboard one which was the RN-42.

    Not sure how your device pairs with other BT devices>

    Tom
  • brockomarksbrockomarks Posts: 6
    edited 2015-01-01 14:15
    well i do a scan for the bt module and it finds it so i connect and put in the pairing key 1234 and it connects to my phone (but light on module still flashes green and i thought it supposed to turn on a blue light) anyway it show up paired on phone.... but then i wrote an app the is supposed to make a connection and has the MAC address from the label, then i run my app and it prompts for the pairing key again, i give 1234 again and it throws an exception with "Error connecting with xxxx" is there anything obvious i am doing wrong
  • twm47099twm47099 Posts: 867
    edited 2015-01-01 15:23
    Make sure that the app is using the same baud as the rn-42. I believe the RN-42 defaults to 115,200 baud (at least mine did).

    You could also try a commercial app, either "joystick BT Commander" or "Sena BTerm Bluetooth Terminal" (at: https://play.google.com/store/apps/details?id=com.sena.bterm ) to see if it connects. If so that would point to your app.

    I have also found that sometimes it can take a few tries to get the device to connect, so try a few times.

    Tom
  • brockomarksbrockomarks Posts: 6
    edited 2015-01-01 22:20
    well the software i am running on the propeller board is the one from the http://learn.parallax.com/project/rn-42-bluetooth-pc-demo/assemble-bluetooth-unit Test_DS1620.spin and when i have a usb cable connected and set the baud rate on the terminal on the computer (which does not have bluetooth) to 9600 I get the default temperature printing to the console... cool that works

    however the message I get while trying to connect via bluetooth to phone is fatal error in the onpause and failed to flush the output stream "unable to pair with RN42-xxx incorrect pin or password"... below is what my main java class looks like and yes i can try this bterm app you suggested but no where in the code for the app do I see a place to put the baud rate

    package com.example.ledonoff;

    import java.io.IOException;
    import java.io.OutputStream;
    import java.util.UUID;

    import com.example.ledonoff.R;

    import android.app.Activity;
    import android.bluetooth.BluetoothAdapter;
    import android.bluetooth.BluetoothDevice;
    import android.bluetooth.BluetoothSocket;
    import android.content.Intent;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.Toast;

    public class LEDOnOff extends Activity {
    private static final String TAG = "LEDOnOff";

    Button btnOn, btnOff;

    private static final int REQUEST_ENABLE_BT = 1;
    private BluetoothAdapter btAdapter = null;
    private BluetoothSocket btSocket = null;
    private OutputStream outStream = null;

    // Well known SPP UUID
    private static final UUID MY_UUID =
    UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

    // Insert your bluetooth devices MAC address

    private static String address = "00:66:66:66:7B:81";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Log.d(TAG, "In onCreate()");

    setContentView(R.layout.main);

    btnOn = (Button) findViewById(R.id.btnOn);
    btnOff = (Button) findViewById(R.id.btnOff);

    btAdapter = BluetoothAdapter.getDefaultAdapter();
    checkBTState();

    btnOn.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
    // sendData("1");
    Toast msg = Toast.makeText(getBaseContext(),
    "You have clicked On", Toast.LENGTH_SHORT);
    msg.show();
    }
    });

    btnOff.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
    // sendData("0");
    Toast msg = Toast.makeText(getBaseContext(),
    "You have clicked Off", Toast.LENGTH_SHORT);
    msg.show();
    }
    });
    }

    @Override
    public void onResume() {
    super.onResume();

    Log.d(TAG, "...In onResume - Attempting client connect...");

    // Set up a pointer to the remote node using it's address.
    BluetoothDevice device = btAdapter.getRemoteDevice(address);

    // Two things are needed to make a connection:
    // A MAC address, which we got above.
    // A Service ID or UUID. In this case we are using the
    // UUID for SPP.
    try {
    btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
    } catch (IOException e) {
    errorExit("Fatal Error", "In onResume() and socket create failed: " + e.getMessage() + ".");
    }

    // Discovery is resource intensive. Make sure it isn't going on
    // when you attempt to connect and pass your message.
    btAdapter.cancelDiscovery();

    // Establish the connection. This will block until it connects.
    Log.d(TAG, "...Connecting to Remote...");
    try {
    btSocket.connect();
    Log.d(TAG, "...Connection established and data link opened...");
    } catch (IOException e) {
    try {
    btSocket.close();
    } catch (IOException e2) {
    errorExit("Fatal Error", "In onResume() and unable to close socket during connection failure" + e2.getMessage() + ".");
    }
    }

    // Create a data stream so we can talk to server.
    Log.d(TAG, "...Creating Socket...");

    try {
    outStream = btSocket.getOutputStream();
    } catch (IOException e) {
    errorExit("Fatal Error", "In onResume() and output stream creation failed:" + e.getMessage() + ".");
    }
    }

    @Override
    public void onPause() {
    super.onPause();

    Log.d(TAG, "...In onPause()...");

    if (outStream != null) {
    try {
    outStream.flush();
    } catch (IOException e) {
    errorExit("Fatal Error", "In onPause() and failed to flush output stream: " + e.getMessage() + ".");
    }
    }

    try {
    btSocket.close();
    } catch (IOException e2) {
    errorExit("Fatal Error", "In onPause() and failed to close socket." + e2.getMessage() + ".");
    }
    }

    private void checkBTState() {
    // Check for Bluetooth support and then check to make sure it is turned on

    // Emulator doesn't support Bluetooth and will return null
    if(btAdapter==null) {
    errorExit("Fatal Error", "Bluetooth Not supported. Aborting.");
    } else {
    if (btAdapter.isEnabled()) {
    Log.d(TAG, "...Bluetooth is enabled...");
    } else {
    //Prompt user to turn on Bluetooth
    Intent enableBtIntent = new Intent(btAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    }
    }
    }

    private void errorExit(String title, String message){
    Toast msg = Toast.makeText(getBaseContext(),
    title + " - " + message, Toast.LENGTH_SHORT);
    msg.show();
    finish();
    }

    private void sendData(String message) {
    byte[] msgBuffer = message.getBytes();

    Log.d(TAG, "...Sending data: " + message + "...");

    try {
    outStream.write(msgBuffer);
    } catch (IOException e) {
    String msg = "In onResume() and an exception occurred during write: " + e.getMessage();
    if (address.equals("00:00:00:00:00:00"))
    msg = msg + ".\n\nUpdate your server address from 00:00:00:00:00:00 to the correct address on line 37 in the java code";
    msg = msg + ".\n\nCheck that the SPP UUID: " + MY_UUID.toString() + " exists on server.\n\n";

    errorExit("Fatal Error", msg);
    }
    }
    }
  • twm47099twm47099 Posts: 867
    edited 2015-01-01 23:10
    I looked at the link you posted. Apparently, the Baud on the parallax RN-42 can be set using jumpers, so 9600 is what that tutorial calls for. I checked my BT apps and I also don't see a way to set baud, so I assume they don't care what it is. (but the propeller software does).

    I don't have any experience with java so really can't follow your code. Sorry but I have no idea why you are getting the pairing error.

    Tom
  • brockomarksbrockomarks Posts: 6
    edited 2015-01-02 11:11
    ok I downloaded the bterm software so i can see if it connects... what should I do with it? its a console program, is there a guide for it?
  • brockomarksbrockomarks Posts: 6
    edited 2015-01-02 16:01
    ok i was able to connect using this bterm app.... still do not know whats wrong with my app though but one thing i noticed was the blue term didnt ask for the pin like my app does
  • twm47099twm47099 Posts: 867
    edited 2015-01-02 17:04
    ok I downloaded the bterm software so i can see if it connects... what should I do with it? its a console program, is there a guide for it?

    There ia a guide located at:
    http://www.sena.com/download/manual_bterm/overview.html

    Glad to read that you were able to connect. I used this app when I was trying to learn how to interface to BT from my propeller QuickStart Board to my android tablet. I was using Forth at the time (pfth) and simply used it as a terminal when was writing a forth program to send and receive characters. Once I figured out how to do that, I started using the BT joystick commander app since I wanted to use my tablet to remotely control my ActivityBot.

    Tom
  • brockomarksbrockomarks Posts: 6
    edited 2015-01-02 17:30
    i got my app to connect... sweet... now i going to figure out how to send and receive some characters
  • twm47099twm47099 Posts: 867
    edited 2015-01-02 18:53
    i got my app to connect... sweet... now i going to figure out how to send and receive some characters

    Here's a post I wrote when I was learning to write a C program to get characters from the BT Terminal and use them to control the 8 Leds on a quickstart board.

    http://forums.parallax.com/showthread.php/154413-RN-42-Bluetooth-with-Prop-Quickstart-board-%28Simple-IDE-C-program%29

    The things to note are the use of fdserial functions to write to the android device and to read the characters from the android device.
    In the blut = fdserial_open( ) function you would use the baud set on your RN-42 (9600 instead of 115200).

    Since you are using an Activity Board, you could easily start by toggling the P26 and P27 Leds.

    Tom
  • MJHanaganMJHanagan Posts: 189
    edited 2015-04-20 12:52
    Mike Green wrote: »
    Check out the Learn tutorials, particularly the one on full-duplex serial connections since that's what the RN-42 uses. Although you can use handshaking (RTS/CTS) with Spin and the 4-port serial driver in the Propeller Object Exchange, the Propeller doesn't need it and the Full-Duplex-Serial driver in the C library and the Spin default object doesn't support handshaking.

    I haven't been able to find a sample program for the Propeller like the one for the Basic Stamp, but you should be able to translate it into C or Spin. Note that the RN-42 won't work with an iPhone, but should work with others like Android phones.

    Can iPhone apps such as the "BLExplr", or "Bluetooth 4.0 UART" talk to the RN-42 bluetooth module?
  • Mike GreenMike Green Posts: 23,101
    edited 2015-04-20 14:08
    No. Bluetooth Low Energy (BLE or Bluetooth 4.0) is not really compatible with other Bluetooth protocols. The RN-42 can't speak BLE and IOS can't speak SPP which is the serial protocol used by the RN-42.
  • MJHanaganMJHanagan Posts: 189
    edited 2015-04-20 16:13
    What would be the best way to communicate with the Propeller using a non-jail broken iPhone? Would an Xbee WiFi work?
  • banjobanjo Posts: 447
    edited 2015-04-21 09:09
    Xbee WiFi works together with iOs, if it's best or not depends on your requirements.
  • MJHanaganMJHanagan Posts: 189
    edited 2015-04-21 09:48
    banjo wrote: »
    Xbee WiFi works together with iOs, if it's best or not depends on your requirements.

    I have a small Xbee RF network that controls a few things in my house. I now want to control a few of these devices using my iPhone. It looks like techBASIC might be a good app for the iPhone and presumably this app can then communicate with an Xbee S6B Wifi module connected to the Propeller. I can't seem to find any newbie level documentation on how to get started in setting this up. Any suggestions?
  • banjobanjo Posts: 447
    edited 2015-04-21 10:15
    You can search the forums with e.g. 'ipad xbee' and this happens to be one of the hits: http://forums.parallax.com/showthread.php/153214-Open-Propeller-Project-1-iPad-to-ActivityBot?highlight=xbee+ipad
    I was partly involved in this exercise so am a bit biased :-)
    And yes, TechBasic and iPad was used but also iPhone is possible.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2015-04-21 13:44
    There is some SPIN code using the RN-42 BT module here for an Android controlled ActivityBot. Not sure if it will help or not.

    http://www.savagecircuits.com/content.php?187-Bluetooth-BoE-Bot-Demo
  • MJHanaganMJHanagan Posts: 189
    edited 2015-04-21 14:22
    There is some SPIN code using the RN-42 BT module here for an Android controlled ActivityBot. Not sure if it will help or not.

    http://www.savagecircuits.com/content.php?187-Bluetooth-BoE-Bot-Demo

    According to Mike Green iOS does not support this version of Bluetooth, only Bluetooth 4.0 (aka BLE).

    I have been doing a bit of looking into using "techBASIC" on the iPhone and linking it to the Prop using an Xbee WiFi module. Not much documentation available on how to go about getting this setup. It seems like on the techBASIC side it is pretty easy to send out some characters over the WiFi by opening a file link using CommTCPIP() and the classic PRINT #1 <string characters here>. Presumably one can also read characters sent from the Xbee WiFi module using the equally classic INPUT #1. Seems simple enough.

    What is unclear to me at this point is how the Xbee WiFi module connects/interfaces with the Propeller. Is it as simple as establishing a simple serial link using Rx and Tx pins (e.g. Parallax Serial Terminal object and using its strIn() and str() functions)? If so then it should be fairly easy to get some form of rudimentary communication between the iPhone and the Propeller. I don't see any SPIN code for the Xbee S6B WiFi module in the OBEX. The only documentation links in the Parallax store is to the offsite at Digi for the module manual which isn't very helpful to the beginner.

    I see lots of forum discussion in the thread banjo cited but it is difficult to assemble a clear picture of how do this from what appears to be a WIP among those well versed in this type of stuff. I am further hampered by not programming in C, just SPIN and some PASM.

    I'm still searching for some very simple "how to" documentation before buying the Xbee WiFi module sitting in my Parallax online shopping cart.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2015-04-22 08:03
    MJHanagan wrote: »
    According to Mike Green iOS does not support this version of Bluetooth, only Bluetooth 4.0 (aka BLE).

    Mike is right. iPhones don't support a Bluetooth connection to the RN-42 (or most other such devices). It is for this reason I recently shifted gears and started a tutorial showing how to use the XBee Wi-Fi to control a robot using an Android app. While working with a teacher on this in the thread, he found an iPhone app that does the same basic thing as the Android apps I have been using and he now uses the app to control his school's robots over Wi-Fi rather than the method he had been using.

    http://www.savagecircuits.com/showthread.php?659-XBee-Wi-Fi-Robot-Control-via-Android-App
  • MJHanaganMJHanagan Posts: 189
    edited 2015-04-22 13:45
    Mike is right. iPhones don't support a Bluetooth connection to the RN-42 (or most other such devices). It is for this reason I recently shifted gears and started a tutorial showing how to use the XBee Wi-Fi to control a robot using an Android app. While working with a teacher on this in the thread, he found an iPhone app that does the same basic thing as the Android apps I have been using and he now uses the app to control his school's robots over Wi-Fi rather than the method he had been using.

    http://www.savagecircuits.com/showthread.php?659-XBee-Wi-Fi-Robot-Control-via-Android-App

    Perfect! That is just what I needed to know before pulling the trigger on the S6B module. Your YouTube videos on the Xbee module filled in the gaps in my understanding nicely.

    It's tutorials like these that really help the newbies get started with new devices, thank you! There should be links to these videos on the Parallax store website.
Sign In or Register to comment.