Pages: [1]   Go Down
Author Topic: Connect Arduino with Bluetooth JY-MCU and Andoid  (Read 2616 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi I have bought from ebay this module:
http://www.ebay.com/itm/261128344954;jsessionid=6D5073A7E7772BFE64045CEB536A0D23?ru=http%3A%2F%2Fwww.ebay.com%2Fsch%2Fi.html%3F_sacat%3D0%26_from%3DR40%26_nkw%3D261128344954%26_rdc%3D1

I would like to connect it with arduino and to give it commands from android cellphone. I found some sites
with instructions but nothing worked.
Can anyone help me about the code and the application on android that I should use to send commands ?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello.

I am doing some thing like this, I have been able to send comms to the blue tooth and monitor via the serial monitor. I use the Blue term in google apps (free).

I am using soft serial for another serial port to Rx / Tx  for the blue tooth unit.. as I am using the original hw version to monitor the comms on the pc.

you can use the  soft serial example included in the library.

WARNING..... the unit you wish to purchase only runs of 3.3v....check the max voltage for your tx/rx pins..as most that run off 3.3 will only take 3.3v serial signals, any higher will damage them..( uno will put out 5V).

Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello.

I am doing some thing like this, I have been able to send comms to the blue tooth and monitor via the serial monitor. I use the Blue term in google apps (free).

I am using soft serial for another serial port to Rx / Tx  for the blue tooth unit.. as I am using the original hw version to monitor the comms on the pc.

you can use the  soft serial example included in the library.

WARNING..... the unit you wish to purchase only runs of 3.3v....check the max voltage for your tx/rx pins..as most that run off 3.3 will only take 3.3v serial signals, any higher will damage them..( uno will put out 5V).



First of all thank you for your immediate response.

Can send the link for blue term application ?
Can you tell me what to do as far as 3.3 V ? Should I use any resistor or it will create any issue with data ?
Logged

Dee Why NSW
Offline Offline
Full Member
***
Karma: 5
Posts: 206
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It seems that success depends very much on the Android device. I have tried a raft of apps on my ICS tablet, Blue Term included, and never had one that works. It can see the JY-MCU and I have had it working as a bluetooth mouse.

My JY-MCU is version 1.05. The VCC is specifically labelled 3.6 - 6v, which implies an on-board regulator. It has now been running continuously for two days without any peripherals
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello , I am using my nextus 7 tablet and both blueterm and bluetooth spp both work. I have included my sw below that will show what you send on a lcd so you can see what you do send ....

Code:
Quote


#include <SoftwareSerial.h>

#include <LiquidCrystal.h>

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

SoftwareSerial mySerial(3, 2); // RX, TX
#define text
#define text=myserial

void setup() 
{
  
   // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);//added
 // Open serial communications and wait for port to open:
  Serial.begin(115200);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  mySerial.begin(115200);
  mySerial.println("test comms");
  delay(500);
}

void loop()
{

  if (Serial.available())
  delay(100);
    mySerial.write(Serial.read());
    delay(500);
    lcd.clear();
     if (mySerial.available())
    lcd.write(mySerial.read());
       }
  



Logged

Dee Why NSW
Offline Offline
Full Member
***
Karma: 5
Posts: 206
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Interesting, but I can't see any point in that. I will try to find out more, but there is nothing to suggest a universal result.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello , I am using my nextus 7 tablet and both blueterm and bluetooth spp both work. I have included my sw below that will show what you send on a lcd so you can see what you do send ....

Code:
Quote


#include <SoftwareSerial.h>

#include <LiquidCrystal.h>

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

SoftwareSerial mySerial(3, 2); // RX, TX
#define text
#define text=myserial

void setup() 
{
  
   // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);//added
 // Open serial communications and wait for port to open:
  Serial.begin(115200);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  mySerial.begin(115200);
  mySerial.println("test comms");
  delay(500);
}

void loop()
{

  if (Serial.available())
  delay(100);
    mySerial.write(Serial.read());
    delay(500);
    lcd.clear();
     if (mySerial.available())
    lcd.write(mySerial.read());
       }
  





Peter1929 thank you very much ! I have not yet tested your code but I think that it is the same as a code that I found and worked for me.
Here it is :

char val;
int ledpin = 8;
#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);
void setup(){
   pinMode(ledpin, OUTPUT);
  Serial.begin(9600);
  BluetoothSerial.begin(9600);
}

void loop() {
  if(BluetoothSerial.available()){ 
    val = BluetoothSerial.read();}
  if( val == 'H' ){
    digitalWrite(ledpin, HIGH);} 
else {
   digitalWrite(ledpin, LOW);}
  }

However I would like to ask you some thing else. As you can see at the code I gave it
the ability when receives a character to execute a command (at this case to power a led).
The problem I have is that when I change the text from 'H' to 'HA' , this does not work.
Why this happens and what can I do to make it work ?
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Does anyone know why when I change the text from 'H' to 'HA' , the code does not work.
Why this happens and what can I do to make it work ?

char val;
int ledpin = 8;
#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);
void setup(){
   pinMode(ledpin, OUTPUT);
  Serial.begin(9600);
  BluetoothSerial.begin(9600);
}

void loop() {
  if(BluetoothSerial.available()){ 
    val = BluetoothSerial.read();}
  if( val == 'H' ){
    digitalWrite(ledpin, HIGH);} 
else {
   digitalWrite(ledpin, LOW);}
  }

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 634
Posts: 50243
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Does anyone know why when I change the text from 'H' to 'HA' , the code does not work.
Yes. You are reading one character from the bluetooth device. Which ONE key did you press to get 'HA'?

Hmmm, that's what I thought.

How to deal with serial data only comes up about 10 times a day here. You need to do some investigation on your own.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Does anyone know why when I change the text from 'H' to 'HA' , the code does not work.
Yes. You are reading one character from the bluetooth device. Which ONE key did you press to get 'HA'?

Hmmm, that's what I thought.

How to deal with serial data only comes up about 10 times a day here. You need to do some investigation on your own.

Thank you for your response.
I know that serial data is very common question but I did not find anywhere something
 that answers at this certain question.
When you say that I am reading one character from the bluetooth device, you mean that I read one character because I am writing ==>   char val;                   
                                     and         if( val == 'H' ){   
 
or that generally we can read only one character each time from bluetooth ?


Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 634
Posts: 50243
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
When you say that I am reading one character from the bluetooth device, you mean that I read one character because I am writing ==>   char val;                   
                                     and         if( val == 'H' ){   
 
or that generally we can read only one character each time from bluetooth ?
You are reading one character because that is what:
Code:
    val = BluetoothSerial.read();
does.

You can collect the characters in an array of chars (NULL terminating the array after adding each character), and then use strcmp() to see if the array contains a string of interest.

Of course, you need to know when to stop reading, and process the string you have. Packets need to be delimited to allow you to know when to start and stop storing data.

                                                                                                                               
Code:
#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup()
{
   Serial.begin(57600);
   // Other stuff...
}

void loop()
{
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}
This code reads packets delimited by < and >, so, if you send "<HA>", inData will contains "HA", and strcmp() would be useful. (Of course, you need to change Serial to BluetoothSerial.)
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

PaulS Thank you very much! I think that this is what I wanted ! I will analyse the code you sent me,
I will test it and I will inform you for the results ! Thank you again !
Logged

Pages: [1]   Go Up
Jump to: