I've been working on a project and it needs to have bluetooth. I have chosen the HC-05 and I've conected it to the arduino to the digital ports 2 and 3 and used SoftwareSerial
to handle it.
The thing is, Bluetooth.available()
is always false, even when I try to send any kind of data to it.
This is my code:
...
#include <SoftwareSerial.h>
...
// Bluetooth Handler
SoftwareSerial Bluetooth(2, 3);
...
void setup() {
// Internal
Serial.begin(9600);
...
// Bluetooth
Bluetooth.begin(9600);
...
}
void loop() {
...
// Bluetooth
listenBluetooth();
...
delay(10);
...
delay(60000);
...
listenBluetooth();
...
listenBluetooth();
delay(5000);
}
void listenBluetooth() {
if(Bluetooth.available()) {
Serial.println("Listening for bluetooth");
String receivedText = Bluetooth.readString();
String parsedText = parseReceivedText(receivedText);
Serial.print("Bluetooth text: ");
Serial.print(parsedText);
Serial.println();
if(receivedText == "current_timestamp") {
Bluetooth.write(millis());
} else if (receivedText == "get_history") {
Bluetooth.write("History");
} else {
Bluetooth.write("Unknown Command");
}
}
}
String parseReceivedText(String text) {
text.replace("\n", "");
text.replace(" ", "");
text.replace("\r", "");
return text;
}
The ...
added is parts of code that don't interact with the Bluetooth Serial whatsoever.
Maybe delays and bluetooth don't work well together?
Help is appreciated, thanks