I'm currently trying to send data from esp32 to my android app that I made. The sensor readings are showing in the serial monitor, however, what's showing on my app are either @ or &. Please help and thank you. I'm new to bluetooth coding.

Here's the code:

#include "BluetoothSerial.h" 
int sensorPin = 35;  
int sensorreading;

BluetoothSerial SerialBT;

void  setup ()  
    pinMode (sensorPin, INPUT);
void  loop ()
   sensorreading = analogRead(sensorPin); 
   String toSend = String (sensorreading);

void sendData(String textToSend){

You see very different output via bluetooth and Serial Monitor, because you are sending completely different things. On the Serial Monitor you see the sensor reading, since that is exactly, what you send:


The print() function will convert the integer variable sensorreading into human readable text (meaning ASCII encoded text) and send this over Serial.

But then you do this:


textToSend is a String object here. So with the parentheses [ ] you are taking one character of that string. You try to use the sensorreadingth character. Since sensorreading comes from an analogRead(), it is most likely, that is value is rather big, meaning bigger than the length of the string. So you are basically reading some random memory, that happens to lie behind your string variable. The write() function will then send that random data out over your bluetooth serial interface, without any conversion.

I guess you want to transmit the data over bluetooth also in human readable form. So just use the same code as with the Serial interface (for the Serial Monitor).

And remove all that manual conversion of the measured analog value to a String. That is not needed. If you want to also display some text, you can just use multiple print() commands, like this:

Serial.print("Value: ")
Serial.print(" units");

Note, that it is rather bad to use the String object at all. The String class uses dynamic memory allocation, which leads to memory fragmentation, which can eat up your available memory. This is not as important on chips with rather big memory (as the ESP32) than on chips with smaller memory (like the Atmega328p, which is used in Arduino Uno and Nano), but you should still keep that in mind. Instead of the String class you would work with char arrays, aka C-strings.

| improve this answer | |
  • thank you sir. i tried a different approach using serialbt.write(sensorreading); instead of SerialBT.write(textToSend[sensorreading]); i get values that displays ranges from 2300 to 2400 on my serial monitor. When I try to connect it to another app, serial terminal bluetooth app which i downloaded on playstore. in the app it just displays random letters this time. – Dan Young Aug 28 at 14:48
  • this is my current code: #include "BluetoothSerial.h" int sensorPin = 35; int sensorreading = 0; BluetoothSerial SerialBT; void setup () { Serial.begin(115200); SerialBT.begin("ESP32_ABO"); pinMode (sensorPin, INPUT); } void loop () { sensorreading = analogRead(sensorPin); Serial.print(sensorreading); Serial.println("mV"); SerialBT.write(sensorreading); delay(1000);} sorry i'm really new to arduino – Dan Young Aug 28 at 14:51
  • thank you so much for your help sir! what is was lacking was a serialBT.print(sensorreading); to display it to the device app thnk you very much for your help – Dan Young Aug 28 at 15:22
  • Thats great. As mentioned in the answer, SoftwareSerial.write() will send out the data untouched, meaning, that you send that data as binary (just the binary representation of the number). SoftwareSerial.print() instead creates an ASCII encoded representation of the number, which is human readable text. The app on your phone just receives the data from serial and interprets it as ASCII encoded text. So you get some random characters, when sending binary data. – chrisl Aug 28 at 15:44
  • If that answered your question, you can accept my answer as correct – chrisl Aug 28 at 15:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.