I am attempting to get my Nano 33 to search for bluetooth classic devices using the HC05 module, however I am getting stuck as the Nano doesn't use SoftwareSerial, and I am unable to get the device working.
Any advice to get this functioning, if it can at all?
How would I call it in the code however, I have a SoftwareSerial define BTserial, however I'm not sure how to substitute this in the code.
My aim is to create a code that simply counts how many classic bluetooth devices are found in 10 seconds, print that number and repeat. However, all the codes I've tried haven't been able to do this.
I am a big novice so sorry if the questions are quite simple! I've posted below the code I am working with
Serial Serial1(0, 1); // RX | TX
char c = ' ';
void delayAndRead()
{
delay(50);
while(Serial1.available())
{
c = Serial1.read();
}
delay(800);
}
void initHC05ToInq()
{
Serial1.println("AT+CMODE=1");// Enable connect to any device
delayAndRead();
Serial1.println("AT+ROLE=1");// Set to master in order to enable scanning
delayAndRead();
Serial1.println("AT+INQM=1,10,24");//RSSI, Max 10 devices, ~30s
delayAndRead();
Serial1.println("AT+CLASS=0");// Disable COD filter
delayAndRead();
Serial1.println("AT+INIT");// Init.
delayAndRead();
}
void setup()
{
Serial.begin(115200);
Serial1.begin(115200);
// HC-05 default serial speed for AT mode is 38400
Serial1.begin(38400);
// Wait for hardware to initialize
delay(1000);
// Set correct states for inq
initHC05ToInq();
// Start inq
initMessage();
Serial1.println("AT+INQ");
}
char lineBuffer[100];
char subBuffer[30];
int index = 0;
int index2 = 0;
int total = 0;
bool capture = false;
String send = "";
void initMessage()
{
send = "Bluetooth1;{\"state\":{\"reported\":{\"devices\":[";
}
void loop()
{
// Keep reading from HC-05 and send to Arduino Serial Monitor
if (Serial1.available())
{
// Read character and append into buffer
c = Serial1.read();
lineBuffer[index] = c;
index++;
// When line ends
if(c=='\n')
{
// Remove line end characters from buffer
lineBuffer[index-1]=0;// \r\n
// Reset buffer index for next line
index = 0;
if(lineBuffer[0] == 'O' && lineBuffer[1] == 'K')
{
// Finish message
send += "]}}}";
// DEBUG / TODO actually send this message
if(total > 0)
{
Serial.println(send);
}
// Restart INQ
Serial1.println("AT+INQ");
total = 0;
initMessage();
}
else
{
capture = false;
index2 = 0;
for(index = 0; index < 30; index++)
{
if(!capture)
{
if(lineBuffer[index]==':')
{
capture = true;
}
}
else
{
subBuffer[index2] = lineBuffer[index];
if(lineBuffer[index] == ',')
{
subBuffer[index2] = 0;
break;
}
index2++;
}
}
index = 0;
// Add this line buffer
String str((char*)subBuffer);
if(send.indexOf(str) <= 0)
{
// If not first then add comma
if(total > 0)
{
send += ",";
}
send += "\"";
send += str;
send += "\"";
// Keep count
total++;
}
}
}
}
}
I am now getting a bunch of errors! It is essentially the example code given from the HC05 library, which I think runs off the SoftwareSerial library...
Serial Serial1(0, 1); // RX | TX
I had the feeling you'd do that.
// Serial Serial1(0, 1); // RX | TX
Then you have --
void setup()
{
Serial.begin(115200); Serial1.begin(115200); // TOO FAST
// HC-05 default serial speed for AT mode is 38400
Serial1.begin(38400); // Not TWICE
Thank you for the help! Next problem though, there isn't anything printing to the serial monitor at all, the red LED is blinking quite quickly yet not displaying anything
Maybe I've got this a bit wrong, I don't want to pair the HC05, I want it to only search for how many devices it detects and then count how many it can detect every x period
You are way off the beaten path with this project. I would suggest that you modify the program to mirror the AT commands and the responses to Serial in order to know more about what is going on.
I think the HC05 has two ways of entering AT mode, and not all commands are recognized in one of the modes. How do you put the HC05 into AT mode?
So yes, I worked out that the HC05 module I have is the v3 one, and so there are 2 AT Modes. I got it returning something the serial monitor now, and my phone can somewhat connect (Weirdly it doesn't connect by searching, but by automatically attempting to connect). I am now attempting to put it into the second AT mode as mentioned in the post below, though that isn't working as it doesn't return anything in the serial monitor,
void setup()
{
//Begin serial communication with Arduino and Arduino IDE (Serial Monitor)
Serial.begin(9600);
//Begin serial communication with Arduino and HC-05
Serial1.begin(38400);
Serial.println("Initializing...");
Serial.println("The device started, now you can pair it with bluetooth!");
}
void loop()
{
if(Serial.available())
{
Serial1.write(Serial.read());//Forward what Serial received to Software Serial Port
}
if(Serial1.available())
{
Serial.write(Serial1.read());//Forward what Software Serial received to Serial Port
}
delay(20);
}
So it returns AT nicely, and can change settings, but AT+INQ returns Error (1F), I managed to put it into the second AT mode, yet again it doesn't even register AT+INQ as anything
It has version 3, and I just can't get it to simply return how many devices it can detect
I have it altenating, the slow speed allows some AT Commands, and the fast doesn't which is fine.
I think I have purchased a V2 (may just be a V3), but we will see, if not I will just call it quits and use the BLE from the nano which is already working! WIll update you Monday