[SOLVED] Esp32 doesn't receive all the data from pc (processing) via bluetooth

Hi everyone!

I am reading data from a sensor (MPU6050) with my ESP32 microprocessor and sending it to my pc via Bluetooth. Processing takes this data, makes calculations, and sends it back to my ESP32. This is illustrated below with the communication protocols/speeds:

Sending data from esp32 to pc works perfect. The problem comes when sending data from pc to esp32, where I get data loss. After sending (from pc to esp32) the first (approx.) 100 values correctly, I notice, when reading the incoming data on my serial monitor (Arduino IDE), that not all the data is being received, and above that, the received data is only received partially. This is illustrated below (the problem begins at data number 115).

Data Number Real data Data received by esp32
1 -3,56489 -3,56489
2 -3,55725 -3,55725
3 -3,1145 -3,1145
4 -3,74809 -3,74809
5 -3,48092 -3,48092
6 -3,29771 -3,29771
7 -3,32061 -3,32061
8 -3,45038 -3,45038
9 -3,61069 -3,61069
10 -3,74046 -3,74046
11 -3,54962 -3,54962
12 -3,58779 -3,58779
13 -3,44275 -3,44275
14 -3,28244 -3,28244
15 -3,38168 -3,38168
16 -3,66412 -3,66412
17 -3,59542 -3,59542
18 -3,54962 -3,54962
19 -3,53435 -3,53435
20 -3,79389 -3,79389
21 -3,38931 -3,38931
22 -3,58015 -3,58015
23 -3,45038 -3,45038
24 -3,62595 -3,62595
25 -3,41985 -3,41985
26 -3,31298 -3,31298
27 -3,25191 -3,25191
28 -3,48855 -3,48855
29 -3,83969 -3,83969
30 -3,74046 -3,74046
31 -3,70229 -3,70229
32 -3,51908 -3,51908
33 -3,39695 -3,39695
34 -3,70992 -3,70992
35 -3,67939 -3,67939
36 -3,44275 -3,44275
37 -3,56489 -3,56489
38 -3,40458 -3,40458
39 -3,53435 -3,53435
40 -3,71756 -3,71756
41 -3,71756 -3,71756
42 -3,65649 -3,65649
43 -3,45038 -3,45038
44 -3,74809 -3,74809
45 -3,64122 -3,64122
46 -3,32061 -3,32061
47 -3,19084 -3,19084
48 -3,57252 -3,57252
49 -3,32061 -3,32061
50 -3,19847 -3,19847
51 -3,48092 -3,48092
52 -3,47328 -3,47328
53 -3,31298 -3,31298
54 -3,49618 -3,49618
55 -3,71756 -3,71756
56 -3,43511 -3,43511
57 -3,19847 -3,19847
58 -3,28244 -3,28244
59 -3,62595 -3,62595
60 -3,56489 -3,56489
61 -3,37405 -3,37405
62 -3,74046 -3,74046
63 -3,61069 -3,61069
64 -3,52672 -3,52672
65 -3,74046 -3,74046
66 -3,53435 -3,53435
67 -3,51908 -3,51908
68 -3,74046 -3,74046
69 -3,9313 -3,9313
70 -3,54198 -3,54198
71 -3,38931 -3,38931
72 -3,41985 -3,41985
73 -3,41221 -3,41221
74 -3,56489 -3,56489
75 -3,28244 -3,28244
76 -3,44275 -3,44275
77 -3,31298 -3,31298
78 -3,8626 -3,8626
79 -3,69466 -3,69466
80 -3,48855 -3,48855
81 -3,41985 -3,41985
82 -3,1145 -3,1145
83 -3,51145 -3,51145
84 -3,69466 -3,69466
85 -3,57252 -3,57252
86 -3,32061 -3,32061
87 -3,29771 -3,29771
88 -3,62595 -3,62595
89 -3,30534 -3,30534
90 -3,46565 -3,46565
91 -3,66412 -3,66412
92 -3,35115 -3,35115
93 -3,67939 -3,67939
94 -3,35115 -3,35115
95 -3,45038 -3,45038
96 -3,41985 -3,41985
97 -3,33588 -3,33588
98 -3,67176 -3,67176
99 -3,45038 -3,45038
100 -3,62595 -3,62595
101 -3,75573 -3,75573
102 -3,30534 -3,30534
103 -3,67176 -3,67176
104 -3,32824 -3,32824
105 -3,35115 -3,35115
106 -3,69466 -3,69466
107 -3,54962 -3,54962
108 -3,45038 -3,45038
109 -3,77863 -3,77863
110 -3,39695 -3,39695
111 -3,48092 -3,48092
112 -3,33588 -3,33588
113 -3,14504 -3,14504
114 -3,58779 -3,58779
115 -3,78626 -3,7
116 -3,57252 -3,572
117 -3,45038 -3,45
118 -3,54962 -3,54962
119 -3,35115 -3,35114
120 -3,67176 -3
121 -3,29771 -3,29
122 -3,75573 -3,2519
123 -3,71756 -3
124 -3,58015 -3,7
125 -3,25191 -3,35
126 -3,31298 -3,2
127 -3,72519 -3,618
128 -3,35115 -3,37
129 -3,21374 -3,41
130 -3,61832 -3,71
131 -3,37405 -3,6
132 -3,41221 -3,351
133 -3,71756 -3,85
134 -3,64122 -3,8
135 -3,35115 -3,61
136 -3,85496 -3,4
137 -3,80153 -3,68702
138 -3,61832 -3,94657
139 -3,44275 -3,4
140 -3,68702 -3,29
141 -3,94657 -3,56
142 -3,40458 -3,2
143 -3,29008 -3,45038
144 -3,56489 -3,5
145 -3,27481 -3,3
146 -3,45038 -3,3
147 -3,59542 -3,54
148 -3,38931 -3,51
149 -3,36641 -3,328
150 -3,54962 -3,74
151 -3,51908 -3,58
152 -3,32824 -3,58
153 -3,74046 -3,35
154 -3,58779 -3,29008
155 -3,58779 -3,66412
156 -3,35878 -3,4
157 -3,29008 -3,34
158 -3,66412 -3,6
159 -3,41985 -3,52
160 -3,34351 -3,6
161 -3,68702 -3,64
162 -3,52672 -3,43
163 -3,62595 -3,435
164 -3,64886 -3,64
165 -3,43511 -3,54
166 -3,43511 -3,74
167 -3,64886 -3,32
168 -3,54198 -3,54
169 -3,74046 -3,74
170 -3,32061 -3,41985
171 -3,54962 -3,62595
172 -3,74046 -3,396
173 -3,41985 -3
174 -3,62595 -3,595
175 -3,39695 -3,58015
176 -3,8855 -
177 -3,59542 -3,65
178 -3,58015 -3,19084
179 -3,51908 -3,42
180 -3,35115 -
181 -3,44275 -3,5
182 -3,65649 -3,2
183 -3,19084 -3,71
184 -3,42748 -3,26718
185 -3,76336 -3,79389
186 -3,41985 -3,1
187 -3,52672 -3,5
188 -3,22137 -3,6
189 -3,71756 -3,45
190 -3,26718 -3,4
191 -3,79389 -3,709
192 -3,80916 -3,305
193 -3,12214 -3,5
194 -3,58015 -3,55
195 -3,60305 -3,45
196 -3,45802 -3,84
197 -3,45802 -3,79
198 -3,70992 -3,41
199 -3,30534 -3,52672
200 -3,50382 -3,73
201 -3,55725 -3,61832
202 -3,45038 -
203 -3,84733 -3
204 -3,79389 -3,442
205 -3,41221 -3,3
206 -3,52672 -3,29
207 -3,73282 -3,16
208 -3,61832 -3,66
209 -3,51145 -3,3
210 -3,74046 -3,38
211 -3,44275 -3,22
212 -3,32824 -3,72
213 -3,29771 -3,396
214 -3,16031 -3,3
215 -3,66412 -3,29
216 -3,30534 -3,35
217 -3,38931 -3,6
218 -3,22901 -3,72519
219 -3,72519 -3,68702
220 -3,39695 -3,63359
221 -3,37405 -3,42
222 -3,29771 -3,7
223 -3,35115 -3,59
224 -3,60305 -3,71
225 -3,72519 -3,47
226 -3,68702 -3,46
227 -3,63359 -3,52
228 -3,42748 -3,38168
229 -3,72519 -3,5
230 -3,59542 -3,6
231 -3,71756 -3,2
232 -3,47328 -3,29
233 -3,46565 -3,83
234 -3,52672 -3,26718
235 -3,38168 -3,41221
236 -3,59542 -3
237 -3,62595 -3,41985
238 -3,29771 -3,4
239 -3,29771 -
240 -3,83206 -3
241 -3,26718 -3,8
242 -3,41221 -3,74
243 -3,70229 -3,63
244 -3,61832 -3,27
245 -3,41985 -3,84
246 -3,45802 -3,53
247 -3,36641 -3,725
248 -3,48092 -3,5
249 -3,38168 -3,38
250 -3,80153 -3,78626
251 -3,74046 -3,74046
252 -3,63359 -3,4
253 -3,27481 -3,4
254 -3,84733 -3,5
255 -3,53435 -3,65
256 -3,72519 -3,43511
257 -3,58015 -3,7
258 -3,38168 -3,7
259 -3,78626 -3,3
260 -3,74046 -3,35
261 -3,47328 -3,5
262 -3,40458 -3,465
263 -3,59542 -3,64
264 -3,65649 -3,27
265 -3,43511 -3,57
266 -3,77099 -3,503
267 -3,70992 -3,23
268 -3,36641 -3,26718
269 -3,35878 -3,1984
270 -3,53435 -3
271 -3,46565 -3,55
272 -3,64886 -3,5
273 -3,27481 -3,496
274 -3,57252 -3,5
275 -3,50382 -3,641
276 -3,23664 -3,56
277 -3,26718 -3,24
278 -3,19847 -3,61
279 -3,77099 -3,34
280 -3,55725 -3,4
281 -3,51908 -3,58
282 -3,49618 -3,32
283 -3,57252 -3,23
284 -3,64122 -3,358
285 -3,56489 -3,72519
286 -3,24427 -3,5725
287 -3,61832 -3
288 -3,34351 -3
289 -3,40458 -3,27
290 -3,58015 -3,68
291 -3,32824 -3,541
292 -3,23664 -3,396
293 -3,35878 -3,4
294 -3,72519 -3,27481
295 -3,57252 -3,5954
296 -3,32061 -3,77
297 -3,42748 -3
298 -3,27481 -3,48092
299 -3,68702 -3,229
300 -3,54198 -3,5
301 -3,39695 -3,2
302 -3,44275 -3,6
303 -3,27481 -3,3
304 -3,59542 -3,969
305 -3,77099 -3,3
306 -3,65649 -3,33
307 -3,39695 -3,76
308 -3,74809 -3,29
309 -3,48092 -3,58
310 -3,22901 -3,65
311 -3,59542
312 -3,58779
313 -3,21374
314 -3,60305
315 -3,33588
316 -3,96947
317 -3,30534
318 -3,33588
319 -3,76336
320 -3,29008
321 -3,58015
322 -3,65649

As can be seen, only 310 from the total of 322 data are received by the esp32, and a lot of data is received partially, for example data number 115, where “-3.7” is received instead of “3,78626”.

This is the arduino code:

#include <Wire.h>
#include "BluetoothSerial.h"

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

BluetoothSerial SerialBT;


int transient_delay = 3000;

const byte MPU6050_address = 0x68;
const byte PWR_MGMT_1 = 0x6B;
const byte ACCEL_XOUT_H = 0x3B;
const byte GYRO_XOUT_H = 0x43;

int16_t gyro_read[3], *gyro_stored[3];  
int16_t acc_read[3], *acc_stored[3];



//------------------------------------------------------------------------------------
char message;

void setup() {
  Wire.begin(21, 22, 400000);
  Wire.beginTransmission(MPU6050_address);
  Wire.write(PWR_MGMT_1);
  Wire.write(0x00);
  Wire.endTransmission(true);
  Serial.begin(115200);
  SerialBT.begin("ESP32test"); //Bluetooth device name

  //time = millis();
  while (millis() <= transient_delay) {
    readSensor();
  }
}


void I2C_transmission(const int address1, const int address2, int K) {
  Wire.beginTransmission(address1);
  Wire.write(address2);
  Wire.endTransmission(false);
  Wire.requestFrom(address1, K, true);
}

void readSensor() {
  readAcc();
  readGyro();
}

void readAcc() {
  I2C_transmission(MPU6050_address, ACCEL_XOUT_H, 6);
  for (byte z = 0; z < 3; z++) {
    acc_read[z] = Wire.read() << 8 | Wire.read();
    if (acc_read[z] < -32768 * 0.95 || acc_read[z] > 32767 * 0.95) { //overflow controle
      acc_read[z] = 0;
    }
  }
}

void readGyro() {
  I2C_transmission(MPU6050_address, GYRO_XOUT_H, 6);
  for (byte z = 0; z < 3; z++) {
    gyro_read[z] = Wire.read() << 8 | Wire.read();
    if (gyro_read[z] < -32768 * 0.95 || gyro_read[z] > 32767 * 0.95) { //overflow controle
      gyro_read[z] = 0;
    }
  }
}

void printResults() {
  SerialBT.print(gyro_read[0]);  
  SerialBT.print("\t");             
  SerialBT.print(gyro_read[1]);  
  SerialBT.print("\t");             
  SerialBT.print(gyro_read[2]);  
  SerialBT.print("\t");             
  SerialBT.print(gyro_read[0]);   
  SerialBT.print("\t");            
  SerialBT.print(gyro_read[1]);   
  SerialBT.print("\t");            
  SerialBT.print(gyro_read[2]);   
  SerialBT.println();
}

void loop() {
  readSensor();
  printResults();
  if (SerialBT.available() > 0) {
    message = SerialBT.read();
    Serial.println(message);
  }
}

Reading the data from my serial monitor happens with:

Serial.println(message);

I don’t know whether the problem is with the Arduino IDE part of my project, or the processing part. That’s why I am asking on this forum.

Thanks in advance!

Wireless communications are inherently unreliable.

If you want to make sure that data are received reliably, you must have a protocol that detects missing or erroneous data, and that allows you to request that transmission of those data be repeated.

Thanks for your reply @jremington.

I understand your point, but I am wondering why, in that case, I don't find any data loss at all sending data via bluetooth from esp32 to pc, and when sending data from pc to esp32, there's a lot of data loss.

The communication works in the beginning but then the trouble starts.
Please post schematics, especially showing the powering.
Check the temperature for the different devices. Check the components on the boards. Anyone getting a bit warm?

Thanks for your reply @Railroader, but the issue had to do with increasing the RX queue size and not temperature.

[SOLUTION]

I found the solution on the internet: increasing the RX queue size.

To solve the problem you have to open the BluetoothSerial.cpp file. You can find it on the following path:

"C:\Users\username\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.6\libraries\BluetoothSerial\src\BluetoothSerial.cpp"

You open the file with notepad and change 512 to 2048:

From
#define RX_QUEUE_SIZE 512
to
#define RX_QUEUE_SIZE 2048

That's all.

[UPDATE]

This modification solved my problem, but led to another problem: It only worked for very low baud rates (up to 1200). The solution for this new problem is to change the

if(SerialBT.available() > 0) 
{
...
  }

to

while(SerialBT.available() > 0) {
..
  }

By doing this modification, I can now use very high baud rates.

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