ESP32 doesn't respond properly when get command from Mobile App through BLE mode - Page 1
Forum section: electronics / general

author: arul
author_rank: Regular Contributor
timestamp: March 17, 2022, 05:56:14 am
content: 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:#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;int ADC_Buffer_35;int ADC_34_min, ADC_34_max, IO_34_min, IO_34_max;int ADC_35_min, ADC_35_max, 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 loopfloat AVERAGE(int arr[], int n){int i;int sum=0;float avg;for(i=0; i<n; i++){sum = sum + arr ;}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);}Serial.println();Serial.println("*********");}}};void setup() {Serial.begin(115200);// pinMode(LED, OUTPUT);// Create the BLE DeviceBLEDevice::init("ESP32 UART Test"); // Give it a name// Create the BLE ServerBLEServer *pServer = BLEDevice::createServer();pServer->setCallbacks(new MyServerCallbacks());// Create the BLE ServiceBLEService *pService = pServer->createService(SERVICE_UUID);// Create a BLE CharacteristicpCharacteristic = 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 servicepService->start();// Start advertisingpServer->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 = analogRead(IO34_ADC);ADC_Buffer_35 = 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;char tx2String;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.

author: PlainName
author_rank: Super Contributor
timestamp: March 17, 2022, 09:09:06 am
content: 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).

author: arul
author_rank: Regular Contributor
timestamp: March 17, 2022, 02:23:21 pm
content: Hi @ dunkemhigh,I modified code as follow asCode:#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;int ADC_Buffer_35;int ADC_34_min, ADC_34_max, IO_34_min, IO_34_max;int ADC_35_min, ADC_35_max, 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 loopfloat AVERAGE(int arr[], int n){int i;int sum=0;float avg;for(i=0; i<n; i++){sum = sum + arr ;}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);}Serial.println();Serial.println("*********");}}};void setup() {Serial.begin(115200);// pinMode(LED, OUTPUT);// Create the BLE DeviceBLEDevice::init("ESP32 UART Test"); // Give it a name// Create the BLE ServerBLEServer *pServer = BLEDevice::createServer();pServer->setCallbacks(new MyServerCallbacks());// Create the BLE ServiceBLEService *pService = pServer->createService(SERVICE_UUID);// Create a BLE CharacteristicpCharacteristic = 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 servicepService->start();// Start advertisingpServer->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

author: PlainName
author_rank: Super Contributor
timestamp: March 17, 2022, 04:52:46 pm
content: 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.