Bluetooth pairing and sending data

Hello, I'm new to app making and arduino and I have this project that will send data from my esp32 to my app using BLE. I'm trying to learn how this comms work and would be very much be grateful for your help. Here is my current block for the app, but still figuring stuffs out, I cant make my bluetooth device list appear when i press the button. And yes, the main problem is this block somehow right, and be able to send the reading from my light sensor to the app? Thank you very much for your help!

here is also my arduino program

#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#define pinnumber 35
int sensorreading = 0;
int tempSensorReading = 0;
int aveSensorReading = 0;

BLECharacteristic *pCharacteristic;
bool deviceConnected = false;
//int txValue = 0;

#define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E"
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"

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

void setup () {
Serial.begin (9600);
pinMode (pinnumber, INPUT);

//Create the BLE Device
BLEDevice::init ("ESP32");

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

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

//Create BLE Characteristics
pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID_TX,
BLECharacteristic::PROPERTY_NOTIFY
);

//BLE2902 needed to notify
pCharacteristic->addDescriptor(new BLE2902());

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

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

}

void loop() {
if (deviceConnected) {
//txValue = random(-10, 20);
tempSensorReading = 0;
for (int x = 0; x < 50; x++){
tempSensorReading = tempSensorReading + analogRead (pinnumber);
delay(20);
}
sensorreading = tempSensorReading / 50;
// aveSensorReading = tempSensorReading / 10;
// sensorreading = map(aveSensorReading, 10, 4095, 0, 5); //map(lowest reading, max reading, lowest value to display, highest value to display)

  //Conversion of txValue
  char txString[8];
  dtostrf(sensorreading, 5, 2, txString);

  //Setting the value to the characteristic
  pCharacteristic->setValue(txString);

  //Notifying the connected client
  pCharacteristic->notify();
  Serial.println (sensorreading);
}

}

I am working with ESP32 for quite a while, and I can say that, if you can use Bluetooth Classic, use it!
All my projects worked better using Bluetooth Classic, so I don't even try to use BLE anymore.
If you want to give it a try, check this: https://howtomechatronics.com/tutorials/arduino/how-to-build-custom-android-app-for-your-arduino-project-using-mit-app-inventor/

1 Like

Hi again, I've been trying making an app with the esp32, I tried it with some other programs and the icon of esp32 in my phone is bluetooth, and here I tried making my own app and the icon is now of a cellphone. When the icon is bluetooth i can pair it easily but when I try it with my app with the phone icon, it wont pair at all. I cant seem to check for answers on google. help

Mine is shown as a PC and I have no problems. Try to delete it from your cellphone's paired devices list and pair it again.

Hi, I tried the other bt comm, but i wasnt successful when pairing the nodemcu esp32 with the app I made. Can you pls guide me where I did wrong? Thank you

Here is the program I am trying it withcode1 code2

Check if "TimerEnable" of your clock is enabled, it was my first mistake here!
Another crucial point is that the Bluetooth connection is lost everytime you open another screen!
Try doing everything using the same screen!
You can test if your ESP32 is correctly programmed using another terminal app (I use Serial Bluetooth Terminal for some tests, it works perfectly). If not, use this code to check if your module is OK:

#include "BluetoothSerial.h"
BluetoothSerial SerialBT;
void setup() {
  Serial.begin(115200);
  if(!SerialBT.begin("ESP32")){
    Serial.println("An error occurred initializing Bluetooth");
  }
} 
void loop() {
 /*Just echo!*/ 
 while(SerialBT.available()){
    Serial.write(SerialBT.read());
  }
  delay(50);
}

The sensor readings are generally not integer values, how will we combat the issue of sending data and having it displayed in the required form on the app inventor side.

Hello,
for example: if you want three (3) decimal digits in a tramsmitted value, you can:

  1. multiply by 1000 the float value
  2. cast it as integer (or long integer whatever needed)
  3. transmit it
  4. receive it as text on AI2
  5. divide it by 1000 like in the following blocks:

As a general comment to the Arduino code:
possibly, avoid the use of the delay() function, because it blocks the CPU activities.
Better if you do:

#define WANTED_DELAY xxxxx // xxxx = the value in milliseconds to wait
unsigned long timeout = millis(); // read the clock to fix the time from which starts waiting
while ((millis() - timeout) < WANTED_DELAY) ; // do nothing until Wanted_Delay elapses, but CPU is free to do some other interruptive jobs

With the above code the Arduino CPU can do other tasks (for example serial Tx and Rx handling that with the delay() won't work).

2 Likes

Dear @Daryl_Danoy,
I don' t know if you have already solved the issue, but I tell you what I see.

First of all, I suppose that you have changed the type of transmission from BLE to standard BT.
In any case in the BLE the strings for UUID and CHARACTERISTICS cannot be the same. (there is a post by @ChrisWard that has already treated a similar issue).

Then, supposing that your Arduino code is the second one, I see that you wait for a BT incoming character to start the conversion, but who is sendig what ? Your app never sends nothing (unless the blocks you have annexed are not all), So the Arduino code will never start the conversion, neither will send anything.
Then you read the temperature input for 1 second (50*20ms) and you compute the average dividing the sum of values by 50. I suppose that the analogue reading is at max 1024 (10 bits) otherwise sensorreading shall not be "only" an integer but a long integer.
What is the difference between sensorreading and tempsensorreading, since both are referring to the same input pin "pinnumber" ? You don't need to have two variables.for what I see in your code.
When you use the dtostrf() function, the output is txString, but you send on the BT the variable sensorreading, therefore you don't send actually the string, but the float value.
Then, as a last hint, you better use ESP_BT.println(txString); instead of ESP_BT.write(txString); because the BT client in the app works better as follows (and it needs a LF, 0x0A, terminator.

And, as per my previous post, avoid to use delay() but use millis() instead.
Best wishes.

1 Like