9 #ifndef TinyGsmClientXBee_h
10 #define TinyGsmClientXBee_h
14 #if !defined(TINY_GSM_RX_BUFFER)
15 #define TINY_GSM_RX_BUFFER 256
18 #define TINY_GSM_MUX_COUNT 1 // Multi-plexing isn't supported using command mode
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;
52 class GsmClient :
public Client
59 GsmClient(
TinyGsm& modem, uint8_t mux = 0)
64 bool init(
TinyGsm* modem, uint8_t mux = 0)
68 sock_connected =
false;
70 at->sockets[mux] =
this;
76 virtual int connect(
const char *host, uint16_t port)
80 sock_connected = at->modemConnect(host, port, mux,
false);
83 return sock_connected;
86 virtual int connect(
IPAddress ip, uint16_t port)
90 sock_connected = at->modemConnect(ip, port, mux,
false);
93 return sock_connected;
102 at->sendAT(GF(
"TM0"));
106 at->modemSend(
"", 1, mux);
108 at->sendAT(GF(
"TM64"));
113 sock_connected =
false;
116 virtual size_t write(
const uint8_t *buf,
size_t size)
120 return at->modemSend(buf, size, mux);
123 virtual size_t write(uint8_t c)
128 virtual int available()
131 return at->stream.available();
134 virtual int read(uint8_t *buf,
size_t size)
137 return at->stream.readBytes(buf, size);
143 return at->stream.read();
148 return at->stream.peek();
155 virtual uint8_t connected()
160 return sock_connected;
162 virtual operator bool()
171 String remoteIP() TINY_GSM_ATTR_NOT_IMPLEMENTED;
179 class GsmClientSecure : public GsmClient
184 GsmClientSecure(
TinyGsm& modem, uint8_t mux = 1)
185 : GsmClient(modem, mux)
189 virtual int connect(
const char *host, uint16_t port)
193 sock_connected = at->modemConnect(host, port, mux,
true);
196 return sock_connected;
199 virtual int connect(
IPAddress ip, uint16_t port)
203 sock_connected = at->modemConnect(ip, port, mux,
true);
206 return sock_connected;
212 explicit TinyGsm(Stream& stream)
215 memset(sockets, 0,
sizeof(sockets));
237 unsigned long startMillis = millis();
238 while (!stream.available() && millis() - startMillis < 1000) {};
239 String res = streamReadUntil(
'\r');
250 bool testAT(
unsigned long timeout = 10000L)
252 for (
unsigned long start = millis(); millis() - start < timeout; ) {
255 if (waitResponse(200) == 1) {
267 bool factoryDefault()
271 bool ret_val = waitResponse() == 1;
279 if (beeType == S6B) {
294 if (waitResponse() != 1) {
298 for (
unsigned long start = millis(); millis() - start < 60000L; ) {
313 if (beeType == S6B) {
314 sendAT(GF(
"SO"),200);
325 bool simUnlock(
const char *pin)
335 unsigned long startMillis = millis();
336 while (!stream.available() && millis() - startMillis < 1000) {};
337 String res = streamReadUntil(
'\r');
347 unsigned long startMillis = millis();
348 while (!stream.available() && millis() - startMillis < 1000) {};
349 String res = streamReadUntil(
'\r');
354 SimStatus getSimStatus(
unsigned long timeout = 10000L)
359 RegStatus getRegistrationStatus()
364 unsigned long startMillis = millis();
365 while (!stream.available() && millis() - startMillis < 1000) {};
366 String res = streamReadUntil(
'\r');
373 else if(res == GF(
"13") || res == GF(
"2A")) {
374 return REG_UNREGISTERED;
377 else if(res == GF(
"FF") || res == GF(
"22") || res == GF(
"23") ||
378 res == GF(
"40") || res == GF(
"41") || res == GF(
"42")) {
379 return REG_SEARCHING;
382 else if(res == GF(
"24") || res == GF(
"25") || res == GF(
"27")) {
396 unsigned long startMillis = millis();
397 while (!stream.available() && millis() - startMillis < 1000) {};
398 String res = streamReadUntil(
'\r');
407 int getSignalQuality()
410 if (beeType == S6B) {
416 unsigned long startMillis = millis();
417 while (!stream.available() && millis() - startMillis < 1000) {};
419 buf[0] = streamRead();
420 buf[1] = streamRead();
423 int intr = strtol(buf, 0, 16);
424 if (beeType == S6B) {
431 bool isNetworkConnected()
433 RegStatus s = getRegistrationStatus();
434 return (s == REG_OK_HOME || s == REG_OK_ROAMING);
437 bool waitForNetwork(
unsigned long timeout = 60000L)
439 for (
unsigned long start = millis(); millis() - start < timeout; ) {
440 if (isNetworkConnected()) {
451 bool networkConnect(
const char* ssid,
const char* pwd)
459 sendAT(GF(
"ID"), ssid);
460 if (waitResponse() != 1) {
464 sendAT(GF(
"PK"), pwd);
465 if (waitResponse() != 1) {
479 bool networkDisconnect()
491 unsigned long startMillis = millis();
492 while (stream.available() < 8 && millis() - startMillis < 30000) {};
493 IPaddr = streamReadUntil(
'\r');
499 return TinyGsmIpFromString(getLocalIP());
505 bool gprsConnect(
const char* apn,
const char* user = NULL,
const char* pwd = NULL)
508 sendAT(GF(
"AN"), apn);
515 bool gprsDisconnect()
524 String sendUSSD(
const String& code) TINY_GSM_ATTR_NOT_IMPLEMENTED;
526 bool sendSMS(
const String& number,
const String& text)
531 sendAT(GF(
"PH"), number);
538 stream.write((
char)0x0D);
545 int modemConnect(
const char* host, uint16_t port, uint8_t mux = 0,
bool ssl =
false)
547 sendAT(GF(
"LA"), host);
551 unsigned long startMillis = millis();
552 while (stream.available() < 8 && millis() - startMillis < 30000) {};
553 strIP = streamReadUntil(
'\r');
554 IPAddress ip = TinyGsmIpFromString(strIP);
555 return modemConnect(ip, port, mux, ssl);
558 int modemConnect(
IPAddress ip, uint16_t port, uint8_t mux = 0,
bool ssl =
false)
576 sendAT(GF(
"DL"), host);
578 sendAT(GF(
"DE"), String(port, HEX));
579 int rsp = waitResponse();
583 int modemSend(
const void* buff,
size_t len, uint8_t mux = 0)
585 stream.write((uint8_t*)buff, len);
590 bool modemGetConnected(uint8_t mux = 0)
594 int res = waitResponse(GF(
"0"));
604 void streamWrite(T
last)
609 template<
typename T,
typename... Args>
610 void streamWrite(T head, Args... tail)
613 streamWrite(tail...);
618 return stream.read();
621 String streamReadUntil(
char c)
624 String return_string = stream.readStringUntil(c);
625 return_string.trim();
627 return return_string;
630 void streamClear(
void)
632 while (stream.available()) {
637 bool commandMode(
void)
640 streamWrite(GF(
"+++"));
642 return 1 == waitResponse(guardTime*2);
645 void writeChanges(
void)
653 void exitCommand(
void)
659 template<
typename... Args>
660 void sendAT(Args... cmd)
662 streamWrite(
"AT", cmd..., GSM_NL);
669 uint8_t waitResponse(uint32_t timeout, String&
data,
670 GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR),
671 GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL)
681 unsigned long startMillis = millis();
684 while (stream.available() > 0) {
685 int a = streamRead();
690 if (r1 &&
data.endsWith(r1)) {
693 }
else if (r2 &&
data.endsWith(r2)) {
696 }
else if (r3 &&
data.endsWith(r3)) {
699 }
else if (r4 &&
data.endsWith(r4)) {
702 }
else if (r5 &&
data.endsWith(r5)) {
707 }
while (millis() - startMillis < timeout);
711 data.replace(GSM_NL GSM_NL, GSM_NL);
712 data.replace(GSM_NL,
"\r\n" " ");
714 DBG(
"### Unhandled:",
data,
"\r\n");
716 DBG(
"### NO RESPONSE!\r\n");
720 data.replace(GSM_NL GSM_NL, GSM_NL);
721 data.replace(GSM_NL,
"\r\n ");
729 uint8_t waitResponse(uint32_t timeout,
730 GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR),
731 GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL)
734 return waitResponse(timeout,
data, r1, r2, r3, r4, r5);
737 uint8_t waitResponse(GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR),
738 GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL)
740 return waitResponse(1000, r1, r2, r3, r4, r5);
749 GsmClient* sockets[TINY_GSM_MUX_COUNT];