Problems in receiving data of android to bluetooth

I don´t know what is the problem with my code, the android app is fine, I tested the same app with other arduino code and it runs, turn on and off the lights at the pins 10 and 9.

When I active the buttons in the app and the app send the characters to bluetooth, the arduino do nothing.

#include <SoftwareSerial.h>

SoftwareSerial Bluetooth(3, 4); // RX TX

String dataIn =""; //variable para almacenar el comando enviado por el bluetooth

#define resistencia 9
#define bomba 10

// variables para contar el tiempo entre procesos, modo automatico.
unsigned long tiempo1=0;
unsigned long tiempo2=0;

int segundosResis=0;
int minutosResis=0;

int segundosBomba=0;
int minutosBomba=0;

void setup() {
// put your setup code here, to run once:
Bluetooth.begin(38400);
Bluetooth.setTimeout(1);

pinMode(resistencia, OUTPUT);
pinMode(bomba, OUTPUT);

// INICIALIZADA DE LOS ESTADOS DE RESISTENCIA Y BOMBA
digitalWrite(resistencia, LOW);
digitalWrite(bomba, LOW);

}

void loop() {
// put your main code here, to run repeatedly:

if (Bluetooth.available()>0)
{
dataIn = Bluetooth.readString(); // Read the data as string

if (dataIn=="A") 

{
digitalWrite(resistencia, HIGH); // ESTADO A, RESISTENCIA PRENDIDA
}

if (dataIn=="B") 

{
digitalWrite(bomba, HIGH); // ESTADO B, BOMBA PRENDIDA
}

if (dataIn == "C")
{
digitalWrite(bomba, LOW); // ESTADO C, BOMBA Y RESISTENCIA APAGADA
digitalWrite(resistencia, LOW);
}

if (dataIn == "D")
{
digitalWrite(resistencia, LOW); // ESTADO D, RESISTENCIA APAGADA
}

if (dataIn == "E") 

{
digitalWrite(bomba, LOW); // ESTADO E, BOMBA APAGADA
}

// Se toman los datos enviados por el bluetooth en las casillas de tiempos de bomba y de resistencia
 if(dataIn.startsWith("SR"))
 {
    String dataInS= dataIn.substring(2, dataIn.length());
    segundosResis= dataInS.toFloat();
    
  }

  if(dataIn.startsWith("MR"))
 {
    String dataInS= dataIn.substring(2, dataIn.length());
    minutosResis= dataInS.toFloat();
    
  }
 
 if(dataIn.startsWith("SB"))
 {
    String dataInS= dataIn.substring(2, dataIn.length());
    segundosBomba= dataInS.toFloat();
    
  }

  if(dataIn.startsWith("MB"))
 {
    String dataInS= dataIn.substring(2, dataIn.length());
    minutosBomba= dataInS.toFloat();
    
  }


  // se convierten los tiempos a segundos. 
  long tiempoResis= minutosResis*60+segundosResis;
  long tiempoBomba= minutosBomba*60+segundosBomba;

  long tiempoTotal=tiempoResis+tiempoBomba;

}
}

The default baud rate, in communication mode, for the HC05 is 9600, unless you changed it. 38400 is for AT mode.

dataIn = Bluetooth.readString(); // Read the data as string
if (dataIn=="A") 

In your single letter String values are you certain that you are not sending them without any CR/LF appended? The comparison will not be true if there are terminators.

oh yes, I also tested in 9600 and not worked. I just put 38400 to see what happened

It only sends A.
look. I will send you the code that works for me. The problem with this new code is that I can´t take the values that are in a slider. (SR,MR, SB,MB)

int estado = 0;
#define resistencia 9
#define bomba 10

// variables para contar el tiempo entre procesos, modo automatico.
unsigned long tiempo1= 0;
unsigned long tiempo2=0;

int segundosResis= 0;
int minutosResis=0;

int segundosBomba=0;
int minutosBomba=0;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(resistencia, OUTPUT);
pinMode(bomba, OUTPUT);

// INICIALIZADA DE LOS ESTADOS DE RESISTENCIA Y BOMBA
digitalWrite(resistencia, LOW);
digitalWrite(bomba, LOW);

}

void loop() {
// put your main code here, to run repeatedly:

if (Serial.available() > 0 )
{

// dataIn = Serial.readString(); // Read the data as string

estado = Serial.read();

 switch( estado)
 {
  case 'A':
  digitalWrite(resistencia, HIGH); // ESTADO A, RESISTENCIA PRENDIDA
  break;

  case 'B':
  digitalWrite(bomba, HIGH); // ESTADO B, BOMBA PRENDIDA
  break;

  case 'C':
   digitalWrite(bomba, LOW); // ESTADO C, BOMBA Y RESISTENCIA APAGADA
 digitalWrite(resistencia, LOW);
 break;

 case 'D':
  digitalWrite(resistencia, LOW); // ESTADO D, RESISTENCIA APAGADA
  break;

  case 'E':
  digitalWrite(bomba, LOW); // ESTADO E, BOMBA APAGADA
  break;
  
 }

Why this new code works and the other one no?

Try

dataIn.trim() // remove terminators

Also try

Serial.print("'"); Serial.print(dataIn); Serial.print("'"); // see what you are receiving

to see what you are receiving

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.