0

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

Lysander12 PT
  • 154
  • 16
  • Is the hc-05 Bluetooth paired and connected to something? – ukBaz Oct 23 '21 at 17:48
  • @ukBaz Yes, it is paired with my mobile and when I connect the blinking light stops, then it only blinks like once in 3 seconds or so. I'm assuming that means its connected and ready to receive – Lysander12 PT Oct 23 '21 at 19:00
  • Are you using an app like [Serial Bluetooth Terminal](https://play.google.com/store/apps/details?id=de.kai_morich.serial_bluetooth_terminal)? – ukBaz Oct 23 '21 at 19:17
  • @ukBaz Yeah, that's exactly the app I'm using – Lysander12 PT Oct 23 '21 at 20:38
  • Blink once in 2 sec sounds like the module has entered Command Mode (rather than data mode). More information at: https://www.instructables.com/AT-command-mode-of-HC-05-Bluetooth-module/ – ukBaz Oct 24 '21 at 06:13

0 Answers0