hello I am making an app for my esp32 project. To check if the app is able to send esp32 text message by bluetoothclient. I connected motor to esp32 to turn on when it recieves 1 on bluetooth serial. in the app, I added button for on and off that sends 1 and 0 respectively through bluetooth but when I tested it, it is not working as it should be. i checked if there was a problem in esp32 side by using another bluetooth terminal app but it was working fine
#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 received;// received value will be stored in this variable
char receivedChar;// received value will be stored as CHAR in this variable
const char turnON ='1';
const char turnOFF ='0';
const int MOTORpin = 23;
void setup() {
Serial.begin(9600);
SerialBT.begin("bluetooth test"); //Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
Serial.println("To turn ON send: 1");//print on serial monitor
Serial.println("To turn OFF send: 0"); //print on serial monitor
pinMode(MOTORpin, OUTPUT);
}
void loop() {
receivedChar =(char)SerialBT.read();
if (Serial.available()) {
SerialBT.write(Serial.read());
SerialBT.print("Received:");// write on BT app
SerialBT.println(receivedChar);// write on BT app
Serial.print ("Received:");//print on serial monitor
Serial.println(receivedChar);//print on serial monitor
//SerialBT.println(receivedChar);//print on the app
//.write(receivedChar); //print on serial monitor
if(receivedChar == turnON)
{
SerialBT.println("MOTOR ON:");// write on BT app
Serial.println("MOTOR ON:");//write on serial monitorSerialBT
digitalWrite(MOTORpin, HIGH);// turn the LED ON
}
if(receivedChar == turnOFF)
{
SerialBT.println("MOTOR OFF:");// write on BT app
Serial.println("MOTOR OFF:");//write on serial monitor
digitalWrite(MOTORpin, LOW);// turn the LED off
}
}
}
#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 received;// received value will be stored in this variable
char receivedChar;// received value will be stored as CHAR in this variable
const char turnON ='1';
const char turnOFF ='0';
const int MOTORpin = 23;
I've wondered why some BlueTooth serial.begin calls send a comment instead of a baud rate. Not saying it's wrong, just curious.
void setup() {
Serial.begin(9600);
SerialBT.begin("bluetooth test"); //Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
Serial.println("To turn ON send: 1");//print on serial monitor
Serial.println("To turn OFF send: 0"); //print on serial monitor
pinMode(MOTORpin, OUTPUT);
}
Now this is fishy.
You've got two serial data streams, one for the console and the other for BlueTooth.
In your main loop, you ask SerialBT for input without first testing if there is any available from the BlueTooth data stream.
void loop() {
receivedChar =(char)SerialBT.read();
if (Serial.available()) {
SerialBT.write(Serial.read());
SerialBT.print("Received:");// write on BT app
SerialBT.println(receivedChar);// write on BT app
Serial.print ("Received:");//print on serial monitor
Serial.println(receivedChar);//print on serial monitor
//SerialBT.println(receivedChar);//print on the app
//.write(receivedChar); //print on serial monitor
if(receivedChar == turnON)
{
SerialBT.println("MOTOR ON:");// write on BT app
Serial.println("MOTOR ON:");//write on serial monitorSerialBT
digitalWrite(MOTORpin, HIGH);// turn the LED ON
}
if(receivedChar == turnOFF)
{
SerialBT.println("MOTOR OFF:");// write on BT app
Serial.println("MOTOR OFF:");//write on serial monitor
digitalWrite(MOTORpin, LOW);// turn the LED off
}
}
}
That looks like a show stopper to me.
Then you look for input from your serial console?
I thought the app was supposed to receive BT data and echo it to the serial console and use it to drive motors?
#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 received;// received value will be stored in this variable
char receivedChar;// received value will be stored as CHAR in this variable
const char turnON ='1';
const char turnOFF ='0';
const int MOTORpin = 23;
void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32"); //Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
Serial.println("To turn ON send: 1");//print on serial monitor
Serial.println("To turn OFF send: 0"); //print on serial monitor
pinMode(MOTORpin, OUTPUT);
}
void loop() {
receivedChar =(char)SerialBT.read();
if (Serial.available()) {
SerialBT.write(Serial.read());
}
if (SerialBT.available()) {
SerialBT.print("Received:");// write on BT app
SerialBT.println(receivedChar);// write on BT app
Serial.print ("Received:");//print on serial monitor
Serial.println(receivedChar);//print on serial monitor
//SerialBT.println(receivedChar);//print on the app
//SerialBT.write(receivedChar); //print on serial monitor
if(receivedChar == turnON)
{
SerialBT.println("MOTOR ON:");// write on BT app
Serial.println("MOTOR ON:");//write on serial monitor
digitalWrite(MOTORpin, HIGH);// turn the MOTOR ON
}
if(receivedChar == turnOFF)
{
SerialBT.println("MOTOR OFF:");// write on BT app
Serial.println("MOTOR OFF:");//write on serial monitor
digitalWrite(MOTORpin, LOW);// turn the MOTOR off
}
}
delay(20);
}
it still didnt work with my app though(worked with other bt app)
Also I don't know how can I make bluetooth serial moniter on MIT app inventor nor I could find any tutorial to do this. So I don't think I can recieve and display data on MIT app inventor unless I make it🤔