Skip to content

Commit a7872ae

Browse files
authored
Add files via upload
Added custom time out function
1 parent 641e825 commit a7872ae

File tree

2 files changed

+167
-129
lines changed

2 files changed

+167
-129
lines changed

Sim800L.cpp

Lines changed: 137 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/*
22
* This library was written by Vittorio Esposito
33
* https://github.com/VittorioEsposito
44
*
@@ -7,31 +7,31 @@
77
* ENG
88
* This library uses SoftwareSerial, you can define RX and TX pins
99
* in the header "Sim800L.h", by default pins are RX=10 and TX=11.
10-
* Be sure that GND is connected to arduino too.
10+
* Be sure that GND is connected to arduino too.
1111
* You can also change the RESET_PIN as you prefer.
12-
*
12+
*
1313
* ESP
1414
* Esta libreria usa SoftwareSerial, se pueden cambiar los pines de RX y TX
1515
* en el archivo header, "Sim800L.h", por defecto los pines vienen configurado en
16-
* RX=10 TX=11.
16+
* RX=10 TX=11.
1717
* Tambien se puede cambiar el RESET_PIN por otro que prefiera
18-
*
18+
*
1919
* ITA
2020
* Questa libreria utilizza la SoftwareSerial, si possono cambiare i pin di RX e TX
2121
* dall' intestazione "Sim800L.h", di default essi sono impostati come RX=10 RX=11
2222
* Assicurarsi di aver collegato il dispositivo al pin GND di Arduino.
2323
* E' anche possibile cambiare il RESET_PIN.
2424
*
2525
*
26-
* DEFAULT PINOUT:
26+
* DEFAULT PINOUT:
2727
* _____________________________
2828
* | ARDUINO UNO >>> Sim800L |
2929
* -----------------------------
3030
* GND >>> GND
31-
* RX 10 >>> TX
31+
* RX 10 >>> TX
3232
* TX 11 >>> RX
33-
* RESET 2 >>> RST
34-
*
33+
* RESET 2 >>> RST
34+
*
3535
* POWER SOURCE 4.2V >>> VCC
3636
*
3737
*
@@ -44,16 +44,16 @@
4444
* 3. Not all pins on the Leonardo and Micro support change interrupts, so only the following can be used for RX: 8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).
4545
* 4. On Arduino or Genuino 101 the current maximum RX speed is 57600bps
4646
* 5. On Arduino or Genuino 101 RX doesn't work on Pin 13
47-
*
47+
*
4848
* BAUD RATE
4949
* Supported baud rates are 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 31250, 38400, 57600, and 115200.
5050
*
5151
*
5252
* Edited on: December 24, 2016
5353
* Editor: Vittorio Esposito
54-
*
54+
*
5555
* Original version by: Cristian Steib
56-
*
56+
*
5757
*
5858
*/
5959

@@ -64,37 +64,41 @@
6464
//SoftwareSerial SIM(RX_PIN,TX_PIN);
6565
//String _buffer;
6666

67-
Sim800L::Sim800L(void) : SoftwareSerial(DEFAULT_RX_PIN, DEFAULT_TX_PIN) {
68-
RX_PIN = DEFAULT_RX_PIN;
69-
TX_PIN = DEFAULT_TX_PIN;
70-
RESET_PIN = DEFAULT_RESET_PIN;
71-
LED_PIN = DEFAULT_LED_PIN;
72-
LED_FLAG = DEFAULT_LED_FLAG;
67+
Sim800L::Sim800L(void) : SoftwareSerial(DEFAULT_RX_PIN, DEFAULT_TX_PIN)
68+
{
69+
RX_PIN = DEFAULT_RX_PIN;
70+
TX_PIN = DEFAULT_TX_PIN;
71+
RESET_PIN = DEFAULT_RESET_PIN;
72+
LED_PIN = DEFAULT_LED_PIN;
73+
LED_FLAG = DEFAULT_LED_FLAG;
7374
}
7475

75-
Sim800L::Sim800L(uint8_t rx, uint8_t tx) : SoftwareSerial(rx, tx) {
76-
RX_PIN = rx;
77-
TX_PIN = tx;
78-
RESET_PIN = DEFAULT_RESET_PIN;
79-
LED_PIN = DEFAULT_LED_PIN;
80-
LED_FLAG = DEFAULT_LED_FLAG;
76+
Sim800L::Sim800L(uint8_t rx, uint8_t tx) : SoftwareSerial(rx, tx)
77+
{
78+
RX_PIN = rx;
79+
TX_PIN = tx;
80+
RESET_PIN = DEFAULT_RESET_PIN;
81+
LED_PIN = DEFAULT_LED_PIN;
82+
LED_FLAG = DEFAULT_LED_FLAG;
8183
}
8284

83-
Sim800L::Sim800L(uint8_t rx, uint8_t tx, uint8_t rst) : SoftwareSerial(rx, tx) {
84-
RX_PIN = rx;
85-
TX_PIN = tx;
86-
RESET_PIN = rst;
87-
LED_PIN = DEFAULT_LED_PIN;
88-
LED_FLAG = DEFAULT_LED_FLAG;
85+
Sim800L::Sim800L(uint8_t rx, uint8_t tx, uint8_t rst) : SoftwareSerial(rx, tx)
86+
{
87+
RX_PIN = rx;
88+
TX_PIN = tx;
89+
RESET_PIN = rst;
90+
LED_PIN = DEFAULT_LED_PIN;
91+
LED_FLAG = DEFAULT_LED_FLAG;
8992
}
9093

91-
Sim800L::Sim800L(uint8_t rx, uint8_t tx, uint8_t rst, uint8_t led) : SoftwareSerial(rx, tx) {
92-
RX_PIN = rx;
93-
TX_PIN = tx;
94-
RESET_PIN = rst;
95-
LED_PIN = led;
96-
LED_FLAG = true;
97-
}
94+
Sim800L::Sim800L(uint8_t rx, uint8_t tx, uint8_t rst, uint8_t led) : SoftwareSerial(rx, tx)
95+
{
96+
RX_PIN = rx;
97+
TX_PIN = tx;
98+
RESET_PIN = rst;
99+
LED_PIN = led;
100+
LED_FLAG = true;
101+
}
98102

99103
void Sim800L::begin()
100104
{
@@ -107,7 +111,7 @@ void Sim800L::begin()
107111
_sleepMode = 0;
108112
_functionalityMode = 1;
109113

110-
if (LED_FLAG) pinMode(LED_PIN, OUTPUT);
114+
if (LED_FLAG) pinMode(LED_PIN, OUTPUT);
111115

112116
_buffer.reserve(BUFFER_RESERVE_MEMORY); // Reserve memory to prevent intern fragmention
113117
}
@@ -120,7 +124,10 @@ void Sim800L::begin(uint32_t baud)
120124
_baud = baud;
121125
this->begin(_baud);
122126

123-
if (LED_FLAG) pinMode(LED_PIN, OUTPUT);
127+
_sleepMode = 0;
128+
_functionalityMode = 1;
129+
130+
if (LED_FLAG) pinMode(LED_PIN, OUTPUT);
124131

125132
_buffer.reserve(BUFFER_RESERVE_MEMORY); // Reserve memory to prevent intern fragmention
126133
}
@@ -136,7 +143,7 @@ bool Sim800L::setSleepMode(bool state)
136143
_sleepMode = state;
137144

138145
if (_sleepMode) this->print(F("AT+CSCLK=1\r\n "));
139-
else this->print(F("AT+CSCLK=0\r\n "));
146+
else this->print(F("AT+CSCLK=0\r\n "));
140147

141148
if ( (_readSerial().indexOf("ER")) == -1)
142149
{
@@ -160,30 +167,33 @@ bool Sim800L::getSleepMode()
160167
bool Sim800L::setFunctionalityMode(uint8_t fun)
161168
{
162169

163-
if (fun!=0 || fun!=1 || fun!=4) return false;
170+
if (fun==0 || fun==1 || fun==4)
171+
{
164172

165-
_functionalityMode = fun;
173+
_functionalityMode = fun;
166174

167-
switch(_functionalityMode)
168-
{
169-
case 0:
170-
this->print(F("AT+CFUN=0\r\n "));
171-
break;
172-
case 1:
173-
this->print(F("AT+CFUN=1\r\n "));
174-
break;
175-
case 4:
176-
this->print(F("AT+CFUN=4\r\n "));
177-
break;
178-
}
175+
switch(_functionalityMode)
176+
{
177+
case 0:
178+
this->print(F("AT+CFUN=0\r\n "));
179+
break;
180+
case 1:
181+
this->print(F("AT+CFUN=1\r\n "));
182+
break;
183+
case 4:
184+
this->print(F("AT+CFUN=4\r\n "));
185+
break;
186+
}
179187

180-
if ( (_readSerial().indexOf("ER")) == -1)
181-
{
182-
return false;
188+
if ( (_readSerial().indexOf("ER")) == -1)
189+
{
190+
return false;
191+
}
192+
else return true;
193+
// Error found, return 1
194+
// Error NOT found, return 0
183195
}
184-
else return true;
185-
// Error found, return 1
186-
// Error NOT found, return 0
196+
return false;
187197
}
188198

189199
uint8_t Sim800L::getFunctionalityMode()
@@ -192,20 +202,18 @@ uint8_t Sim800L::getFunctionalityMode()
192202
}
193203

194204

195-
196205
bool Sim800L::setPIN(String pin)
197206
{
198207
String command;
199208
command = "AT+CPIN=";
200209
command += pin;
201210
command += "\r";
202211

203-
this->print(command);
204-
205212
// Can take up to 5 seconds
206-
delay(100);
207213

208-
if ( (_readSerial().indexOf("ER")) == -1)
214+
this->print(command);
215+
216+
if ( (_readSerial(5000).indexOf("ER")) == -1)
209217
{
210218
return false;
211219
}
@@ -229,7 +237,7 @@ String Sim800L::getOperatorsList()
229237

230238
this->print("AT+COPS=?\r");
231239

232-
return _readSerial();
240+
return _readSerial(45000);
233241

234242
}
235243

@@ -243,41 +251,14 @@ String Sim800L::getOperator()
243251
}
244252

245253

246-
//
247-
//PRIVATE METHODS
248-
//
249-
String Sim800L::_readSerial()
250-
{
251-
252-
uint64_t timeOld = millis();
253-
254-
while (!this->available() && !(millis() > timeOld + TIME_OUT_READ_SERIAL))
255-
{
256-
delay(13);
257-
}
258-
259-
String replyString;
260-
261-
while(this->available())
262-
{
263-
if (this->available()>0)
264-
{
265-
replyString += (char) this->read();
266-
}
267-
}
268-
269-
return replyString;
270-
271-
}
272-
273254

274255
//
275256
//PUBLIC METHODS
276257
//
277258

278259
void Sim800L::reset()
279260
{
280-
if (LED_FLAG) digitalWrite(LED_PIN,1);
261+
if (LED_FLAG) digitalWrite(LED_PIN,1);
281262

282263
digitalWrite(RESET_PIN,1);
283264
delay(1000);
@@ -293,8 +274,8 @@ if (LED_FLAG) digitalWrite(LED_PIN,1);
293274

294275
//wait for sms ready
295276
while (_readSerial().indexOf("SMS")==-1 );
296-
297-
if (LED_FLAG) digitalWrite(LED_PIN,0);
277+
278+
if (LED_FLAG) digitalWrite(LED_PIN,0);
298279

299280
}
300281

@@ -423,10 +404,9 @@ bool Sim800L::sendSms(char* number,char* text)
423404
_buffer=_readSerial();
424405
this->print (text);
425406
this->print ("\r");
426-
//change delay 100 to readserial
427407
_buffer=_readSerial();
428408
this->print((char)26);
429-
_buffer=_readSerial();
409+
_buffer=_readSerial(60000);
430410
//expect CMGS:xxx , where xxx is a number,for the sending sms.
431411
if ( (_buffer.indexOf("ER")) == -1)
432412
{
@@ -462,7 +442,8 @@ String Sim800L::readSms(uint8_t index)
462442
// Can take up to 5 seconds
463443

464444
this->print (F("AT+CMGF=1\r"));
465-
if (( _readSerial().indexOf("ER")) ==-1)
445+
446+
if (( _readSerial(5000).indexOf("ER")) ==-1)
466447
{
467448
this->print (F("AT+CMGR="));
468449
this->print (index);
@@ -481,8 +462,10 @@ String Sim800L::readSms(uint8_t index)
481462

482463
bool Sim800L::delAllSms()
483464
{
465+
// Can take up to 25 seconds
466+
484467
this->print(F("at+cmgda=\"del all\"\n\r"));
485-
_buffer=_readSerial();
468+
_buffer=_readSerial(25000);
486469
if ( (_buffer.indexOf("ER")) == -1)
487470
{
488471
return false;
@@ -583,3 +566,57 @@ bool Sim800L::updateRtc(int utc)
583566

584567

585568
}
569+
570+
571+
572+
//
573+
//PRIVATE METHODS
574+
//
575+
String Sim800L::_readSerial()
576+
{
577+
578+
uint64_t timeOld = millis();
579+
580+
while (!this->available() && !(millis() > timeOld + TIME_OUT_READ_SERIAL))
581+
{
582+
delay(13);
583+
}
584+
585+
String str;
586+
587+
while(this->available())
588+
{
589+
if (this->available()>0)
590+
{
591+
str += (char) this->read();
592+
}
593+
}
594+
595+
return str;
596+
597+
}
598+
599+
String Sim800L::_readSerial(uint32_t timeout)
600+
{
601+
602+
uint64_t timeOld = millis();
603+
604+
while (!this->available() && !(millis() > timeOld + timeout))
605+
{
606+
delay(13);
607+
}
608+
609+
String str;
610+
611+
while(this->available())
612+
{
613+
if (this->available()>0)
614+
{
615+
str += (char) this->read();
616+
}
617+
}
618+
619+
return str;
620+
621+
}
622+

0 commit comments

Comments
 (0)