//This example code is in the Public Domain (or CC0 licensed, at your option.)
//By Evandro Copercini - 2018
//
//This example creates a bridge between Serial and Classical Bluetooth (SPP)
//and also demonstrate that SerialBT have the same functionalities of a normal Serial

#include "BluetoothSerial.h"
#define RXD2 16
#define TXD2 17


#define USE_PIN
//#define USE_PIN // Uncomment this to use PIN during pairing. The pin is specified on the line below
const char *pin = "1234";  // Change this to more secure PIN.

String device_name = "M-ROBOT";



/*
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

#if !defined(CONFIG_BT_SPP_ENABLED)
#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
#endif
*/

const uint8_t bitmap[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x1c, 0x03, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x3e, 0x03, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x3f, 0x03, 0xf7, 0xfc, 0x00, 0x00, 0x00, 0x3f, 0x83, 0xf3, 0xfe, 0x00, 0x00, 0x00, 0x1f, 0xc3, 0xf0, 0xff, 0x00, 0x00, 0x00, 0x0f, 0xe3, 0xf0, 0x7f, 0x00, 0x00, 0x00, 0x07, 0xf3, 0xf0, 0xff, 0x00, 0x00, 0x00, 0x03, 0xfb, 0xf3, 0xfe, 0x00, 0x00, 0x00, 0x01, 0xff, 0xf7, 0xfc, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x7b, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0xfb, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x01, 0xfb, 0xf7, 0xfc, 0x00, 0x00, 0x00, 0x03, 0xfb, 0xf3, 0xff, 0x00, 0x00, 0x00, 0x07, 0xf3, 0xf0, 0xff, 0x00, 0x00, 0x00, 0x0f, 0xe3, 0xf0, 0x7f, 0x00, 0x00, 0x00, 0x1f, 0xc3, 0xf0, 0xff, 0x00, 0x00, 0x00, 0x3f, 0x83, 0xf3, 0xfe, 0x00, 0x00, 0x00, 0x3f, 0x03, 0xf7, 0xfc, 0x00, 0x00, 0x00, 0x3e, 0x03, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x3c, 0x03, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x03, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
BluetoothSerial SerialBT;

bool connect = false;
unsigned long previousMillis = 0;  // will store last time LED was updated

// constants won't change:
const long interval = 350;
char str[200];
long _index = 0;

int m=0;
void callback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) {
  if (event == ESP_SPP_SRV_OPEN_EVT) {
    Serial.println("Client Connected");
    connect = true;
  }
  if (event == ESP_SPP_CLOSE_EVT) {
    Serial.println("Client disconnected");
    connect = false;
  }
}


void setup() {
  Serial.begin(115200);
  SerialBT.begin(device_name);  //Bluetooth device name
  SerialBT.register_callback(callback);
  Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);
  Serial.printf("The device with name \"%s\" is started.\nNow you can pair it with Bluetooth!\n", device_name.c_str());

//Serial.printf("The device with name \"%s\" and MAC address %s is started.\nNow you can pair it with Bluetooth!\n", device_name.c_str(), SerialBT.getMacString()); // Use this after the MAC method is implemented
#ifdef USE_PIN
  SerialBT.setPin(pin);
  Serial.println("Using PIN");
#endif
  pinMode(32,OUTPUT);
}

void loop() {


  if (Serial2.available()) {
    char read_data_a = Serial2.read();
    str[_index] = char(read_data_a);
    _index++;
    Serial.write(read_data_a);
    SerialBT.write(read_data_a);
  }
  if (Serial.available()) {
    SerialBT.write(Serial.read());
  }
  if (SerialBT.available()) {
    char read_data = SerialBT.read();
    m=read_data;
    Serial.write(read_data);
    Serial2.write(read_data);
  }
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    if (connect)
    digitalWrite(32,HIGH);
    else
     digitalWrite(32,!digitalRead(32));
    
  }

}
  
