GSM - Bluetooth

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.

#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)
  BT.begin(9600); // set the data rate for the SoftwareSerial port
  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

  mySerial.println("AT+CMGD=1"); //Delete Message at index 1


char a;  // stores incoming character from other device
void loop()
 if (mySerial.available()>0) {
     char c =;
     if(c=='1') {
      digitalWrite(13, HIGH); 
      digitalWrite(relay, HIGH);    //relay open 
     else if(c=='2') {
      digitalWrite(relay, LOW);    //relay close
      digitalWrite(13, LOW);

 if (BT.available()) { // if text arrived in from BT serial...
      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

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: