#include #include #include // See the following for generating UUIDs: // https://www.uuidgenerator.net/ #define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b" #define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8" #define LED_PIN 2 #define BTN_PIN 5 #define PILL_1_PIN 25 #define PILL_2_PIN 13 #define PILL_3_PIN 12 #define PILL_4_PIN 26 #define PILL_5_PIN 27 #define PILL_6_PIN 14 #define DETECT_PIN 18 int deviceConnected = false; int waitingForUpdate = 0; class MyCallbacks: public BLECharacteristicCallbacks { void onConnect(BLEServer* pServer) { deviceConnected = true; Serial.println("device connected"); }; void onDisconnect(BLEServer* pServer) { deviceConnected = false; Serial.println("device disconnected"); } void onWrite(BLECharacteristic *pCharacteristic) { std::string value = pCharacteristic->getValue(); if (value.length() > 0) { Serial.print("*********"); Serial.print(value.c_str()); Serial.print("-"); Serial.print(atoi(value.c_str())); Serial.print("-"); if(atoi(value.c_str())==1) { digitalWrite(LED_PIN,HIGH); Serial.print("LEDON"); } else if (atoi(value.c_str())==2) { digitalWrite(LED_PIN,LOW); Serial.print("LEDOFF"); } else if (atoi(value.c_str())==3) { Serial.print("UPDATE_PILL_TRACK"); waitingForUpdate = 1; } else { Serial.println(); Serial.println(atoi(value.c_str())); Serial.println(); } Serial.println("*********"); } } }; BLECharacteristic *pCharacteristic; void setup() { Serial.begin(115200); pinMode(LED_PIN,OUTPUT); pinMode(PILL_1_PIN,INPUT_PULLUP); pinMode(PILL_2_PIN,INPUT_PULLUP); pinMode(PILL_3_PIN,INPUT_PULLUP); pinMode(PILL_4_PIN,INPUT_PULLUP); pinMode(PILL_5_PIN,INPUT_PULLUP); pinMode(PILL_6_PIN,INPUT_PULLUP); pinMode(BTN_PIN,INPUT_PULLUP); pinMode(DETECT_PIN,INPUT_PULLUP); digitalWrite(LED_PIN,LOW); Serial.println("1- Download and install an BLE scanner app in your phone"); Serial.println("2- Scan for BLE devices in the app"); Serial.println("3- Connect to MyESP32"); Serial.println("4- Go to CUSTOM CHARACTERISTIC in CUSTOM SERVICE and write something"); Serial.println("5- See the magic =)"); BLEDevice::init("PILL_TRACKER"); BLEServer *pServer = BLEDevice::createServer(); BLEService *pService = pServer->createService(SERVICE_UUID); pCharacteristic= pService->createCharacteristic( CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_NOTIFY ); pCharacteristic->setCallbacks(new MyCallbacks()); pCharacteristic->setValue("Hello World"); pService->start(); BLEAdvertising *pAdvertising = pServer->getAdvertising(); pAdvertising->start(); } int prevVal = LOW; int prevVal2 = LOW; int getPillCount() { String bay0Intact = "0"; String bay1Intact = "0"; String bay2Intact = "0"; String bay3Intact = "0"; String bay4Intact = "0"; String bay5Intact = "0"; String result = "7"; if(digitalRead(DETECT_PIN) == LOW) { Serial.println("PROBE_ATTACHED"); if(digitalRead(PILL_1_PIN) == LOW){bay0Intact = "1";Serial.println("25:FULL");} if(digitalRead(PILL_2_PIN) == LOW){bay1Intact = "1";Serial.println("13:FULL");} if(digitalRead(PILL_3_PIN) == LOW){bay2Intact = "1";Serial.println("12:FULL");} if(digitalRead(PILL_4_PIN) == LOW){bay3Intact = "1";Serial.println("26:FULL");} if(digitalRead(PILL_5_PIN) == LOW){bay4Intact = "1";Serial.println("27:FULL");} if(digitalRead(PILL_6_PIN) == LOW){bay5Intact = "1";Serial.println("14:FULL");} result = result + bay0Intact + bay1Intact + bay2Intact + bay3Intact + bay4Intact + bay5Intact; Serial.println("**********RESULT**********"); Serial.println(result); Serial.println("**********RESULT**********"); } else { Serial.println("PROBE_DETACHED"); result = "55"; } Serial.print("#########################################Sending: "); Serial.println(result.toInt()); return result.toInt(); } void loop() { // put your main code here, to run repeatedly: int currentVal = digitalRead(BTN_PIN); if(currentVal!=prevVal) { prevVal=currentVal; if(currentVal==HIGH) { int value = 99; pCharacteristic->setValue((uint8_t*)&value, 4); pCharacteristic->notify(); } else { int value = 98; pCharacteristic->setValue((uint8_t*)&value, 4); pCharacteristic->notify(); } } if(waitingForUpdate == 1){ Serial.println("**********************************"); Serial.println("**************UPDATE AND SEND PILL COUNT********************"); Serial.println("**********************************"); waitingForUpdate = 0; int value = getPillCount(); pCharacteristic->setValue((uint8_t*)&value, 4); pCharacteristic->notify(); } }