Hello there,
I have to pair a specific slave module to the master and read the data. The name of the HC06 im working with its on a txt file.
Once paired with the HC05 connected to the Arduino UNO, i have to read the data and save it.
I dont know if i can do all the parametrization at once, or if i have to compile different codes to do this task.
Here is the code example:
// BT Data Logger
// BlueTooth Configuration
/* Include the software serial port library */
#include <SoftwareSerial.h>
#include <SPI.h>
#include <SD.h>
#define BT_SERIAL_RX 0 /* to communicate with the Bluetooth module's RXD pin */
#define BT_SERIAL_TX 1 /* to communicate with the Bluetooth module's TXD pin */
#define GREENLED1 3
/* Initialize the software serial port */
SoftwareSerial BTserial(BT_SERIAL_RX, BT_SERIAL_TX);
char c = ' ';
bool succesful = false;
void delayAndRead()
{
delay(50);
while(BTserial.available())
{
c = BTserial.read();
}
delay(800);
}
void initHC05ToInq()
{
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();
}
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;
}
deviceFile.close();
return device;
}
bool IsDeviceAvailable(string device)
{
static char input[64];
static uint8_t i;
char data = '';
bool result = false;
while (BTSerial.available > 0)
{
BTserial.println("AT+INQ");
delay(50);
data = BTSerial.read();
if ( data != '\r' && data != '\n' && i < sizeof( input ) - 1 )
input[i++] = data;
else
{
input[i] = '\0';
i = 0;
if ( strcmp( input, device ) )
{
Serial.println("Device Found");
result = true;
break;
}
else
result = false;
}
}
if (!result)
Serial.println("Device not found");
return result;
}
void setup()
{
// 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.");
// HC-05 default serial speed for AT mode is 38400
BTserial.begin(38400);
initHC05ToInq();
}
void loop()
{
string deviceAddress = ReadDeviceAddressAndSearch();
if (!succesful)
{
if (deviceAddress == "")
{
Serial.println("There is no device available to connect...");
Serial.end();
BTserial.end();
BTserial.begin(38400);
initHC05ToInq(); // Try again
}
else
{
BTserial.println("AT+LINK=" + deviceAddress);// Link to known device
delayAndRead();
succesfull = true; //BT configuration ended OK
}
}
else
{
//Do Stuff here
File logFile = SD.open("datalog.txt", FILE_WRITE);
//BT is ready to communicate to the device
BTserial.begin(57600);
// make a string for assembling the data to log:
char data = "";
if(BluetoothSerial.available())
{
digitalWrite(BUZZER, LOW);
data = BluetoothSerial.read();
BluetoothSerial.print(data);
BluetoothSerial.print("\n");
// print to the serial port too:
Serial.println("Value: ");
Serial.print(data);
if (logFile)
{
logFile.println("Value: ");
logFile.print(data);
logFile.close();
}
if (data =='1')
{
digitalWrite(GREENLED1, HIGH);
}
else digitalWrite(GREENLED1, LOW);
}
else
{
Serial.println("No signal available, data not received");
digitalWrite(BUZZER, HIGH); //BT not available, not able to retrieve data
}
}