9 #ifndef TinyGsmClientU201_h
10 #define TinyGsmClientU201_h
14 #if !defined(TINY_GSM_RX_BUFFER)
15 #define TINY_GSM_RX_BUFFER 64
18 #define TINY_GSM_MUX_COUNT 5
20 #include "TinyGsmCommon.h"
23 static const char GSM_OK[] TINY_GSM_PROGMEM =
"OK" GSM_NL;
24 static const char GSM_ERROR[] TINY_GSM_PROGMEM =
"ERROR" GSM_NL;
65 sock_connected =
false;
72 virtual int connect(
const char *host, uint16_t port)
77 sock_connected = at->modemConnect(host, port, &mux);
78 at->sockets[mux] =
this;
79 return sock_connected;
82 virtual int connect(
IPAddress ip, uint16_t port)
93 return connect(host.c_str(), port);
99 at->sendAT(GF(
"+USOCL="), mux);
100 sock_connected =
false;
105 virtual size_t write(
const uint8_t *buf,
size_t size)
109 return at->modemSend(buf, size, mux);
112 virtual size_t write(uint8_t c)
117 virtual int available()
120 if (!rx.size() && sock_connected) {
123 return rx.size() + sock_available;
126 virtual int read(uint8_t *buf,
size_t size)
132 size_t chunk = TinyGsmMin(size-cnt, rx.size());
141 if (sock_available > 0) {
142 at->modemRead(rx.free(), mux);
153 if (read(&c, 1) == 1) {
168 virtual uint8_t connected()
173 return sock_connected;
175 virtual operator bool()
184 String remoteIP() TINY_GSM_ATTR_NOT_IMPLEMENTED;
189 uint16_t sock_available;
205 virtual int connect(
const char *host, uint16_t port)
210 sock_connected = at->modemConnect(host, port, &mux,
true);
211 at->sockets[mux] =
this;
212 return sock_connected;
218 #ifdef GSM_DEFAULT_STREAM
219 explicit TinyGsmU201(Stream& stream = GSM_DEFAULT_STREAM)
225 memset(sockets, 0,
sizeof(sockets));
231 bool begin(
const char* pin = NULL)
236 bool init(
const char* pin = NULL)
242 if (waitResponse() != 1) {
245 int ret = getSimStatus();
246 if (ret != SIM_READY && pin != NULL && strlen(pin) > 0) {
249 return (getSimStatus() == SIM_READY);
252 void setBaud(
unsigned long baud)
254 sendAT(GF(
"+IPR="), baud);
257 bool testAT(
unsigned long timeout = 10000L)
259 for (
unsigned long start = millis(); millis() - start < timeout; ) {
261 if (waitResponse(200) == 1) {
272 for (
int mux = 0; mux < TINY_GSM_MUX_COUNT; mux++) {
273 GsmClient* sock = sockets[mux];
274 if (sock && sock->got_data) {
275 sock->got_data =
false;
276 sock->sock_available = modemGetAvailable(mux);
279 while (stream.available()) {
280 waitResponse(10, NULL, NULL);
284 bool factoryDefault()
286 sendAT(GF(
"+UFACTORY=0,1"));
288 sendAT(GF(
"+CFUN=16"));
289 return waitResponse() == 1;
292 String getModemInfo() TINY_GSM_ATTR_NOT_IMPLEMENTED;
308 sendAT(GF(
"+CFUN=16"));
309 if (waitResponse(10000L) != 1) {
316 bool poweroff() TINY_GSM_ATTR_NOT_IMPLEMENTED;
322 bool simUnlock(const
char *pin)
324 sendAT(GF(
"+CPIN=\""), pin, GF(
"\""));
325 return waitResponse() == 1;
331 if (waitResponse(GF(GSM_NL
"+CCID:")) != 1) {
334 String res = stream.readStringUntil(
'\n');
343 if (waitResponse(GF(GSM_NL)) != 1) {
346 String res = stream.readStringUntil(
'\n');
352 SimStatus getSimStatus(
unsigned long timeout = 10000L)
354 for (
unsigned long start = millis(); millis() - start < timeout; ) {
355 sendAT(GF(
"+CPIN?"));
356 if (waitResponse(GF(GSM_NL
"+CPIN:")) != 1) {
360 int status = waitResponse(GF(
"READY"), GF(
"SIM PIN"), GF(
"SIM PUK"), GF(
"NOT INSERTED"));
375 RegStatus getRegistrationStatus()
377 sendAT(GF(
"+CGREG?"));
378 if (waitResponse(GF(GSM_NL
"+CGREG:")) != 1) {
381 streamSkipUntil(
',');
382 int status = stream.readStringUntil(
'\n').toInt();
384 return (RegStatus)status;
389 sendAT(GF(
"+COPS?"));
390 if (waitResponse(GF(GSM_NL
"+COPS:")) != 1) {
393 streamSkipUntil(
'"');
394 String res = stream.readStringUntil(
'"');
403 int getSignalQuality()
406 if (waitResponse(GF(GSM_NL
"+CSQ:")) != 1) {
409 int res = stream.readStringUntil(
',').toInt();
414 bool isNetworkConnected()
416 RegStatus s = getRegistrationStatus();
417 return (s == REG_OK_HOME || s == REG_OK_ROAMING);
420 bool waitForNetwork(
unsigned long timeout = 60000L)
422 for (
unsigned long start = millis(); millis() - start < timeout; ) {
423 if (isNetworkConnected()) {
434 bool gprsConnect(
const char* apn,
const char* user = NULL,
const char* pwd = NULL)
438 sendAT(GF(
"+CGATT=1"));
441 sendAT(GF(
"+UPSD=0,1,\""), apn,
'"');
444 if (user && strlen(user) > 0) {
445 sendAT(GF(
"+UPSD=0,2,\""), user,
'"');
448 if (pwd && strlen(pwd) > 0) {
449 sendAT(GF(
"+UPSD=0,3,\""), pwd,
'"');
453 sendAT(GF(
"+UPSD=0,7,\"0.0.0.0\""));
456 sendAT(GF(
"+UPSDA=0,3"));
460 sendAT(GF(
"+UPSND=0,8"));
461 if (waitResponse(GF(
",8,1")) != 1) {
467 bool gprsDisconnect()
469 sendAT(GF(
"+UPSDA=0,4"));
470 if (waitResponse(60000L) != 1) {
474 sendAT(GF(
"+CGATT=0"));
475 if (waitResponse(60000L) != 1) {
482 bool isGprsConnected()
484 sendAT(GF(
"+CGATT?"));
485 if (waitResponse(GF(GSM_NL
"+CGATT:")) != 1) {
488 int res = stream.readStringUntil(
'\n').toInt();
494 sendAT(GF(
"+CIFSR"));
495 if (waitResponse() != 1) {
504 sendAT(GF(
"+CIFSR;E0"));
506 if (waitResponse(10000L, res) != 1) {
515 return TinyGsmIpFromString(getLocalIP());
522 bool setGsmBusy(
bool busy =
true) TINY_GSM_ATTR_NOT_IMPLEMENTED;
524 bool callAnswer() TINY_GSM_ATTR_NOT_IMPLEMENTED;
526 bool callNumber(const String& number) TINY_GSM_ATTR_NOT_IMPLEMENTED;
528 bool callHangup() TINY_GSM_ATTR_NOT_IMPLEMENTED;
534 String sendUSSD(const String& code) TINY_GSM_ATTR_NOT_IMPLEMENTED;
536 bool sendSMS(const String& number, const String& text) TINY_GSM_ATTR_NOT_IMPLEMENTED;
538 bool sendSMS_UTF16(const String& number, const
void* text,
539 size_t len) TINY_GSM_ATTR_NOT_IMPLEMENTED;
546 String getGsmLocation()
548 sendAT(GF(
"+ULOC=2,3,0,120,1"));
549 if (waitResponse(GF(GSM_NL
"+UULOC:")) != 1) {
552 String res = stream.readStringUntil(
'\n');
562 uint16_t getBattVoltage()
565 if (waitResponse(GF(GSM_NL
"+CIND:")) != 1) {
569 uint16_t res = stream.readStringUntil(
',').toInt();
574 int getBattPercent() TINY_GSM_ATTR_NOT_IMPLEMENTED;
578 bool modemConnect(const
char* host, uint16_t port, uint8_t* mux,
bool ssl = false)
580 sendAT(GF(
"+USOCR=6"));
581 if (waitResponse(GF(GSM_NL
"+USOCR:")) != 1) {
584 *mux = stream.readStringUntil(
'\n').toInt();
588 sendAT(GF(
"+USOSEC="), *mux,
",1");
592 sendAT(GF(
"+USOCO="), *mux,
",\"", host,
"\",", port);
593 int rsp = waitResponse(75000L);
597 int modemSend(
const void* buff,
size_t len, uint8_t mux)
599 sendAT(GF(
"+USOWR="), mux,
',', len);
600 if (waitResponse(GF(
"@")) != 1) {
605 stream.write((uint8_t*)buff, len);
607 if (waitResponse(GF(GSM_NL
"+USOWR:")) != 1) {
610 streamSkipUntil(
',');
611 return stream.readStringUntil(
'\n').toInt();
614 size_t modemRead(
size_t size, uint8_t mux)
616 sendAT(GF(
"+USORD="), mux,
',', size);
617 if (waitResponse(GF(GSM_NL
"+USORD:")) != 1) {
620 streamSkipUntil(
',');
621 size_t len = stream.readStringUntil(
',').toInt();
622 streamSkipUntil(
'\"');
624 for (
size_t i=0; i<len; i++) {
625 while (!stream.available()) {
628 char c = stream.read();
629 sockets[mux]->rx.put(c);
631 streamSkipUntil(
'\"');
636 size_t modemGetAvailable(uint8_t mux)
638 sendAT(GF(
"+USORD="), mux,
',', 0);
640 if (waitResponse(GF(GSM_NL
"+USORD:")) == 1) {
641 streamSkipUntil(
',');
642 result = stream.readStringUntil(
'\n').toInt();
646 sockets[mux]->sock_connected = modemGetConnected(mux);
651 bool modemGetConnected(uint8_t mux)
653 sendAT(GF(
"+USOCTL="), mux,
",10");
654 if (waitResponse(GF(GSM_NL
"+USOCTL:")) != 1) {
658 streamSkipUntil(
',');
659 streamSkipUntil(
',');
660 int result = stream.readStringUntil(
'\n').toInt();
670 void streamWrite(T
last)
675 template<
typename T,
typename... Args>
676 void streamWrite(T head, Args... tail)
679 streamWrite(tail...);
682 bool streamSkipUntil(
char c)
685 while (!stream.available()) {
688 if (stream.read() == c) {
695 template<
typename... Args>
696 void sendAT(Args... cmd)
698 streamWrite(
"AT", cmd..., GSM_NL);
705 uint8_t waitResponse(uint32_t timeout, String&
data,
706 GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR),
707 GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL)
717 unsigned long startMillis = millis();
720 while (stream.available() > 0) {
721 int a = stream.read();
726 if (r1 &&
data.endsWith(r1)) {
729 }
else if (r2 &&
data.endsWith(r2)) {
732 }
else if (r3 &&
data.endsWith(r3)) {
735 }
else if (r4 &&
data.endsWith(r4)) {
738 }
else if (r5 &&
data.endsWith(r5)) {
741 }
else if (
data.endsWith(GF(GSM_NL
"+UUSORD:"))) {
742 int mux = stream.readStringUntil(
',').toInt();
743 if (mux >= 0 && mux < TINY_GSM_MUX_COUNT && sockets[mux]) {
744 sockets[mux]->got_data =
true;
747 }
else if (
data.endsWith(GF(GSM_NL
"+UUSOCL:"))) {
748 int mux = stream.readStringUntil(
'\n').toInt();
749 if (mux >= 0 && mux < TINY_GSM_MUX_COUNT && sockets[mux]) {
750 sockets[mux]->sock_connected =
false;
753 DBG(
"### Closed: ", mux);
756 }
while (millis() - startMillis < timeout);
761 DBG(
"### Unhandled:",
data);
768 uint8_t waitResponse(uint32_t timeout,
769 GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR),
770 GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL)
773 return waitResponse(timeout,
data, r1, r2, r3, r4, r5);
776 uint8_t waitResponse(GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR),
777 GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL)
779 return waitResponse(1000, r1, r2, r3, r4, r5);
786 GsmClient* sockets[TINY_GSM_MUX_COUNT];