Buenas,
Que tal gente, tengo dudas con el concepto de maestro esclavo trabajando con la transmisión bluetooth, soy nuevo programando con Arduino, compre un Arduino UNO, estoy trabajando en un proyecto para procesar una señal que envia el modulo esclavo y ejecutar una respuesta en el master.
El HC06 esta conectado a un sensor y solo transmite el resultado.
El HC05 esta conectado al arduino y lo que quiero hacer es ubicar el dispositivo, aparearlo, y leer la informacion que este envia.
El nombre del dispositivo HC06 lo tengo guardado en un txt el cual lee el arduino para saber a quien aparear con el HC05, los datos que recibe del esclavo los almaceno en un txt.
Mi duda es si tengo que compilar un codigo que solo configure el HC06 para conectarse al HC05 y luego compilar otro codigo que solo se dedique a ejecutar respuestas. O si puedo hacer todo junto que es lo que estoy haciendo ahora:
Este es el codigo que fui pensando (los comentarios y descripciones estan en ingles porque es un proyecto para la facu)
// 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
}
}