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
}
}