Communications problems bluetooth Arduino/Android

riscrivo in inglese

Hi,

premise: i have read a lot of topic's and online tutorial, but i haven't find a solution to my problem, that is i can't send/receive data from android (app blueterm) to arduino UNO rev 3 equipped with BLUETOOH BEE (SKU:TEL0023) and XBee Shield V2.0 .

The hardware consist on 4 led's managed with the digital port's 4-5-6-7, based on the command send by smartphone. With a manual test with a direct command to set port to HIGH, led's work, but via bluetooth, they didn't work.

To make another test, i have open a second serial connection, to see from pc what arduino received from smartphone, but i read only incomprensible characters. On the smartphone, only one time i have read an Hello World!:cry:

The actual code:

#include <SoftwareSerial.h>
SoftwareSerial blueToothSerial(9,10);
char incoming;
void setup()
{
  pinMode(9, INPUT);
  pinMode(10, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  blueToothSerial.begin(115200); 
  blueToothSerial.println("prova");
}

void loop()
{
  incoming=blueToothSerial.read();
  switch (incoming)
  {
  case '1':
    digitalWrite(4, HIGH);
    blueToothSerial.println("pin 4 HIGH");
    break;
  case '2':
    digitalWrite(5, HIGH);
    blueToothSerial.println("pin 5 HIGH");
    break;
  case '3':
    digitalWrite(6, HIGH);
    blueToothSerial.println("pin 6 HIGH");
    break;
  case '4':
    digitalWrite(7, HIGH);
    blueToothSerial.println("pin 7 HIGH");
    break;
  }
  delay(100);
}

On another topic i have read who if arduino is usb connected to pc, the bluetooth connection didn't work correctly, but this solution didn't change anything.
Can you please help me?
Thanks!


Buonasera a tutti,

premessa: ho letto i vari topic (in particolare quello di pitusso) e guide in rete, però non sto riuscendo a trovare una soluzione al mio problema, ovvero Non riesco a inviare/ricevere dati tramite android (app BlueTerm) e arduino UNO rev3 equipaggiato con BLUETOOH BEE (SKU:TEL0023) e XBee Shield V2.0 .

A livello hardware, ci sono 4 led che vengono gestiti dalle porte digitali 4-5-6-7 funzionanti in base a cosa si invia dal cellulare. Facendo un test, ovvero inviando il comando di accensione manualmente, funzionano, però tramite comando bluetooth non vanno.

Avevo pasticciato un pò col codice per aprire una seconda connessione seriale in modo da vedere da pc cosa ricevevo dal cellulare, ma ricevevo strani simboli, mentre sul cellulare solo una volta son riuscito a far comparire un hello world! :cry:

Vi allego il codice "attuale"

#include <SoftwareSerial.h>
SoftwareSerial blueToothSerial(9,10);
char incoming;
void setup()
{
  pinMode(9, INPUT);
  pinMode(10, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  blueToothSerial.begin(115200); 
  blueToothSerial.println("prova");
}

void loop()
{
  incoming=blueToothSerial.read();
  switch (incoming)
  {
  case '1':
    digitalWrite(4, HIGH);
    blueToothSerial.println("pin 4 HIGH");
    break;
  case '2':
    digitalWrite(5, HIGH);
    blueToothSerial.println("pin 5 HIGH");
    break;
  case '3':
    digitalWrite(6, HIGH);
    blueToothSerial.println("pin 6 HIGH");
    break;
  case '4':
    digitalWrite(7, HIGH);
    blueToothSerial.println("pin 7 HIGH");
    break;
  }
  delay(100);
}

In un altro topic ho letto che stando con connessione usb non va, quindi ho alimentato arduino esternamente, ma non è cambiato nulla.

Potreste per favore darmi una mano?

Grazie

Mashenron:

  blueToothSerial.begin(115200); 

}

Are you sure the bluetooth is configured to work at 115200? I have never heard of one working at that speed as it comes out of the box, and I bet you haven't configured it yourself. Most are 9600 and your code might work if you change it to that.

You might find the following background notes useful

http://homepages.ihug.com.au/~npyner/Arduino/GUIDE_2BT.pdf
http://homepages.ihug.com.au/~npyner/Arduino/BT_2_WAY.ino

Thanks Nick_Pyner :slight_smile:

Now works, but only send data from arduino (via serial monitor) to android device, not vice versa.

byte byteRead;

void setup()
{
    Serial.begin(9600);
    Serial.println("OK then, you first, say something.....");
    Serial.println("Go on, type something in the space above and hit Send,");
    Serial.println("or just hit the Enter key, then I will repeat it!");
    Serial.println("");
}

void loop() {
   //  check if data has been sent from the computer: 
  if (Serial.available()) {
    // read the most recent byte 
    byteRead = Serial.read();
    //ECHO the value that was read, back to the serial port. 
    Serial.write(byteRead);
    
      if(byteRead==10)  // chk for CR
      {
      Serial.println("Did you say that?");
      Serial.println();
      }
  }
}

I understand you are using the notes I made.

Note the bit at the end of item 15.

I don't think there is anything wrong with your code, hardware, or procedure.

This is an exercise to prove two way traffic between Arduino and Android, nothing more. I suspect you have succeeded, and it is probably all you really need.

What you are trying to do now is get two way traffic between PC and Android, using Arduino as an interface - not the same thing.

If you want communications between PC and Android, you don't need Arduino, and it can be better employed elsewhere. Having said that, you can do it, but not with the programme given, because the serial monitor shares the port with Bluetooth.

The solution is to get Bluetooth off the port.

If you have a Mega, you can do this by the simple expedient of connecting Bluetooth to serial1 instead, and declaring it in the code, rather like that in item 11.

If you have a Uno or similar with only one hardware serial port, you can still do it, but are obliged to use SoftwareSerial. Now that you have the confidence and know what you are doing, you might like to have a go at that, but I wouldn't bother.

I'm glad you brought this up, and I will reinforce that point in the notes!

I maded this code to send an on/off command to two led: via serial monitor (pin 0-1) work's fine, but when i use the software serial pin (arduino uno, pin 2-3) i can't connect android to arduino (unable to connect to device).

what do you think?

#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3);
const int ledsx = 10;     // LED connected to digital pin 10
const int leddx = 11;    // LED connected to digital pin 11
byte val;
int ledsxd = LOW;    
int leddxd = LOW;

void setup(){
    Serial.begin(19200); //serial monitor via usb-pc
    Serial.println("OK");
    pinMode(ledsx, OUTPUT);
    pinMode(leddx, OUTPUT);
    digitalWrite(ledsx, ledsxd);
    digitalWrite(leddx, leddxd);
    mySerial.begin(9600);// set the data rate for the SoftwareSerial port
    mySerial.println("ready");//send message confirming that it is ready
}
void loop() {
   //  check if data has been sent from the computer: 
  if (mySerial.available()) {
    Serial.println("myserial avaiable");
    // read the most recent byte 
    val = mySerial.read();
    //ECHO the value that was read, back to the serial port. 
    mySerial.write(val);
    Serial.println(val);
    if(val=='s'){
      if(ledsxd==LOW){
        ledsxd = HIGH; 
      }else{
        ledsxd = LOW;
      }
      digitalWrite(ledsx, ledsxd);
    }
  if(val=='d'){
      if(leddxd==LOW){
        leddxd = HIGH; 
      }else{
        leddxd = LOW;
      }
      digitalWrite(leddx, leddxd);
    }
  }
}

Mashenron:
(unable to connect to device).

Is that Androidspeak - can't connect to bluetooth? If so, it has nothing to do with Arduino.

If LED is solid, check data wiring.

I don't know anything about flashing LEDs, or software serial, but you don't need software serial to flash LEDs. The code looks reasonable and you obviously know what you are doing as far as the LEDs are concerned.

I understand you already have two way text on hardware serial working. You might try adding the LED commands to that, so you have both. Then try moving that to software serial

Solved!

I dint know why, but now work ;D