#include<SoftwareSerial.h> //software serial library for serial communication between Arduino & sim900 GSM
SoftwareSerial mySerial(10, 11);//connect Tx pin of GSM to pin 8 of Arduino && Rx pin of GSM to pin no 9
int REY = 7; // relay output
int REY2 = 8;
String message;
String lampState = "";
void setup() {
mySerial.begin(9600); // Setting the baud rate of GSM Module
Serial.begin(9600);
delay(2000);
mySerial.println("AT+CMGF=1"); // set text mode
delay(1000);
mySerial.println("AT+CNMI=2,2,0,0,0"); // AT Command to receive a live SMS
delay(1000);
pinMode(REY, OUTPUT);
pinMode(REY2, OUTPUT);
digitalWrite(REY, HIGH);
digitalWrite(REY2, HIGH);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
Serial.println("lamp state AT START is "+lampState);
}
void loop()
{
if (mySerial.available() > 0) {message = mySerial.readString(); }
if (message.indexOf("on")>-1){digitalWrite(REY, LOW); delay(1000);digitalWrite(REY, HIGH);lampState = "on";Serial.println("lamp state is "+lampState);}
if (message.indexOf("off") > -1) {digitalWrite(REY2, LOW);delay(1000);digitalWrite(REY2, HIGH);lampState = "off"; Serial.println("lamp state is "+lampState);}
if (message.indexOf("state") > -1) {mySerial.print("AT+CMGS=\"");mySerial.print("+917588032373");mySerial.print("\"\r\n"); delay(500);mySerial.print("Motor is "+ lampState);delay(500);mySerial.print((char)26);delay(5000); }
}
What is the data type of GSM_Msg?
You cannot use equality (==) on a string (null terminated character array). You can use equality on Strings.
We do not like snippets of code here. Snippets leave out too much information. Post the whole code, please.
Please read the forum guidelines to see how to properly post code and some information on making a good post.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in a code block.
assuming you are entering in the if clause (cf @groundFungus comment) - you set the variable to 0 then wait for 1s then set it to 1.
is there something magic related to that variable ? setting variables to a given value don't drive pins or motors by magic (unless you have some pin abstraction class)... do you want a digitalWrite or something ?
Sir I posted code in that I want relay will be on for one sec. but at practices it's wont turn off until other command wont receive.
did you just change the first post so that now existing answers don't make sense?...
you should have made a new post... Please read the forum guidelines
you should react to a message when there is one that has been received, not constantly checking the content of the message, especially as you don't clear it up....
That code is hard to read. Why not put one statement per line and indent properly so people can read your code?
void loop()
{
if (mySerial.available() > 0)
{
message = mySerial.readString();
}
if (message.indexOf("on")>-1)
{
digitalWrite(REY, LOW);
delay(1000);
digitalWrite(REY, HIGH);
lampState = "on";
Serial.println("lamp state is "+lampState);
}
if (message.indexOf("off") > -1)
{
digitalWrite(REY2, LOW);
delay(1000);
digitalWrite(REY2, HIGH);
lampState = "off";
Serial.println("lamp state is "+lampState);
}
if (message.indexOf("state") > -1)
{
mySerial.print("AT+CMGS=\"");
mySerial.print("+917588032373");
mySerial.print("\"\r\n"); delay(500);
mySerial.print("Motor is "+ lampState);
delay(500);mySerial.print((char)26);
delay(5000);
}
}
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.