So the project is about a Strain Gauge sensor I'm getting data through an analog port of ESP32 and after some processing I'm sending these data on Android app to display, which I've developed through MIT app inventor.
The problem is that after every connection the first time I'm getting the correct data ONE time (as i want) but when i stop the request (but still connected) and then request the them again I'm getting the correct data 3-4 times. If I (as a client through Android) disconnect from the ESP32 and then connect again, I will get the correct data one time but every time I'm stopping the request and then requesting again the data show on android more and more times.
I've changed the MIT app inventor code and the Arduino main loop code many times but with no luck.
Thanks in advance!!
Here is the code for anyone interested.
Code: Select all
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
String analogSensor;
int sentFlag = 0; //flag for the "sent" loop
int i = 0;
int s[100];
int endOfStrikeFlag = 1;
int beginStrike = 0;
int maxStrike = 0;
int strikeTime = 0;
std::string androidtxt;
BLECharacteristic *pCharacteristic;
float kilo (int x){
float y = 0;
if ((x >= 640) and (x < 695)){
y = 2.5;
}
else if ((x >= 695) and (x < 730)){
y = 5;
}
else if ((x >= 730) and (x < 800)){
y = 7.5;
}
else if ((x >= 800) and (x < 845)){
y = 10;
}
else if ((x >= 845) and (x < 880)){
y = 12.5;
}
else if ((x >= 880) and (x < 950)){
y = 15;
}
else if ((x >= 950) and (x < 1020)){
y = 17.5;
}
else if (x >= 1020){
y = 20;
}
else {
y = 0;
}
return y;
}
void setup() {
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
analogReadResolution(10);
BLEDevice::init("ESP32");
BLEServer *pServer = BLEDevice::createServer();
BLEService *pService = pServer->createService(SERVICE_UUID);
pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE |
BLECharacteristic::PROPERTY_NOTIFY |
BLECharacteristic::PROPERTY_INDICATE
);
//pCharacteristic->setCallbacks(new MyCallbacks());
pCharacteristic->setValue("Hello World");
pService->start();
BLEAdvertising *pAdvertising = pServer->getAdvertising();
pAdvertising->start();
}
void loop() {
// main code running every 10ms
delay(1);
std::string androidtxt = pCharacteristic->getValue();
//Check if the signal frome the andoid is for power or speed or free practice
if (androidtxt.find("pwrstrt") != -1) {
strikeTime = 99; //Power comsumes more time and less strikes
sentFlag = 1;
}
else if (androidtxt.find("spdstrt") != -1) {
sentFlag = 2;
strikeTime = 99; //Speed consumes less time and more strikes
}
else if (androidtxt.find("frstrt") != -1) {
sentFlag = 3;
strikeTime = 99; //Free training must have both speed and power, so strikes must be in the midle
}
//interupt measurement for FREE TRAINING
else if (((androidtxt.find("pwrstp") != -1) or (androidtxt.find("spdstp") != -1) or (androidtxt.find("frstp") != -1)) and (sentFlag != 0)) {
Serial.println("ANDROID DOES NOT WANT A VALUE");
sentFlag = 0;
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(500); // wait for 500ms
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(500); //wait for 500ms
}
//Measure POWER
if ((sentFlag == 1) or (sentFlag == 2) or (sentFlag == 3)) {
delay(1);
//Measure ONLY 1 strike
beginStrike = analogRead(34);
if (beginStrike >= 630){
endOfStrikeFlag = 0;
}
while ((beginStrike >= 630) or (endOfStrikeFlag == 0)){
// read the ongoing strike
if (i >= 97){
endOfStrikeFlag = 1;
i = 0;
break;
}
s[i] = analogRead(34);
if (maxStrike != s[i]){
maxStrike = max(maxStrike, s[i]); //get the max value on each iteration
}
s[i] = 0; //zeroing each cell of the array to use it in the next strike
delay(1);
i = i + 1;
beginStrike = analogRead(34);
}
if (maxStrike >= 630){
analogSensor = String(kilo(maxStrike));
pCharacteristic->setValue(analogSensor.c_str()); //sent analogSensor value on android
pCharacteristic->notify();
Serial.println(maxStrike);
Serial.println(maxStrike);
Serial.println(maxStrike);
}
maxStrike = 0;
}
}