Power on defaults

// --------------------------------------------------------------------------------------------------

// Existing Firebase Path Check
void firebase_path_check(){
Serial.println("------------------------------------");
Serial.println("Path exist test...");
if (Firebase.pathExist(firebaseData, path))
{
Serial.println("Path " + path + " exists: Device is not first time to run!");
}
else
{
Serial.println("Path " + path + " is not exist, this is the first time that the device is running!");
}
Serial.println("------------------------------------");
Serial.println();
}

// --------------------------------------------------------------------------------------------------

// User Defined Firebase setInt Function
static void firebase_device_set(String str, int integer){
if (Firebase.setInt(firebaseData, path + str, (int) integer))
{
Serial.println("SET PASSED");
Serial.println("PATH: " + firebaseData.dataPath());
Serial.println("TYPE: " + firebaseData.dataType());
Serial.print("VALUE: ");
if (firebaseData.dataType() == "int")
Serial.println(firebaseData.intData());
else if (firebaseData.dataType() == "float")
Serial.println(firebaseData.floatData(), 5);
else if (firebaseData.dataType() == "double")
Serial.println(firebaseData.doubleData(), 9);
else if (firebaseData.dataType() == "boolean")
Serial.println(firebaseData.boolData() == 1 ? "true" : "false");
else if (firebaseData.dataType() == "string")
Serial.println(firebaseData.stringData());
else if (firebaseData.dataType() == "json")
//Serial.println(firebaseData.jsonData());
Serial.println("------------------------------------");
Serial.println();
}
else
{
Serial.println("SET FAILED");
Serial.println("REASON: " + firebaseData.errorReason());
Serial.println("------------------------------------");
Serial.println();
}
}

// --------------------------------------------------------------------------------------------------

// User Defined Firebase getInt Function
static int firebase_device_get(String str){
if (Firebase.getInt(firebaseData, path + str))
{
Serial.println("GET PASSED");
Serial.println("PATH: " + firebaseData.dataPath());
Serial.println("TYPE: " + firebaseData.dataType());
Serial.print("VALUE: ");
Serial.println(firebaseData.intData());
Serial.println("------------------------------------");
Serial.println();
return firebaseData.intData();
}
else
{
Serial.println("GET FAILED");
Serial.println("REASON: " + firebaseData.errorReason());
Serial.println("------------------------------------");
Serial.println();
return 9;
}
}

static void issue_update(){

Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
Firebase.reconnectWiFi(true); 
delay(100);
firebase_path_check();
Serial.printf("Free heap(): %d\n", xPortGetFreeHeapSize() );
delay(100); 
Get_Device_Value();
Serial.printf("Free heap(): %d\n", xPortGetFreeHeapSize() );
delay(100);
Set_Device();
Temp_Value_Update();
Serial.printf("Free heap(): %d\n", xPortGetFreeHeapSize() );    

}

// --------------------------------------------------------------------------------------------------

// Device status update to Firebase
static void Device_Status_Update(){
Serial.println("Sending Device Status to Firebase!");
firebase_device_set("/Device/Fan_status", device_fan);
firebase_device_set("/Device/Fire_status", device_fire);
firebase_device_set("/Device/MP3_status", device_mp3);
}

// --------------------------------------------------------------------------------------------------

// Send temperature & humidity value to Firebase
static void Temp_Value_Update(){
Serial.println("Sending temperature and humidity value to Firebase!");
firebase_device_set("/Device/Temp_value", temp_rtc);
firebase_device_set("/Device/Humi_value", humi_rtc);
}

// --------------------------------------------------------------------------------------------------

// Get device setting value from Firebase
static void Get_Device_Value(){
Serial.println("Get device setting value from Firebase!");
temp = firebase_device_get("/Device/Temp_value");
app_mp3 = firebase_device_get("/App/mp3_status");
app_fan = firebase_device_get("/App/fan_status");
app_fire = firebase_device_get("/App/fire_status");
app_temp = firebase_device_get("/App/standard_temp");
app_volume = firebase_device_get("/App/volume_value");
firebase_device_get("/App/CMD_FAN");
}

// --------------------------------------------------------------------------------------------------

// Set device
static void Set_Device(){
Serial.println("Device Setting!");

// ------- FAN Setting ---------
if (app_fan == 1){
FAN_CTL_ret = 1;
Serial.printf(" \n\n FanCtl: %d \n\n", app_fan);
FAN_ON;
device_fan = 1;
}else if (app_fan == 0){
FAN_OFF;
FAN_CTL_ret = 0;
Serial.printf(" \n\n FanCtl: %d \n\n", app_fan);
device_fan = 0;
}

// ------- MP3 Setting ---------
if (app_mp3 == 1){
MP3_PWR_CTL_ret = 1;
MP3_PWR_ON;
delay(500);
mp3.play(1,app_volume);
device_mp3 = 1;
}else if (app_mp3 == 0){
MP3_PWR_CTL_ret = 0;
mp3.stop();
delay(100);
MP3_PWR_OFF;
device_mp3 = 0;
}

// ------- FIRE Setting ---------
if ((app_fire == 1) && (app_temp + 2 > temp)){
FIRE_ON;
device_fire = 1;
}else if ((app_fire == 0) || (app_temp + 2 <= temp)){
FIRE_OFF;
device_fire = 0;
}
delay(100);
Device_Status_Update();
firebase_device_set("/Set_changed", 0);
}

void IRAM_ATTR onTimer(){
// Increment the counter and set the time of ISR
portENTER_CRITICAL_ISR(&timerMux);
isrCounter++;
lastIsrAt = millis();
if(!didItRun){
Serial.println("--ISR Restart--");
ESP.restart();
}
didItRun = false;
portEXIT_CRITICAL_ISR(&timerMux);

// Give a semaphore that we can check in the loop
xSemaphoreGiveFromISR(timerSemaphore, NULL);
// It is safe to use digitalRead/Write here if you want to toggle an output

}

void IRAM_ATTR resetModule() {
ets_printf("REBOOT\n");
esp_restart();
}

class MyClientCallback : public BLEClientCallbacks {
void onConnect(BLEClient* pclient) {
Serial.println("on connect");
}

void onDisconnect(BLEClient* pclient) {
  connected_ = false;
  scanning = true;
  Serial.println("onDisconnect");
     
}

};

// ####################################################
// ############### BLE FUNCTIONS ######################
// ####################################################
static void notifyCallback(
BLERemoteCharacteristic* pBLERemoteCharacteristic,
uint8_t* pData,
size_t length,
bool isNotify) {
Serial.print("Notify callback for characteristic ");
Serial.print(pBLERemoteCharacteristic->getUUID().toString().c_str());
Serial.print(" of data length ");
Serial.println(length);
String data = String((char *)pData);
temperature = data.substring(2, 6).toDouble();
humidity = data.substring(9, 13).toDouble();

Serial.println(pBLERemoteCharacteristic->toString().c_str());
Serial.printf("Temp: %.1f / Humid: %.1f\n", temperature, humidity);
temp_rtc = temperature;
humi_rtc = humidity;
didItRun = true;

lastUpdate = millis();
// Disconnect from BT

Serial.printf("Free heap(): %d\n", xPortGetFreeHeapSize() );
if (xPortGetFreeHeapSize() <= 10000){
  Serial.println("----CONTROLLED----RESTART------");
  ESP.restart();
   //resetModule();
} 
connected_ = false;

}

bool connectToServer(BLEAddress *pAddress) {

if (pAddress->toString() == "4c:65:a8:d4:c3:5d") {
  current++;
  return false;
}
Serial.print("Forming a connection to ");
Serial.println(pAddress->toString().c_str());

BLEClient*  pClient;
BLERemoteCharacteristic* pRemoteCharacteristic;

pClient  = BLEDevice::createClient();
pClient->setClientCallbacks(new MyClientCallback());
Serial.println(" - Created client");
vTaskDelay(200); 

// Connect to the remove BLE Server.
pClient->connect(*pAddress);
Serial.println(" - Connected to server");
vTaskDelay(200); 

// Obtain a reference to the service we are after in the remote BLE server.
BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
vTaskDelay(200); 
if (pRemoteService == nullptr) {
  Serial.print("Failed to find our service UUID: ");
  Serial.println(serviceUUID.toString().c_str());
  return false;
}
Serial.println(" - Found our service");