// https://www.electronicshub.org/esp32-ble-tutorial/
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
//#include "WiFi.h"

#define potPin 36 //connect the potentiometer to gpio36

int sss = 0;
int sensorRead = 0;
int pack_num = 0; // calculate number of pack
int stop = 0;

std::string value ="";
std::string valueline ="";
std::string valuesend ="";
std::string pack_num_str = "";

//uint8_t value = 0;  //the set value function only accepts unsigned 8 bit integers

/* Define the UUID for our Custom Service */
#define serviceID BLEUUID("91bad492-b950-4226-aa2b-4ede9fa42f59")

/* Define our custom characteristic along with it's properties */
BLECharacteristic customCharacteristic(
  BLEUUID("beb5483e-36e1-4688-b7f5-ea07361b26a8"), 
  BLECharacteristic::PROPERTY_READ | 
  BLECharacteristic::PROPERTY_NOTIFY
);

/* This function handles the server callbacks */
bool deviceConnected = false;
class ServerCallbacks: public BLEServerCallbacks {
    void onConnect(BLEServer* MyServer) {
      deviceConnected = true;
    };

    void onDisconnect(BLEServer* MyServer) {
      deviceConnected = false;
    }
};

void setup() {
 // WiFi.mode(WIFI_OFF);
  Serial.begin(115200);

  // Create and name the BLE Device
  BLEDevice::init("SERVERESP32");
  BLEDevice::setMTU(517);

  /* Create the BLE Server */
  BLEServer *MyServer = BLEDevice::createServer();
  MyServer->setCallbacks(new ServerCallbacks());  // Set the function that handles Server Callbacks

  /* Add a service to our server */
  BLEService *customService = MyServer->createService(BLEUUID("91bad492-b950-4226-aa2b-4ede9fa42f59")); //  A random ID has been selected

  /* Add a characteristic to the service */
  customService->addCharacteristic(&customCharacteristic);  //customCharacteristic was defined above

  /* Add Descriptors to the Characteristic*/
  customCharacteristic.addDescriptor(new BLE2902());  //Add this line only if the characteristic has the Notify property

  BLEDescriptor VariableDescriptor(BLEUUID((uint16_t)0x2901));  /*```````````````````````````````````````````````````````````````*/
  VariableDescriptor.setValue("server messages");          /* Use this format to add a hint for the user. This is optional. */
  customCharacteristic.addDescriptor(&VariableDescriptor);    /*```````````````````````````````````````````````````````````````*/

  /* Configure Advertising with the Services to be advertised */
  MyServer->getAdvertising()->addServiceUUID(serviceID);

  // Start the service
  customService->start();

  // Start the Server/Advertising
  MyServer->getAdvertising()->start();

  Serial.println("Waiting for a Client to connect...");
}

void loop() {

  /*std::string value = "hello"; //change the value to a range of 0-255 so that it can fit in a single byte
 
  if (deviceConnected) {
    /* Set the value 
    customCharacteristic.setValue(value);  // This is a value of a single byte
    customCharacteristic.notify();  // Notify the client of a change
  } */
sss=sss+1;  
if (stop == 600) {
   Serial.println("broadcast complete");
   delay (900000);
}
else
{
valueline = "(beight)(beight)(beight)(beight)(beight)(beight)(beight)";
//Serial.println("Setting new characteristic value to \"" + newValue + "\"");

//customCharacteristic.notify();

if (pack_num == 10) {
  pack_num=0;
}
pack_num_str = std::to_string(pack_num);
//valuesend ="(" + pack_num_str + pack_num_str + pack_num_str + pack_num_str + pack_num_str + pack_num_str ")" + valueline;
valuesend ="(" + pack_num_str + pack_num_str + pack_num_str + pack_num_str + pack_num_str + pack_num_str +")" + valueline;
customCharacteristic.setValue(valuesend.c_str());
//Serial.println(valuesend.c_str());
pack_num = pack_num + 1;
stop = stop + 1;
delay(80);
}
}

 
