Bluetooth no working app

I have a bluetooth code, when I try to use some bluetooth application serialmonitor and type on why it doesn't work to turn on the light but when I type on in serialmonitor arduino ide with baud rate 115200 can it work?
HM-10 Bluetooth

#include <SoftwareSerial.h>
#define LED1Pin 2
SoftwareSerial mySerial(23, 19);

String serialCommand = "";

void setup()  {
  pinMode(LED1Pin, OUTPUT);
  mySerial.begin(9600);   
  Serial.begin(115200);
  delay(100);
  sendCommand("AT");
  sendCommand("AT+NAMEcars");
  sendCommand("AT+PASSb0bc1b62");
}

void sendCommand(const char * command){
  Serial.print("Command send :");
  Serial.println(command);
  mySerial.println(command);
  //delay bentar
  delay(5000);
  
  char reply[5000];
  int i = 0;
  while (mySerial.available()) {
    reply[i] = mySerial.read();
    i += 1;
  }
  //biar string end
  reply[i] = '\0';
  Serial.print(reply); //tulis jawaban dari BLE
  Serial.println("Sukses");
  delay(5000);
}

void loop() {
  if (mySerial.available() > 0) { // Ketik di baud rate bluetooth
    serialCommand = mySerial.readString();
    Serial.println("Received via BT: " + serialCommand);
  }
 
  if (Serial.available() > 0) { // Yang akan muncul di serial monitor
    serialCommand = Serial.readString();
    mySerial.print(serialCommand);
    Serial.println("Sent via Serial: " + serialCommand);
  }

  serialCommand.trim(); // Remove any leading/trailing whitespace

  if (serialCommand == "on") {
    digitalWrite(LED1Pin, HIGH);
    Serial.println("LED ON");
  } else if (serialCommand == "off") {
    digitalWrite(LED1Pin, LOW);
    Serial.println("LED OFF");
  }

  serialCommand = ""; // Clear the command after processing
}

Let's start with some basics

Which Arduino board do you have ?
What voltage does the HM-10 run at ?
How is the HM-10 powered ?

I use esp32, the serial monitor successfully displays AT-Command but when I type on bluetooth it doesn't send a response to turn on the light

Sometimes the code above works and sometimes it doesn't. I don't know what the cause is

I assume that you have checked carefully for loose connections

What exactly do you mean by this. Where exactly are you typing ?

A better explanation of what you are trying to do would be helpful. What is Bluetooth supposed to be talking to? Arduino can talk to serial monitor at 115200 so long as the monitor is also set to that speed, which you apparently have already done. I bet you have no good reason for including AT commands in what appears to be an operational programme. Once you configure Bluetooth, it stays configured until you change it - which your programme doesn't do anyway.

The reason why your serial monitor displays the AT command is simply because it was told to do so. It has nothing to do with sending an AT command to Bluetooth.

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