I’m working on a new iteration of the OpenExo project. The problem is is that the microcontroller and project box is going to be located underneath the device. Running wires to a control is going to be cumbersome and also get in the way of the operation of the device so I thought that communicating wirelessly with the Arduino would be the best approach. And I can use my Android phone (or any Android phone) running an App to as the remote!

I’m using the Seeed Bluetooth Shield available here on Amazon. You can also find it sometimes at RadioShack.

The nice thing about using the shield as opposed to something like the Bluesmirf bluetooth module is that you don’t have to bother with lashing it to the Arduino and then working it into a proto board. The shield just sits nicely and securely on the Arduino.

SoftwareSerial vs. Hardware Serial

The Seeed shield has jumpers that join any of digital pins 0-7 to the RX and TX serial lines that the Bluetooth module uses for communication to and from the Arduino.

Seeed Bluetooth Shield Serial Selection Jumpers

Here are the jumpers for selecting which pins to use for the Serial Bluetooth communication

If you choose to use pins 2-7 then you’ll be need to use the SoftwareSerial (or NewSoftSerial if you’re in the Arduino environment < version 1) Arduino library to talk to the shield. If you use pins 0 and 1 then you can just use the Serial commands (the same as you’d use w/ the Serial monitor) to communicate with the shield.

The downside of using the hardware serial pins (0 and 1) is that, since the Bluetooth shield is basically hijacking the same line of communication that the Arduino uses to communicate to and from the computer then you can no longer use the serial monitor and also you’ll need to remove the shield each time you want to upload new code to the Arduino.

The upside of using the hardware serial pins is that a lot of libraries that you may want to use with Bluetooth are setup to work over the default Serial implementation. So in using SoftwareSerial, you may need to tweak these libraries like I had to in order to use Amarino with SoftwareSerial.

One curious issue I found was that I simply could not get the shield and code to work when I was using the default jumper positions that the shield came with (pins 6 and 7). I’m not sure if it’s a problem with my Arduino in particular or a broader issue pertaining to all Unos but everything worked correctly once I changed the jumpers to pins 2 and 3.

Initializing Bluetooth Shield, Making it Pairable

In order to allow the Bluetooth shield to connect with other devices (making it pairable), it’s necessary to send initialization code every time the Bluetooth shield is powered up. Unlike setting the baud rate, the pairable configuration is reset to the default of not being pairable each time the board is reset.

This initialization code is available in the Seeed Bluetooth Library. Once you install the library in the libraries directory of your Arduino installation (/Applications/Arduino.app/Contents/Resources/Java/libraries/ for me on OSX), you’ll be able to open the “Slave” demo in the examples dropdown in your Arduino environment.

Here is the Slave sketch code:

/*
BluetoothShield Demo Code Slave.pde. This sketch could be used with
Master.pde to establish connection between two Arduino. It can also
be used for one slave bluetooth connected by the device(PC/Smart Phone)
with bluetooth function.
2011 Copyright (c) Seeed Technology Inc.  All right reserved.
 
Author: Steve Chang
 
This demo code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
 
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.
 
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
For more details about the product please check http://www.seeedstudio.com/depot/
 
*/
 
/* Upload this sketch into Seeeduino and press reset*/
 
#include <SoftwareSerial.h> //Software Serial Port
#define RxD 6
#define TxD 7
 
#define DEBUG_ENABLED  1
 
NewSoftSerial blueToothSerial(RxD,TxD);
 
void setup()
{
  Serial.begin(9600);
  pinMode(RxD, INPUT);
  pinMode(TxD, OUTPUT);
  setupBlueToothConnection();
 
} 
 
void loop()
{
  char recvChar;
  while(1){
    if(blueToothSerial.available()){//check if there's any data sent from the remote bluetooth shield
      recvChar = blueToothSerial.read();
      Serial.print(recvChar);
    }
    if(Serial.available()){//check if there's any data sent from the local serial terminal, you can add the other applications here
      recvChar  = Serial.read();
      blueToothSerial.print(recvChar);
    }
  }
} 
 
void setupBlueToothConnection()
{
  blueToothSerial.begin(38400); //Set BluetoothBee BaudRate to default baud rate 38400
  blueToothSerial.print("\r\n+STWMOD=0\r\n"); //set the bluetooth work in slave mode
  blueToothSerial.print("\r\n+STNA=SeeedBTSlave\r\n"); //set the bluetooth name as "SeeedBTSlave"
  blueToothSerial.print("\r\n+STOAUT=1\r\n"); // Permit Paired device to connect me
  blueToothSerial.print("\r\n+STAUTO=0\r\n"); // Auto-connection should be forbidden here
  delay(2000); // This delay is required.
  blueToothSerial.print("\r\n+INQ=1\r\n"); //make the slave bluetooth inquirable
  Serial.println("The slave bluetooth is inquirable!");
  delay(2000); // This delay is required.
  blueToothSerial.flush();
}

The first few lines need to be changed:

#include <SoftwareSerial.h>    //Software Serial Port
#define RxD 6
#define TxD 7

If you’re in the Arduino environment greater than version 1, then you can use this SoftwareSerial library inclusion:

#include <SoftwareSerial.h>

For versions less than 1, you need to get the NewSoftSerial library and put it in your Arduino libraries directory and include it like this:

#include <NewSoftSerial.h>

The other thing that needs changing is the Rx and Tx pin configuration. The default in the Slave sketch should work with the default of the shield but, like I mentioned, I needed to change the jumpers and the config to pins 2 and 3 like this:

#include <SoftwareSerial.h> //Software Serial Port
#define RxD 3
#define TxD 2

Then you can run the Slave code and you should see the board LEDs go to blinking between red and green instead of just the green LED blinking in two pulses. This means that the Bluetooth module is initialized and it is able to be paired with a device.

Changing the Baud Rate

Another thing you might need to do is change the Baud rate of the Bluetooth shield which basically determines how fast it communicates with the paired device. There are different recommended Baud rates for different applications. For using Amarino and Android, one of the recommended Baud rates is 57600.

So to change the Baud rate, you can insert this line into the Slave sketch:

blueToothSerial.print("\r\n+STBD=57600\r\n");

Changing the Baud rate on the shield is permanent so you’ll need to connect to it using that Baud rate once the change is made. You can of course change it again using the command above but it will keep the latest Baud rate even if you power down the board.

Connecting Android and Arduino using Bluetooth

You can follow these installation instructions to get Amarino setup both on your Android device as well as for the Arduino development:

http://www.amarino-toolkit.net/index.php/download.html

Next, you can test that you can get communication going between the Android and Arduino by using the SoftwareSerialExample sketch included with Arduino. You’ll need to edit that though in order to the initialization routine for the Bluetooth shield.

Here’s what mine looks like after adding it in:

/*
  Software serial multple serial test
 
 Receives from the hardware serial, sends to software serial.
 Receives from software serial, sends to hardware serial.
 
 The circuit:
 * RX is digital pin 10 (connect to TX of other device)
 * TX is digital pin 11 (connect to RX of other device)
 
 Note:
 Not all pins on the Mega and Mega 2560 support change interrupts,
 so only the following can be used for RX:
 10, 11, 12, 13, 50, 51, 52, 53, 62, 63, 64, 65, 66, 67, 68, 69
 
 Not all pins on the Leonardo support change interrupts,
 so only the following can be used for RX:
 8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).
 
 created back in the mists of time
 modified 25 May 2012
 by Tom Igoe
 based on Mikal Hart's example
 
 This example code is in the public domain.
 
 */
#include <SoftwareSerial.h>
 
SoftwareSerial blueToothSerial(3,2); // RX, TX
 
void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(38400);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
 
  Serial.println("Goodnight moon!");
 
  blueToothSerial.begin(57600);
  blueToothSerial.print("\r\n+STWMOD=0\r\n"); //set the bluetooth work in slave mode
  blueToothSerial.print("\r\n+STNA=SeeedBTSlave\r\n"); //set the bluetooth name as "SeeedBTSlave"
  blueToothSerial.print("\r\n+STOAUT=1\r\n"); // Permit Paired device to connect me
  blueToothSerial.print("\r\n+STAUTO=0\r\n"); // Auto-connection should be forbidden here
  delay(2000); // This delay is required.
  blueToothSerial.print("\r\n+INQ=1\r\n"); //make the slave bluetooth inquirable
  Serial.println("The slave bluetooth is inquirable!");
  delay(2000); // This delay is required.
  blueToothSerial.flush();
}
 
void loop() // run over and over
{
  if (blueToothSerial.available())
    Serial.write(blueToothSerial.read());
  if (Serial.available())
    blueToothSerial.write(Serial.read());
}

Now, you can open the Amarino app in Android, connect to the device listed (should be there as “SeeedBTSlave”) and go to the monitoring tool inside the Amarino app. Then you should be able to send commands from the Amarino app to the Arduino serial monitor like I did in the video above.

Using SoftwareSerial with the Amarino bridge and MeetAndroid Library

One other hangup when you actually start making your Android app and have it talk with Arduino is that the MeetAndroid Amarino libraries that use with Amarino are setup with the assumption that you’ll be using the hardware Serial, pins 0 and 1 for Bluetooth communication. So if you are using pins 0 and 1 with the shield, you are all set. But if you’re not, you’ll need to get a modified version of the MeetAndroid library.

Thankfully someone ran into this problem already and was kind enough to modify the libraries and put them up for download here:

http://www.double-oops.org/mini-blog/amarinowithsoftwareserial

So this is excellent. Except there is one other problem. When using the shield, you need to run that initialization code first. I tried doing this first by putting the initialization code right in the sketch after created the modified MeetAndroid connection like we’ve been doing in the sketches above. However, there was a conflict in that the MeetAndroid library takes control of the Bluetooth serial lines once it’s initialized and therefore trying to initialize the module inside the sketch doesn’t work.

So I modified the the library again, adding a new “send_plain” method which can be used to send Bluetooth configuration commands. This was necessary since the “send” command built into the library modifies the input string in order to communicate appropriately with the Amarino API on the Android side.

I put it up at Github:

MeetAndroid SoftwareSerial Library With Bluetooth Configuration Command