diff --git a/PillTracker/PillTracker/Base.lproj/Main.storyboard b/PillTracker/PillTracker/Base.lproj/Main.storyboard index d0fce9d..8430289 100644 --- a/PillTracker/PillTracker/Base.lproj/Main.storyboard +++ b/PillTracker/PillTracker/Base.lproj/Main.storyboard @@ -17,7 +17,7 @@ - + - @@ -121,6 +114,13 @@ + @@ -139,7 +139,7 @@ - + diff --git a/PillTracker/PillTracker/SimpleBluetoothIO.swift b/PillTracker/PillTracker/SimpleBluetoothIO.swift index e16f1d7..ddb5c01 100644 --- a/PillTracker/PillTracker/SimpleBluetoothIO.swift +++ b/PillTracker/PillTracker/SimpleBluetoothIO.swift @@ -48,14 +48,14 @@ extension SimpleBluetoothIO: CBCentralManagerDelegate { func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) { peripheral.discoverServices(nil) self.connected = true - print("Connected! With \(peripheral.name)") + print("Connected! With \(String(describing: peripheral.name))") delegate?.simpleBluetoothIO(simpleBluetoothIO: self, didConnect: true) } func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) { connectedPeripheral = peripheral if (peripheral.name != "[TV] Samsung Q7 Series (55)" && peripheral.name != "MacDeMax"){ - print("Discovered \(peripheral.name)") + print("Discovered \(String(describing: peripheral.name))") } if (peripheral.name != nil) { if(((peripheral.name?.contains("PILL_TRACKER"))!)) @@ -114,7 +114,7 @@ extension SimpleBluetoothIO: CBPeripheralDelegate { } func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) { - print("Disconnected: \(peripheral.name)") + print("Disconnected: \(String(describing: peripheral.name))") print("Restarting scan...") centralManager.scanForPeripherals(withServices: nil, options: nil) delegate?.simpleBluetoothIO(simpleBluetoothIO: self, didDisconnect: true) diff --git a/PillTracker/PillTracker/ViewController.swift b/PillTracker/PillTracker/ViewController.swift index f6be7b9..2514154 100644 --- a/PillTracker/PillTracker/ViewController.swift +++ b/PillTracker/PillTracker/ViewController.swift @@ -21,9 +21,10 @@ class ViewController: UIViewController { @IBOutlet weak var refreshToggle: UISwitch! let totalPills: Int = 6 + var usedPills: Int = 0 + var remainingPills: Int = 0 let updateFreq: Float = 2.0 weak var timer: Timer? - var pillIndex = [3, 3, 3, 3, 3, 3] override func viewDidLoad() { super.viewDidLoad() @@ -53,20 +54,6 @@ class ViewController: UIViewController { timer?.invalidate() } - func updatePillArray(data: String) { - for i in 0...5 { - let dataSlice = String(data.character(at: i) ?? "3") - let isIntact = Int(dataSlice) ?? 3 - print("Index: \(i) and value: \(isIntact)") - self.pillIndex[i] = isIntact - } - print("PillIndex: \(self.pillIndex)") - } - - func updatePillArrayUI() { - - } - @IBAction func ledToggleButtonDown(_ sender: UIButton) { simpleBluetoothIO.writeValue(value:49) } @@ -104,16 +91,14 @@ extension ViewController: SimpleBluetoothIODelegate { self.pillsTotal.text = "X" self.pillsAttached.text = "Detached" self.pillsAttached.textColor = UIColor.red - } else if String(value).prefix(2) == "7" { - let remainingPills = Int(value) - let usedPills = self.totalPills - remainingPills + } else { + self.remainingPills = Int(value) + self.usedPills = self.totalPills - self.remainingPills self.pillsConsumed.text = String(usedPills) self.pillsRemaining.text = String(remainingPills) self.pillsTotal.text = String(self.totalPills) self.pillsAttached.text = "Attached" self.pillsAttached.textColor = UIColor.green - self.updatePillArray(data: String(value)) - self.updatePillArrayUI() } } diff --git a/embedded/main/main.ino b/embedded/main/main.ino index ec7c64a..c0b24e8 100644 --- a/embedded/main/main.ino +++ b/embedded/main/main.ino @@ -1,16 +1,14 @@ #include #include #include - // See the following for generating UUIDs: // https://www.uuidgenerator.net/ -#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b" +#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 @@ -19,162 +17,124 @@ #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("*********"); - } - } +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; +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(); + 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(); + int totalCount = 0; + if (digitalRead(DETECT_PIN) == LOW) { + Serial.println("PROBE_ATTACHED"); + if (digitalRead(PILL_1_PIN) == LOW){totalCount++;Serial.println("25:FULL");} + if (digitalRead(PILL_2_PIN) == LOW){totalCount++;Serial.println("13:FULL");} + if (digitalRead(PILL_3_PIN) == LOW){totalCount++;Serial.println("12:FULL");} + if (digitalRead(PILL_4_PIN) == LOW){totalCount++;Serial.println("26:FULL");} + if (digitalRead(PILL_5_PIN) == LOW){totalCount++;Serial.println("27:FULL");} + if (digitalRead(PILL_6_PIN) == LOW){totalCount++;Serial.println("14:FULL");} + } else { + Serial.println("PROBE_DETACHED"); + totalCount = 55; + } + return totalCount; } + 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(); - } + // 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(); + } }