Nano/Uno with HC-0x module

Hi,
i try to connect a Uno(or Nano) to a HC-0x module using this sketch:

#include <SoftwareSerial.h>
SoftwareSerial BT(4,2); // RX de la Uno=pin AT4; TX de la Uno=pin 2

void setup(){
  Serial.begin(9600);
  Serial.println("Enter AT commands:");
  BT.begin(9600);
}

void loop(){
  if (BT.available())
    Serial.write(BT.read());
       
  if (Serial.available()){
    String S = GetLine();
    BT.println(S); // Si avec cela le bluetooth ne marche pas éliminer le saut de ligne, remplacer par BT.print(S);
    Serial.println("---> " + S);
  }
}

String GetLine(){
  String S = "" ;
  if (Serial.available()){
    char c = Serial.read(); ;
    while (c != '\n'){
      S = S + c ;
      delay(25) ;
      c = Serial.read();
    }
    return( S ) ;
  }
}

My Bluetooth module is this one:

I don't know if it is a HC-06 or a HC-08 (?) and the LED of the module flashes quickly, it does dot reply anything to the AT command...
Any solution ?
Thank you

In this piece of code you tested Serial for availability of only one character, but then try to read whole string from it.

You should make a Serial.available() test before each read, otherwise you will get incorrect string from Serial.

In addition, you need to think about the logic of your program. You are trying to read whole String in once, this may take time. While you are waiting a string from the serial, the data coming from the bluetooth module may be lost

1 Like

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