Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Yesid León
Published © Apache-2.0

HelloPills: The IoT-Enabled Mobile Cover Pill Dispenser

HelloPills ensures timely medication for seniors, integrating with mobile apps for reminders, treatment tracking, and family oversight.

IntermediateWork in progress5 hours157
HelloPills: The IoT-Enabled Mobile Cover Pill Dispenser

Things used in this project

Hardware components

Seeed Studio XIAO ESP32S3 Sense
Seeed Studio XIAO ESP32S3 Sense
×1
Slide Switch
Slide Switch
×1
PTS 645 Series Switch
C&K Switches PTS 645 Series Switch
×1

Software apps and online services

Xcode
Apple Xcode
Arduino IDE
Arduino IDE

Story

Read more

Code

Conectivity with ESP32

Arduino
Connection with the iOS application
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>

BLEServer *pServer = NULL;
BLECharacteristic *pCharacteristic = NULL;
bool deviceConnected = false;
bool oldDeviceConnected = false;
bool openStateSent = false;
bool closeStateSent = false;
int val = 0;

#define SERVICE_UUID        "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"

class MyServerCallbacks: public BLEServerCallbacks {
  void onConnect(BLEServer* pServer) {
    deviceConnected = true;
    Serial.println("Conectado");
  }

  void onDisconnect(BLEServer* pServer) {
    deviceConnected = false;
    Serial.println("Desconectado");
  }
};

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 1;  // the number of the pushbutton pin
const int ledPin = LED_BUILTIN;    // the number of the LED pin

// variables will change:
int buttonState = 0;  // variable for reading the pushbutton status


void setup() {
  
  Serial.begin(115200);
  Serial.println("Starting BLE work!");

  BLEDevice::init("XIAO_ESP32S3");
  pServer = BLEDevice::createServer();
  pServer->setCallbacks(new MyServerCallbacks());
  
  BLEService *pService = pServer->createService(SERVICE_UUID);
  pCharacteristic = pService->createCharacteristic(
                                         CHARACTERISTIC_UUID,
                                         BLECharacteristic::PROPERTY_READ   |
                                         BLECharacteristic::PROPERTY_WRITE 
                                       );
  //pCharacteristic->addDescriptor(new BLE2902());

  pCharacteristic->setValue("Hello World");
  pService->start();
  // BLEAdvertising *pAdvertising = pServer->getAdvertising();  // this still is working for backward compatibility
  
  BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  pAdvertising->addServiceUUID(SERVICE_UUID);
  pAdvertising->setScanResponse(true);
  pAdvertising->setMinPreferred(0x06);  // functions that help with iPhone connections issue
  pAdvertising->setMinPreferred(0x12);
  BLEDevice::startAdvertising();
  Serial.println("Characteristic defined! Now you can read it in your phone!");

  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {

  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  if (deviceConnected) {

    if (buttonState == HIGH && openStateSent == false) {
      digitalWrite(ledPin, HIGH);
      pCharacteristic->setValue("Contenedor Cerrado");
      pCharacteristic->notify();
      openStateSent = true;
      closeStateSent = false;
    } else if (buttonState == LOW && closeStateSent == false) {
      digitalWrite(ledPin, LOW);
      pCharacteristic->setValue("Contenedor Abierto");
      pCharacteristic->notify();
      closeStateSent = true;
      openStateSent = false;
    }
    
    delay(5000);
    Serial.println("Conectado y enviando");
  }

  if (!deviceConnected && oldDeviceConnected) {
    delay(500);
    pServer->startAdvertising();
    Serial.println("Start Advertising");
    oldDeviceConnected = deviceConnected;
  }

  if (deviceConnected && !oldDeviceConnected) {
    oldDeviceConnected = deviceConnected;
  }
}

Credits

Yesid León

Yesid León

3 projects • 2 followers

Comments