[RESOLVED] Controlling the Arduino with an Android over Bluetooth?

Hi guys,

I need your help and guidance with this please :slight_smile: Basically, I wish to control my Arduino which has a USB Host Shield (Arduino meets Android shield) and a Bluetooth dongle connected to it. I wish that instead of having a PS3 controller controlling my project (which is too easy) I instead want my Android phone to control it by connecting to the Bluetooth dongle through an app on the Android device called BlueTerm. This so far thankfully with the example registers and picks up the buttons pressed. So, an example, I press 'a' on the keyboard of my Android and the Arduino detects the button I pressed and displays it on the Serial Monitor. I did this by using example provided in the USB Host Library called "SPP". What I would like to know is how and what code do I use to program the Arduino so that if I press the key 'a' on the Android, an LED light switches on or a servo would turn. I know that the PS3BT library had the really straight forward commands, e.g. "getButtonClick" which makes sense to me. But, I can't figure out this dongle SPP crap :stuck_out_tongue:

I hope this post made sense. If you would like to know more or need some code please ask and I will reply asap.

Many thanks guys, any help at all will be really appreciated!

Well, you can check the incoming char and compare it to the character 'A' or 'a' and if they match, LED ON, if not then LED OFF.

Could you help me out with an example code or even a link to something that would help please? (although I would the example) I honestly wouldn't know what to begin typing as I'm fairly new to Arduino

This here is a basic code that uses a Serial connection to compare the incoming char to a specific char to change the state of the LED. It should be quite the same with your particular library.

char val; // variable to receive data from the serial port
int ledpin = 13; // LED connected to pin 13 (on-board LED) 


void setup() { 
  pinMode(ledpin, OUTPUT);  // pin 13 (on-board LED) as OUTPUT
  Serial.begin(9600);       // start serial communication at 9600bps
  delay(10);               // 10 millisecond delay between starting the serial communication and sending out a message
  Serial.println("get data");    // read it and store it in 'val'
}

void loop() { 

  if( Serial.available() > 0)       // if data is available to read
  {
    val = Serial.read();
    Serial.println(val);
    if( val == 'H' || val == 'h' )               // if 'H' was received
    {
      digitalWrite(ledpin, HIGH);  // turn ON the LED
    } 
    if( val == 'L' || val == 'l' ) 
    { 
      digitalWrite(ledpin, LOW);   // otherwise turn it OFF
    }
  }
}

Great! this seems like it may actually work, I will report back soon with hopefully some good news. Thanks for your help!

EDIT: If anyone else has other ways of going about doing this then please share your thoughts!

For some reason its not working, here's the code I tried.

Note: I didn't put up the serial monitor when I tried this sketch as remember I am trying to get this to work without having to put up the serial monitor.

/*
 Example sketch for the RFCOMM/SPP Bluetooth library - developed by Kristian Lauszus
 For more information visit my blog: http://blog.tkjelectronics.dk/ or 
 send me an e-mail:  kristianl@tkjelectronics.com
 */

#include <SPP.h>
USB Usb;
BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
/* You can create the instance of the class in two ways */
SPP SerialBT(&Btd); // This will set the name to the defaults: "Arduino" and the pin to "1234"
//SPP SerialBT(&Btd, "Lauszus's Arduino","0000"); // You can also set the name and pin like so
char val;
boolean firstMessage = true;

void setup() {
  Serial.begin(115200);
  pinMode(3, OUTPUT);
  if (Usb.Init() == -1) {
    Serial.print(F("\r\nOSC did not start"));
    while(1); //halt
  }
  Serial.print(F("\r\nSPP Bluetooth Library Started"));
}
void loop() {
  Usb.Task();
  if(SerialBT.connected) {
    if(firstMessage) {
      firstMessage = false;
      SerialBT.println(F("Hello from Arduino")); // Send welcome message
    }
    if(Serial.available())
      SerialBT.print(Serial.read());
    if(SerialBT.available())
      Serial.write(SerialBT.read());
  }
  else 
    firstMessage = true;
    
    if( Serial.available() > 0 )       // if data is available to read
  {
    val = Serial.read();
    Serial.println(val);
    if( val == 'H' || val == 'h' )               // if 'H' was received
    {
      digitalWrite(3, HIGH);  // turn ON the LED
    } 
    if( val == 'L' || val == 'l' ) 
    { 
      digitalWrite(3, LOW);   // otherwise turn it OFF
  }
 }
}

So, an example, I press 'a' on the keyboard of my Android and the Arduino detects the button I pressed and displays it on the Serial Monitor. I did this by using example provided in the USB Host Library called "SPP".

Ok, so you can get data from the Android with Serial.write(SerialBT.read()); and see it on the serial monitor. So in the example I gave you, change Serial.read() to SerialBT.read() and see if that helps.

Also from my example, only copy what you need. You don't need the whole thing, just the IF statements.

Hi,

it's still not working. Can anyone help?

Bump

The problem is the val variable is coming from the serial monitor, not from the bluetooth. You need to do something like the following. (Changes marked with /-->/)

/*
 Example sketch for the RFCOMM/SPP Bluetooth library - developed by Kristian Lauszus
 For more information visit my blog: http://blog.tkjelectronics.dk/ or 
 send me an e-mail:  kristianl@tkjelectronics.com
 */

#include <SPP.h>
USB Usb;
BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
/* You can create the instance of the class in two ways */
SPP SerialBT(&Btd); // This will set the name to the defaults: "Arduino" and the pin to "1234"
//SPP SerialBT(&Btd, "Lauszus's Arduino","0000"); // You can also set the name and pin like so
char val;
boolean firstMessage = true;

void setup() {
  Serial.begin(115200);
  pinMode(3, OUTPUT);
  if (Usb.Init() == -1) {
    Serial.print(F("\r\nOSC did not start"));
    while(1); //halt
  }
  Serial.print(F("\r\nSPP Bluetooth Library Started"));
}
void loop() {
  Usb.Task();
  if(SerialBT.connected) {
    if(firstMessage) {
      firstMessage = false;
      SerialBT.println(F("Hello from Arduino")); // Send welcome message
    }
    if(Serial.available())
      SerialBT.print(Serial.read());
    if(SerialBT.available())
    {
/*-->*/
      val = SerialBT.read();
      Serial.write(val);
      if( val == 'H' || val == 'h' )               // if 'H' was received
      {
        digitalWrite(3, HIGH);  // turn ON the LED
      } 
      if( val == 'L' || val == 'l' ) 
      { 
        digitalWrite(3, LOW);   // otherwise turn it OFF
      }
    }
/*-->*/
  }
  else 
    firstMessage = true;
    
    if( Serial.available() > 0 )       // if data is available to read
  {
    val = Serial.read();
    Serial.println(val);
    if( val == 'H' || val == 'h' )               // if 'H' was received
    {
      digitalWrite(3, HIGH);  // turn ON the LED
    } 
    if( val == 'L' || val == 'l' ) 
    { 
      digitalWrite(3, LOW);   // otherwise turn it OFF
    }
 }
}

Yeah sadly, it still does not work

Here's the code I tried, I would really appreciate if someone got this to work for me

#include <SPP.h>
USB Usb;
BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
/* You can create the instance of the class in two ways */
SPP SerialBT(&Btd); // This will set the name to the defaults: "Arduino" and the pin to "1234"
//SPP SerialBT(&Btd, "Lauszus's Arduino","0000"); // You can also set the name and pin like so
char val;
boolean firstMessage = true;

void setup() {
  Serial.begin(115200);
  pinMode(3, OUTPUT);
  if (Usb.Init() == -1) {
    Serial.print(F("\r\nOSC did not start"));
    while(1); //halt
  }
  Serial.print(F("\r\nSPP Bluetooth Library Started"));
}
void loop() {
  Usb.Task();
    if(SerialBT.connected) {
    /*if(firstMessage) {
      firstMessage = false;
      SerialBT.println(F("Hello from Arduino")); // Send welcome message
    }
    if(Serial.available())
      SerialBT.print(Serial.read());
  */if(SerialBT.available())
      //Serial.write(SerialBT.read());
  
    val = SerialBT.read();
     Serial.write(val);
      if( val == 'h' )               // if 'H' was received
      {
        digitalWrite(3, HIGH);  // turn ON the LED
      } 
      if( val == 'l' ) 
      { 
        digitalWrite(3, LOW);   // otherwise turn it OFF
      }
     }
    }

Try this code.

/*
 Example sketch for the RFCOMM/SPP Bluetooth library - developed by Kristian Lauszus
 For more information visit my blog: http://blog.tkjelectronics.dk/ or 
 send me an e-mail:  kristianl@tkjelectronics.com
 */

#include <SPP.h>
USB Usb;
BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
/* You can create the instance of the class in two ways */
SPP SerialBT(&Btd); // This will set the name to the defaults: "Arduino" and the pin to "1234"
//SPP SerialBT(&Btd, "Lauszus's Arduino","0000"); // You can also set the name and pin like so
char val;
boolean firstMessage = true;

void setup() {
  Serial.begin(115200);
  pinMode(3, OUTPUT);
  if (Usb.Init() == -1) {
    Serial.print(F("\r\nOSC did not start"));
    while(1); //halt
  }
  Serial.print(F("\r\nSPP Bluetooth Library Started"));
}
void loop() {
  Usb.Task();
  if(SerialBT.connected) {
    if(firstMessage) {
      firstMessage = false;
      SerialBT.println(F("Hello from Arduino")); // Send welcome message
    }
    if(Serial.available())
      SerialBT.print(Serial.read());

    if(SerialBT.available())
    {
      val = SerialBT.read();      
      Serial.println(val);
      if( val == 'H' || val == 'h' )               // if 'H' was received
      {
        digitalWrite(3, HIGH);  // turn ON the LED
      } 
      if( val == 'L' || val == 'l' ) 
      { 
        digitalWrite(3, LOW);   // otherwise turn it OFF
      }
    }
  }
  else 
    firstMessage = true;
}

You can use this tool to check on what Arduino send.
http://blog.iteadstudio.com/make-arduino-talk-with-android-by-bluetooth/

The code I posted below, basically the code tobiusnc provided now works perfectly what happened before was that the battery's wire to the device I wanted to switch on and off separated. I have connected the wire and it is working now. Thanks to everyone who tried to help me, I really appreciate it. Special thanks to tobiusnc (for providing code that worked) and HazardsMind (for not giving up on me) Oh and Hazard maybe your code was working but I haven't tried it again.

#include <SPP.h>
USB Usb;
BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
/* You can create the instance of the class in two ways */
SPP SerialBT(&Btd); // This will set the name to the defaults: "Arduino" and the pin to "1234"
//SPP SerialBT(&Btd, "Lauszus's Arduino","0000"); // You can also set the name and pin like so
char val;
boolean firstMessage = true;

void setup() {
  Serial.begin(115200);
  pinMode(8, OUTPUT);
  if (Usb.Init() == -1) {
    Serial.print(F("\r\nOSC did not start"));
    while(1); //halt
  }
  Serial.print(F("\r\nSPP Bluetooth Library Started"));
}
void loop() {
  Usb.Task();
    if(SerialBT.connected) {
    /*if(firstMessage) {
      firstMessage = false;
      SerialBT.println(F("Hello from Arduino")); // Send welcome message
    }
    if(Serial.available())
      SerialBT.print(Serial.read());
  */if(SerialBT.available())
      //Serial.write(SerialBT.read());
  
    val = SerialBT.read();
     Serial.write(val);
      if( val == 'h' )               // if 'H' was received
      {
        digitalWrite(8, HIGH);  // turn ON the LED
      } 
      if( val == 'l' ) 
      { 
        digitalWrite(8, LOW);   // otherwise turn it OFF
      }
     }
    }

Hello

Perhaps isn't the right place to post this... but... I don't know where to put it...

Few months ago, I've bought the following bluetooth module - 'JY-MCU'
http://cgi.ebay.co.uk/ws/eBayISAPI.dll?ViewItem&item=230735842973&ssPageName=ADME:L:OU:ES:3160

I usually use it to stablish communication between Arduino and my Laptop, without any problem.

It happens that now I'm trying to develop an application to communicate between my Android phone (Samsung Galaxy Mini GT-S5570 with Android 2.3.6) and Arduino.
The Android phone don't detect the bluetooth module...

I've tried to pair an 'AEG Glamour'-'bluetooth module', detects the module, ask the code, but it seems that also doesn't pair, because the red light don't stop blinking...

With my Android phone I can pair with my laptop, iPod and even with my Bose Mini SoundLink, but not with Bluetooth Module Slave.

So perhaps I'm missing some configuration on my Bluetooth Module Slave or some protocol... I really don't know...

Please let me know
Thanks on advance
Best regards
Pedro Ferrer