Skip to content

Commit 1c992b4

Browse files
authored
Merge pull request #21 from sekyHC/master
Changed how SMS receiving is handled
2 parents 834ea13 + 6da8b6c commit 1c992b4

File tree

3 files changed

+60
-28
lines changed

3 files changed

+60
-28
lines changed

Sim800L.cpp

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,30 @@ bool Sim800L::sendSms(char* number,char* text)
493493
}
494494

495495

496+
void Sim800L::prepareForSmsReceive()
497+
{
498+
// Configure SMS in text mode
499+
this->SoftwareSerial::print (F("AT+CMGF=1\r"));
500+
this->SoftwareSerial::print(F("AT+CNMI=2,1,0,0,0\r"));
501+
}
502+
503+
const uint8_t Sim800L::checkForSMS()
504+
{
505+
_buffer = _readSerial(100);
506+
if(_buffer.length() == 0)
507+
{
508+
return 0;
509+
}
510+
//Serial.println(_buffer);
511+
// +CMTI: "SM",1
512+
if(_buffer.indexOf("CMTI") == -1)
513+
{
514+
return 0;
515+
}
516+
return _buffer.substring(_buffer.indexOf(',')+1).toInt();
517+
}
518+
519+
496520
String Sim800L::getNumberSms(uint8_t index)
497521
{
498522
_buffer=readSms(index);
@@ -513,25 +537,28 @@ String Sim800L::getNumberSms(uint8_t index)
513537

514538
String Sim800L::readSms(uint8_t index)
515539
{
516-
517540
// Can take up to 5 seconds
518541

519-
this->SoftwareSerial::print (F("AT+CMGF=1\r"));
542+
if (( _readSerial(5000).indexOf("ER")) != -1)
543+
{
544+
return "";
545+
}
520546

521-
if (( _readSerial(5000).indexOf("ER")) ==-1)
547+
this->SoftwareSerial::print (F("AT+CMGR="));
548+
this->SoftwareSerial::print (index);
549+
this->SoftwareSerial::print ("\r");
550+
_buffer=_readSerial();
551+
//Serial.println(_buffer);
552+
if (_buffer.indexOf("CMGR") == -1)
522553
{
523-
this->SoftwareSerial::print (F("AT+CMGR="));
524-
this->SoftwareSerial::print (index);
525-
this->SoftwareSerial::print ("\r");
526-
_buffer=_readSerial();
527-
if (_buffer.indexOf("CMGR:")!=-1)
528-
{
529-
return _buffer;
530-
}
531-
else return "";
554+
return "";
532555
}
533-
else
534-
return "";
556+
557+
_buffer=_readSerial();
558+
//Serial.println(_buffer);
559+
byte first = _buffer.indexOf('\n', 2) + 1;
560+
byte second = _buffer.indexOf('\n', first);
561+
return _buffer.substring(first, second);
535562
}
536563

537564

Sim800L.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@
7474
#define DEFAULT_BAUD_RATE 9600
7575
#define TIME_OUT_READ_SERIAL 5000
7676

77-
7877
class Sim800L : public SoftwareSerial
7978
{
8079
private:
@@ -91,7 +90,6 @@ class Sim800L : public SoftwareSerial
9190
String _readSerial();
9291
String _readSerial(uint32_t timeout);
9392

94-
9593
public:
9694

9795
uint8_t RX_PIN;
@@ -130,6 +128,8 @@ class Sim800L : public SoftwareSerial
130128
bool hangoffCall();
131129
uint8_t getCallStatus();
132130

131+
const uint8_t checkForSMS();
132+
void prepareForSmsReceive();
133133
bool sendSms(char* number,char* text);
134134
String readSms(uint8_t index);
135135
String getNumberSms(uint8_t index);

examples/readSms/readSms.ino

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,25 @@ Sim800L GSM(RX, TX);
7171
* Sim800L GSM(RX, TX, RESET);
7272
* Sim800L GSM(RX, TX, RESET, LED);
7373
*/
74-
75-
String text; // to save the text of the sms
76-
uint8_t index; // to indicate the message to read.
77-
74+
75+
/*
76+
* NOTICE:
77+
* FOR THIS TO WORK YOU HAVE TO INCREASE RX BUFFER SIZE
78+
* Open SoftwareSerial.h and change _SS_MAX_RX_BUFF to 256
79+
*
80+
*/
7881

7982
void setup() {
80-
Serial.begin(9600);
81-
GSM.begin(4800);
82-
index = 1;
83-
text = GSM.readSms(index);
84-
Serial.println(text);
85-
83+
Serial.begin(9600);
84+
GSM.begin(4800);
85+
GSM.delAllSms(); // this is optional
86+
GSM.prepareForSmsReceive();
8687
}
8788

8889
void loop() {
89-
//do nothing
90-
}
90+
byte index = GSM.checkForSMS();
91+
if(index != 0)
92+
{
93+
Serial.println(gsm.readSms(index));
94+
}
95+
}

0 commit comments

Comments
 (0)