From 3b0e70441d95890ae7c3c9b64a836978aa168509 Mon Sep 17 00:00:00 2001 From: SRGDamia1 Date: Sun, 9 Apr 2017 01:39:49 -0400 Subject: [PATCH 01/15] Added XBee (untested) --- TinyGsmClient.h | 2 + TinyGsmClientSIM800.h | 4 +- TinyGsmClientXBee.h | 551 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 555 insertions(+), 2 deletions(-) create mode 100644 TinyGsmClientXBee.h diff --git a/TinyGsmClient.h b/TinyGsmClient.h index b9de490..2ed9b17 100644 --- a/TinyGsmClient.h +++ b/TinyGsmClient.h @@ -17,6 +17,8 @@ #include #elif defined(TINY_GSM_MODEM_ESP8266) #include +#elif defined(TINY_GSM_MODEM_XBEE) + #include #else #error "Please define GSM modem model" #endif diff --git a/TinyGsmClientSIM800.h b/TinyGsmClientSIM800.h index cceac96..f0c8738 100644 --- a/TinyGsmClientSIM800.h +++ b/TinyGsmClientSIM800.h @@ -343,11 +343,11 @@ public: sendAT(GF("+SAPBR=3,1,\"APN\",\""), apn, '"'); waitResponse(); - if (!strcmp(user, "")) { + if (user) { sendAT(GF("+SAPBR=3,1,\"USER\",\""), user, '"'); waitResponse(); } - if (!strcmp(pwd, "")) { + if (pwd) { sendAT(GF("+SAPBR=3,1,\"PWD\",\""), pwd, '"'); waitResponse(); } diff --git a/TinyGsmClientXBee.h b/TinyGsmClientXBee.h new file mode 100644 index 0000000..ff7f1e2 --- /dev/null +++ b/TinyGsmClientXBee.h @@ -0,0 +1,551 @@ +/** + * @file TinyWiFiClientESP8266.h + * @author Volodymyr Shymanskyy + * @license LGPL-3.0 + * @copyright Copyright (c) 2016 Volodymyr Shymanskyy + * @date Nov 2016 + */ + +#ifndef TinyWiFiClientXBee_h +#define TinyWiFiClientXBee_h + +#define TINY_GSM_DEBUG Serial + +#if !defined(TINY_GSM_RX_BUFFER) + #define TINY_GSM_RX_BUFFER 256 +#endif + +#include + +#define GSM_NL "\r" +static const char GSM_OK[] TINY_GSM_PROGMEM = "OK" GSM_NL; +static const char GSM_ERROR[] TINY_GSM_PROGMEM = "ERROR" GSM_NL; + +enum RegStatus { + REG_UNREGISTERED = 0, + REG_SEARCHING = 2, + REG_DENIED = 3, + REG_OK_HOME = 1, + REG_OK_ROAMING = 5, + REG_UNKNOWN = 4, +}; + +class TinyGsm +{ + +public: + TinyGsm(Stream& stream) + : stream(stream) + {} + +public: + +class GsmClient : public Client +{ + friend class TinyGsm; + typedef TinyGsmFifo RxFifo; + +public: + GsmClient() {} + + GsmClient(TinyGsm& modem, uint8_t mux = 1) { + init(&modem, mux); + } + + bool init(TinyGsm* modem, uint8_t mux = 1) { + this->at = modem; + this->mux = mux; + sock_connected = false; + + at->sockets[mux] = this; + + return true; + } + +public: + virtual int connect(const char *host, uint16_t port) { + TINY_GSM_YIELD(); + rx.clear(); + at->commandMode(); + sock_connected = at->modemConnect(host, port, mux); + at->writeChanges(); + at->exitCommand(); + return sock_connected; + } + + virtual int connect(IPAddress ip, uint16_t port) { + TINY_GSM_YIELD(); + rx.clear(); + at->commandMode(); + sock_connected = at->modemConnect(ip, port, mux); + at->writeChanges(); + at->exitCommand(); + return sock_connected; + } + + virtual void stop() { + sock_connected = false; + } + + virtual size_t write(const uint8_t *buf, size_t size) { + TINY_GSM_YIELD(); + //at->maintain(); + return at->modemSend(buf, size, mux); + } + + virtual size_t write(uint8_t c) { + return write(&c, 1); + } + + virtual int available() { + TINY_GSM_YIELD(); + if (!rx.size()) { + at->maintain(); + } + return rx.size(); + } + + virtual int read(uint8_t *buf, size_t size) { + TINY_GSM_YIELD(); + size_t cnt = 0; + while (cnt < size) { + size_t chunk = TinyGsmMin(size-cnt, rx.size()); + if (chunk > 0) { + rx.get(buf, chunk); + buf += chunk; + cnt += chunk; + continue; + } + // TODO: Read directly into user buffer? + if (!rx.size()) { + at->maintain(); + //break; + } + } + return cnt; + } + + virtual int read() { + uint8_t c; + if (read(&c, 1) == 1) { + return c; + } + return -1; + } + + virtual int peek() { return at->stream.peek(); } + virtual void flush() { at->stream.flush(); } + + virtual uint8_t connected() { + if (available()) { + return true; + } + return sock_connected; + } + virtual operator bool() { return connected(); } +private: + TinyGsm* at; + uint8_t mux; + bool sock_connected; + RxFifo rx; +}; + +public: + + /* + * Basic functions + */ + bool begin() { + return init(); + } + + bool init() { + if (!autoBaud()) { + return false; + } + return true; + } + + bool autoBaud(unsigned long timeout = 10000L) { // not supported + return false; + } + + void maintain() { + //while (stream.available()) { + waitResponse(10, NULL, NULL); + //} + } + + bool factoryDefault() { + commandMode(); + sendAT(GF("RE")); + bool ret_val = waitResponse() == 1; + writeChanges(); + exitCommand(); + return ret_val; + } + + /* + * Power functions + */ + + bool restart() { + commandMode(); + sendAT(GF("FR")); + if (waitResponse() != 1) { + return false; + } + delay (2000); // Actually resets about 2 seconds later + for (unsigned long start = millis(); millis() - start < 60000L; ) { + if (commandMode()) { + exitCommand(); + return true; + } + } + exitCommand(); + return false;; + } + + /* + * SIM card & Networ Operator functions + */ + + bool simUnlock(const char *pin) { // Not supported + return false; + } + + String getSimCCID() { + commandMode(); + sendAT(GF("S#")); + String res = streamReadUntil('\r'); + exitCommand(); + return res; + } + + String getIMEI() { + commandMode(); + sendAT(GF("IM")); + String res = streamReadUntil('\r'); + exitCommand(); + return res; + } + + int getSignalQuality() { + commandMode(); + sendAT(GF("DB")); + char buf[4] = { 0, }; + buf[0] = streamRead(); + buf[1] = streamRead(); + buf[2] = streamRead(); + buf[3] = streamRead(); + exitCommand(); + int intr = strtol(buf, 0, 16); + return intr; + } + + RegStatus getRegistrationStatus() { + commandMode(); + sendAT(GF("AI")); + String res = streamReadUntil('\r'); + exitCommand(); + + if(res == GF("0x00")) + return REG_OK_HOME; + + else if(res == GF("0x13") || res == GF("0x2A")) + return REG_UNREGISTERED; + + else if(res == GF("0xFF") || res == GF("0x22") || res == GF("0x23") || + res == GF("0x40") || res == GF("0x41") || res == GF("0x42")) + return REG_SEARCHING; + + else if(res == GF("0x24")) + return REG_DENIED; + + else return REG_UNKNOWN; + } + + String getOperator() { + commandMode(); + sendAT(GF("MN")); + String res = streamReadUntil('\r'); + exitCommand(); + return res; + } + + + bool waitForNetwork(unsigned long timeout = 60000L) { + for (unsigned long start = millis(); millis() - start < timeout; ) { + if (modemGetConnected()) { + return true; + } + delay(1000); + } + return false; + } + + /* + * WiFi functions + */ + bool networkConnect(const char* ssid, const char* pwd) { + + commandMode(); + + sendAT(GF("AP"), 0); // Put in transparent mode + waitResponse(); + sendAT(GF("IP"), 1); // Put in TCP mode + waitResponse(); + + sendAT(GF("ID"), ssid); + if (waitResponse() != 1) { + goto fail; + } + + sendAT(GF("PK"), pwd); + if (waitResponse() != 1) { + goto fail; + } + + writeChanges(); + exitCommand(); + + return true; + + fail: + exitCommand(); + return false; + } + + bool networkDisconnect() { + return false; + } + + /* + * GPRS functions + */ + bool gprsConnect(const char* apn) { + + commandMode(); + + sendAT(GF("AP"), 0); // Put in transparent mode + waitResponse(); + sendAT(GF("IP"), 0); // Put in UDP mode + waitResponse(); + + sendAT(GF("AN"), apn); + waitResponse(); + + return true; + } + + bool gprsDisconnect() { // TODO + return false; + } + + /* + * Messaging functions + */ + + void sendUSSD() { + } + + void sendSMS() { + } + + bool sendSMS(const String& number, const String& text) { + commandMode(); + sendAT(GF("AP"), 0); + waitResponse(); + sendAT(GF("IP"), 2); + waitResponse(); + sendAT(GF("PH"), number); + waitResponse(); + sendAT(GF("TD D")); + waitResponse(); + sendAT(GF("TD D")); + waitResponse(); + writeChanges(); + exitCommand(); + stream.print(text); + stream.write((char)0x0D); + return true; + } + + /* Public Utilities */ + template + void sendAT(Args... cmd) { + streamWrite("AT", cmd..., GSM_NL); + stream.flush(); + TINY_GSM_YIELD(); + DBG(GSM_NL, ">>> AT:", cmd...); + } + + bool commandMode(void){ + delay(1000); // cannot send anything for 1 second before entering command mode + streamWrite("+++"); // enter command mode + waitResponse(1100); + return 1 == waitResponse(1100); // wait another second for an "OK\r" + } + + void writeChanges(void){ + streamWrite("ATWR", GSM_NL); // Write changes to flash + waitResponse(); + streamWrite("ATAC", GSM_NL); // Apply changes + waitResponse(); + } + + void exitCommand(void){ + streamWrite("ATCN", GSM_NL); // Exit command mode + waitResponse(); + } + + // TODO: Optimize this! + uint8_t waitResponse(uint32_t timeout, String& data, + GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR), + GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL) + { + /*String r1s(r1); r1s.trim(); + String r2s(r2); r2s.trim(); + String r3s(r3); r3s.trim(); + String r4s(r4); r4s.trim(); + String r5s(r5); r5s.trim(); + DBG("### ..:", r1s, ",", r2s, ",", r3s, ",", r4s, ",", r5s);*/ + data.reserve(64); + int index = 0; + unsigned long startMillis = millis(); + do { + TINY_GSM_YIELD(); + while (stream.available() > 0) { + int a = streamRead(); + if (a <= 0) continue; // Skip 0x00 bytes, just in case + data += (char)a; + if (r1 && data.endsWith(r1)) { + index = 1; + goto finish; + } else if (r2 && data.endsWith(r2)) { + index = 2; + goto finish; + } else if (r3 && data.endsWith(r3)) { + index = 3; + goto finish; + } else if (r4 && data.endsWith(r4)) { + index = 4; + goto finish; + } else if (r5 && data.endsWith(r5)) { + index = 5; + goto finish; + } else if (data.endsWith(GF(GSM_NL "+IPD,"))) { + int mux = stream.readStringUntil(',').toInt(); + int len = stream.readStringUntil(':').toInt(); + if (len > sockets[mux]->rx.free()) { + DBG("### Buffer overflow: ", len, "->", sockets[mux]->rx.free()); + } else { + DBG("### Got: ", len, "->", sockets[mux]->rx.free()); + } + while (len--) { + while (!stream.available()) {} + sockets[mux]->rx.put(stream.read()); + } + data = ""; + return index; + } else if (data.endsWith(GF(GSM_NL "1,CLOSED" GSM_NL))) { //TODO: use mux + sockets[1]->sock_connected = false; + data = ""; + } + } + } while (millis() - startMillis < timeout); + finish: + if (!index) { + data.trim(); + if (data.length()) { + DBG("### Unhandled:", data); + } + data = ""; + } + return index; + } + + uint8_t waitResponse(uint32_t timeout, + GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR), + GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL) + { + String data; + return waitResponse(timeout, data, r1, r2, r3, r4, r5); + } + + uint8_t waitResponse(GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR), + GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL) + { + return waitResponse(1000, r1, r2, r3, r4, r5); + } + +private: + int modemConnect(const char* host, uint16_t port, uint8_t mux = 1) { + sendAT(GF("LA"), host); + String ipadd; ipadd.reserve(16); + ipadd = streamReadUntil('\r'); + IPAddress ip; + ip.fromString(ipadd); + return modemConnect(ip, port); + } + + int modemConnect(IPAddress ip, uint16_t port, uint8_t mux = 1) { + String host; host.reserve(16); + host += ip[0]; + host += "."; + host += ip[1]; + host += "."; + host += ip[2]; + host += "."; + host += ip[3]; + sendAT(GF("DL"), host); + waitResponse(); + sendAT(GF("DE"), String(port, HEX)); + int rsp = waitResponse(); + return rsp; + } + + int modemSend(const void* buff, size_t len, uint8_t mux = 1) { + stream.write((uint8_t*)buff, len); + return len; + } + + bool modemGetConnected(uint8_t mux = 1) { + commandMode(); + sendAT(GF("AI")); + int res = waitResponse(GF("0")); + exitCommand(); + return 1 == res; + } + + /* Private Utilities */ + template + void streamWrite(T last) { + stream.print(last); + } + + template + void streamWrite(T head, Args... tail) { + stream.print(head); + streamWrite(tail...); + } + + int streamRead() { return stream.read(); } + + String streamReadUntil(char c) { + String return_string = stream.readStringUntil(c); + return_string.trim(); + if (String(c) == GSM_NL || String(c) == "\n"){ + DBG(return_string, c, " "); + } else DBG(return_string, c); + return return_string; + } + +private: + Stream& stream; + GsmClient* sockets[1]; +}; + +typedef TinyGsm::GsmClient TinyGsmClient; + +#endif From 0a5baf7e2a102db282904be82fc85b0ebadffb5a Mon Sep 17 00:00:00 2001 From: SRGDamia1 Date: Sun, 9 Apr 2017 02:10:40 -0400 Subject: [PATCH 02/15] Debug off by default --- TinyGsmClientSIM800.h | 4 +++- TinyGsmClientXBee.h | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/TinyGsmClientSIM800.h b/TinyGsmClientSIM800.h index f0c8738..046483a 100644 --- a/TinyGsmClientSIM800.h +++ b/TinyGsmClientSIM800.h @@ -674,9 +674,11 @@ private: buf[0] = streamRead(); buf[1] = streamRead(); char c = strtol(buf, NULL, 16); + // DBG(c); #else while (!stream.available()) {} - char c = streamRead(); // DBG(c); + char c = streamRead(); + // DBG(c); #endif sockets[mux]->rx.put(c); } diff --git a/TinyGsmClientXBee.h b/TinyGsmClientXBee.h index ff7f1e2..69f9ff4 100644 --- a/TinyGsmClientXBee.h +++ b/TinyGsmClientXBee.h @@ -9,7 +9,7 @@ #ifndef TinyWiFiClientXBee_h #define TinyWiFiClientXBee_h -#define TINY_GSM_DEBUG Serial +// #define TINY_GSM_DEBUG Serial #if !defined(TINY_GSM_RX_BUFFER) #define TINY_GSM_RX_BUFFER 256 From 282d89119b9ec82998b974608f18d094bd187254 Mon Sep 17 00:00:00 2001 From: SRGDamia1 Date: Sun, 9 Apr 2017 12:04:01 -0400 Subject: [PATCH 03/15] Unified interface a bit more --- TinyGsmClientA6.h | 11 +++++++++++ TinyGsmClientESP8266.h | 11 +++++++++++ TinyGsmClientM590.h | 12 +++++++++++- TinyGsmClientSIM800.h | 22 ++++++++++++++++------ TinyGsmClientXBee.h | 40 ++++++++++++++++++++-------------------- 5 files changed, 69 insertions(+), 27 deletions(-) diff --git a/TinyGsmClientA6.h b/TinyGsmClientA6.h index 4c4aa6f..4b9e80e 100644 --- a/TinyGsmClientA6.h +++ b/TinyGsmClientA6.h @@ -315,6 +315,17 @@ public: return false; } + /* + * WiFi functions + */ + bool networkConnect(const char* ssid, const char* pwd) { + return false; + } + + bool networkDisconnect() { + return false; + } + /* * GPRS functions */ diff --git a/TinyGsmClientESP8266.h b/TinyGsmClientESP8266.h index 7d51d5a..d6dad03 100644 --- a/TinyGsmClientESP8266.h +++ b/TinyGsmClientESP8266.h @@ -233,6 +233,17 @@ public: return waitResponse(10000L) == 1; } + /* + * GPRS functions + */ + bool gprsConnect(const char* apn, const char* user, const char* pwd) { + return false; + } + + bool gprsDisconnect() { + return false; + } + /* Public Utilities */ template void sendAT(Args... cmd) { diff --git a/TinyGsmClientM590.h b/TinyGsmClientM590.h index 670e550..7cee3fe 100644 --- a/TinyGsmClientM590.h +++ b/TinyGsmClientM590.h @@ -324,6 +324,17 @@ public: return false; } + /* + * WiFi functions + */ + bool networkConnect(const char* ssid, const char* pwd) { + return false; + } + + bool networkDisconnect() { + return false; + } + /* * GPRS functions */ @@ -529,7 +540,6 @@ private: } int modemConnect(const char* host, uint16_t port, uint8_t mux) { - int rsp = 0; for (int i=0; i<3; i++) { String ip = dnsIpQuery(host); diff --git a/TinyGsmClientSIM800.h b/TinyGsmClientSIM800.h index 046483a..8a65522 100644 --- a/TinyGsmClientSIM800.h +++ b/TinyGsmClientSIM800.h @@ -331,6 +331,17 @@ public: return false; } + /* + * WiFi functions + */ + bool networkConnect(const char* ssid, const char* pwd) { + return false; + } + + bool networkDisconnect() { + return false; + } + /* * GPRS functions */ @@ -570,21 +581,21 @@ public: index = 5; goto finish; } else if (data.endsWith(GF(GSM_NL "+CIPRXGET:"))) { + index = 6; String mode = streamReadUntil(','); if (mode.toInt() == 1) { mux = streamReadUntil('\n').toInt(); gotData = true; - data = ""; } else { data += mode; } } else if (data.endsWith(GF("CLOSED" GSM_NL))) { + index = 7; int nl = data.lastIndexOf(GSM_NL, data.length()-8); int coma = data.indexOf(',', nl+2); mux = data.substring(nl+2, coma).toInt(); if (mux) { sockets[mux]->sock_connected = false; - data = ""; } } } @@ -595,7 +606,6 @@ public: if (data.length()) { DBG("### Unhandled:", data); } - data = ""; } else { data.trim(); @@ -604,11 +614,11 @@ public: if (data.length()) { DBG(GSM_NL, "<<< ", data); } - data = ""; } if (gotData) { sockets[mux]->sock_available = modemGetAvailable(mux); } + data = ""; return index; } @@ -674,11 +684,11 @@ private: buf[0] = streamRead(); buf[1] = streamRead(); char c = strtol(buf, NULL, 16); - // DBG(c); + DBG(c); #else while (!stream.available()) {} char c = streamRead(); - // DBG(c); + DBG(c); #endif sockets[mux]->rx.put(c); } diff --git a/TinyGsmClientXBee.h b/TinyGsmClientXBee.h index 69f9ff4..2d926ba 100644 --- a/TinyGsmClientXBee.h +++ b/TinyGsmClientXBee.h @@ -323,7 +323,7 @@ public: /* * GPRS functions */ - bool gprsConnect(const char* apn) { + bool gprsConnect(const char* apn, const char* user = "", const char* pw = "") { commandMode(); @@ -380,25 +380,6 @@ public: DBG(GSM_NL, ">>> AT:", cmd...); } - bool commandMode(void){ - delay(1000); // cannot send anything for 1 second before entering command mode - streamWrite("+++"); // enter command mode - waitResponse(1100); - return 1 == waitResponse(1100); // wait another second for an "OK\r" - } - - void writeChanges(void){ - streamWrite("ATWR", GSM_NL); // Write changes to flash - waitResponse(); - streamWrite("ATAC", GSM_NL); // Apply changes - waitResponse(); - } - - void exitCommand(void){ - streamWrite("ATCN", GSM_NL); // Exit command mode - waitResponse(); - } - // TODO: Optimize this! uint8_t waitResponse(uint32_t timeout, String& data, GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR), @@ -541,6 +522,25 @@ private: return return_string; } + bool commandMode(void){ + delay(1000); // cannot send anything for 1 second before entering command mode + streamWrite(GF("+++")); // enter command mode + waitResponse(1100); + return 1 == waitResponse(1100); // wait another second for an "OK\r" + } + + void writeChanges(void){ + sendAT(GF("WR")); // Write changes to flash + waitResponse(); + sendAT(GF("AC")); // Apply changes + waitResponse(); + } + + void exitCommand(void){ + sendAT(GF("CN")); // Exit command mode + waitResponse(); + } + private: Stream& stream; GsmClient* sockets[1]; From ffc07b15fab9fff9f46b1e15ca75b68f00c52312 Mon Sep 17 00:00:00 2001 From: SRGDamia1 Date: Sun, 9 Apr 2017 12:42:08 -0400 Subject: [PATCH 04/15] Corrected name in common --- TinyGsmClient.h | 2 +- TinyGsmClientXBee.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/TinyGsmClient.h b/TinyGsmClient.h index 2ed9b17..cb2fa08 100644 --- a/TinyGsmClient.h +++ b/TinyGsmClient.h @@ -18,7 +18,7 @@ #elif defined(TINY_GSM_MODEM_ESP8266) #include #elif defined(TINY_GSM_MODEM_XBEE) - #include + #include #else #error "Please define GSM modem model" #endif diff --git a/TinyGsmClientXBee.h b/TinyGsmClientXBee.h index 2d926ba..2e12cb1 100644 --- a/TinyGsmClientXBee.h +++ b/TinyGsmClientXBee.h @@ -6,8 +6,8 @@ * @date Nov 2016 */ -#ifndef TinyWiFiClientXBee_h -#define TinyWiFiClientXBee_h +#ifndef TinyGsmClientXBee_h +#define TinyGsmClientXBee_h // #define TINY_GSM_DEBUG Serial From 5602888f876d9de24842aeecb16f16af94343fb9 Mon Sep 17 00:00:00 2001 From: SRGDamia1 Date: Sun, 9 Apr 2017 14:13:29 -0400 Subject: [PATCH 05/15] Tweaked Bee a bit --- TinyGsmClientXBee.h | 48 ++++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/TinyGsmClientXBee.h b/TinyGsmClientXBee.h index 2e12cb1..6b7134c 100644 --- a/TinyGsmClientXBee.h +++ b/TinyGsmClientXBee.h @@ -21,6 +21,12 @@ static const char GSM_OK[] TINY_GSM_PROGMEM = "OK" GSM_NL; static const char GSM_ERROR[] TINY_GSM_PROGMEM = "ERROR" GSM_NL; +enum SimStatus { + SIM_ERROR = 0, + SIM_READY = 1, + SIM_LOCKED = 2, +}; + enum RegStatus { REG_UNREGISTERED = 0, REG_SEARCHING = 2, @@ -30,6 +36,7 @@ enum RegStatus { REG_UNKNOWN = 4, }; + class TinyGsm { @@ -160,9 +167,7 @@ public: } bool init() { - if (!autoBaud()) { - return false; - } + factoryDefault(); return true; } @@ -243,6 +248,10 @@ public: return intr; } + SimStatus getSimStatus(unsigned long timeout = 10000L) { + return SIM_READY; // unsupported + } + RegStatus getRegistrationStatus() { commandMode(); sendAT(GF("AI")); @@ -317,7 +326,7 @@ public: } bool networkDisconnect() { - return false; + return false; // Doesn't support disconnecting } /* @@ -377,7 +386,7 @@ public: streamWrite("AT", cmd..., GSM_NL); stream.flush(); TINY_GSM_YIELD(); - DBG(GSM_NL, ">>> AT:", cmd...); + DBG("\r\n", ">>> AT:", cmd...); } // TODO: Optimize this! @@ -415,23 +424,6 @@ public: } else if (r5 && data.endsWith(r5)) { index = 5; goto finish; - } else if (data.endsWith(GF(GSM_NL "+IPD,"))) { - int mux = stream.readStringUntil(',').toInt(); - int len = stream.readStringUntil(':').toInt(); - if (len > sockets[mux]->rx.free()) { - DBG("### Buffer overflow: ", len, "->", sockets[mux]->rx.free()); - } else { - DBG("### Got: ", len, "->", sockets[mux]->rx.free()); - } - while (len--) { - while (!stream.available()) {} - sockets[mux]->rx.put(stream.read()); - } - data = ""; - return index; - } else if (data.endsWith(GF(GSM_NL "1,CLOSED" GSM_NL))) { //TODO: use mux - sockets[1]->sock_connected = false; - data = ""; } } } while (millis() - startMillis < timeout); @@ -443,6 +435,18 @@ public: } data = ""; } + else { + data.trim(); + data.replace(GSM_NL GSM_NL, GSM_NL); + data.replace(GSM_NL, "\r\n" " "); + if (data.length()) { + DBG(GSM_NL, "<<< ", data); + } + } + // if (gotData) { + // sockets[mux]->sock_available = modemGetAvailable(mux); + // } + data = ""; return index; } From 7e806775695ef6e5b3bb77ce5f42e76e270e6b7c Mon Sep 17 00:00:00 2001 From: SRGDamia1 Date: Sun, 9 Apr 2017 14:24:06 -0400 Subject: [PATCH 06/15] A bit of debugging --- TinyGsmClientXBee.h | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/TinyGsmClientXBee.h b/TinyGsmClientXBee.h index 6b7134c..bd92e3f 100644 --- a/TinyGsmClientXBee.h +++ b/TinyGsmClientXBee.h @@ -338,12 +338,15 @@ public: sendAT(GF("AP"), 0); // Put in transparent mode waitResponse(); - sendAT(GF("IP"), 0); // Put in UDP mode + sendAT(GF("IP"), 1); // Put in TCP mode waitResponse(); - sendAT(GF("AN"), apn); + sendAT(GF("AN"), apn); // Set the APN waitResponse(); + writeChanges(); + exitCommand(); + return true; } @@ -430,10 +433,11 @@ public: finish: if (!index) { data.trim(); + data.replace(GSM_NL GSM_NL, GSM_NL); + data.replace(GSM_NL, "\r\n" " "); if (data.length()) { DBG("### Unhandled:", data); } - data = ""; } else { data.trim(); @@ -529,6 +533,7 @@ private: bool commandMode(void){ delay(1000); // cannot send anything for 1 second before entering command mode streamWrite(GF("+++")); // enter command mode + DBG("+++\r\n"); waitResponse(1100); return 1 == waitResponse(1100); // wait another second for an "OK\r" } From ad278fb834b06440f45f25e398280d1c93d3710b Mon Sep 17 00:00:00 2001 From: SRGDamia1 Date: Sun, 9 Apr 2017 14:30:24 -0400 Subject: [PATCH 07/15] Extra debugging --- TinyGsmClientXBee.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/TinyGsmClientXBee.h b/TinyGsmClientXBee.h index bd92e3f..c3d8031 100644 --- a/TinyGsmClientXBee.h +++ b/TinyGsmClientXBee.h @@ -519,7 +519,11 @@ private: streamWrite(tail...); } - int streamRead() { return stream.read(); } + int streamRead() { + int c = stream.read(); + DBG((char)c) + return c; + } String streamReadUntil(char c) { String return_string = stream.readStringUntil(c); From e4716cabc4041240808b33433fc930fd8ba59601 Mon Sep 17 00:00:00 2001 From: SRGDamia1 Date: Sun, 9 Apr 2017 14:31:25 -0400 Subject: [PATCH 08/15] DBG --- TinyGsmClientXBee.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TinyGsmClientXBee.h b/TinyGsmClientXBee.h index c3d8031..a5cf2ac 100644 --- a/TinyGsmClientXBee.h +++ b/TinyGsmClientXBee.h @@ -537,7 +537,7 @@ private: bool commandMode(void){ delay(1000); // cannot send anything for 1 second before entering command mode streamWrite(GF("+++")); // enter command mode - DBG("+++\r\n"); + DBG("\r\n+++\r\n"); waitResponse(1100); return 1 == waitResponse(1100); // wait another second for an "OK\r" } From baa9768532cc156b2e893e12833c87a3aef2ff22 Mon Sep 17 00:00:00 2001 From: SRGDamia1 Date: Sun, 9 Apr 2017 14:34:04 -0400 Subject: [PATCH 09/15] Fixed missing semicolon --- TinyGsmClientXBee.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TinyGsmClientXBee.h b/TinyGsmClientXBee.h index a5cf2ac..684d8f4 100644 --- a/TinyGsmClientXBee.h +++ b/TinyGsmClientXBee.h @@ -521,7 +521,7 @@ private: int streamRead() { int c = stream.read(); - DBG((char)c) + DBG((char)c); return c; } From b0827e9565897995c3ae38745fe88d05cd9c162c Mon Sep 17 00:00:00 2001 From: SRGDamia1 Date: Thu, 13 Apr 2017 01:37:26 -0400 Subject: [PATCH 10/15] Minor debugging tweaks --- TinyGsmClientSIM800.h | 1 - TinyGsmClientXBee.h | 12 ++++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/TinyGsmClientSIM800.h b/TinyGsmClientSIM800.h index 8a65522..62c0bf9 100644 --- a/TinyGsmClientSIM800.h +++ b/TinyGsmClientSIM800.h @@ -618,7 +618,6 @@ public: if (gotData) { sockets[mux]->sock_available = modemGetAvailable(mux); } - data = ""; return index; } diff --git a/TinyGsmClientXBee.h b/TinyGsmClientXBee.h index 684d8f4..3dd48a0 100644 --- a/TinyGsmClientXBee.h +++ b/TinyGsmClientXBee.h @@ -285,7 +285,12 @@ public: bool waitForNetwork(unsigned long timeout = 60000L) { for (unsigned long start = millis(); millis() - start < timeout; ) { - if (modemGetConnected()) { + commandMode(); + sendAT(GF("AI")); + waitResponse(); + String res = streamReadUntil('\r'); + exitCommand(); + if (res == 0) { return true; } delay(1000); @@ -389,7 +394,7 @@ public: streamWrite("AT", cmd..., GSM_NL); stream.flush(); TINY_GSM_YIELD(); - DBG("\r\n", ">>> AT:", cmd...); + DBG("\r\n", ">>> AT ", cmd..., "\r\n"); } // TODO: Optimize this! @@ -444,13 +449,12 @@ public: data.replace(GSM_NL GSM_NL, GSM_NL); data.replace(GSM_NL, "\r\n" " "); if (data.length()) { - DBG(GSM_NL, "<<< ", data); + DBG("\r\n", "<<< ", data); } } // if (gotData) { // sockets[mux]->sock_available = modemGetAvailable(mux); // } - data = ""; return index; } From 4522c31ee2ee1c39920f9225e35e6e7cf46e688b Mon Sep 17 00:00:00 2001 From: SRGDamia1 Date: Thu, 13 Apr 2017 12:37:58 -0400 Subject: [PATCH 11/15] IT WORKS! --- TinyGsmClientXBee.h | 91 ++++++++++++++++++--------------------------- 1 file changed, 36 insertions(+), 55 deletions(-) diff --git a/TinyGsmClientXBee.h b/TinyGsmClientXBee.h index 3dd48a0..fe36ba6 100644 --- a/TinyGsmClientXBee.h +++ b/TinyGsmClientXBee.h @@ -90,7 +90,7 @@ public: return sock_connected; } - virtual void stop() { + virtual void stop() { // Not supported sock_connected = false; } @@ -106,38 +106,16 @@ public: virtual int available() { TINY_GSM_YIELD(); - if (!rx.size()) { - at->maintain(); - } - return rx.size(); + return at->stream.available(); } virtual int read(uint8_t *buf, size_t size) { - TINY_GSM_YIELD(); - size_t cnt = 0; - while (cnt < size) { - size_t chunk = TinyGsmMin(size-cnt, rx.size()); - if (chunk > 0) { - rx.get(buf, chunk); - buf += chunk; - cnt += chunk; - continue; - } - // TODO: Read directly into user buffer? - if (!rx.size()) { - at->maintain(); - //break; - } - } - return cnt; + return available(); } virtual int read() { - uint8_t c; - if (read(&c, 1) == 1) { - return c; - } - return -1; + TINY_GSM_YIELD(); + return at->stream.read(); } virtual int peek() { return at->stream.peek(); } @@ -167,7 +145,15 @@ public: } bool init() { - factoryDefault(); + guardTime = 1100; + commandMode(); + sendAT(GF("AP0")); // Put in transparent mode + waitResponse(); + sendAT(GF("GTFA")); // shorten the guard time to 250ms + waitResponse(); + writeChanges(); + exitCommand(); + guardTime = 300; return true; } @@ -222,7 +208,7 @@ public: String getSimCCID() { commandMode(); sendAT(GF("S#")); - String res = streamReadUntil('\r'); + String res = streamReadUntil('\r'); // Does not send an OK, just the result exitCommand(); return res; } @@ -230,7 +216,7 @@ public: String getIMEI() { commandMode(); sendAT(GF("IM")); - String res = streamReadUntil('\r'); + String res = streamReadUntil('\r'); // Does not send an OK, just the result exitCommand(); return res; } @@ -238,7 +224,7 @@ public: int getSignalQuality() { commandMode(); sendAT(GF("DB")); - char buf[4] = { 0, }; + char buf[4] = { 0, }; // Does not send an OK, just the result buf[0] = streamRead(); buf[1] = streamRead(); buf[2] = streamRead(); @@ -255,7 +241,7 @@ public: RegStatus getRegistrationStatus() { commandMode(); sendAT(GF("AI")); - String res = streamReadUntil('\r'); + String res = streamReadUntil('\r'); // Does not send an OK, just the result exitCommand(); if(res == GF("0x00")) @@ -277,7 +263,7 @@ public: String getOperator() { commandMode(); sendAT(GF("MN")); - String res = streamReadUntil('\r'); + String res = streamReadUntil('\r'); // Does not send an OK, just the result exitCommand(); return res; } @@ -287,10 +273,9 @@ public: for (unsigned long start = millis(); millis() - start < timeout; ) { commandMode(); sendAT(GF("AI")); - waitResponse(); - String res = streamReadUntil('\r'); + String res = streamReadUntil('\r'); // Does not send an OK, just the result exitCommand(); - if (res == 0) { + if (res == GF("0")) { return true; } delay(1000); @@ -309,6 +294,8 @@ public: waitResponse(); sendAT(GF("IP"), 1); // Put in TCP mode waitResponse(); + sendAT(GF("EE"), 2); // Set security to WPA2 + waitResponse(); sendAT(GF("ID"), ssid); if (waitResponse() != 1) { @@ -338,20 +325,15 @@ public: * GPRS functions */ bool gprsConnect(const char* apn, const char* user = "", const char* pw = "") { - commandMode(); - sendAT(GF("AP"), 0); // Put in transparent mode waitResponse(); sendAT(GF("IP"), 1); // Put in TCP mode waitResponse(); - sendAT(GF("AN"), apn); // Set the APN waitResponse(); - writeChanges(); exitCommand(); - return true; } @@ -371,20 +353,18 @@ public: bool sendSMS(const String& number, const String& text) { commandMode(); - sendAT(GF("AP"), 0); - waitResponse(); - sendAT(GF("IP"), 2); + sendAT(GF("AP"), 0); // Put in transparent mode waitResponse(); - sendAT(GF("PH"), number); + sendAT(GF("IP"), 2); // Put in text messaging mode waitResponse(); - sendAT(GF("TD D")); + sendAT(GF("PH"), number); // Set the phone number waitResponse(); - sendAT(GF("TD D")); + sendAT(GF("TDD")); // Set the text delimiter to the standard 0x0D (carriabe return) waitResponse(); writeChanges(); exitCommand(); stream.print(text); - stream.write((char)0x0D); + stream.write((char)0x0D); // close off with the carriage return return true; } @@ -394,7 +374,7 @@ public: streamWrite("AT", cmd..., GSM_NL); stream.flush(); TINY_GSM_YIELD(); - DBG("\r\n", ">>> AT ", cmd..., "\r\n"); + DBG(">>> AT ", cmd..., "\r\n"); } // TODO: Optimize this! @@ -447,9 +427,9 @@ public: else { data.trim(); data.replace(GSM_NL GSM_NL, GSM_NL); - data.replace(GSM_NL, "\r\n" " "); + data.replace(GSM_NL, "\r\n "); if (data.length()) { - DBG("\r\n", "<<< ", data); + DBG("<<< ", data, "\r\n"); } } // if (gotData) { @@ -532,17 +512,17 @@ private: String streamReadUntil(char c) { String return_string = stream.readStringUntil(c); return_string.trim(); - if (String(c) == GSM_NL || String(c) == "\n"){ - DBG(return_string, c, " "); + if (String(c) == GSM_NL){ + DBG(return_string, "\r\n"); } else DBG(return_string, c); return return_string; } bool commandMode(void){ - delay(1000); // cannot send anything for 1 second before entering command mode + delay(guardTime); // cannot send anything for 1 second before entering command mode streamWrite(GF("+++")); // enter command mode DBG("\r\n+++\r\n"); - waitResponse(1100); + waitResponse(guardTime); return 1 == waitResponse(1100); // wait another second for an "OK\r" } @@ -559,6 +539,7 @@ private: } private: + int guardTime; Stream& stream; GsmClient* sockets[1]; }; From eac16fb02ef3cc67f1dd4def1b543112223c2f88 Mon Sep 17 00:00:00 2001 From: SRGDamia1 Date: Thu, 13 Apr 2017 13:51:33 -0400 Subject: [PATCH 12/15] Added pin sleep, removed FIFO --- TinyGsmClientXBee.h | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/TinyGsmClientXBee.h b/TinyGsmClientXBee.h index fe36ba6..1231ce9 100644 --- a/TinyGsmClientXBee.h +++ b/TinyGsmClientXBee.h @@ -50,7 +50,6 @@ public: class GsmClient : public Client { friend class TinyGsm; - typedef TinyGsmFifo RxFifo; public: GsmClient() {} @@ -72,7 +71,6 @@ public: public: virtual int connect(const char *host, uint16_t port) { TINY_GSM_YIELD(); - rx.clear(); at->commandMode(); sock_connected = at->modemConnect(host, port, mux); at->writeChanges(); @@ -82,7 +80,6 @@ public: virtual int connect(IPAddress ip, uint16_t port) { TINY_GSM_YIELD(); - rx.clear(); at->commandMode(); sock_connected = at->modemConnect(ip, port, mux); at->writeChanges(); @@ -132,7 +129,6 @@ private: TinyGsm* at; uint8_t mux; bool sock_connected; - RxFifo rx; }; public: @@ -149,11 +145,11 @@ public: commandMode(); sendAT(GF("AP0")); // Put in transparent mode waitResponse(); - sendAT(GF("GTFA")); // shorten the guard time to 250ms + sendAT(GF("GTC8")); // shorten the guard time to 200ms waitResponse(); writeChanges(); exitCommand(); - guardTime = 300; + guardTime = 225; return true; } @@ -197,6 +193,16 @@ public: return false;; } + void setupPinSleep() { + commandMode(); + sendAT(GF("SM"),1); + waitResponse(); + sendAT(GF("SO"),200); + waitResponse(); + writeChanges(); + exitCommand(); + } + /* * SIM card & Networ Operator functions */ From 88fe00e98e8bcc00bad698288ae12afb2db8fe9f Mon Sep 17 00:00:00 2001 From: SRGDamia1 Date: Thu, 13 Apr 2017 14:10:11 -0400 Subject: [PATCH 13/15] Added hack to close sockets --- TinyGsmClientXBee.h | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/TinyGsmClientXBee.h b/TinyGsmClientXBee.h index 1231ce9..9cd7276 100644 --- a/TinyGsmClientXBee.h +++ b/TinyGsmClientXBee.h @@ -87,7 +87,21 @@ public: return sock_connected; } - virtual void stop() { // Not supported + // This is a hack to shut the socket by setting the timeout to zero and + // then sending an empty line to the server. + virtual void stop() { + TINY_GSM_YIELD(); + at->commandMode(); + at->sendAT(GF("TM0")); // Set socket timeout to 0; + at->writeChanges(); + at->exitCommand(); + at->modemSend("", 1, mux); + at->waitResponse(); + delay(200); + at->commandMode(); + at->sendAT(GF("TM64")); // Set socket timeout back to 10seconds; + at->writeChanges(); + at->exitCommand(); sock_connected = false; } From 5794d7b9101fa889ab5ad4f0d21a6bc6a2ec496d Mon Sep 17 00:00:00 2001 From: SRGDamia1 Date: Thu, 13 Apr 2017 14:37:29 -0400 Subject: [PATCH 14/15] Added missing wait resopnse --- TinyGsmClientXBee.h | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/TinyGsmClientXBee.h b/TinyGsmClientXBee.h index 9cd7276..adb7546 100644 --- a/TinyGsmClientXBee.h +++ b/TinyGsmClientXBee.h @@ -93,13 +93,15 @@ public: TINY_GSM_YIELD(); at->commandMode(); at->sendAT(GF("TM0")); // Set socket timeout to 0; + at->waitResponse(); at->writeChanges(); at->exitCommand(); at->modemSend("", 1, mux); at->waitResponse(); - delay(200); + delay(100); at->commandMode(); at->sendAT(GF("TM64")); // Set socket timeout back to 10seconds; + at->waitResponse(); at->writeChanges(); at->exitCommand(); sock_connected = false; @@ -159,11 +161,11 @@ public: commandMode(); sendAT(GF("AP0")); // Put in transparent mode waitResponse(); - sendAT(GF("GTC8")); // shorten the guard time to 200ms + sendAT(GF("GT64")); // shorten the guard time to 100ms waitResponse(); writeChanges(); exitCommand(); - guardTime = 225; + guardTime = 125; return true; } From cdfad5274d1862495c86eb0deedfa462998af0c1 Mon Sep 17 00:00:00 2001 From: SRGDamia1 Date: Thu, 13 Apr 2017 15:33:11 -0400 Subject: [PATCH 15/15] Extra wait to clear buffer --- TinyGsmClient.h | 2 +- TinyGsmClientXBee.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/TinyGsmClient.h b/TinyGsmClient.h index cb2fa08..421914c 100644 --- a/TinyGsmClient.h +++ b/TinyGsmClient.h @@ -9,7 +9,7 @@ #ifndef TinyGsmClient_h #define TinyGsmClient_h -#if defined(TINY_GSM_MODEM_SIM800) || defined(TINY_GSM_MODEM_SIM900) +#if defined(TINY_GSM_MODEM_SIM800) || defined(TINY_GSM_MODEM_SIM900) #include #elif defined(TINY_GSM_MODEM_A6) || defined(TINY_GSM_MODEM_A7) #include diff --git a/TinyGsmClientXBee.h b/TinyGsmClientXBee.h index adb7546..e88f43d 100644 --- a/TinyGsmClientXBee.h +++ b/TinyGsmClientXBee.h @@ -98,7 +98,7 @@ public: at->exitCommand(); at->modemSend("", 1, mux); at->waitResponse(); - delay(100); + at->waitResponse(); // To clear the buffer at->commandMode(); at->sendAT(GF("TM64")); // Set socket timeout back to 10seconds; at->waitResponse();