/* 8 entradas, 5 salidas */
#include "BluetoothSerial.h"
#define USE_NAME // Comment this to use MAC address instead of a slaveName
// Check if Bluetooth is available
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
// Check Serial Port Profile
#if !defined(CONFIG_BT_SPP_ENABLED)
#error Serial Port Profile for Bluetooth is not available or not enabled. It is only available for the ESP32 chip.
#endif
BluetoothSerial SerialBT;
#ifdef USE_NAME
String slaveName = "ESP32-BT-Slave"; // Change this to reflect the real name of your slave BT device
#else
String MACadd = "AA:BB:CC:11:22:33"; // This only for printing
uint8_t address[6] = {0xAA, 0xBB, 0xCC, 0x11, 0x22, 0x33}; // Change this to reflect real MAC address of your slave BT device
#endif
#define ALTO LOW
#define BAJO HIGH
String myName = "ESP32-BT-Master";
int pulsador1 = 4, pulsador2 = 13, pulsador3 = 25, pulsador4 = 26, pulsador5 = 27,
pulsador6 = 32, pulsador7 = 33, pulsador8 = 5;
int salida1 = 16, salida2 = 17, salida3 = 18, salida4 = 19, salida5 = 21, salidaErr=22;
void setup() {
bool connected;
Serial.begin(115200);
pinMode(pulsador1, INPUT_PULLUP);
pinMode(pulsador2, INPUT_PULLUP);
pinMode(pulsador3, INPUT_PULLUP);
pinMode(pulsador4, INPUT_PULLUP);
pinMode(pulsador5, INPUT_PULLUP);
pinMode(pulsador6, INPUT_PULLUP);
pinMode(pulsador7, INPUT_PULLUP);
pinMode(pulsador8, INPUT_PULLUP);
pinMode(salida1, OUTPUT);
pinMode(salida2, OUTPUT);
pinMode(salida3, OUTPUT);
pinMode(salida4, OUTPUT);
pinMode(salida5, OUTPUT);
pinMode(salidaErr, OUTPUT);
digitalWrite(salida1, BAJO);
digitalWrite(salida2, BAJO);
digitalWrite(salida3, BAJO);
digitalWrite(salida4, BAJO);
digitalWrite(salida5, BAJO);
digitalWrite(salidaErr, LOW);
SerialBT.begin(myName, true);
//SerialBT.deleteAllBondedDevices(); // Uncomment this to delete paired devices; Must be called after begin
Serial.printf("The device \"%s\" started in master mode, make sure slave BT device is on!\n", myName.c_str());
#ifndef USE_NAME
SerialBT.setPin(pin);
Serial.println("Using PIN");
#endif
// connect(address) is fast (up to 10 secs max), connect(slaveName) is slow (up to 30 secs max) as it needs
// to resolve slaveName to address first, but it allows to connect to different devices with the same name.
// Set CoreDebugLevel to Info to view devices Bluetooth address and device names
#ifdef USE_NAME
connected = SerialBT.connect(slaveName);
Serial.printf("Connecting to slave BT device named \"%s\"\n", slaveName.c_str());
#else
connected = SerialBT.connect(address);
Serial.print("Connecting to slave BT device with MAC "); Serial.println(MACadd);
#endif
if (connected) {
Serial.println("Connected Successfully!");
digitalWrite(salidaErr, LOW);
} else {
while (!SerialBT.connected(10000)) {
digitalWrite(salidaErr, HIGH);
delay(100);
digitalWrite(salidaErr, LOW);
delay(100);
Serial.println("Failed to connect. Make sure remote device is available and in range, then restart app.");
}
digitalWrite(salidaErr, LOW);
}
// Disconnect() may take up to 10 secs max
if (SerialBT.disconnect()) {
Serial.println("Disconnected Successfully!");
}
// This would reconnect to the slaveName(will use address, if resolved) or address used with connect(slaveName/address).
SerialBT.connect();
if (connected) {
Serial.println("Reconnected Successfully!");
digitalWrite(salidaErr, LOW);
} else {
while (!SerialBT.connected(10000)) {
Serial.println("Failed to reconnect. Make sure remote device is available and in range, then restart app.");
digitalWrite(salidaErr, HIGH);
delay(100);
digitalWrite(salidaErr, LOW);
delay(100);
}
digitalWrite(salidaErr, LOW);
}
}
// const char* mensaje="Hola soy el pulsador";
unsigned long tiempo1, tiempo2, tiempo3, tiempo4, tiempo5;
bool flagTiempo1 = true, flagTiempo2 = true, flagTiempo3 = true, flagTiempo4 = true, flagTiempo5 = true;
void loop() {
// SerialBT.write('x');
if (!digitalRead(pulsador1))
{
SerialBT.write('a');
}
else
SerialBT.write('1');
if (!digitalRead(pulsador2))
{
SerialBT.write('b');
}
else
SerialBT.write('2');
if (!digitalRead(pulsador3))
{
SerialBT.write('c');
}
else
SerialBT.write('3');
if (!digitalRead(pulsador4))
{
SerialBT.write('d');
}
else
SerialBT.write('4');
if (!digitalRead(pulsador5))
{
SerialBT.write('e');
}
else
SerialBT.write('5');
if (!digitalRead(pulsador6))
{
SerialBT.write('f');
}
else
SerialBT.write('6');
if (!digitalRead(pulsador7))
{
SerialBT.write('g');
}
else
SerialBT.write('7');
if (!digitalRead(pulsador8))
{
SerialBT.write('h');
}
else
SerialBT.write('8');
// if (Serial.available()) {
// SerialBT.write(Serial.read());
// }
if (SerialBT.available()) {
char data = SerialBT.read();
// Serial.write(data);
// Bloque para 'a'
if (data == 'a') {
if (flagTiempo1) {
tiempo1 = millis();
flagTiempo1 = false;
digitalWrite(salida1, ALTO);
}
}
// Bloque para 'b'
if (data == 'b') {
if (flagTiempo2) {
tiempo2 = millis();
flagTiempo2 = false;
digitalWrite(salida2, ALTO);
}
}
// Bloque para 'c'
if (data == 'c') {
if (flagTiempo3) {
tiempo3 = millis();
flagTiempo3 = false;
digitalWrite(salida3, ALTO);
}
}
// Bloque para 'd'
if (data == 'd') {
if (flagTiempo4) {
tiempo4 = millis();
flagTiempo4 = false;
digitalWrite(salida4, ALTO);
}
}
if (data == 'e')
{
digitalWrite(salida5, ALTO);
}
else if(data=='5')
digitalWrite(salida5, BAJO);
}
delay(10);
// Primer bloque
if (!flagTiempo1) {
if ((millis() - tiempo1) >= 3000) {
digitalWrite(salida1, BAJO);
flagTiempo1 = true;
}
}
// Segundo bloque
if (!flagTiempo2) {
if ((millis() - tiempo2) >= 3000) {
digitalWrite(salida2, BAJO);
flagTiempo2 = true;
}
}
// Tercer bloque
if (!flagTiempo3) {
if ((millis() - tiempo3) >= 3000) {
digitalWrite(salida3, BAJO);
flagTiempo3 = true;
}
}
// Cuarto bloque
if (!flagTiempo4) {
if ((millis() - tiempo4) >= 3000) {
digitalWrite(salida4, BAJO);
flagTiempo4 = true;
}
}
}