From 8ae0be958ac05c473dd8a1f858b533fba54cde24 Mon Sep 17 00:00:00 2001 From: sridhar-mothe Date: Mon, 4 Nov 2024 10:22:45 +0000 Subject: [PATCH] code to send weights to the EMcontroller --- Emcontroller-Code.md | 172 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 171 insertions(+), 1 deletion(-) diff --git a/Emcontroller-Code.md b/Emcontroller-Code.md index 5d08b7b..ea60554 100644 --- a/Emcontroller-Code.md +++ b/Emcontroller-Code.md @@ -1 +1,171 @@ -Welcome to the Wiki. \ No newline at end of file +#include +#include +#include +#include + +const int pwmPin = 16; +const int dirPin = 17; +const int doorSensorPin = 13; +float i; +String Command; +int repCount; +bool deviceConnected = false; +bool doorWasClosed = false; +int doorState; +String temp; +String repCountStr; + +const String serviceUuid = "12345678-1234-1234-1234-123456789abc"; +const String weightcharacteristicUuid = "abcd1234-1234-1234-1234-123456789abc"; //(weight) +const String repscharacteristicUuid = "abcd5678-1234-1234-1234-123456789abc"; //(repeation) +const String resetCharacteristicUuid = "abcd9101-1234-1234-1234-123456789abc"; //(reset) + +BLEServer* pServer = NULL; +BLECharacteristic* pRepCountCharacteristic = NULL; +BLECharacteristic* pWeightCharacteristic = NULL; +BLECharacteristic* pStringCharacteristic = NULL; + +class MyServerCallbacks : public BLEServerCallbacks { + void onConnect(BLEServer* pServer) { + deviceConnected = true; + Serial.println("Device connected"); + } + + void onDisconnect(BLEServer* pServer) { + deviceConnected = false; + repCount = 0; + Command = "reset"; + Serial.println("Device disconnected"); + BLEDevice::startAdvertising(); // Restart advertising + } +}; + +class MyCallbacks : public BLECharacteristicCallbacks { + void onWrite(BLECharacteristic* pCharacteristic) { + std::string value = pCharacteristic->getValue().c_str(); + temp = String(value.c_str()); + + // Check which characteristic received data + + if (pCharacteristic->getUUID().equals(BLEUUID(weightcharacteristicUuid))) { + Serial.println("Received Weight Command: " + temp); + Command = temp; + + // Set PWM value based on the received command using switch-case + switch (Command.toInt()) { + case 0: i = 0; break; + case 5: i = 1; break; + case 10: i = 6; break; + case 15: i = 10; break; + case 20: i = 15; break; + case 25: i = 16.12; break; + case 30: i = 19; break; + case 35: i =23 ; break; + case 40: i = 28; break; + // Equivalent to OFF + default: i = 0; break; + } + + Serial.print("Received Command: "); + Serial.println(Command); + + // Echo the command back to the app + pWeightCharacteristic->setValue((unsigned char*)value.c_str(), value.length()); + pWeightCharacteristic->notify(); + pRepCountCharacteristic->setValue(repCountStr.c_str()); + pRepCountCharacteristic->notify(); + } else if (pCharacteristic->getUUID().equals(BLEUUID(resetCharacteristicUuid))) { + // Handle the string data received from the app + Serial.println("Received String from APK: " + temp); + // Process the received string as needed + if (temp == "reset") { + repCount = 0; + i = 0; + Command = "0"; + } + } + } +}; + + +//pWeightCharacteristic->setValue((unsigned char*)value.c_str(), value.length()); +void setup() { + + Serial.begin(115200); + + pinMode(pwmPin, OUTPUT); + pinMode(dirPin, OUTPUT); + pinMode(doorSensorPin, INPUT); + + digitalWrite(dirPin, HIGH); + + BLEDevice::init("FlexiFitPro"); + pServer = BLEDevice::createServer(); + pServer->setCallbacks(new MyServerCallbacks()); + BLEService* Service = pServer->createService(BLEUUID(serviceUuid)); + + // Repetition Count Service + pRepCountCharacteristic = Service->createCharacteristic( + BLEUUID(repscharacteristicUuid), + BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_NOTIFY); + pRepCountCharacteristic->addDescriptor(new BLE2902()); + + pWeightCharacteristic = Service->createCharacteristic( + BLEUUID(weightcharacteristicUuid), + BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_NOTIFY | BLECharacteristic::PROPERTY_READ); + pWeightCharacteristic->setCallbacks(new MyCallbacks()); + Service->start(); + + pStringCharacteristic = Service->createCharacteristic( + BLEUUID(resetCharacteristicUuid), + BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_NOTIFY | BLECharacteristic::PROPERTY_READ); + pStringCharacteristic->setCallbacks(new MyCallbacks()); + Service->start(); + + BLEAdvertising* pAdvertising = BLEDevice::getAdvertising(); + pAdvertising->addServiceUUID(BLEUUID(serviceUuid)); + pAdvertising->start(); + + Serial.println("BLE services are defined and advertising."); +} + +void loop() { + doorState = digitalRead(doorSensorPin); + + int doorState = digitalRead(doorSensorPin); + + // Check if the door is closed + if (doorState == LOW) { + if (!doorWasClosed) { + // Transition from open to closed circuit + repCount++; + doorWasClosed = true; // Update the flag + delay(1000); // Add a 1-second delay to debounce or prevent multiple counts + } + } else { + // Door is open + doorWasClosed = false; // Reset the flag + } + + + // Send rep count over BLE + + if (deviceConnected) { + repCountStr = String(repCount); + pRepCountCharacteristic->setValue(repCountStr.c_str()); + pWeightCharacteristic->setValue(Command.c_str()); + pRepCountCharacteristic->notify(); + pWeightCharacteristic->notify(); + //Serial.println(" hi apk "); + } + + Serial.print("command received :"); + Serial.println(temp); + Serial.print("PWM value: "); + Serial.println(i); + Serial.print("Rep Count: "); + Serial.println(repCount); + + analogWrite(pwmPin, i); + delay(10); +} \ No newline at end of file