I've been stuck on this problem for many days and I'm just not sure why I dont see anything in the serial monitor except for the messages in setup(). What could I be doing wrong?
Here is my code:
ESP32:
#include <BluetoothSerial.h>
BluetoothSerial SerialBT;
void setup() {
// Serial.begin(9600);
delay(30000);
SerialBT.begin("ESP32"); // Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
}
void loop() {
if (SerialBT.available()) {
char data = SerialBT.read();
Serial.print("Received from Arduino: ");
Serial.println(data);
}
if (Serial.available()) {
char data = Serial.read();
SerialBT.print(data);
}
}
Arduino Uno:
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(13, 12); // RX, TX pins of HC-05
void setup() {
Serial.begin(9600);
BTSerial.begin(9600);
}
void loop() {
if (BTSerial.available()) {
char data = BTSerial.read();
Serial.print("Received from ESP32: ");
Serial.println(data);
}
if (Serial.available()) {
char data = Serial.read();
BTSerial.print(data);
}
}
Also, ideally the ESP32 should be powered by battery and disconnected from my computer. Right now I'm assuming a computer is required to see the ESP32 in the list of bluetooth devices. How do I make it completely independent?
Yes I want to use bluetooth modules to send data between ESP32 and UNO. The ESP32 controls a servo but it should only do so once it gets the signal from the UNO. Once the servo has reached its goal position, the ESP32 should send a confirmation message back to the UNO.
It looks like task 12.1 in the pdf you have attached somewhat does this (I have only skimmed it). But can you explain the need for the android phone? I only want communication between the two boards. Is the phone necessary?
I have referred you to Task-12.3 and NOT Task-12.1.
Anyway, you can use the BT Terminal of your Android Phone to communicate with BT of ESP32 for onward control of Servo Motor and to receive message from ESP32.
My project does not include a phone in the final. Regardless, I am following Task 12.3.
I am able to use the AT Commands with the HC-05 module and set the Arduino Uno as a master. But the AT commands are not working with the ESP32 board. I tried multiple baud rates too, including 9600, 38400, 115200, and 921600. For reference, I am using the Servo Driver with ESP32 from Wavehshare: https://www.waveshare.com/wiki/Servo_Driver_with_ESP32#ESP32_Flash_Download_Tool
Is there a different way to set up AT commands on an ESP32 board?
Yes I've seen and tried this tutorial many times. But the problem is that this tutorial interfaces with a phone. It's a bluetooth serial bus between the Android and the ESP32. I am looking for a BT serial bus between the HC-05 module and ESP32.
I would plug in both boards to my computer and look for the ESP32 in the list of BT devices on my computer. My computer would connect but the connection was very unstable which is why it was rare to see any messages in the HC-05 SM. But it did happen.
Earlier I was told to put the ESP32 board into master/slave mode but I am unable to do that since AT commands are not being responsive. Does anyone have experience configuring the ESP32 as a slave board? or any other advice on creating a more secure BT connection between the two boards?
I had been doing this earlier but thought that both master and slave had to be ESP32 boards since the BluetoothSerial library is not compatible on the Uno. But, I used the BT master code on the ESP32 and the SoftwareSerial library on the uno and the boards connected! Note that the boards have to be extremely close to one another.
For reference, I uploaded the SerialToSerialBTM example on to the ESP32 (changed baud rate to 9600) and the following code (from @GolamMostafa) for the Uno:
#include <SoftwareSerial.h>
SoftwareSerial SUART(13, 12); // SRX = 10, STX = 11
void setup()
{
Serial.begin(9600);
SUART.begin(9600);
}
void loop() {
if (SUART.available())
{
char x = SUART.read();
Serial.print(x);
}
if (Serial.available()) {
char c = Serial.read();
SUART.print(c);
}
}
While there is connection, the data is gibberish. I opened both SM at a baud rate of 9600 and have NL & CR on. Why is the data incorrect?
Procedures to operate built-in Classic Bluetooth Module of 30-pin ESP32 Dev Module as a Slave Device from Bluetooth Device (Master) of Android Phone: 1. Upload the following sketch into ESP32.
#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;
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()
{
if (Serial.available())
{
SerialBT.write(Serial.read());
}
if (SerialBT.available())
{
Serial.write(SerialBT.read());
}
delay(20);
}
2. Open Serial Monitor of ESP32 at Bd = 115200. 3. Open BT Terminal in your Android Phone. 4. Execute "Scan" to detect the presence of the BT Module of ESP32 as "ESP32test". 5. Pair the two BT Modules. 6. Send 'A' from the ASCII-Mode BT Terminal of Andriod Phoe. 7. Check that 'A' has appeared on the OutputBox of Serial Monitor. 8. Send "OK" from the InputBox of Serial Monitor. 9. Check that "OK" has appeared on the BT Terminal of Android Phone.
InputBox of Serial MOnitor.
10. Check that "OK" has appeared on the BT Terminal of Android Phone
Borrow an Android phone, or buy a cheap pay as you go Android phone.
You have to break down your trouble shooting debug actions into small pieces, and confirming that the HC05 is working with a phone is step number 1. It may be configured wrong from previous AT mode messing about or it may be defective.
Thank you for the advice! I wanted to update that before I tested with the phone I checked the baud rate configured on the HC-05 module using the AT commands (AT+UART?).
This is the baud rate I had to set up for both the arduino uno and the ESP32. Once I set this set up, I was able to send messages between the two!
So to reiterate:
Run the softwareSerial code I posted earlier on the uno
Enter AT command mode and get the baud rate of the hc-05 module. Also ensure the hc-05 is in slave mode.
Run the SerialToSerialBTM code from the BluetoothSerial example files. Set up parameters according to your set up (hc05 name, hc05 Mac address, baud rate found in step 2).
Set up serial monitor for both boards to NL & CR with your specified baud rate.
Exit command mode on the hc-05 by disconnecting the enable pin
Hello, I am currently using this thread as a guide for connecting my esp32 to an hc05 bluetooth module but am having some trouble and think that I have an idea of where the trouble might lie.
In this snippet from the SerialToSerialBTM code, the mac address is defined as so:
#else
String MACadd = "AA:BB:CC:11:22:33"; // This only for printing uint8_t address[6] = {0xAA, 0xBB, 0xCC, 0x11, 0x22, 0x33}; // Change this to reflect real MAC address of your slave BT device #endif
There are 6 items in the address array as a mac address should be 12 hex digits. When I retrieve the mac address of the hc05 using AT commands (specifically AT+ADDR?) I am given an address of only 8 hex digits. Everything online says that these addresses are supposed to be 12 digits and I am confused as to why I don't receive one of that length.
I attempted to ignore my suspicions and define like so (except with the mac address retrieved from my bluetooth module):
uint8_t address[4] = {0xAA, 0xBB, 0xCC, 0x11}; //size now 4
Other than that, i follow your process step by step and at the end, it is printed to the screen that an attempt to connect to the device is being made, and then it fails. I was wondering how you went about this address formatting issue if you had it.
Also, do any other default settings of the hc05 aside from changing the master mode to slave mode need to be changed by AT commands? AT+CMOD was one I was considering, as it defines whether the module is open to connect to any device or only one specific device.
When connected to an android device, we received the address of the hc-05 to be 00:14:03:05:0c:38. The address of our esp32 is A8:42:E3:D8:95:E2. The HC-05 is configured with the following AT commands:
-ROLE: 0
-CMODE: 1
-PSWRD: 1234
-UART: 38400,0,0
With the HC-05 in slave mode, and the esp32 in master mode, we uploaded the SoftwareSerial code below to the HC-05 and the BluetoothSerial code below to the esp32. After the slave code is uploaded to the Arduino with the HC-05, the module blinks at a slow rate of once per second. We are able to connect to the HC-05 with a phone or pc and we are also able to connect to the ESP32 with a phone or pc, but the esp32 won't connect to the HC-05 directly with the uploaded code. We are not sure how to proceed.
HC-05 Slave Code:
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(2,3); // RX, TX pins of HC-05
void setup() {
Serial.begin(38400);
BTSerial.begin(38400);
}
void loop() {
if (BTSerial.available()) {
char data = BTSerial.read();
Serial.print("Received from ESP32: ");
Serial.println(data);
}
if (Serial.available()) {
char data = Serial.read();
BTSerial.print(data);
}
}
ESP32 Master Code:
#include "BluetoothSerial.h"
#define Slave_Name "H-C-2010-06-01"
#define MACadd "00:14:03:05:0C:38"
uint8_t address[6] {0x00, 0x14, 0x03, 0x05, 0x0C, 0x38};
const char *pin = "1234";
#if !defined(CONFIG_BT_SPP_ENABLED)
#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
#endif
BluetoothSerial SerialBT;
String myName = "ESP32-BT-Master";
void setup() {
bool connected;
Serial.begin(38400);
SerialBT.begin(myName, true);
Serial.print("The device started in master mode, make sure slave BT device is on!\n");
SerialBT.setPin(pin);
Serial.println("Using PIN");
connected = SerialBT.connect(address);
Serial.print("Connecting to slave BT device with MAC ");
Serial.println(MACadd);
if(connected) {
Serial.println("Connected Successfully!");
} else {
while(!SerialBT.connected(10000)) {
Serial.println("Failed to connect. Make sure remote device is available and in range, then restart app.");
}
}
// Disconnect() may take up to 10 secs max
if (SerialBT.disconnect()) {
Serial.println("Disconnected Successfully!");
}
// This would reconnect to the slaveName(will use address, if resolved) or address used with connect(slaveName/address).
SerialBT.connect();
if(connected) {
Serial.println("Reconnected Successfully!");
} else {
while(!SerialBT.connected(10000)) {
Serial.println("Failed to reconnect. Make sure remote device is available and in range, then restart app.");
}
}
}
void loop() {
if (Serial.available()) {
SerialBT.write(Serial.read());
}
if (SerialBT.available()) {
Serial.write(SerialBT.read());
}
delay(20);
}
I will leave this code up for anyone who may need this in the future. The issue seemed to be the fast blinking, I thought it was blinking fast enough but then after resetting the module it started blinking very fast. Then, I reset both modules that used these two codes and it worked. Thank you so much for the help!