Unable to send text to esp32 by bluetoothclient

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

I have added a picture of codeblocks I am using

Show us the sketch?

the one uploaded in esp32?

Yes.

This way we can confirm that data types line up, and that there is consistency in the technique used to delimit messages.

#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 
    }
  }
}

Data types look good:

#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?

my bad. it was just result of some recent changes

what about this:

#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)

The other BT app probably is receiving data in addition to letting you send data.

I see your sketch sending to serialBT, but I do not see anywhere in your blocks where you ask to receive BT data.

Yeah but I don't think it should cause any issue because the data received is just mere information like

Recieved:1
Motor On:

It's just information that is letting us know that motor has been turned on.

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🤔

it started working after i made some recent edits in codeblocks

i also added a label to display the received text. though it dont actually work like serial moniter but still it is fine for now

the problm is that to update the label i need to press a button twice.

for e.g. when i press on, the motor does turn on but label doesnt update but when i press the same button the second time,it does update

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