My project sends temperature sensor data to a phone using GSM module 900. But also on receiving some specific command from a number it switches ON or OFF some pins(see the 'fan()' function). The problem is that when I don't connect my GSM Module to Arduino, it works perfectly fine. but when I connect it to the module, it doesnt work properly. I guess its due to data coming from Module other than the command I sent is causing trouble. Please tell me how to filter out the data. Here is the code :
double tempD,tempA;
char inchar;
long prevtime=0;
////////////////////////////////////////////////////////
void setup()
{
Serial.begin(9600);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
Serial.println("AT+CSMS=1\r");
delay(200);
Serial.print("AT+CNMI=2,2,0,0,0");
delay(300);
Serial.print('\n');
delay(1000);
}
/////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////
void loop()
{ unsigned long time = millis();
if(time-prevtime>100000)
{ prevtime=time;
message();
}
fan();
delay(500);
}
/////////////////////////////////////////////////////////
void message()
{ temperature();
Serial.print("\r");
delay(1000);
Serial.print("AT+CMGF=1\r");
delay(1000);
Serial.print("AT+CMGS=\"+917709777239\"\r");
delay(1000);
Serial.println('\r');
Serial.print("\n");
Serial.print("Temperature=");
Serial.println(tempD);
Serial.print('\r');
Serial.print("---REPLY---");
Serial.print('\n');
Serial.print("'1' for FAN ON, Motor ON");
Serial.print('\n');
Serial.print("'2' for FAN ON, Motor OFF");
Serial.print('\n');
Serial.print("'3' for FAN OFF, Motor ON");
Serial.print('\n');
Serial.print("'0' for FAN OFF, Motor OFF");
Serial.print('\n');
delay(800);
Serial.write(0x1A);
}
//////////////////////////////////////////////////////////////////
void fan()
{
if(Serial.available()>0)
{ inchar=Serial.read();
if (inchar=='1')
{
digitalWrite(12, HIGH);
delay(500);
digitalWrite(13,HIGH);
}
else if (inchar=='2')
{
digitalWrite(12, HIGH);
delay(500);
digitalWrite(13, LOW);
}
else if (inchar=='3')
{
digitalWrite(12, LOW);
delay(500);
digitalWrite(13, HIGH);
}
else if (inchar=='0')
{ digitalWrite(12, LOW);
delay(500);
digitalWrite(13, LOW);
}
else if (inchar=='M' || inchar=='m')
{
message();
}
Serial.println("AT+CMGR=1\r");
delay(2000);
}
}
////////////////////////////////////////////////////////////////////////////////////////////
double temperature()
{
tempA=analogRead(A0);
tempD = (5.0 *tempA * 100.0) / 1023;
return tempD;
}