Author Topic: ESP32 doesn't respond properly when get command from Mobile App through BLE mode  (Read 1209 times)

0 Members and 1 Guest are viewing this topic.

Offline arulTopic starter

  • Regular Contributor
  • *
  • Posts: 50
  • Country: in
Hi All,

I am writing code for ESP32 module in Arduino IDE for interfacing between ESP32 module and Mobile application through Bluetooth BLE mode.

I had established connection between ESP32 and Mobile application. And also I could send data between mobile to ESP32 and ESP32 to Mobile.

My task is capturing ADC data in ESP32 module and send to Mobile application. But ADC capturing starts when Mobile send command to ESP32, i.e command "START", then send 1000 times ADC data to Mobile Application. After that wait for next "START" command, then starts the same process again.

The problem is, ESP32 receives command from Mobile, but ESP32 didn't starts the ADC capturing process. And also I tried , like once Mobile connects the ESP32 through Bluetooth BLE mode, ESP 32 starts capturing ADC data and send to Mobile Application properly. I also received data in Mobile Application. But this ADC capturing didn't happened in send command from Mobile to ESP32. The following is the code what I wrote.
Code: [Select]
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>


//////////Variable Declaration of BLE/////////////////////////////
BLECharacteristic *pCharacteristic;
bool deviceConnected = false;
float txValue = 0;
std::string rxValue;
volatile bool BLE_CMD = true;


#define SERVICE_UUID           "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
#define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
/////////////////////////////////////////////////////////////////////


/////Variable Declaration of ADC calculation////////////////////
const int IO34_ADC = 34;
const int IO35_ADC = 35;   

float IO34_ADC_VALUE = 0;
float IO34_voltage_value_min = 0;
float IO34_voltage_value_max = 0;
int IO34_ADC_VALUE_SUM = 0;
float IO34_ADC_VALUE_AVG = 0;
float IO34_VOLtAGE = 0;
 

float IO35_ADC_VALUE = 0;
float IO35_voltage_value_min = 0;
float IO35_voltage_value_max = 0;
int IO35_ADC_VALUE_SUM = 0;
float IO35_ADC_VALUE_AVG = 0;
float IO35_VOLtAGE = 0;

int ADC_Buffer_34[500];
int ADC_Buffer_35[500];
int ADC_34_min[50], ADC_34_max[55], IO_34_min, IO_34_max;
int ADC_35_min[50], ADC_35_max[55], IO_35_min, IO_35_max;

#define Vref 3.3             
#define ADCResolution 4096    //12-bit SAR ADC
/////////////////////////////////////////////////////////////////////


int f_l_i, f_l_j, f_l_k;   // FOR for loop






float AVERAGE(int arr[], int n)
{
  int i;
  int sum=0;
  float avg;

  for(i=0; i<n; i++)
  {
    sum = sum + arr[i] ;
  }

  avg = sum / n;

  return avg;
 
}

class MyServerCallbacks: public BLEServerCallbacks {
    void onConnect(BLEServer* pServer) {
      deviceConnected = true;
    };

    void onDisconnect(BLEServer* pServer) {
      deviceConnected = false;
    }
};

class MyCallbacks: public BLECharacteristicCallbacks
{
  void onWrite(BLECharacteristic *pCharacteristic)
    {
      rxValue = pCharacteristic->getValue();

      if (rxValue.length() > 0) {
        Serial.println("*********");
        Serial.print("Received Value: ");

        for (int i = 0; i < rxValue.length(); i++) {
          Serial.print(rxValue[i]);
        }

        Serial.println();
        Serial.println("*********");
      }
    }
};

void setup() {
  Serial.begin(115200);

 // pinMode(LED, OUTPUT);

  // Create the BLE Device
  BLEDevice::init("ESP32 UART Test"); // Give it a name

  // Create the BLE Server
  BLEServer *pServer = BLEDevice::createServer();
  pServer->setCallbacks(new MyServerCallbacks());

  // Create the BLE Service
  BLEService *pService = pServer->createService(SERVICE_UUID);

  // Create a BLE Characteristic
  pCharacteristic = pService->createCharacteristic(
                      CHARACTERISTIC_UUID_TX,
                      BLECharacteristic::PROPERTY_NOTIFY
                    );
                     
  pCharacteristic->addDescriptor(new BLE2902());

  BLECharacteristic *pCharacteristic = pService->createCharacteristic(
                                         CHARACTERISTIC_UUID_RX,
                                         BLECharacteristic::PROPERTY_WRITE
                                       );

  pCharacteristic->setCallbacks(new MyCallbacks());

  // Start the service
  pService->start();

  // Start advertising
  pServer->getAdvertising()->start();
  Serial.println("Waiting a client connection to notify...");
}

void loop() {
  if (deviceConnected) {
    rxValue = pCharacteristic->getValue();
    BLE_CMD = true ;
    if (rxValue =="START")
     {
      for(unsigned long i_1min = 0; i_1min < 1000; i_1min++)
      {
        if(BLE_CMD == true)
        {
          for(f_l_i = 0;f_l_i < 145; f_l_i++)
          {
            ADC_Buffer_34[f_l_i] = analogRead(IO34_ADC);
            ADC_Buffer_35[f_l_i] = analogRead(IO35_ADC);
       
          }       

         
          IO34_ADC_VALUE = AVERAGE(ADC_Buffer_34, 145);
          IO35_ADC_VALUE = AVERAGE(ADC_Buffer_35, 145);

   
          ///////////////////////////////////////ADC Calculation//////////////////////////////////////////////////////////////////////////////////////////
         
          IO34_VOLtAGE = (float) (IO34_ADC_VALUE * Vref ) / (ADCResolution);
     
          IO35_VOLtAGE = (float) (IO35_ADC_VALUE * Vref ) / (ADCResolution);


            char tx1String[8];
            char tx2String[8];
            dtostrf(IO34_VOLtAGE, 1, 2, tx1String);
            dtostrf(IO35_VOLtAGE, 1, 2, tx2String); 

         
          //////////////////UART Window///////////////////////
          Serial.print("9999TH1_");Serial.print(IO34_VOLtAGE);Serial.print("XXTH2_");Serial.print(IO35_VOLtAGE);Serial.print("XXXX\n");


            ////////////////BLE Window///////////////////////
            pCharacteristic->setValue("9999TH1_"); pCharacteristic->notify();
            pCharacteristic->setValue(tx1String); pCharacteristic->notify();
            pCharacteristic->setValue("XXTH2_"); pCharacteristic->notify();
            pCharacteristic->setValue(tx2String); pCharacteristic->notify();
            pCharacteristic->setValue("XXXX\n"); pCharacteristic->notify();
   
          ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


         
        }

      }

  ///////////////////End Packet : UART////////////////
  Serial.print("9999EEEEEEEEXXEEEEEEEEXXXX\n");
 
  ///////////////////End Packet : BLE//////////////// 
  pCharacteristic->setValue("9999EEEEEEEEXXEEEEEEEEXXXX\n");  pCharacteristic->notify();

  rxValue = "TEMP_STOP";


    }
 }
  delay(50);
}


Please let me know, where I did mistake and suggest how I can finish the task expected way.

Advances in thank you All.
« Last Edit: March 17, 2022, 06:00:18 am by arul »
 

Offline PlainName

  • Super Contributor
  • ***
  • Posts: 7746
  • Country: va
If you fake the data instead of calling analogRead(), does that fake data get transmitted? That is, replace "analogRead(IO34_ADC)" with "f_l_i + i_1min" (or any other identifiable data).
 
The following users thanked this post: arul

Offline arulTopic starter

  • Regular Contributor
  • *
  • Posts: 50
  • Country: in
Hi @ dunkemhigh,

I modified code as follow as
Code: [Select]
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>


//////////Variable Declaration of BLE/////////////////////////////
BLECharacteristic *pCharacteristic;
bool deviceConnected = false;
float txValue = 0;
std::string rxValue;
volatile bool BLE_CMD = true;


#define SERVICE_UUID           "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
#define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
/////////////////////////////////////////////////////////////////////


/////Variable Declaration of ADC calculation////////////////////
const int IO34_ADC = 34;
const int IO35_ADC = 35;   

float IO34_ADC_VALUE = 0;
float IO34_voltage_value_min = 0;
float IO34_voltage_value_max = 0;
int IO34_ADC_VALUE_SUM = 0;
float IO34_ADC_VALUE_AVG = 0;
float IO34_VOLtAGE = 0;
 

float IO35_ADC_VALUE = 0;
float IO35_voltage_value_min = 0;
float IO35_voltage_value_max = 0;
int IO35_ADC_VALUE_SUM = 0;
float IO35_ADC_VALUE_AVG = 0;
float IO35_VOLtAGE = 0;

int ADC_Buffer_34[500];
int ADC_Buffer_35[500];
int ADC_34_min[50], ADC_34_max[55], IO_34_min, IO_34_max;
int ADC_35_min[50], ADC_35_max[55], IO_35_min, IO_35_max;

#define Vref 3.3             
#define ADCResolution 4096    //12-bit SAR ADC
/////////////////////////////////////////////////////////////////////


int f_l_i, f_l_j, f_l_k;   // FOR for loop






float AVERAGE(int arr[], int n)
{
  int i;
  int sum=0;
  float avg;

  for(i=0; i<n; i++)
  {
    sum = sum + arr[i] ;
  }

  avg = sum / n;

  return avg;
 
}

class MyServerCallbacks: public BLEServerCallbacks {
    void onConnect(BLEServer* pServer) {
      deviceConnected = true;
    };

    void onDisconnect(BLEServer* pServer) {
      deviceConnected = false;
    }
};

class MyCallbacks: public BLECharacteristicCallbacks
{
  void onWrite(BLECharacteristic *pCharacteristic)
    {
      rxValue = pCharacteristic->getValue();

      if (rxValue.length() > 0) {
        Serial.println("*********");
        Serial.print("Received Value: ");

        for (int i = 0; i < rxValue.length(); i++) {
          Serial.print(rxValue[i]);
        }

        Serial.println();
        Serial.println("*********");
      }
    }
};

void setup() {
  Serial.begin(115200);

 // pinMode(LED, OUTPUT);

  // Create the BLE Device
  BLEDevice::init("ESP32 UART Test"); // Give it a name

  // Create the BLE Server
  BLEServer *pServer = BLEDevice::createServer();
  pServer->setCallbacks(new MyServerCallbacks());

  // Create the BLE Service
  BLEService *pService = pServer->createService(SERVICE_UUID);

  // Create a BLE Characteristic
  pCharacteristic = pService->createCharacteristic(
                      CHARACTERISTIC_UUID_TX,
                      BLECharacteristic::PROPERTY_NOTIFY
                    );
                     
  pCharacteristic->addDescriptor(new BLE2902());

  BLECharacteristic *pCharacteristic = pService->createCharacteristic(
                                         CHARACTERISTIC_UUID_RX,
                                         BLECharacteristic::PROPERTY_WRITE
                                       );

  pCharacteristic->setCallbacks(new MyCallbacks());

  // Start the service
  pService->start();

  // Start advertising
  pServer->getAdvertising()->start();
  Serial.println("Waiting a client connection to notify...");
}

void loop() {
  if (deviceConnected) {
    rxValue = pCharacteristic->getValue();
    BLE_CMD = true ;
    if (rxValue =="START")
     {
      for(unsigned long i_1min = 0; i_1min < 1000; i_1min++)
      {
        if(BLE_CMD == true)
        {
   

            ///////////////////End Packet : UART////////////////
            Serial.print("9999EEEEEEEEXXEEEEEEEEXXXX\n");
           
            ///////////////////End Packet : BLE////////////////
            pCharacteristic->setValue("9999EEEEEEEEXXEEEEEEEEXXXX\n");  pCharacteristic->notify();


         
        }

      }



  rxValue = "TEMP_STOP";


    }
 }
  delay(50);
}


I got the attached response on serial monitor and mobile App (using Serial Bluetooth Terminal).

Please check and let me know
 

Offline PlainName

  • Super Contributor
  • ***
  • Posts: 7746
  • Country: va
No, that's changed far too much. The idea is to change just one thing and see if that affects things, and either that will show that it works in principle or will allow you to pinpoint where it is going wrong.

In the original you say that the ADC isn't tripped, so we want to know if that's because of the call to the ADC library (maybe it's not setup right) or is the loop() code never gets to there. So replace the ADC call with something you know MUST work if the code gets there, and then we know where to look more closely.

So, I would suggest you go back to the original code and make only the changes I suggest, then see what happens. The result of that will point to where you need to look at the next step.
 
The following users thanked this post: arul


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf