0

Im trying too simply communicate between my android phone and my arduino using the HC-05 bluetooth module.

While my module works and i am able too send and receive data on my phone, the data of my btData variable seems too instantly get lost after being received. On my android app i get the input data output like written in my code and then instantly an empty output/line.

Writing "1"/"off" into my console does not trigger my if(btData == "1")... part of the code.

I have attached my code, aswell as the android terminal and my arduino HC-05 connection.

android terminal

arduino with wiring

Hopefully someone is able too help since i cant find any error.

#include <SoftwareSerial.h>
#define rxPin 10
#define txPin 11

SoftwareSerial btSerial(rxPin, txPin);
String btData;

void setup() {
  btSerial.begin(9600);
  btSerial.println("bluetooth available");
}

void loop() {
  if (btSerial.available()) {
    btData = btSerial.readString();
    btSerial.println(btData);
    if (btData == "1") {
      btSerial.println("LED on Pin 13 is on");
    }
    if (btData == "off") {
      btSerial.println("LED on Pin 13 is off");
    }
  }
  delay(100);
}
Piglet
  • 27,501
  • 3
  • 20
  • 43
  • why is this tagged C? this is C++ tag removed. I think a good starting point is to know the language you're dealing with – Piglet Feb 13 '21 at 15:56

1 Answers1

0

Could it be that the data that is being sent has a new line character at the end of the string?

This is why you are seeing the empty line after the data that you have sent.

A possible solution might be:

void loop() {
  if (btSerial.available()) {
    btData = btSerial.readString();
    btData.trim()
    btSerial.println(btData);
    if (btData == "1") {
      btSerial.println("LED on Pin 13 is on");
    }
    if (btData == "off") {
      btSerial.println("LED on Pin 13 is off");
    }
  }
  delay(100);
}
ukBaz
  • 6,985
  • 2
  • 8
  • 31