Transmitting String from Android to Arduino

In my program, it is strange that arduino will make responds (turn the LED on or off) only after the Serial monitor is first available. Also, I found that it works well if I transmit a character only instead of a String. Here is my code:

#include <SoftwareSerial.h>
#define RxD 6 // This is the pin that the Bluetooth (BT_TX) will transmit to the Arduino (RxD)
#define TxD 7 // This is the pin that the Bluetooth (BT_RX) will receive from the Arduino (TxD)
#define DEBUG_ENABLED 1
#define RELAY 4

SoftwareSerial blueToothSerial(RxD, TxD);  
int led = 4 ;




/*----------------------SETUP----------------------------*/
void setup() { 
Serial.begin(9600);//low Serial communication via USB cable to computer (if required)
pinMode(RxD, INPUT); // Setup the Arduino to receive INPUT from the bluetooth shield on Digital Pin 6
pinMode(TxD, OUTPUT); // Setup the Arduino to send data (OUTPUT) to the bluetooth shield on Digital Pin 7
pinMode(4, OUTPUT); // Use onboard LED if required.
setupBlueToothConnection(); //Used to initialise the Bluetooth shield
} 


/*----------------------LOOP----------------------------*/
void loop() { 
digitalWrite(4, LOW); //Turn off the onboard Arduino LED
String inputString;
while (1) {
if(blueToothSerial.available()){
//check if there's any data sent from the remote bluetooth shield
char recvChar=blueToothSerial.read();
 String recvString=String(recvChar);
  inputString+=recvString;

if (inputString=="yes") {  
    digitalWrite(led,HIGH);
    blueToothSerial.write('1');
    inputString="";
   // Serial.println(inputString);
}


if (inputString=="no") {
  digitalWrite(led,LOW);
   blueToothSerial.write('0');
   inputString="";
 //  Serial.println(inputString);
}
}
} 
}


//The following code is necessary to setup the bluetooth shield ------copy and paste----------------
void setupBlueToothConnection()
{
blueToothSerial.begin(9600);// BluetoothBee BaudRate to default baud rate 38400
blueToothSerial.print("\r\n+STWMOD=0\r\n"); //set the bluetooth work in slave mode
blueToothSerial.print("\r\n+STNA=HC-06\r\n"); //set the bluetooth name as "SeeedBTSlave"
blueToothSerial.print("\r\n+STOAUT=1\r\n"); // Permit Paired device to connect me
blueToothSerial.print("\r\n+STAUTO=0\r\n"); // Auto-connection should be forbidden here
delay(2000); // This delay is required.
blueToothSerial.print("\r\n+INQ=1\r\n"); //make the slave bluetooth inquirable 
//Serial.println("The slave bluetooth is inquirable!");
delay(2000); // This delay is required.
blueToothSerial.flush();
}

Do you have a question?

What bluetooth device are you using - post a link to its datasheet.
Why have you while(1) when loop() does that for you?

Look at how the Arduino code in this demo receives data. Receiving data from bluetooth would be the same.

...R

The problem is I cannot read a string from android in arduino, but it works fine if I only need to read a single character.
My bluetooth module is HC-06.
I have looked in your post and tried the readSeveralChars() function but it still doesn't have any response to the LED.
Moreover, it is strange that with my code, it can give correct response if I open the Serial monitor and I dont understand the reason.

char recvChar=blueToothSerial.read();
 String recvString=String(recvChar);
  inputString+=recvString;

Why are you converting the char that you read to a String? Why are you using String at all?

Why are you NOT reading all the serial data that may have arrived on any given pass through loop?

By opening the Serial Monitor, you are slowing the Arduino down, by writing to the serial port. That allows more characters to arrive.

Knock of the nonsense of sending undelimited packets. Send “” or “”, and read and store, in a NULL terminated char array, the data, starting after the < arrives, ending, and using the data, when the > arrives.

I have modified the code as follows but it still doesn’t work:

String inputString;
void loop() { 
digitalWrite(4, LOW); //Turn off the onboard Arduino LED
while (1) {
inputString="";
while(blueToothSerial.available()){
//check if there's any data sent from the remote bluetooth shield
char recvChar=blueToothSerial.read();
inputString+=recvChar;
Serial.println(inputString);
if(recvChar=='>')
break;
}

if (inputString=="<YES>") {  
    digitalWrite(led,HIGH);
    blueToothSerial.write('1');
   // Serial.println(inputString);
}
if (inputString=="<NO>") {
  digitalWrite(led,LOW);
   blueToothSerial.write('0');
 //  Serial.println(inputString);
}
} 
}

In the Serial monitor, the output is as follows:
<
<
<Y
<Y
<YE
<YE
<YES
<YES

The suggestion was to read incoming characters until you got a < then to read characters and add them to a NULL terminated char array until you got a > At that point you would have a C style string in the array which you could examine and take action on.

That is not what you have done.

yykcw:
I have looked in your post and tried the readSeveralChars() function but it still doesn’t have any response to the LED.

That doesn’t tell me anything. What does happen?

Moreover, it is strange that with my code, it can give correct response if I open the Serial monitor and I dont understand the reason.

What do you mean by this? Do you mean that it works if you send the characters from the serial monitor?

I have modified the code as follows but it still doesn’t work:

There is nothing to be gained by making changes to code that already works - unless you know that it works and have some specific test you want to try.

Have you allowed for the fact that my code expects the “data” to be preceded by < and followed by > so the Arduino knows where it starts and finishes. That means that you should send or .

…R

Robin2:

yykcw:
I have looked in your post and tried the readSeveralChars() function but it still doesn’t have any response to the LED.

That doesn’t tell me anything. What does happen?

Moreover, it is strange that with my code, it can give correct response if I open the Serial monitor and I dont understand the reason.

What do you mean by this? Do you mean that it works if you send the characters from the serial monitor?

I have modified the code as follows but it still doesn’t work:

There is nothing to be gained by making changes to code that already works - unless you know that it works and have some specific test you want to try.

Have you allowed for the fact that my code expects the “data” to be preceded by < and followed by > so the Arduino knows where it starts and finishes. That means that you should send or .

…R

I have used your code as shown below, it shows correct output in the serial monitor if I communicate with the computer, however, it still cannot turn the LED on, which I think it cannot verify inputSeveral=="" but it is strange since the output in the Serial monitor is exactly equals to .
Also, if I switch to bluetoothSerial, the charCount will doubled (eg: charCount of “” change to 10 from 5), and the monitor cannot show the string which is like this: Num Chars Rcvd — 10 —

void loop() { 
digitalWrite(led, LOW); //Turn off the onboard Arduino LED
while (1) {
readSeveralChars();
delay(800);
} 
}

void readSeveralChars() {

      // this reads all the characters in the input buffer
      // if there are too many for the inputSeveral array the extra chars will be lost

    inputSeveral[0] = 0; // makes inputSeveral an empty string with just a terminator
    
    byte charCount = 0;  // the number of characters actually received - some may be lost
    byte ndx = 0;        // the index position for storing the character
    
    if (Serial.available() > 0) {
      while (Serial.available() > 0) { // keep going until buffer is empty
        if (ndx > maxChars - 1) { // -1 because arrays count from 0
          ndx = maxChars;     // if there are too many chars the extra ones are 
        }                     //   dumped into the last array element which will
                              //   be overwritten by the string terminator
        inputSeveral[ndx] = Serial.read();
        ndx ++;        
        charCount ++;
      }
      
      if (ndx > maxChars) {  // to make sure the terminator is not written beyond the array
        ndx = maxChars;
      }
      inputSeveral[ndx] = 0; // add a zero terminator to mark the end of the string
    }
    
    Serial.print("Num Chars Rcvd --- ");
    Serial.print(charCount);
    Serial.print("  ---  ");
    Serial.println(inputSeveral);
    
    if (inputSeveral=="<YES>") {  
    digitalWrite(led,HIGH);
    Serial.println('1');
   // Serial.println(inputString);
}
    if (inputSeveral=="<NO>") {
  digitalWrite(led,LOW);
   Serial.write('0');
 //  Serial.println(inputString);
}   
}

Thanks for the help, I found the solution now :smiley: