ESP32 bluetooth app

What android? From android 12 you need to ask for bluetooth_scan and bluetooth_connect permissions. You can take the askForPermision block from the Screen1 block drawer.

Here should be the data received from the arduino:

I use Android 12. Thanks, I'll try it.

The way in which data is received can also cause problems. You should set the DelimiterByte property in the bluetooth component to 10. Then add value -1 to the ReceiveText block to the numbersOfBytes number.

Then in the esp or arduino code, send the data via Serial.print(), and the last value send via Serial.println(). The line break will be detected in the app.

So I upgraded my code acording to your advice, but I don't understand what you mean by

Current code:

I keep getting error: ' Select list item: Attempt to get item number 2 of a list of length1: [""] ', but sending messages to ESP32 works.

ReceiveText.numberOfBytes =-1. Include a math block with a value of -1 there.

Artistic edition :grin::

Also adapt the arduino code as suggested in the post above.

1 Like

Thank you for your help, all is now well. Could you please explain why it has to be -1?

See the documentation for the ReceiveText block. Right-click on it and then click on help.
In short, it receives text until it encounters byte 10 (delimiterByte), which corresponds to the new-line character that the Serial.println() command inserts.

I have another problem. After several tests I found that it takes almost 7 seconds to receive data from ESP32 to phone (receiving data from phone to ESP32 takes only a few ms). Any ideas? I also found that if I press any button in the app after a minute since the last press, nothing happens, the same behavior is when I launch the app. I have to press it twice.
My code:

void Bluetooth(double& pitchKp, double& pitchKi, double& pitchKd) {
  if (SerialBT.available() > 1) {
    switch (SerialBT.read()) {
      case 'A':
        pitchKp += 0.1;
        break;
      case 'B':
        pitchKp -= 0.1;
        break;
      case 'C':
        EEPROM.write(0, pitchKp);
        break;
      case 'D':
        pitchKi += 0.01;
        break;
      case 'E':
        pitchKi -= 0.01;
        break;
      case 'F':
        EEPROM.write(1, pitchKi);
        break;
      case 'G':
        pitchKd += 0.5;
        break;
      case 'H':
        pitchKd -= 0.5;
        break;
      case 'I':
        EEPROM.write(2, pitchKd);
        break;
    }
  }
  String ConsPID = "(" + String(pitchKp) + "|" + String(pitchKi) + "|" + String(pitchKd) + ")";
  Serial.println(ConsPID);
  SerialBT.println(ConsPID);
}

Hi @mart1nekx, probably in your ESP32 code you should change the line
if (SerialBT.available() > 1) into :
if (SerialBT.available()).
This is to avoid that you have to press twice any button (I see that in your app you have "n" buttons, each one is sending a single character for the related command).

PS Just for my knowledge: are you sending flying commands to your drone by means of a BT comm's ? At which distance you can keep the communication working ? Though in open air I'm afraid that the BT cannot go much far :fearful: To this purpose "commercial" drones are using the WiFi instead...

PPSS about the 7 seconds gap between two ESP32 sendings: I see that you send the same data also to the serial monitor of the ESP (I suppose you have your PC attached to the USB) . If that is true, please set the time visible feature of the Monitor, so to check which is the real gap between two sendings.
Once you are done with that, you can verify whether is the ESP32 that takes 7 seconds to transmit or whether it is the app that waits 7 seconds.
Another hint is: how much milliseconds have you set for the Clock1 ? Typically to get the fastest update of any BT incoming I set 10 milliseconds.
Another suggestion (for now the last one :slight_smile: ) is: why you send the parenthesis "()" from the ESP to the app, while you discard them in your app ? Just avoid to send them and you save time on both ESP32 and app sides. Have a great weekend !

1 Like

If you want more range (150m - 900m) to control your drone, you can take a look at the radio frequency.

1 Like

How often do you call Bluetooth functions in Esp32? I think it could also be a buffer full issue in the app. If the data to the app is sent quickly, in a loop without delay, then the app receives a lot of data, which is read at the clock frequency in the app. You may then receive outdated (cached) data for a while. I think that the clock in the app should be set to, for example, 50ms, and in esp you should not send data more often than every 100ms.

That's why I think the function of clearing the BT buffer would be useful. Before reading the data in the loop, you could clear the buffer and then wait for the current data.

However, when it comes to double-clicking, I think the @uskiara advice should help.

1 Like

I made a full arduino powered radio controller and drone, the app is just for easier PID tunning.

5ms

I wanted it to be like a package of data.

Thanks, you too.

Thanks, but I already use radio moudle in my controller.

Every 40ms

That looks like the possible problem. I will add a statement to not send the data unless the values have changed.

Now is everything fine, thanks for help guys!

1 Like

Dear @mart1nekx,
could you please share with us what has been the key point solution for this last issue ?
So to enrich everybody's knowledge.
Thanks a lot and best wishes.

I added a if statement to not send the data unless the values have changed to avoid overflowing the application buffer.

1 Like

Dear @mart1nekx , thanks a lot for your feedback !
It sounds pretty reasonable and a good solution.
Best wishes for your next project !
Kind regards.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.