Bluetooth Module not connecting to App but connected to phone - help!

Hello everyone!
I have a quick question. I made an app using this tutorial (Arduino Tutorial - MQ7 gas sensors whith Arduino - YouTube - see time 2:58 to end). I followed the instructions step-by-step. There is a slight problem. The app keeps giving me an error statement: “Error 507: Unable to connect. Is the device turned on?” It is turned on. Is there something wrong with my code? The bluetooth module has connected to my phone’s bluetooth but not the app. Thank you!

Code:

#include <SoftwareSerial.h>
 
/************************Hardware Related Macros************************************/

SoftwareSerial mySerial(11, 10); //Assign 10 as Rx and 11 as Tx
int state = 0;
int flag = 0;

#define         MQ_PIN                       (2)     //define which analog input channel you are going to use
#define         RL_VALUE                     (5)     //define the load resistance on the board, in kilo ohms
#define         RO_CLEAN_AIR_FACTOR          (9.83)  //RO_CLEAR_AIR_FACTOR=(Sensor resistance in clean air)/RO,
                                                     //which is derived from the chart in datasheet
 
/***********************Software Related Macros************************************/
#define         CALIBARAION_SAMPLE_TIMES     (50)    //define how many samples you are going to take in the calibration phase
#define         CALIBRATION_SAMPLE_INTERVAL  (500)   //define the time interal(in milisecond) between each samples in the
                                                     //cablibration phase
#define         READ_SAMPLE_INTERVAL         (50)    //define how many samples you are going to take in normal operation
#define         READ_SAMPLE_TIMES            (5)     //define the time interal(in milisecond) between each samples in 
                                                     //normal operation
 
/**********************Application Related Macros**********************************/
#define         GAS_LPG                      (0)
#define         GAS_CO                       (1)
#define         GAS_SMOKE                    (2)
 
/*****************************Globals***********************************************/
float           LPGCurve[3]  =  {2.3,0.21,-0.47};   //two points are taken from the curve. 
                                                    //with these two points, a line is formed which is "approximately equivalent"
                                                    //to the original curve. 
                                                    //data format:{ x, y, slope}; point1: (lg200, 0.21), point2: (lg10000, -0.59) 
float           COCurve[3]  =  {2.3,0.72,-0.34};    //two points are taken from the curve. 
                                                    //with these two points, a line is formed which is "approximately equivalent" 
                                                    //to the original curve.
                                                    //data format:{ x, y, slope}; point1: (lg200, 0.72), point2: (lg10000,  0.15) 
float           SmokeCurve[3] ={2.3,0.53,-0.44};    //two points are taken from the curve. 
                                                    //with these two points, a line is formed which is "approximately equivalent" 
                                                    //to the original curve.
                                                    //data format:{ x, y, slope}; point1: (lg200, 0.53), point2: (lg10000,  -0.22)                                                     
float           Ro           =  10;                 //Ro is initialized to 10 kilo ohms
 
void setup()
{
  Serial.begin(9600);                               //UART setup, baudrate = 9600bps
  Serial.print("Calibrating...\n");                
  Ro = MQCalibration(MQ_PIN);                       //Calibrating the sensor. Please make sure the sensor is in clean air 
                                                    //when you perform the calibration                    
  Serial.print("Calibration is done...\n"); 
  // Serial.print("Ro="); 
  // Serial.print(Ro);
 // Serial.print("kohm");
  // Serial.print("\n");
  
  Serial.begin(9600); //Serial communication for Arduino Serial Monitor
  mySerial.begin(38400); //Serial communcation for Aircasting Application
  
}
 
void loop()
{
   Serial.print("LPG:"); 
   Serial.print(MQGetGasPercentage(MQRead(MQ_PIN)/Ro,GAS_LPG) );
   Serial.print( "ppm" );
   Serial.print("    ");   
   Serial.print("CO:"); 
   Serial.print(MQGetGasPercentage(MQRead(MQ_PIN)/Ro,GAS_CO) );
   Serial.print( "ppm" );
   Serial.print("    ");   
   Serial.print("SMOKE:"); 
   Serial.print(MQGetGasPercentage(MQRead(MQ_PIN)/Ro,GAS_SMOKE) );
   Serial.print( "ppm" );
   Serial.print("\n");
   delay(200);
}
 
/****************** MQResistanceCalculation ****************************************
Input:   raw_adc - raw value read from adc, which represents the voltage
Output:  the calculated sensor resistance
Remarks: The sensor and the load resistor forms a voltage divider. Given the voltage
         across the load resistor and its resistance, the resistance of the sensor
         could be derived.
************************************************************************************/ 
float MQResistanceCalculation(int raw_adc)
{
  return ( ((float)RL_VALUE*(1023-raw_adc)/raw_adc));
}
 
/***************************** MQCalibration ****************************************
Input:   mq_pin - analog channel
Output:  Ro of the sensor
Remarks: This function assumes that the sensor is in clean air. It use  
         MQResistanceCalculation to calculates the sensor resistance in clean air 
         and then divides it with RO_CLEAN_AIR_FACTOR. RO_CLEAN_AIR_FACTOR is about 
         10, which differs slightly between different sensors.
************************************************************************************/ 
float MQCalibration(int mq_pin) 
{
  int i;
  float val=0;
 
  for (i=0;i<CALIBARAION_SAMPLE_TIMES;i++) {            //take multiple samples
    val += MQResistanceCalculation(analogRead(mq_pin));
    delay(CALIBRATION_SAMPLE_INTERVAL);
  }
  val = val/CALIBARAION_SAMPLE_TIMES;                   //calculate the average value
 
  val = val/RO_CLEAN_AIR_FACTOR;                        //divided by RO_CLEAN_AIR_FACTOR yields the Ro 
                                                        //according to the chart in the datasheet 
 
  return val; 
}
/*****************************  MQRead *********************************************
Input:   mq_pin - analog channel
Output:  Rs of the sensor
Remarks: This function use MQResistanceCalculation to caculate the sensor resistenc (Rs).
         The Rs changes as the sensor is in the different consentration of the target
         gas. The sample times and the time interval between samples could be configured
         by changing the definition of the macros.
************************************************************************************/ 
float MQRead(int mq_pin)
{
  int i;
  float rs=0;
 
  for (i=0;i<READ_SAMPLE_TIMES;i++) {
    rs += MQResistanceCalculation(analogRead(mq_pin));
    delay(READ_SAMPLE_INTERVAL);
  }
 
  rs = rs/READ_SAMPLE_TIMES;
 
  return rs;  
}
 
/*****************************  MQGetGasPercentage **********************************
Input:   rs_ro_ratio - Rs divided by Ro
         gas_id      - target gas type
Output:  ppm of the target gas
Remarks: This function passes different curves to the MQGetPercentage function which 
         calculates the ppm (parts per million) of the target gas.
************************************************************************************/ 
int MQGetGasPercentage(float rs_ro_ratio, int gas_id)
{
  if ( gas_id == GAS_LPG ) {
     return MQGetPercentage(rs_ro_ratio,LPGCurve);
  } else if ( gas_id == GAS_CO ) {
     return MQGetPercentage(rs_ro_ratio,COCurve);
  } else if ( gas_id == GAS_SMOKE ) {
     return MQGetPercentage(rs_ro_ratio,SmokeCurve);
  }    
 
  return 0;
}
 
/*****************************  MQGetPercentage **********************************
Input:   rs_ro_ratio - Rs divided by Ro
         pcurve      - pointer to the curve of the target gas
Output:  ppm of the target gas
Remarks: By using the slope and a point of the line. The x(logarithmic value of ppm) 
         of the line could be derived if y(rs_ro_ratio) is provided. As it is a 
         logarithmic coordinate, power of 10 is used to convert the result to non-logarithmic 
         value.
************************************************************************************/ 
int  MQGetPercentage(float rs_ro_ratio, float *pcurve)
{
  return (pow(10,( ((log(rs_ro_ratio)-pcurve[1])/pcurve[2]) + pcurve[0])));
}

jkg9:
The bluetooth module has connected to my phone's bluetooth but not the app. Thank you!

Phone problem - possibly just language.
Bluetooth is not "connected" to phone it is "paired" with it, which just means getting on the dance card for future connection by the app. If you have successfully paired with Bluetooth, there is nothing wrong with bluetooth per se but note that it does not mean

  1. you are connected to Bluetooth, you have to do that from the phone app.

  2. Bluetooth is properly connected to Arduino

  3. Arduino is talking properly with Bluetooth. Indeed I don't think Arduino is talking to mySerial at all, which may be your problem.

Not very wise to start a new thread for the same project.

I didn't see a final breadboard picture there.
Told you twice there that TX/RX need to be crossed over.
Did you do that?

Did you try to send someting to a BT serial monitor app on the phone, to test the connection?
Leo..

Wawa:
Not very wise to start a new thread for the same project.
http://forum.arduino.cc/index.php?topic=544751.0

I didn't see a final breadboard picture there.
Told you twice there that TX/RX need to be crossed over.
Did you do that?

Did you try to send someting to a BT serial monitor app on the phone, to test the connection?
Leo..

I did cross over tx and rx and i tried sending something to a BT serial monitor app on the phone but that didn't work either.

So how about a final picture of all the connections (in the other thread).
Leo..

I'll post it there and here too

here it is

First thing I noticed it that the tap (center) of the voltage divider isn't connected to anything.
So you made a mess of it again (after you had it right).
I wonder if you have understood how a breadboard works.

Can't see how you have connected the rest of the BT wiring from this image.

Why did you move the BT module off the breadboard.
Leo..

Wawa:
First thing I noticed it that the tap (center) of the voltage divider isn't connected to anything.
So you made a mess of it again (after you had it right).
I wonder if you have understood how a breadboard works.

Can't see how you have connected the rest of the BT wiring from this image.

Why did you move the BT module off the breadboard.
Leo..

  1. the center is connected to the ground pin on the bt module
  2. i had to move things around b/c everything was getting squished together
  3. i had to move the bt module off the breadboard b/c it won't work on the breadboard for some reason

jkg9:

  1. the center is connected to the ground pin on the bt module
  2. i had to move things around b/c everything was getting squished together
  3. i had to move the bt module off the breadboard b/c it won't work on the breadboard for some reason

No it's not.
Two resistors are joined in the third vertical "row of 5" top left.
And nothing else is connected to it.

Not a problem, as long as the resistor wires are not touching each other.

Maybe you connected it wrong (happende before,
or the breadboard has a bad row. If so, move the module to another spot.
Leo..

jkg9:
here it is

I guess we can always run a lottery on which wire is connected to which bluetooth pin. I'm not sure that matters anyway. As I said before, the code is clearly suss and the picture explains that well enough. Once you get the wiring mess sorted out, you might then try connecting Bluetooth to pins 0,1, and see if anything comes up on the phone. If you get joy, you might want to revert to the current pins and sort out the mySerial commands, but that might not be necessary, and you can just delete them.

Just went through 60+ posts trying to explain OP how to use softwareSerial.
Trying to use pin 0,1 could bring that back to square one.
Leo..

God knows where the 60+ posts are, but it is probably 59 too many, particularly as there is no result, and OP has clearly no understanding of what you are saying. However, IF the power etc. is kosher, and he moves the Bluetooth signal to pins 0,1, he may actually get a result - no change of code required.

In the meantime, you can all ignore the code, and rabbit on about breadboards till next Guy Fawkes Day, but he will never get a result until the code is fixed.

OP might find the following background notes useful.

http://homepages.ihug.com.au/~npyner/Arduino/GUIDE_2BT.pdf
http://homepages.ihug.com.au/~npyner/Arduino/BT_2_WAY.ino

There are some readable pictures therein.

Or try add to or replace loop with

void loop()
{
   mySerial.print("LPG:");
   mySerial.print(MQGetGasPercentage(MQRead(MQ_PIN)/Ro,GAS_LPG) );
   mySerial.print( "ppm" );
   mySerial.print("    ");  
   mySerial.print("CO:");
   mySerial.print(MQGetGasPercentage(MQRead(MQ_PIN)/Ro,GAS_CO) );
   mySerial.print( "ppm" );
   mySerial.print("    ");  
   mySerial.print("SMOKE:");
   mySerial.print(MQGetGasPercentage(MQRead(MQ_PIN)/Ro,GAS_SMOKE) );
   mySerial.print( "ppm" );
   mySerial.print("\n");
   delay(200);
}

@jkg9, do not cross-post.

 mySerial.begin(38400); //Serial communcation for Aircasting Application

You are not using an AirCasting module. The default data transmission mode baud rate for the HC 05 is 9600 baud. Have you have gone into AT mode and changed it? If not, set the software serial baud rate to 9600.

You really should be working with a standard BT terminal app on yuour phone instead of something you put together. I use the Serial Bluetooth Terminal app from Kai Morich https://play.google.com/store/apps/details?id=de.kai_morich.serial_bluetooth_terminal&hl=en_US

  1. Arduino is talking properly with Bluetooth. Indeed I don't think Arduino is talking to mySerial at all, which may be your problem.

SoftwareSerial mySerial(11, 10); //Assign 10 as Rx and 11 as Tx

DPin-11 (SRX) is the RX-pin of the Software UART Port. It must be connected with the TX-pin of the Bluetooth Module (assume HC-05).

DPin-10 (STX) is the TX-pin of the Software UART Port. It must be connected to the RX-pin of the Bluetooth Module through a voltage divider circuit so that approx. 3.3V logic-High exits at the RX-pin of BT. The RX-pin of BT is not 5V tolerant. A 1k + 2.2k network works well.

Ummm. When I said

I don't think Arduino is talking to mySerial at all,

,apparently I didn't explain myself properly. What I meant was that Arduino is not talking to the Software serial port.

sorry about that. i didn't want to confuse people

That's OK, and I don't want to add to the confusion by commenting further on the above - just so long as you understand where your main problem really lies.