rn-42 bluetooth to propeller code example
brockomarks
Posts: 6
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
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
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.
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
Not sure how your device pairs with other BT devices>
Tom
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
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);
}
}
}
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
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
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
Can iPhone apps such as the "BLExplr", or "Bluetooth 4.0 UART" talk to the RN-42 bluetooth module?
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?
I was partly involved in this exercise so am a bit biased :-)
And yes, TechBasic and iPad was used but also iPhone is possible.
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.
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.