Skip to content

improved sms receiving #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions Sim800L.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ uint8_t Sim800L::getFunctionalityMode()
return _functionalityMode;
}


bool Sim800L::setPIN(String pin)
{
String command;
Expand Down Expand Up @@ -480,6 +479,7 @@ bool Sim800L::sendSms(char* number,char* text)
_buffer=_readSerial();
this->SoftwareSerial::print((char)26);
_buffer=_readSerial(60000);
// Serial.println(_buffer);
//expect CMGS:xxx , where xxx is a number,for the sending sms.
if ((_buffer.indexOf("ER")) != -1) {
return true;
Expand All @@ -493,11 +493,24 @@ bool Sim800L::sendSms(char* number,char* text)
}


void Sim800L::prepareForSmsReceive()
bool Sim800L::prepareForSmsReceive()
{
// Configure SMS in text mode
this->SoftwareSerial::print (F("AT+CMGF=1\r"));
this->SoftwareSerial::print(F("AT+CMGF=1\r"));
_buffer=_readSerial();
//Serial.print(_buffer);
if((_buffer.indexOf("OK")) == -1)
{
return false;
}
this->SoftwareSerial::print(F("AT+CNMI=2,1,0,0,0\r"));
_buffer=_readSerial();
//Serial.print(_buffer);
if((_buffer.indexOf("OK")) == -1)
{
return false;
}
return true;
}

const uint8_t Sim800L::checkForSMS()
Expand All @@ -507,7 +520,8 @@ const uint8_t Sim800L::checkForSMS()
{
return 0;
}
//Serial.println(_buffer);
_buffer += _readSerial(1000);
// Serial.println(_buffer);
// +CMTI: "SM",1
if(_buffer.indexOf("CMTI") == -1)
{
Expand Down Expand Up @@ -539,7 +553,7 @@ String Sim800L::readSms(uint8_t index)
{
// Can take up to 5 seconds

if (( _readSerial(5000).indexOf("ER")) != -1)
if(( _readSerial(5000).indexOf("ER")) != -1)
{
return "";
}
Expand All @@ -554,8 +568,7 @@ String Sim800L::readSms(uint8_t index)
return "";
}

_buffer=_readSerial();
//Serial.println(_buffer);
_buffer = _readSerial(10000);
byte first = _buffer.indexOf('\n', 2) + 1;
byte second = _buffer.indexOf('\n', first);
return _buffer.substring(first, second);
Expand Down
3 changes: 2 additions & 1 deletion Sim800L.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class Sim800L : public SoftwareSerial
uint8_t getCallStatus();

const uint8_t checkForSMS();
void prepareForSmsReceive();
bool prepareForSmsReceive();
bool sendSms(char* number,char* text);
String readSms(uint8_t index);
String getNumberSms(uint8_t index);
Expand All @@ -140,6 +140,7 @@ class Sim800L : public SoftwareSerial
void setPhoneFunctionality();
void activateBearerProfile();
void deactivateBearerProfile();
bool setMode();

void RTCtime(int *day,int *month, int *year,int *hour,int *minute, int *second);
String dateNet();
Expand Down
5 changes: 4 additions & 1 deletion examples/readSms/readSms.ino
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ void setup() {
Serial.begin(9600);
GSM.begin(4800);
GSM.delAllSms(); // this is optional
GSM.prepareForSmsReceive();
while(!GSM.prepareForSmsReceive())
{
delay(1000);
}
}

void loop() {
Expand Down