Tuesday, April 16, 2013

ABA: Arduino + Bluetooth + Android

The Bluetooth module for my Arduino (the JY-MCU) arrived a few weeks ago.  It was really inexpensive ($8.20), but took a long time to ship (3 weeks or so).  I picked it up from Deal Extreme (http://dx.com/p/jy-mcu-arduino-bluetooth-wireless-serial-port-module-104299).

Connecting

I followed some of the instructions from http://www.hobbycomponents.com/index.php?route=product/product&product_id=116 that showed I had to connect VCC to the 5V pin, Ground to Ground (duh), the Bluetooth module's TXD to Arduino pin D10, and the Bluetooth module's RXD to Arduino pin D11.  In the end it looked like this:

Be sure you hook it up correctly! One of the disadvantages of this Bluetooth module is that it apparently doesn't have any power protection.  So you could easily short it out and release the magic smoke.

Once it was powered on I was able to scan for it using my Android phone and pair with the Bluetooth module (It shows up as 'linvor' and the pairing code is 1234).

Programming the Arduino
The next order of business is programming the thing.  I'm not looking to do exactly what this instructable did, but it's a good place to start: http://www.instructables.com/id/Androino-Talk-with-an-Arduino-from-your-Android-d/

I used this code for inspiration (I'm not using the same pins as the instructable [it interferes with the normal serial port on the arduino] so I needed to use the software serial port instead): http://pastebin.com/raw.php?i=xmyr840j and wound up with this:

#include <SoftwareSerial.h>
#define BT_SERIAL_TX_DIO 10
#define BT_SERIAL_RX_DIO 11

SoftwareSerial BluetoothSerial(BT_SERIAL_TX_DIO, BT_SERIAL_RX_DIO);
const int SENS_PIN = A0;
const int LED_PIN = 13;

int sensorValue = 0;
boolean toggle = true;
void setup() {
    Serial.begin(9600);
    BluetoothSerial.begin(9600);
}

void loop() {
    if (BluetoothSerial.available()) {
        BluetoothSerial.read();
        BluetoothSerial.println("hello world");
        digitalWrite(LED_PIN, toggle); // toggle the LED
        toggle = !toggle;
    }
}


Programming Android
Following the instructable I installed:

http://code.google.com/p/android-scripting/
http://code.google.com/p/python-for-android/

For python for android I installed 'Python3ForAndroid_r6.apk'.  Once it's installed  you actually have to start the application (called 'Python3 for Android') and click 'Install' to let it install whatever it needs before you can go any further.

Now I don't have the sensor they're using, so I'm just using this as a quick example to get an LED blinking.

I also followed the instructions and  put the code from http://pastebin.com/raw.php?i=FVxKzf1g in to a file named androino.html and the code from http://pastebin.com/raw.php?i=XEmEsATU in to a file named androino.py

I started SL4A on my Android (to make sure it could create its directories before I uploaded anything).  Then I plugged the phone in to my computer and copied the androino files above to /sdcard/sl4a/scripts/.

Blink Lights

Open the SL4A app and click the 'get sensor data' button.  The LED should toggle (and 'hello world' should show up on the webpage on your Android)

It ain't pretty and definitely needs some work, but it was pretty easy (took maybe 30 minutes total).  Now I can blink an LED on the Arduino wirelessly using Bluetooth.  That's not much, but triggering other hardware should be pretty similar.  That's for next time.