HC-05 receiving data from Android but not sending to Android

I was trying to build a car which can be 1) controlled manually (through remote as well as bluetooth, which are working fine), 2) put to obstacle avoiding mode,
3) put to line following mode

Schematic (very rough diagram, I have tried my best, I am new to all this):

Code (I took various codes from the net and tried to mix them according to my project):

Edit: I have updated the code and made a lot of changes in the code (but the wiring remains same).

The Code:

#include <IRremote.h>
#include <SoftwareSerial.h>
#include <Servo.h>    //Servo motor library. This is standard library
#include <NewPing.h>  //Ultrasonic sensor function library. You must install this library
#include "DHT.h"

//==============================================
//     Data  Definitions
//==============================================

#define ttp223 8
#define buzzer 12

unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 500;   // the debounce time; increase if the output flickers

#define DHTPIN A0      // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11  // DHT 11
DHT dht(DHTPIN, DHTTYPE);


//==============================================
//     Manual Control Definitions
//==============================================

SoftwareSerial BTSerial(10, 11);  // pin10->TX of BT, pin11->RX of BT
#define irPin 9
IRrecv irrecv(irPin);
decode_results rslts;

//==============================================
//     Line Follower Definitions
//==============================================

#define R_S A6  //ir sensor Right
#define L_S A7  //ir sensor Left

//==============================================
//     Obstacle Avoiding Definitions
//==============================================

#define trig_pin A2  //analog input 1
#define echo_pin A1  //analog input 2

#define maximum_distance 200

NewPing sonar(trig_pin, echo_pin, maximum_distance);  //sensor function
Servo servo_motor;                                    //our servo name

boolean goesForward = false;
int distance = 100;


//==============================================
//     General Definitions
//==============================================


// Motor A
#define enA 6
#define in1 7
#define in2 4

// Motor B
#define enB 5
#define in3 3
#define in4 2

//PWM 3, 5, 6, 9, 10, 11

String irNum;
String btNum = "o";
bool send = true;

int mode = 0;
int c = 1;
String data = "";
String dht11_value = "";
bool fetch = false;

void BTControl();
void IRControl();
void ObstacleControl();
void LineControl();

void Forward();
void Back();
void Left();
void Right();
void Stop();
void fetchSensorData();

void (*functionModePointers[4])(){
  BTControl, IRControl, ObstacleControl, LineControl
};

void (*functionMovesPointers[5])(){
  Forward, Back, Left, Right, Stop
};

void setup() {

  //==============================================
  //     Motor Setup
  //==============================================

  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);


  //==============================================
  //     Data  Setup
  //==============================================


  dht.begin();

  //==============================================
  //     Manual Control Setup
  //==============================================


  Serial.begin(9600);

  pinMode(ttp223, INPUT);
  irrecv.enableIRIn();

  BTSerial.begin(9600);
  Serial.println("Enter AT commands:");

  //==============================================
  //     Line Follower Setup
  //==============================================

  pinMode(R_S, INPUT);
  pinMode(L_S, INPUT);

  //==============================================
  //     Obstacle Avoiding Setup
  //==============================================

  servo_motor.attach(13);  //our servo pin

  servo_motor.write(120);
  delay(2000);
  distance = readPing();
  delay(100);
}


void loop() {

  int ttpData = digitalRead(ttp223);
  if (ttpData == HIGH && (millis() - lastDebounceTime) > debounceDelay) {
    lastDebounceTime = millis();
    mode = (mode + 1) % 4;
    Serial.println(mode);
    // delay(1000);
    send = true;
  }


  if (send == true) {
    data = "mode after d is entered= " + String(mode);
    Serial.println(data);
    BTSerial.println(data);

    if (dht11_value != "")
      BTSerial.println(dht11_value);
    send = false;

    // Serial.println("send is false");
  }

  functionModePointers[mode]();  //working
}

int decode(String data) {

  if (data == "T" || data == "44")
    return 0;
  else if (data == "B" || data == "1d")
    return 1;
  else if (data == "L" || data == "1c")
    return 2;
  else if (data == "R" || data == "48")
    return 3;
  else if (data == "OK" || data == "O" || data == "K" || data == "5c")
    return 4;
  else if (data == "d" || data == "D")
    return 5;
  else if (data == "s" || data == "S") {
    return 6;
    send = true;
  }
}

void BTControl() {
  while (BTSerial.available()) {
    // Serial.write(BTSerial.read()); //writes full words to serial monitor
    char incomingByte = BTSerial.read();
    if (incomingByte >= 97 && incomingByte <= 122) {
      btNum = incomingByte;
      send = true;
    }
    Serial.println(btNum);
  }
  if (decode(btNum) == 5 && fetch != true) {
    // fetchSensorData();
    fetch = true;
  } 
  else if (decode(btNum) != 5){
    functionMovesPointers[decode(btNum)]();  //not working
    fetch = false;
  } 
}

void IRControl() {
  if (irrecv.decode()) {
    Serial.println(rslts.value);
    String irNum = String(irrecv.decodedIRData.command, HEX);
    Serial.println(irrecv.decodedIRData.command, HEX);
    Serial.println(irNum);
    irrecv.resume();
  }
  // Add a small delay to avoid overwhelming the serial buffer
  delay(50);
}

void ObstacleControl() {
  int distanceRight = 0;
  int distanceLeft = 0;
  delay(50);

  if (distance <= 20) {
    moveStop();
    delay(300);
    moveBackward();
    delay(400);
    moveStop();
    delay(300);
    distanceLeft = lookLeft();
    delay(300);
    distanceRight = lookRight();
    delay(300);

    if (distance >= distanceLeft) {
      turnRight();
      moveStop();
    } else {
      turnLeft();
      moveStop();
    }
  } else {
    moveForward();
  }
  distance = readPing();
}

void LineControl() {
  if ((digitalRead(R_S) == 0) && (digitalRead(L_S) == 0)) { Forward(); }  //if Right Sensor and Left Sensor are at White color then it will call forword function
  if ((digitalRead(R_S) == 1) && (digitalRead(L_S) == 0)) { Right(); }    //if Right Sensor is Black and Left Sensor is White then it will call turn Right function
  if ((digitalRead(R_S) == 0) && (digitalRead(L_S) == 1)) { Left(); }     //if Right Sensor is White and Left Sensor is Black then it will call turn Left function
  if ((digitalRead(R_S) == 1) && (digitalRead(L_S) == 1)) { Stop(); }     //if Right Sensor and Left Sensor are at Black color then it will call Stop function
}

void fetchSensorData() {
  // delay(2000);
  float h = dht.readHumidity();
}

//==============================================
//     Manual Control Functions
//==============================================

void Forward() {
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  analogWrite(enA, 150);

  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  analogWrite(enB, 150);
}

void Back() {
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  analogWrite(enA, 150);

  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  analogWrite(enB, 150);
}

void Right() {
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  analogWrite(enA, 150);

  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  analogWrite(enB, 150);
}

void Left() {
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  analogWrite(enA, 150);

  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
  analogWrite(enB, 150);
}

void Stop() {
  analogWrite(enA, 0);
  analogWrite(enB, 0);
}

//==============================================
//     Obstacle Avoiding Functions
//==============================================

int lookRight() {
  servo_motor.write(50);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(115);
  return distance;
  delay(100);
}

int lookLeft() {
  servo_motor.write(180);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(115);
  return distance;
  delay(100);
}

int readPing() {
  delay(70);
  int cm = sonar.ping_cm();
  if (cm == 0) {
    cm = 250;
  }
  return cm;
}

void moveStop() {
  digitalWrite(in3, LOW);
  digitalWrite(in1, LOW);
  digitalWrite(in4, LOW);
  digitalWrite(in2, LOW);
}

void moveForward() {
  if (!goesForward) {
    goesForward = true;

    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    analogWrite(enA, 150);

    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
    analogWrite(enB, 150);
  }
}

void moveBackward() {
  goesForward = false;

  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  analogWrite(enA, 150);

  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  analogWrite(enB, 150);
}

void turnRight() {

  //turning right little
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  analogWrite(enA, 150);

  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  analogWrite(enB, 150);

  delay(500);

  //moving backward little
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  analogWrite(enA, 150);

  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  analogWrite(enB, 150);
}

void turnLeft() {

  //turning left little
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  analogWrite(enA, 150);

  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
  analogWrite(enB, 150);

  delay(500);

  //moving backward little
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  analogWrite(enA, 150);

  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  analogWrite(enB, 150);
}

My Tries:

(i) Even if the condition if (decode(btNum) == 5 && fetch != true) {} in BTControl () is false and the function fetchSensorData() is not executed (i.e the line fetchSensorData(); is it not commented) then also HC05 is not sending.

(ii) in BTControl (), if I comment the line fetchSensorData(); then it works perfectly fine.

(iii) I also added delay (2000) to the function fetchSensorData() , that also didnt work.

(iv) I also tried the DHT Sensors Non-Blocking Library...that also didnt work.

Please help me solve this issue. Thanks a lot..

I'll suggest

BTSerial.begin(9600);

Why not make a simple sketch, an "MRE", that only works the BT?

after changing it to BTSerial.begin(9600); , it is not working. the data which it was receiving that too stopped working, after that change. again changing to 38400 it started working.

You need to change both sides. Arduino and HC-05 need the same baud rate to be able to communicate.

SoftwareSerial @38400 is pushing the limit of what SoftwareSerial can do reliably.

I had set both the baud rates to 9600. Then also it is not working. The data which it was receiving that too stopped working, after that change. Again changing the BTSerial baud rate to 38400 and Serial baud rate to 9600 it started working.

Sorry for not being clear. You need to set btSerial to 9600 and the HC-05 itself to 9600.

You can leave Serial at whatever you need.

I changed the HC05 baud rate to 9600 using AT+UART=9600,0,0. Consequently I had to change the line to BTSerial.begin(9600); for the communication to work. But the same issue persists.

that three needs to be put...no reason why...even if I print different things instead of that, the problem comes up again.

I think I have narrowed down the problem a little.

I have updated the code and made a lot of changes (but the wiring remains same).

The Code:

#include <IRremote.h>
#include <SoftwareSerial.h>
#include <Servo.h>    //Servo motor library. This is standard library
#include <NewPing.h>  //Ultrasonic sensor function library. You must install this library
#include "DHT.h"

//==============================================
//     Data  Definitions
//==============================================

#define ttp223 8
#define buzzer 12

unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 500;   // the debounce time; increase if the output flickers

#define DHTPIN A0      // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11  // DHT 11
DHT dht(DHTPIN, DHTTYPE);


//==============================================
//     Manual Control Definitions
//==============================================

SoftwareSerial BTSerial(10, 11);  // pin10->TX of BT, pin11->RX of BT
#define irPin 9
IRrecv irrecv(irPin);
decode_results rslts;

//==============================================
//     Line Follower Definitions
//==============================================

#define R_S A6  //ir sensor Right
#define L_S A7  //ir sensor Left

//==============================================
//     Obstacle Avoiding Definitions
//==============================================

#define trig_pin A2  //analog input 1
#define echo_pin A1  //analog input 2

#define maximum_distance 200

NewPing sonar(trig_pin, echo_pin, maximum_distance);  //sensor function
Servo servo_motor;                                    //our servo name

boolean goesForward = false;
int distance = 100;


//==============================================
//     General Definitions
//==============================================


// Motor A
#define enA 6
#define in1 7
#define in2 4

// Motor B
#define enB 5
#define in3 3
#define in4 2

//PWM 3, 5, 6, 9, 10, 11

String irNum;
String btNum = "o";
bool send = true;

int mode = 0;
int c = 1;
String data = "";
String dht11_value = "";
bool fetch = false;

void BTControl();
void IRControl();
void ObstacleControl();
void LineControl();

void Forward();
void Back();
void Left();
void Right();
void Stop();
void fetchSensorData();

void (*functionModePointers[4])(){
  BTControl, IRControl, ObstacleControl, LineControl
};

void (*functionMovesPointers[5])(){
  Forward, Back, Left, Right, Stop
};

void setup() {

  //==============================================
  //     Motor Setup
  //==============================================

  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);


  //==============================================
  //     Data  Setup
  //==============================================


  dht.begin();

  //==============================================
  //     Manual Control Setup
  //==============================================


  Serial.begin(9600);

  pinMode(ttp223, INPUT);
  irrecv.enableIRIn();

  BTSerial.begin(9600);
  Serial.println("Enter AT commands:");

  //==============================================
  //     Line Follower Setup
  //==============================================

  pinMode(R_S, INPUT);
  pinMode(L_S, INPUT);

  //==============================================
  //     Obstacle Avoiding Setup
  //==============================================

  servo_motor.attach(13);  //our servo pin

  servo_motor.write(120);
  delay(2000);
  distance = readPing();
  delay(100);
}


void loop() {

  int ttpData = digitalRead(ttp223);
  if (ttpData == HIGH && (millis() - lastDebounceTime) > debounceDelay) {
    lastDebounceTime = millis();
    mode = (mode + 1) % 4;
    Serial.println(mode);
    // delay(1000);
    send = true;
  }


  if (send == true) {
    data = "mode after d is entered= " + String(mode);
    Serial.println(data);
    BTSerial.println(data);

    if (dht11_value != "")
      BTSerial.println(dht11_value);
    send = false;

    // Serial.println("send is false");
  }

  functionModePointers[mode]();  //working
}

int decode(String data) {

  if (data == "T" || data == "44")
    return 0;
  else if (data == "B" || data == "1d")
    return 1;
  else if (data == "L" || data == "1c")
    return 2;
  else if (data == "R" || data == "48")
    return 3;
  else if (data == "OK" || data == "O" || data == "K" || data == "5c")
    return 4;
  else if (data == "d" || data == "D")
    return 5;
  else if (data == "s" || data == "S") {
    return 6;
    send = true;
  }
}

void BTControl() {
  while (BTSerial.available()) {
    // Serial.write(BTSerial.read()); //writes full words to serial monitor
    char incomingByte = BTSerial.read();
    if (incomingByte >= 97 && incomingByte <= 122) {
      btNum = incomingByte;
      send = true;
    }
    Serial.println(btNum);
  }
  if (decode(btNum) == 5 && fetch != true) {
    // fetchSensorData();
    fetch = true;
  } 
  else if (decode(btNum) != 5){
    functionMovesPointers[decode(btNum)]();  //not working
    fetch = false;
  } 
}

void IRControl() {
  if (irrecv.decode()) {
    Serial.println(rslts.value);
    String irNum = String(irrecv.decodedIRData.command, HEX);
    Serial.println(irrecv.decodedIRData.command, HEX);
    Serial.println(irNum);
    irrecv.resume();
  }
  // Add a small delay to avoid overwhelming the serial buffer
  delay(50);
}

void ObstacleControl() {
  int distanceRight = 0;
  int distanceLeft = 0;
  delay(50);

  if (distance <= 20) {
    moveStop();
    delay(300);
    moveBackward();
    delay(400);
    moveStop();
    delay(300);
    distanceLeft = lookLeft();
    delay(300);
    distanceRight = lookRight();
    delay(300);

    if (distance >= distanceLeft) {
      turnRight();
      moveStop();
    } else {
      turnLeft();
      moveStop();
    }
  } else {
    moveForward();
  }
  distance = readPing();
}

void LineControl() {
  if ((digitalRead(R_S) == 0) && (digitalRead(L_S) == 0)) { Forward(); }  //if Right Sensor and Left Sensor are at White color then it will call forword function
  if ((digitalRead(R_S) == 1) && (digitalRead(L_S) == 0)) { Right(); }    //if Right Sensor is Black and Left Sensor is White then it will call turn Right function
  if ((digitalRead(R_S) == 0) && (digitalRead(L_S) == 1)) { Left(); }     //if Right Sensor is White and Left Sensor is Black then it will call turn Left function
  if ((digitalRead(R_S) == 1) && (digitalRead(L_S) == 1)) { Stop(); }     //if Right Sensor and Left Sensor are at Black color then it will call Stop function
}

void fetchSensorData() {
  // delay(2000);
  float h = dht.readHumidity();
}

//==============================================
//     Manual Control Functions
//==============================================

void Forward() {
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  analogWrite(enA, 150);

  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  analogWrite(enB, 150);
}

void Back() {
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  analogWrite(enA, 150);

  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  analogWrite(enB, 150);
}

void Right() {
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  analogWrite(enA, 150);

  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  analogWrite(enB, 150);
}

void Left() {
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  analogWrite(enA, 150);

  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
  analogWrite(enB, 150);
}

void Stop() {
  analogWrite(enA, 0);
  analogWrite(enB, 0);
}

//==============================================
//     Obstacle Avoiding Functions
//==============================================

int lookRight() {
  servo_motor.write(50);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(115);
  return distance;
  delay(100);
}

int lookLeft() {
  servo_motor.write(180);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(115);
  return distance;
  delay(100);
}

int readPing() {
  delay(70);
  int cm = sonar.ping_cm();
  if (cm == 0) {
    cm = 250;
  }
  return cm;
}

void moveStop() {
  digitalWrite(in3, LOW);
  digitalWrite(in1, LOW);
  digitalWrite(in4, LOW);
  digitalWrite(in2, LOW);
}

void moveForward() {
  if (!goesForward) {
    goesForward = true;

    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    analogWrite(enA, 150);

    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
    analogWrite(enB, 150);
  }
}

void moveBackward() {
  goesForward = false;

  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  analogWrite(enA, 150);

  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  analogWrite(enB, 150);
}

void turnRight() {

  //turning right little
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  analogWrite(enA, 150);

  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  analogWrite(enB, 150);

  delay(500);

  //moving backward little
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  analogWrite(enA, 150);

  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  analogWrite(enB, 150);
}

void turnLeft() {

  //turning left little
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  analogWrite(enA, 150);

  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
  analogWrite(enB, 150);

  delay(500);

  //moving backward little
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  analogWrite(enA, 150);

  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  analogWrite(enB, 150);
}

My Tries:

Even if the condition if (decode(btNum) == 5 && fetch != true) {} in BTControl () is false and the function fetchSensorData() is not executed (i.e the line fetchSensorData(); is it not commented) then also HC05 is not sending.

in BTControl (), if I comment the line fetchSensorData(); then it works perfectly fine.

I also added delay (2000) to the function fetchSensorData() , that also didnt work.

I also tried the DHT Sensors Non-Blocking Library...that also didnt work.

Please have a look at it...thanks a lot for the help..

I did a quick compile at the code in post #8. Check the following warnings from the compiler output

/tmp/arduino_modified_sketch_303536/sketch_aug01a.ino: In function 'void ObstacleControl()':
/tmp/arduino_modified_sketch_303536/sketch_aug01a.ino:235:7: warning: variable 'distanceRight' set but not used [-Wunused-but-set-variable]
   int distanceRight = 0;
       ^~~~~~~~~~~~~
/tmp/arduino_modified_sketch_303536/sketch_aug01a.ino: In function 'void fetchSensorData()':
/tmp/arduino_modified_sketch_303536/sketch_aug01a.ino:281:9: warning: unused variable 'h' [-Wunused-variable]
   float h = dht.readHumidity();
         ^
/tmp/arduino_modified_sketch_303536/sketch_aug01a.ino: In function 'int decode(String)':
/tmp/arduino_modified_sketch_303536/sketch_aug01a.ino:200:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^

Further you never set dht11_value as far as I can see and it therefore is always empty.

Note:
You can see the warnings if you set the warning level to all in file/preferences in the IDE.

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