Hello there,
Im working on a proyect where I have saved the addresses of devices that are allowed to connect to the arduino on a SD file. Im using HC05 module
So, I start reading the file, take the device address on a variable and I need to know if the device is available, if not i keep reading the file to try out the next listed device. If it is available, i need to link to it and start working foward.
My problem is, with the AT+INQ command, it returns a list of devices only to the serial monitor? Cannot be storage in a local variable?
How can I scann the devices found by HC05 and see if one of them matches with the one I want to link to it?
This is an example of my code;
#include <SoftwareSerial.h>
#include <SPI.h>
#include <SD.h>
#define BT_SERIAL_RX 0 /* to communicate with the Bluetooth module's TX pin */
#define BT_SERIAL_TX 1 /* to communicate with the Bluetooth module's RX pin */
/* Initialize the software serial port */
SoftwareSerial BTserial(BT_SERIAL_RX, BT_SERIAL_TX);
char c = ' ';
void delayAndRead()
{
delay(50);
while(BTserial.available())
{
c = BTserial.read();
}
delay(800);
}
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial)
{
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
if (!SD.begin(4))
{
Serial.println("Initialization of SD failed!");
while (1);
}
Serial.println("Initialization Done.");
void initHC05ToInq()
{
string deviceAddress = ReadDeviceAddressAndSearch();
BTserial.println("AT+CMODE=1");// Enable connect to any device
delayAndRead();
BTserial.println("AT+ROLE=1");// Set to master in order to enable scanning
delayAndRead();
BTserial.println("AT+INQM=1,2,24");//RSSI, Max 2 devices, ~30s
delayAndRead();
BTserial.println("AT+CLASS=0");// Disable COD filter
delayAndRead();
BTserial.println("AT+INIT");// Init.
delayAndRead();
if (deviceAddress == "")
{
Serial.println("There is no device available to connect...");
//switch on a buzzer
}
else
{
BTserial.println("AT+LINK=" + deviceAddress);// Link to known device
delayAndRead();
}
}
string ReadDeviceAddressAndSearch()
{
File deviceFile = SD.open("devices.txt", FILE_READ);
string device="";
bool deviceAvailable = false;
while(deviceFile.available())
{
device = deviceFile.readStringUntil('\r');
deviceAvailable = IsDeviceAvailable(device);
if(deviceAvailable)
break;
}
return device;
}
bool IsDeviceAvailable(string device)
{
/*
??????????????????????????????
BTserial.println("AT+INQ");
if(device == BTserial.read())
return true;
else
return false;
*/
}
//These are not important right now
void setup(){}
void loop(){}