• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

Bluetooth ESP32 problem

Q&A Forum › Category: ESP32 › Bluetooth ESP32 problem
0 Vote Up Vote Down
Donald Wile asked 3 years ago

99% of this code is from a RANDOM NERD TUTORIAL on the web. The code compiles and runs and I see whatever I send from my tablet appear in the SERIAL window….but when I send “end” the program never detects it. I can’t figure out why? Any help would be appreciated

#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 G_count = 0;
void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32test"); //Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
}
void loop() {
String message = "";
char incomingChar;

if (SerialBT.available())
{
char incomingChar = SerialBT.read();
if (incomingChar != '\n') {
message += String(incomingChar);
}
else {
message = "";
}

Serial.write(incomingChar);

}

if (message == "end")
{
Serial.println("GOT MESSAGE");
Serial.println(message);
delay(1000);
}

}
Question Tags: ESP32 BlueTooth
5 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 3 years ago

Hi.
What does it print in the serial monitor when you send “end”?
If you send other messages ares they printed on the Serial monitor?
Regards,
Sara

0 Vote Up Vote Down
Donald Wile answered 3 years ago

So I figured out the issue. Apparently the following two lines need to be declared prior to the setup() and not in the loop() String message = “”;char incomingChar; That being said, I don\’t understand why?   

0 Vote Up Vote Down
Sara Santos Staff answered 3 years ago

Hi Donald.
With the declaration on the loop(), every time it runs the loop(), the message variable is redeclared as an empty String.
With the declaration on the setup(), the variable keeps its previous value until we attribute a new value.

message += String(incomingChar);

However, I don’t understand why it works in a way and doesn’t work on the other way.
Regards,
Sara

0 Vote Up Vote Down
Donald Wile answered 3 years ago

Hi Sara
Thanks for your help. What you said makes sense. With the bad code, I was able to see each letter coming in “incomingChar” but I didn’t see the letters accumulate to make the “message”. Now I understand why.

0 Vote Up Vote Down
Sara Santos Staff answered 3 years ago

Hi Donald.
You’re right, that’s the reason.
I’ll mark this issue as resolved. If you need further help, you just need to open a new question in our forum.
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] SMART HOME with RPi, ESP32, and ESP8266 (version 1.2) March 8, 2023
  • [eBook Updated] ESP32-CAM Projects (version 1.3) January 7, 2023

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2023 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.