#include <Arduino.h>
/*
Terminal Module is like a chat box. It allows you to send and receive commands between your
board and smartphone.
You can reduce the size of library compiled by enabling only those modules that you
want to use. For this first define CUSTOM_SETTINGS followed by defining
INCLUDE_modulename.
Explore more on: https://thestempedia.com/docs/dabble/terminal-module/
*/
//BLUETOH
unsigned long ti,tf,dt;
String string_pc_a_esp32 = ""; //inicia vacio
String string_ArduinoMega_a_esp32 = ""; //inicia vacio
String canal = "";
String valor_canal = "";
bool cadena_completa = false;
uint8_t conteo = 0;
void escucha_serial2(void); //escucha los datos enviados por el serial2 SERIAL
#define CUSTOM_SETTINGS
#define INCLUDE_TERMINAL_MODULE
#include <DabbleESP32.h>
String Serialdata = "";
bool dataflag = 0;
// configuracion para encendido del LED 2
#define LED 18 // Pin al que está conectado el LED , led sera encendido me diente orden SERIAL
int leds[] = {15,2,4,16,17,5,18,19};
String comando = "";
void inicia_leds(void);
void setup() {
//SERIAL
// put your setup code here, to run once:
ti = tf = dt = 0;
inicia_leds();
Serial.begin(115200); // make sure your Serial Monitor is also set at this baud rate.
Serial2.begin(9600); // Inicia la comunicación serial 2
Dabble.begin("MyEsp32"); //set bluetooth name of your device
Serial.println("Iniciando el ESP32");
}
void loop() {
Dabble.processInput(); //this function is used to refresh data obtained from smartphone.Hence calling this function is mandatory in order to get data properly from your mobile.
//SERIAL
// put your main code here, to run repeatedly:
if (dataflag == 1) //cuando hay algo para transmitir a bluetooth se hace!!!
{
Terminal.print(Serialdata);
Serialdata = "";
dataflag = 0;
}
//aqui se revisa si algo ha llegado desde el bluetooth.
if (Terminal.available() != 0)
{
while (Terminal.available() != 0)
{
//lee cada caracter que se recibe via bluettoth
comando = comando + Terminal.read();
// Serial.write(Terminal.read());
}
//analiza el string recibido para tomar acción
if(comando.equals("led_on")){
digitalWrite(LED,HIGH);
Serial.println("Encendindo el LED");
Terminal.print("Encendindo el LED");
}else if(comando.equals("led_off")){
digitalWrite(LED,LOW);
Serial.println("El LED ha sido apagado");
Terminal.print("El LED ha sido apagado");
}else if(comando.equals("LED15!")){ // EnvÃa el comando al esclavo a través del puerto serial 2
Serial2.print(comando);
Terminal.print("Comando enviado al esclavo: LED15!");
}
else if(comando.equals("MOT01!")){ // EnvÃa el comando al esclavo a través del puerto serial 2
Serial2.print(comando);
Terminal.print("Comando Velocidad al esclavo: LENTO");
}
else if(comando.equals("MOT02!")){ // EnvÃa el comando al esclavo a través del puerto serial 2
Serial2.print(comando);
Terminal.print("Comando Velocidad al esclavo: MEDIA");
}
else if(comando.equals("MOT03!")){ // EnvÃa el comando al esclavo a través del puerto serial 2
Serial2.print(comando);
Terminal.print("Comando Velocidad al esclavo: RAPIDA");
}
Serial.println();
comando="";
}
}
//SERIAL
void escucha_serial2(void){//esta funcion esta destinada a escuchar el puerto serial 2
//escucha lo que le dice el esclavo al ESP32 (maestro)y su funcion es armar en un string
//lo que el otro micrcontrolador ha enviado.
while (Serial2.available()) {
// Leer el byte disponible del puerto serie
char incomingByte = Serial2.read();
string_ArduinoMega_a_esp32 = string_ArduinoMega_a_esp32 + incomingByte;
if(incomingByte == '!'){ //si detecta el final de un comando
cadena_completa = true;
//Serial.println(string_ArduinoMega_a_esp32);
}
}
}
//funcion serial event que lee cada que se comienza un ciclo loop
//si hay datos que hayan llegado desde el puerto serial, con el fin
//de retransmitirlos al bluetooth.
void inicia_leds(void){
u_int8_t i;
for(i=0;i<8;++i){
pinMode(leds[i],OUTPUT);
digitalWrite(leds[i],HIGH);
}
}