I am making a project on gsm module(sim 900a unofficial shield), when I will send a SMS led will turn ON. after uploading code, module works fine, and led turned ON after sending message. But when I power off sim900a and Arduino, and power on back, it's not working, led not power up after sending message, when I reupload sketches it is working fine, means every time after power off I need to upload code again, I am learning c and don't have much knowledge? Is my code right? Or some other fault?
#include <SoftwareSerial.h> //software serial library for serial communication b/w arduino & GSM
SoftwareSerial mySerial(8, 9);//connect Tx pin of GSM to pin 8 of arduino && Rx pin of GSM to pin no 9 of arduino
int led = 7;
String message;
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);
mySerial.println("AT+CNMI=2,2,0,0,0"); // AT Command to receive a live SMS
delay(1000);
pinMode(led,OUTPUT);
digitalWrite(led,LOW);
}
void loop()
{
if (mySerial.available()>0){
message = mySerial.readString();
}
if(message.indexOf("ON") > -1){
Serial.println("LED ON");
digitalWrite(led,HIGH);
}
else if(message.indexOf("OFF") > -1){
Serial.println("LED OFF");
digitalWrite(led,LOW);
}
delay(10);
}
Circuit diagram
1 Answer 1
The GSM takes time to start up. Your program needs to be far more complex than the simple "Throw commands at it and hope" method you are using.
First you need to query the modem repeatedly until it responds. This is usually done with the AT
command. A sequence would look like:
Send: AT
(Wait for OK response or timeout)
Send: AT
(Wait for OK response or timeout)
Send: AT
(Wait for OK response or timeout)
Send: AT
(Wait for OK response or timeout)
Recv: OK
Once the modem has booted and is ready to receive commands you can start sending commands. You shouldn't just "print and delay" though like you are at the moment. Instead you should send the command and then examine the response to see if the command worked ("OK") or failed ("ERR").
-
Is there any librery for it on GitHub, or do you have any prewritten code that work fine, I have not much knowledge of 'C', I am a farmer and my farm is 3 k.m. from my home, so I want to make water motor automatic that I want to control from my home,Devid– Devid2019年12月20日 15:29:50 +00:00Commented Dec 20, 2019 at 15:29
-
@Devid The Arduino IDE comes with a GSM library bundled with it. I have never used it though, so I don't know if it works with the SIM900A.Majenko– Majenko2019年12月20日 16:04:04 +00:00Commented Dec 20, 2019 at 16:04