Android Question ARduino-HC06 bluetooth/Android

daniedb

Active Member
Licensed User
Longtime User
Hi Guys

I'm busy connecting a Arduino Uno with HC-06 to the BlueTooth Example from Erel.
I'm sure someone had this issue, but could find anything. My apologies if it has been answered somewhere else.

The Arduino Serial Monitor receive the Character send from the Bluetooth exampleApp FINE., but when trying to send back from the Arduino Terminal to the Android App, nothing receives, and the "Connection is Broken" Message appear and disconnect Bluetooth
Is there a Specific format I must send from the Arduino To the Android APP?

Here is my Arduino Code if it might help

B4X:
void setup()
{
  Serial.begin(9600);
  Serial.println("Enter AT commands:");
  // HC-06 default serial speed is 9600
  BTserial.begin(115200);  
}
void loop()
{
  // Keep reading from HC-06 and send to Arduino Serial Monitor
  if (BTserial.available())
  {  
  Serial.write(BTserial.read());
  }
  // Keep reading from Arduino Serial Monitor and send to HC-06
  if (Serial.available())
  {
  BTserial.write(Serial.read());
  }
}

[\code]


If tested it with a Android App S2 Terminal and it works fine both ways. The objective will be to receive a Bunch of strings/numbers from the Arduino, and be graphically displayed in the Android App
I first want to get them communicate successfull before going on with the other Arduino code
Any Help will be highly appreciated.

Thanks
Danie
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

derez

Expert
Licensed User
Longtime User
I do a connection using one serial channel,. and maybe yours is too fast. see example attached.
It is for a car with range detection, one motor and one servo to drive it.
B4X:
#include <Servo.h>

Servo myServo ;
int angle ;
String message; //string that stores the incoming message

int minimumRange = 35; // Minimum range needed
long duration, distance; // Duration used to calculate distance

const int switchPinOn = 2 ;
const int switchPinOff = 4 ;
const int motorPin = 11 ;
const int greenLedPin = 6 ;
const int hIn1Pin = 10 ;
const int hIn2Pin = 12 ;
const int echoPin = 7 ;
const int trigPin = 8 ;

String msg1, msg2 ;
float mov, steer ;


int switchState = 0 ;
int prog = 0 ;
int randomTurn ;

void setup() {

  myServo.attach(9);
  Serial.begin(9600);

  pinMode(hIn1Pin, OUTPUT) ;
  pinMode(hIn2Pin, OUTPUT) ;
  digitalWrite(hIn1Pin, LOW) ;
  digitalWrite(hIn2Pin, LOW) ;

  pinMode(motorPin, OUTPUT);
  pinMode(switchPinOn, INPUT) ;
  pinMode(switchPinOff, INPUT);
  pinMode(greenLedPin, OUTPUT);
  analogWrite(motorPin, 0);
  angle = 75 ;

  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  if (digitalRead(switchPinOff) == HIGH) {
    switchState = LOW ;
  }
  if (switchState == LOW) {
    analogWrite(motorPin, 0);
    digitalWrite(hIn2Pin, LOW) ;
    digitalWrite(hIn1Pin, LOW) ;
    if (digitalRead(switchPinOn) == HIGH) {
      switchState = HIGH ;
    }
  }
  else {
    analogWrite(motorPin, 255);
    if (digitalRead(switchPinOff) == HIGH) {
      switchState = LOW ;
    }
    /*    measure() ;
        if (distance <= 25) {
          digitalWrite(hIn2Pin, LOW) ;
          digitalWrite(hIn1Pin, HIGH) ;
          angle = 75 ;
          delay(300);
          digitalWrite(hIn1Pin, LOW) ;
        } */

    while (Serial.available())
    { //while there is data available on the serial monitor
      message += char(Serial.read()); //store string from serial command
    }
    if (!Serial.available())
    {
      if (message != "")
      { //if data is available
        Serial.println( message); //show the data

        msg1 = message.substring(0, 1);
        msg2 = message.substring(1);
        mov = 5 - msg2.toInt() ;

        if (msg1.toInt() < 5) {
          steer = 20 + msg1.toInt() * 11  ;
        }
        else {
          steer = 75 + (msg1.toInt() - 5) * 19  ;
        }
        Serial.print( mov);
        Serial.print( " :  ");
        Serial.print(steer);
        Serial.println("******");
        message = ""; //clear the data
      }
    }

    myServo.write(steer);
    if (mov >= -1 && mov <= 1) {
      digitalWrite(hIn2Pin, LOW) ;
      digitalWrite(hIn1Pin, LOW) ;
    }
    else if (mov < -1) {
      digitalWrite(hIn2Pin, LOW) ;
      digitalWrite(hIn1Pin, HIGH) ;
      analogWrite(motorPin, 155 - 4 * mov); // mov is negative
    }
    else {
      digitalWrite(hIn2Pin, HIGH) ;
      digitalWrite(hIn1Pin, LOW) ;
      analogWrite(motorPin, 155 + 4 * mov);
    }
    digitalWrite(greenLedPin, HIGH);
  }

  delay(40);

}

void checkOff() {
  if (digitalRead(switchPinOff) == HIGH) {
    switchState = LOW ;
    prog = 0;
  }
}

void  measure() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

  digitalWrite(trigPin, HIGH);
  delayMicroseconds(5);

  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);

  //Calculate the distance (in cm) based on the speed of sound.
  distance = duration / 58.2;
}
 

Attachments

  • btarduino.zip
    110.2 KB · Views: 411
Upvote 0

daniedb

Active Member
Licensed User
Longtime User
Thanks for Derez for the help.
Could get the Astream_newdata to work in as service as want, I did a workaround to gather the serial information, and then parse the string the way I want.
Aslong as arduino string to send start with "$" and end with"@" (Which is easy todo in the Arduino Sketch), the following dirty workaround did the job

B4X:
Sub AStream_NewData (Buffer() As Byte)
   Dim findfirst As Int   :findfirst=0
   Dim findlast As Int   :findlast=0
   rbuffer =  BytesToString(Buffer, 0, Buffer.Length, "UTF8")
   longstring=longstring&rbuffer
   findfirst=SearchChar(longstring,"$")
   findlast=SearchChar(longstring,"@")
   If findfirst=1 AND findlast=1 Then
    'parsestring
    Log(" Found first and last characters")
    Log(ParsetempString(longstring))
   End If
End Sub

Sub SearchChar(searchMe As String, findMe As Char) As Int
  If Not(searchMe.Contains(findMe)) Then Return 0
  Dim CountMe As Int = 0
  For x = 0 To searchMe.Length - 1
  If searchMe.CharAt(x) = findMe Then
  CountMe = CountMe + 1
  End If
  Next
  Return CountMe

End Sub

Sub ParseTempString(foundstring As String)   
  For x = 0 To foundstring.Length - 1
     If foundstring.CharAt(x) = "$" Then
      filteredstring=""
     Else
       If foundstring.CharAt(x) = "@" Then
         Log("Clean String = "&filteredstring)
       Else
         filteredstring=filteredstring&foundstring.CharAt(x)
       End If
  End If
  Next

   'Now Process clean string
   FromArduinoLbl.text=filteredstring
  'now parse the CLEAN string
End Sub

Anyone with a better solution, please advice.
 
Upvote 0
Top