I'm using Arduino GSM shield, but whenever I tried to initialize the GSM it works the first time, but after that the Status LED will turn off and the GSM starts to initialize again. I've powered it up with 12 V 1000mA to make sure that it isn't the power source that is the problem. Unfortunately, the issue still persist.
I've tried to use an sample sketch that receives SMS via the GSM. It works perfectly. Is there something wrong with the code? Any suggestion. Please. Thank you
#include <GSM.h>
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"
#if defined(ARDUINO_ARCH_SAMD)
// for Zero, output on USB Serial console, remove line below if using programming port to program the Zero!
#define Serial SerialUSB
#endif
RTC_DS1307 rtc;
#define PINNUMBER ""
// initialize the library instance
GSM gsmAccess;
GSM_SMS sms;
GSMVoiceCall vcs;
String adminpass="ADMIN";//set admin password
char daysOfTheWeek[7][12] = {
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
//SET OPEN AND CLOSING TIME HERE/
String ohr,omin;
String chr,cmin;
//END SET OPEN AND CLOSE TIME//
int ledPin = 13; // choose the pin for the LED
int inputPin = 8; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
String remoteNumber = "09322720367"; // the number you will call
char charbuffer[20];
boolean systemStatus;
void setup() {
//RTC field----//
#ifndef ESP8266
while (!Serial); // for Leonardo/Micro/Zero
#endif
Serial.begin(57600);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
//END RTC FIELD//
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input
Serial.begin(9600);
//----------GSM FIELD---------////
while (!Serial) {
; // wait for serial port to connect. NeedWd for native USB port only
}
digitalWrite(ledPin,LOW);
Serial.println("Wait for the GSM to initialize..");
// connection state
boolean notConnected = true;
// Start GSM shield
// If your SIM has PIN, pass it as a parameter of begin() in quotes
while (notConnected) {
if (gsmAccess.begin(PINNUMBER) == GSM_READY) {
notConnected = false;
}
else {
Serial.println("Not connected");
delay(1000);
}
}
Serial.println("GSM initialized");
digitalWrite(ledPin, HIGH);
delay(5000);
digitalWrite(ledPin, LOW);
Serial.println("Waiting for message");
//---------END GSM FIELD------//
}
void loop(){
Serial.println("Loop started");
DateTime now = rtc.now();
char remoteNum[20] = "09322720367"; // telephone number to send sms
//CHECK MESSAGES FIELD//
char c[200];
String mess=""; //messages that are received are stored here
// If there are any SMSs available()
if (sms.available())
{
// Read message bytes and print them
for (int i = 0; i < 200; i++){
c[i]=sms.read();
}
mess=String(c[0])+String(c[1])+String(c[2])+String(c[3]); //get the message received and compare it below.
if (mess=="UPDT")
{
Serial.println("Update message received");
String ohourset=String(c[5])+String(c[6]);
String ominset=String(c[7])+String(c[8]);
String chourset=String(c[10])+String(c[11]);
String cminset=String(c[12])+String(c[13]);
String pass=String(c[15])+String(c[16])+String(c[17])+String(c[18])+String(c[19]);
if (ohourset.toInt()<=23 && ohourset.toInt()>=00 && ominset.toInt()<=59 && ominset.toInt()>=00 && chourset.toInt()<=23 && chourset.toInt()>=00 && cminset.toInt()<=59 && cminset.toInt()>=00 && pass==adminpass)
{
ohr=ohourset;
omin=ominset;
chr=chourset;
cmin=cminset;
// send the message
sms.beginSMS(remoteNum);
sms.print("Your system schedule has been succesfully updated. Opening time will be: " + ohr+omin+" and your Closing time will be: " +chr+cmin);
sms.endSMS();
Serial.println("Update message sent!");
}
}
Serial.println("\nEND OF MESSAGE");
// Delete message from modem memory
sms.flush();
Serial.println("MESSAGE DELETED");
}
delay(1000);
//END CHECK MESSAGES//
if (now.hour()==chr.toInt() && now.minute()==cmin.toInt()-1)//check if time is equal to the closing time.
{
Serial.println("MOTION IS NOW ACTIVE");
val = digitalRead(inputPin); // read input value
if (val == HIGH) {
// check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion detected!");
///--------GSM FIELD-----------//
char Message[200] = "INTRUDER ALERT!!"; //message to send
//--START CALLING HERE---//
// Call the remote number
remoteNumber.toCharArray(charbuffer, 20);
// Check if the receiving end has picked up the call
vcs.voiceCall(charbuffer);
if (vcs.getvoiceCallStatus()==TALKING)
{
vcs.hangCall();//hang up after 10
}
else
{
delay(10000);// delay call 10 seconds
vcs.hangCall();//hang up after 10
}
Serial.println("Call Finished");
//-END CALL CODE--//
// send the message
sms.beginSMS(remoteNum);
sms.print(Message);
sms.endSMS();
Serial.println("\nCOMPLETE NOTIFICATION!\n");
//-----END GSM FIELD-------//
// We only want to print on the output change, not state
pirState = HIGH;
}
}
else {
digitalWrite(ledPin, LOW); // turn LED OFF
if (pirState == HIGH){
// we have just turned of
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
}
}
}
}