GSM - Bluetooth

Hey,
I am doing a project on home automation using GSM and Bluetooth. I am using HC-06 and SIM900a modules.
I was able to control a light bulb using both, Bluetooth and GSM individually. But, when I tried to merge the code, I am having trouble with the GSM. The code is not responding to the GSM commands, but it works fine in the case of bluetooth.
Following is the code that I used. Please suggest changes.

//ERFINDER CODE
#include <SoftwareSerial.h>
#define relay 4

SoftwareSerial BT(0,1); 
// creates a "virtual" serial port/UART
// connect BT module TX to D10
// connect BT module RX to D11
// connect BT Vcc to 5V, GND to GND
SoftwareSerial mySerial(9, 10);

void setup()
{
  mySerial.begin(9600);   // Setting the baud rate of GSM Module  
  Serial.begin(9600);    // Setting the baud rate of Serial Monitor (Arduino)
  delay(100);
  
  BT.begin(9600); // set the data rate for the SoftwareSerial port
  delay(100);
  
  pinMode(13, OUTPUT);
  pinMode(relay, OUTPUT);
  
  mySerial.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
  delay(1000);  // Delay of 1000 milli seconds or 1 second
  mySerial.println("AT+CNMI=2,2,0,0,0"); // AT Command to recieve a live SMS
  delay(1000);

  
  mySerial.println("AT+CMGD=1"); //Delete Message at index 1
  mySerial.println("AT+CMGD=2"); 
  mySerial.println("AT+CMGD=3");
  mySerial.println("AT+CMGD=4");
  mySerial.println("AT+CMGD=5");
  
  mySerial.println("AT+CMGL=\"ALL\""); 
  delay(10);

}

char a;  // stores incoming character from other device
void loop()
{
 if (mySerial.available()>0) {
     char c = mySerial.read();
     Serial.write(c);
     if(c=='1') {
      digitalWrite(13, HIGH); 
      digitalWrite(relay, HIGH);    //relay open 
      //mySerial.println("AT+CMGD=1");
     }
     else if(c=='2') {
      digitalWrite(relay, LOW);    //relay close
      digitalWrite(13, LOW);
      //mySerial.println("AT+CMGD=1");
     } 
 }

 if (BT.available()) { // if text arrived in from BT serial...
      a=(BT.read());
      if (a=='1') {
        digitalWrite(relay, LOW);    //relay open
        digitalWrite(13, HIGH);
        BT.println("LED on");
      }
      if (a=='2') {
        digitalWrite(relay, HIGH);    //relay close
        digitalWrite(13, LOW);
        BT.println("LED off");
      }
      if (a=='?') {
        BT.println("Send '1' to turn LED on");
        BT.println("Send '2' to turn LED on");
      }   
      // you can add more "if" statements with other characters to add more commands
  }
}
Serial.begin()

uses pins D0 & D1. So using them to talk to the BT device at the same time isn't going to work too well.

Also, using two instances of SoftwareSerial suggests, to paraphrase PaulS, that you need to consider your hardware options. Read the reference page which discusses the problems of trying to use two instances.

You need to be clear on what the real objectives are. I suspect that, if you leave the bluetooth on pins 0,1 i.e. hardware serial, and just use Serial to deal with it instead of BT, it will be fine and will get past the multiple use of software serial. This means that the HC-06 is left disconnected for uploading and debugging.

Thanks a lot Nick_Pyner and dannable. It working fine when I replaced BT with Serial. :slight_smile: