How can i know when the Bluetooth is connected through code to make it light an LED

hello everyone.
i have a small problem i need to make an led light up when the bluetooth connection is successful. iam calling the app_data() function from the void loop and when i connect to Bluetooth data is automatically displayed on the app but if just tried to light up the LED directly from the app_data() function it will always be on. how can i solve this issue.
i tried using .avalible function but its not working for me.

Void app_data()
{
  
  if(Bluetooth.available()){

analogWrite(7,125);
  }
  else
  {
analogWrite(7,0);
  }
Bluetooth.print(G_Tilt);// tilt angle
   Bluetooth.print(",");
  Bluetooth.print(G_OA);// optimal angle
   Bluetooth.print(",");
  Bluetooth.print(G_LAT);// latitude
  Bluetooth.print(",");
  Bluetooth.print(G_PVtemp); //PV Temp
  Bluetooth.print(",");
  Bluetooth.print(G_VOLT);// volts
  Bluetooth.print(",");
  Bluetooth.print(G_AMP); //amps
   Bluetooth.print(",");
   Bluetooth.print(G_Power); // power
  Bluetooth.print(",");
   Bluetooth.print(G_drybulb); //dry bulb temp
   Bluetooth.print(",");
  Bluetooth.print(G_wetbulb); // wet bubl temp
   Bluetooth.print(",");
   Bluetooth.print(G_RH*100); // reltive humidity
   Bluetooth.print(",");
   Bluetooth.print(G_pres/1000); //atm pressure
   Bluetooth.print(",");
   Bluetooth.print(G_pws/1000); // sat pressure
   Bluetooth.print(",");
   Bluetooth.print(G_PW/1000); //partial pressure
   Bluetooth.print(",");
   Bluetooth.print(G_W,5); // humidty ratio 
   Bluetooth.print(",");
   Bluetooth.print(G_v); // specific voulme 
   Bluetooth.print(",");
   Bluetooth.print(G_den); //Air density
   Bluetooth.print(",");
   Bluetooth.print(G_Tdew); //dew temp
   Bluetooth.print(",");
   Bluetooth.print(G_LAT); // latitude
   Bluetooth.print(",");
   Bluetooth.print(G_LONG); //lognitude
     Bluetooth.print(",");
  Bluetooth.print(G_alt);// altitude
   Bluetooth.println(",");
   //Bluetooth.print("23");
  // Bluetooth.print(",");
 //  Bluetooth.println(millis() / 1000);
delay(10);

}

the code shown is zero-informative, no line contains a connect command
and this lines

At this point you can turn it on or off. This is just a guess as there is no schematic.

i apologize for the lack of info i was in a rush , the Bluetooth module connected to tx rx pins SoftwareSerial Bluetooth(27, 26);
i just need a function to know when there is a connection and there is data being sent from the arduino to the app to make the LED turn on

On What? Post an annotated schematic, that should have the data needed to answer your question. Be sure to show power sources.

I have something like this, but only through the STATE pin of the BT module. What kind of module are you using?

iam using HC-06

Does it have 4 or 6 pins?

If you look here you should be able to tell what the STATE pin is. If you connect that to one of you arduino pins you can read it's output.

I'm using a HC-05 and I've connected the STATE to a pin I've called 'btState'. With the following code I determine connection status, and only print stats I want to send while connected:


  if (digitalRead(btState) == HIGH) {
    BTconnected = true;
  } else {
    BTconnected = false; 
  }

I hope this helps!

i tried using the tx rx pins volts and reading them with pins to dectet the change of the state but didnt work, it has 4 pins

You might need to get creative with a soldering iron then. Worth a try, right?

Oh before I forget, you need to pull the pin on your arduino low. If it's unconnected it might give you a false positive reading.

1 Like