From 843b93957adeb420708a3708c6422710e099a892 Mon Sep 17 00:00:00 2001 From: Subhajitdas298 Date: Tue, 23 May 2017 16:54:06 +0530 Subject: [PATCH] Fixed sendSMS & And added SIMWait Fixed sendSMS. Added SIMWait, which waits for serial input, until specified timeout. --- Sim800l.cpp | 462 ++++++++++++++++++++++++++++------------------------ Sim800l.h | 54 +++--- 2 files changed, 275 insertions(+), 241 deletions(-) diff --git a/Sim800l.cpp b/Sim800l.cpp index bacc0fe..b9bfd14 100644 --- a/Sim800l.cpp +++ b/Sim800l.cpp @@ -1,31 +1,31 @@ /* this library is writing by Cristian Steib. * steibkhriz@gmail.com * Designed to work with the GSM Sim800l,maybe work with SIM900L - * + * * This library use SoftwareSerial, you can define de RX and TX pin * in the header "Sim800l.h" ,by default the pin is RX=10 TX=11.. - * be sure that gnd is attached to arduino too. + * be sure that gnd is attached to arduino too. * You can also change the other preferred RESET_PIN - * + * * Esta libreria usa SoftwareSerial, se pueden cambiar los pines de RX y TX * en el archivo header, "Sim800l.h", por defecto los pines vienen configurado en - * RX=10 TX=11. + * RX=10 TX=11. * Tambien se puede cambiar el RESET_PIN por otro que prefiera - * - * PINOUT: + * + * PINOUT: * _____________________________ * | ARDUINO UNO >>> SIM800L | * ----------------------------- * GND >>> GND - * RX 10 >>> TX + * RX 10 >>> TX * TX 11 >>> RX - * RESET 2 >>> RST - * + * RESET 2 >>> RST + * * POWER SOURCE 4.2V >>> VCC * * Created on: April 20, 2016 * Author: Cristian Steib - * + * * */ #include "Arduino.h" @@ -35,31 +35,37 @@ SoftwareSerial SIM(RX_PIN,TX_PIN); //String _buffer; -void Sim800l::begin(){ - SIM.begin(9600); - #if (LED) +void Sim800l::begin() { + SIM.begin(9600); +#if (LED) pinMode(OUTPUT,LED_PIN); - #endif - _buffer.reserve(255); //reserve memory to prevent intern fragmention +#endif + _buffer.reserve(255); //reserve memory to prevent intern fragmention } +void Sim800l::SIMWait(long maxWait) { + long sTime = millis(); + while (!SIM.available()) { + if (millis() - sTime >= maxWait) + return; + } +} // //PRIVATE METHODS // -String Sim800l::_readSerial(){ - _timeout=0; - while (!SIM.available() && _timeout < 12000 ) - { - delay(13); - _timeout++; +String Sim800l::_readSerial() { + _timeout=0; + while (!SIM.available() && _timeout < 12000 ) { + delay(13); + _timeout++; - } - if (SIM.available()) { - return SIM.readString(); - } - + } + if (SIM.available()) { + return SIM.readString(); + } + } @@ -68,115 +74,121 @@ String Sim800l::_readSerial(){ //PUBLIC METHODS // -void Sim800l::reset(){ - #if (LED) +void Sim800l::reset() { +#if (LED) digitalWrite(LED_PIN,1); - #endif - digitalWrite(RESET_PIN,1); - delay(1000); - digitalWrite(RESET_PIN,0); - delay(1000); - // wait for the module response - - SIM.print(F("AT\r\n")); - while (_readSerial().indexOf("OK")==-1 ){ +#endif + digitalWrite(RESET_PIN,1); + delay(1000); + digitalWrite(RESET_PIN,0); + delay(1000); + // wait for the module response + SIM.print(F("AT\r\n")); - } - - //wait for sms ready - while (_readSerial().indexOf("SMS")==-1 ){ - } - #if (LED) + while (_readSerial().indexOf("OK")==-1 ) { + SIM.print(F("AT\r\n")); + } + + //wait for sms ready + while (_readSerial().indexOf("SMS")==-1 ) { + } +#if (LED) digitalWrite(LED_PIN,0); - #endif +#endif } -void Sim800l::setPhoneFunctionality(){ - /*AT+CFUN=[,] - Parameters - 0 Minimum functionality - 1 Full functionality (Default) - 4 Disable phone both transmit and receive RF circuits. - 1 Reset the MT before setting it to power level. - */ - SIM.print (F("AT+CFUN=1\r\n")); +void Sim800l::setPhoneFunctionality() { + /*AT+CFUN=[,] + Parameters + 0 Minimum functionality + 1 Full functionality (Default) + 4 Disable phone both transmit and receive RF circuits. + 1 Reset the MT before setting it to power level. + */ + SIM.print (F("AT+CFUN=1\r\n")); } -void Sim800l::signalQuality(){ -/*Response -+CSQ: ,Parameters - -0 -115 dBm or less -1 -111 dBm -2...30 -110... -54 dBm -31 -52 dBm or greater -99 not known or not detectable - (in percent): -0...7 As RXQUAL values in the table in GSM 05.08 [20] -subclause 7.2.4 -99 Not known or not detectable -*/ - SIM.print (F("AT+CSQ\r\n")); - Serial.println(_readSerial()); +void Sim800l::signalQuality() { + /*Response + +CSQ: ,Parameters + + 0 -115 dBm or less + 1 -111 dBm + 2...30 -110... -54 dBm + 31 -52 dBm or greater + 99 not known or not detectable + (in percent): + 0...7 As RXQUAL values in the table in GSM 05.08 [20] + subclause 7.2.4 + 99 Not known or not detectable + */ + SIM.print (F("AT+CSQ\r\n")); + Serial.println(_readSerial()); } -void Sim800l::activateBearerProfile(){ - SIM.print (F(" AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\" \r\n" ));_buffer=_readSerial(); // set bearer parameter - SIM.print (F(" AT+SAPBR=3,1,\"APN\",\"internet\" \r\n" ));_buffer=_readSerial(); // set apn - SIM.print (F(" AT+SAPBR=1,1 \r\n"));delay(1200);_buffer=_readSerial();// activate bearer context - SIM.print (F(" AT+SAPBR=2,1\r\n "));delay(3000);_buffer=_readSerial(); // get context ip address +void Sim800l::activateBearerProfile() { + SIM.print (F(" AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\" \r\n" )); + _buffer=_readSerial(); // set bearer parameter + SIM.print (F(" AT+SAPBR=3,1,\"APN\",\"internet\" \r\n" )); + _buffer=_readSerial(); // set apn + SIM.print (F(" AT+SAPBR=1,1 \r\n")); + delay(1200); + _buffer=_readSerial();// activate bearer context + SIM.print (F(" AT+SAPBR=2,1\r\n ")); + delay(3000); + _buffer=_readSerial(); // get context ip address } -void Sim800l::deactivateBearerProfile(){ - SIM.print (F("AT+SAPBR=0,1\r\n ")); - delay(1500); +void Sim800l::deactivateBearerProfile() { + SIM.print (F("AT+SAPBR=0,1\r\n ")); + delay(1500); } -bool Sim800l::answerCall(){ - SIM.print (F("ATA\r\n")); - _buffer=_readSerial(); - //Response in case of data call, if successfully connected - if ( (_buffer.indexOf("OK") )!=-1 ) return true; - else return false; +bool Sim800l::answerCall() { + SIM.print (F("ATA\r\n")); + _buffer=_readSerial(); + //Response in case of data call, if successfully connected + if ( (_buffer.indexOf("OK") )!=-1 ) return true; + else return false; } -void Sim800l::callNumber(char* number){ - SIM.print (F("ATD")); - SIM.print (number); - SIM.print (F("\r\n")); +void Sim800l::callNumber(char* number) { + SIM.print (F("ATD")); + SIM.print (number); + SIM.print (F("\r\n")); } -uint8_t Sim800l::getCallStatus(){ -/* - values of return: - - 0 Ready (MT allows commands from TA/TE) - 2 Unknown (MT is not guaranteed to respond to tructions) - 3 Ringing (MT is ready for commands from TA/TE, but the ringer is active) - 4 Call in progress +uint8_t Sim800l::getCallStatus() { + /* + values of return: -*/ - SIM.print (F("AT+CPAS\r\n")); - _buffer=_readSerial(); - return _buffer.substring(_buffer.indexOf("+CPAS: ")+7,_buffer.indexOf("+CPAS: ")+9).toInt(); + 0 Ready (MT allows commands from TA/TE) + 2 Unknown (MT is not guaranteed to respond to tructions) + 3 Ringing (MT is ready for commands from TA/TE, but the ringer is active) + 4 Call in progress + + */ + SIM.print (F("AT+CPAS\r\n")); + _buffer=_readSerial(); + return _buffer.substring(_buffer.indexOf("+CPAS: ")+7,_buffer.indexOf("+CPAS: ")+9).toInt(); } -bool Sim800l::hangoffCall(){ - SIM.print (F("ATH\r\n")); - _buffer=_readSerial(); - if ( (_buffer.indexOf("OK") ) != -1) return true; - else return false; +bool Sim800l::hangoffCall() { + SIM.print (F("ATH\r\n")); + _buffer=_readSerial(); + if ( (_buffer.indexOf("OK") ) != -1) return true; + else return false; } @@ -184,139 +196,161 @@ bool Sim800l::hangoffCall(){ -bool Sim800l::sendSms(char* number,char* text){ - - SIM.print (F("AT+CMGF=1\r")); //set sms to text mode - _buffer=_readSerial(); - SIM.print (F("AT+CMGS=\"")); // command to send sms - SIM.print (number); - SIM.print(F("\"\r")); - _buffer=_readSerial(); +bool Sim800l::sendSms(char* number,char* text) { + /* + SIM.print (F("AT+CMGF=1\r")); //set sms to text mode + _buffer=_readSerial(); + SIM.print (F("AT+CMGS=\"")); // command to send sms + SIM.print (number); + SIM.print(F("\"\r")); + _buffer=_readSerial(); + SIM.print (text); + SIM.print ("\r"); + //change delay 100 to readserial + _buffer=_readSerial(); + SIM.print((char)26); + _buffer=_readSerial(); + //expect CMGS:xxx , where xxx is a number,for the sending sms. + if (((_buffer.indexOf("CMGS") ) != -1 ) ){ + return true; + } + else { + return false; + } + */ + SIM.print(F("AT+CMGF=1\r\n")); + //Serial.print(SIM.readString()); + SIM.print(F("AT+CMGS=\"")); + SIM.print (number); + SIM.print(F("\"\r\n")); + SIM.readString(); + //Serial.print(SIM.readString()); SIM.print (text); - SIM.print ("\r"); - //change delay 100 to readserial - _buffer=_readSerial(); - SIM.print((char)26); - _buffer=_readSerial(); - //expect CMGS:xxx , where xxx is a number,for the sending sms. - if (((_buffer.indexOf("CMGS") ) != -1 ) ){ - return true; - } - else { - return false; + SIM.readString(); + //Serial.print(SIM.readString()); + SIM.write(26); + SIMWait(3000); + //Serial.print(SIM.readString()); + if (((SIM.readString().indexOf("CMGS") ) != -1 ) ) { + return true; + } else { + return false; } } -String Sim800l::getNumberSms(uint8_t index){ - _buffer=readSms(index); - Serial.println(_buffer.length()); - if (_buffer.length() > 10) //avoid empty sms - { - uint8_t _idx1=_buffer.indexOf("+CMGR:"); - _idx1=_buffer.indexOf("\",\"",_idx1+1); - return _buffer.substring(_idx1+3,_buffer.indexOf("\",\"",_idx1+4)); - }else{ - return ""; - } +String Sim800l::getNumberSms(uint8_t index) { + _buffer=readSms(index); + Serial.println(_buffer.length()); + if (_buffer.length() > 10) { //avoid empty sms + uint8_t _idx1=_buffer.indexOf("+CMGR:"); + _idx1=_buffer.indexOf("\",\"",_idx1+1); + return _buffer.substring(_idx1+3,_buffer.indexOf("\",\"",_idx1+4)); + } else { + return ""; + } } -String Sim800l::readSms(uint8_t index){ - SIM.print (F("AT+CMGF=1\r")); - if (( _readSerial().indexOf("ER")) ==-1) { - SIM.print (F("AT+CMGR=")); - SIM.print (index); - SIM.print("\r"); - _buffer=_readSerial(); - if (_buffer.indexOf("CMGR:")!=-1){ - return _buffer; - } - else return ""; - } - else - return ""; +String Sim800l::readSms(uint8_t index) { + SIM.print (F("AT+CMGF=1\r")); + if (( _readSerial().indexOf("ER")) ==-1) { + SIM.print (F("AT+CMGR=")); + SIM.print (index); + SIM.print("\r"); + _buffer=_readSerial(); + if (_buffer.indexOf("CMGR:")!=-1) { + return _buffer; + } else return ""; + } else + return ""; } -bool Sim800l::delAllSms(){ - SIM.print(F("at+cmgda=\"del all\"\n\r")); - _buffer=_readSerial(); - if (_buffer.indexOf("OK")!=-1) {return true;}else {return false;} - +bool Sim800l::delAllSms() { + SIM.print(F("at+cmgda=\"del all\"\n\r")); + _buffer=_readSerial(); + if (_buffer.indexOf("OK")!=-1) { + return true; + } else { + return false; + } + } -void Sim800l::RTCtime(int *day,int *month, int *year,int *hour,int *minute, int *second){ - SIM.print(F("at+cclk?\r\n")); - // if respond with ERROR try one more time. - _buffer=_readSerial(); - if ((_buffer.indexOf("ERR"))!=-1){ - delay(50); +void Sim800l::RTCtime(int *day,int *month, int *year,int *hour,int *minute, int *second) { SIM.print(F("at+cclk?\r\n")); - } - if ((_buffer.indexOf("ERR"))==-1){ - _buffer=_buffer.substring(_buffer.indexOf("\"")+1,_buffer.lastIndexOf("\"")-1); - *year=_buffer.substring(0,2).toInt(); - *month= _buffer.substring(3,5).toInt(); - *day=_buffer.substring(6,8).toInt(); - *hour=_buffer.substring(9,11).toInt(); - *minute=_buffer.substring(12,14).toInt(); - *second=_buffer.substring(15,17).toInt(); - } + // if respond with ERROR try one more time. + _buffer=_readSerial(); + if ((_buffer.indexOf("ERR"))!=-1) { + delay(50); + SIM.print(F("at+cclk?\r\n")); + } + if ((_buffer.indexOf("ERR"))==-1) { + _buffer=_buffer.substring(_buffer.indexOf("\"")+1,_buffer.lastIndexOf("\"")-1); + *year=_buffer.substring(0,2).toInt(); + *month= _buffer.substring(3,5).toInt(); + *day=_buffer.substring(6,8).toInt(); + *hour=_buffer.substring(9,11).toInt(); + *minute=_buffer.substring(12,14).toInt(); + *second=_buffer.substring(15,17).toInt(); + } } //Get the time of the base of GSM String Sim800l::dateNet() { - SIM.print(F("AT+CIPGSMLOC=2,1\r\n ")); - _buffer=_readSerial(); + SIM.print(F("AT+CIPGSMLOC=2,1\r\n ")); + _buffer=_readSerial(); - if (_buffer.indexOf("OK")!=-1 ){ - return _buffer.substring(_buffer.indexOf(":")+2,(_buffer.indexOf("OK")-4)); - } else - return "0"; + if (_buffer.indexOf("OK")!=-1 ) { + return _buffer.substring(_buffer.indexOf(":")+2,(_buffer.indexOf("OK")-4)); + } else + return "0"; } -// Update the RTC of the module with the date of GSM. -bool Sim800l::updateRtc(int utc){ - - activateBearerProfile(); - _buffer=dateNet(); - deactivateBearerProfile(); - - _buffer=_buffer.substring(_buffer.indexOf(",")+1,_buffer.length()); - String dt=_buffer.substring(0,_buffer.indexOf(",")); - String tm=_buffer.substring(_buffer.indexOf(",")+1,_buffer.length()) ; - - int hour = tm.substring(0,2).toInt(); - int day = dt.substring(8,10).toInt(); - - hour=hour+utc; - - String tmp_hour; - String tmp_day; - //TODO : fix if the day is 0, this occur when day is 1 then decrement to 1, - // will need to check the last month what is the last day . - if (hour<0){ - hour+=24; - day-=1; - } - if (hour<10){ +// Update the RTC of the module with the date of GSM. +bool Sim800l::updateRtc(int utc) { - tmp_hour="0"+String(hour); - }else{ - tmp_hour=String(hour); - } - if (day<10){ - tmp_day="0"+String(day); - }else{ - tmp_day=String(day); - } + activateBearerProfile(); + _buffer=dateNet(); + deactivateBearerProfile(); + + _buffer=_buffer.substring(_buffer.indexOf(",")+1,_buffer.length()); + String dt=_buffer.substring(0,_buffer.indexOf(",")); + String tm=_buffer.substring(_buffer.indexOf(",")+1,_buffer.length()) ; + + int hour = tm.substring(0,2).toInt(); + int day = dt.substring(8,10).toInt(); + + hour=hour+utc; + + String tmp_hour; + String tmp_day; + //TODO : fix if the day is 0, this occur when day is 1 then decrement to 1, + // will need to check the last month what is the last day . + if (hour<0) { + hour+=24; + day-=1; + } + if (hour<10) { + + tmp_hour="0"+String(hour); + } else { + tmp_hour=String(hour); + } + if (day<10) { + tmp_day="0"+String(day); + } else { + tmp_day=String(day); + } //for debugging - //Serial.println("at+cclk=\""+dt.substring(2,4)+"/"+dt.substring(5,7)+"/"+tmp_day+","+tmp_hour+":"+tm.substring(3,5)+":"+tm.substring(6,8)+"-03\"\r\n"); - SIM.print("at+cclk=\""+dt.substring(2,4)+"/"+dt.substring(5,7)+"/"+tmp_day+","+tmp_hour+":"+tm.substring(3,5)+":"+tm.substring(6,8)+"-03\"\r\n"); - if ( (_readSerial().indexOf("ER"))!=-1) {return false;}else return true; + //Serial.println("at+cclk=\""+dt.substring(2,4)+"/"+dt.substring(5,7)+"/"+tmp_day+","+tmp_hour+":"+tm.substring(3,5)+":"+tm.substring(6,8)+"-03\"\r\n"); + SIM.print("at+cclk=\""+dt.substring(2,4)+"/"+dt.substring(5,7)+"/"+tmp_day+","+tmp_hour+":"+tm.substring(3,5)+":"+tm.substring(6,8)+"-03\"\r\n"); + if ( (_readSerial().indexOf("ER"))!=-1) { + return false; + } else return true; - - } + +} diff --git a/Sim800l.h b/Sim800l.h index a81adbb..afbbcca 100644 --- a/Sim800l.h +++ b/Sim800l.h @@ -1,31 +1,31 @@ /* this library is writing by Cristian Steib. * steibkhriz@gmail.com * Designed to work with the GSM Sim800l,maybe work with SIM900L - * + * * This library use SoftwareSerial, you can define de RX and TX pin * in the header "Sim800l.h" ,by default the pin is RX=10 TX=11.. - * be sure that gnd is attached to arduino too. + * be sure that gnd is attached to arduino too. * You can also change the other preferred RESET_PIN - * + * * Esta libreria usa SoftwareSerial, se pueden cambiar los pines de RX y TX * en el archivo header, "Sim800l.h", por defecto los pines vienen configurado en - * RX=10 TX=11. + * RX=10 TX=11. * Tambien se puede cambiar el RESET_PIN por otro que prefiera - * - * PINOUT: + * + * PINOUT: * _____________________________ * | ARDUINO UNO >>> SIM800L | * ----------------------------- * GND >>> GND - * RX 10 >>> TX + * RX 10 >>> TX * TX 11 >>> RX - * RESET 2 >>> RST - * + * RESET 2 >>> RST + * * POWER SOURCE 4.2V >>> VCC * * Created on: April 20, 2016 * Author: Cristian Steib - * + * * */ #ifndef Sim800l_h @@ -35,47 +35,47 @@ #define RX_PIN 10 -#define TX_PIN 11 +#define TX_PIN 11 #define RESET_PIN 2 // pin to the reset pin sim800l -#define LED true // used for indicator led, in case that you don want set to false . -#define LED_PIN 13 //pin to indicate states. +#define LED true // used for indicator led, in case that you don want set to false . +#define LED_PIN 13 //pin to indicate states. -class Sim800l -{ +class Sim800l +{ private: int _timeout; String _buffer; String _readSerial(); - - + + void SIMWait(long); public: - void begin(); - void reset(); + void begin(); + void reset(); - // Methods for calling || Funciones de llamadas. - bool answerCall(); + // Methods for calling || Funciones de llamadas. + bool answerCall(); void callNumber(char* number); bool hangoffCall(); - uint8_t getCallStatus(); + uint8_t getCallStatus(); //Methods for sms || Funciones de SMS. - bool sendSms(char* number,char* text); + bool sendSms(char* number,char* text); String readSms(uint8_t index); //return all the content of sms - String getNumberSms(uint8_t index); //return the number of the sms.. - bool delAllSms(); // return : OK or ERROR .. + String getNumberSms(uint8_t index); //return the number of the sms.. + bool delAllSms(); // return : OK or ERROR .. void signalQuality(); void setPhoneFunctionality(); void activateBearerProfile(); void deactivateBearerProfile(); //get time with the variables by reference - void RTCtime(int *day,int *month, int *year,int *hour,int *minute, int *second); + void RTCtime(int *day,int *month, int *year,int *hour,int *minute, int *second); String dateNet(); //return date,time, of the network bool updateRtc(int utc); //Update the RTC Clock with de Time AND Date of red-. }; -#endif \ No newline at end of file +#endif