|
@ -31,7 +31,7 @@ enum SimStatus { |
|
|
|
|
|
|
|
|
enum XBeeType { |
|
|
enum XBeeType { |
|
|
S6B = 0, |
|
|
S6B = 0, |
|
|
LTEC1 = 1, |
|
|
|
|
|
|
|
|
LTEC1 = 1, |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
enum RegStatus { |
|
|
enum RegStatus { |
|
@ -74,7 +74,7 @@ public: |
|
|
virtual int connect(const char *host, uint16_t port) { |
|
|
virtual int connect(const char *host, uint16_t port) { |
|
|
at->streamClear(); // Empty anything remaining in the buffer; |
|
|
at->streamClear(); // Empty anything remaining in the buffer; |
|
|
at->commandMode(); |
|
|
at->commandMode(); |
|
|
sock_connected = at->modemConnect(host, port, mux); |
|
|
|
|
|
|
|
|
sock_connected = at->modemConnect(host, port, mux, false); |
|
|
at->writeChanges(); |
|
|
at->writeChanges(); |
|
|
at->exitCommand(); |
|
|
at->exitCommand(); |
|
|
return sock_connected; |
|
|
return sock_connected; |
|
@ -83,7 +83,7 @@ public: |
|
|
virtual int connect(IPAddress ip, uint16_t port) { |
|
|
virtual int connect(IPAddress ip, uint16_t port) { |
|
|
at->streamClear(); // Empty anything remaining in the buffer; |
|
|
at->streamClear(); // Empty anything remaining in the buffer; |
|
|
at->commandMode(); |
|
|
at->commandMode(); |
|
|
sock_connected = at->modemConnect(ip, port, mux); |
|
|
|
|
|
|
|
|
sock_connected = at->modemConnect(ip, port, mux, false); |
|
|
at->writeChanges(); |
|
|
at->writeChanges(); |
|
|
at->exitCommand(); |
|
|
at->exitCommand(); |
|
|
return sock_connected; |
|
|
return sock_connected; |
|
@ -92,13 +92,13 @@ public: |
|
|
// This is a hack to shut the socket by setting the timeout to zero and |
|
|
// This is a hack to shut the socket by setting the timeout to zero and |
|
|
// then sending an empty line to the server. |
|
|
// then sending an empty line to the server. |
|
|
virtual void stop() { |
|
|
virtual void stop() { |
|
|
|
|
|
at->streamClear(); // Empty anything remaining in the buffer; |
|
|
at->commandMode(); |
|
|
at->commandMode(); |
|
|
at->sendAT(GF("TM0")); // Set socket timeout to 0; |
|
|
at->sendAT(GF("TM0")); // Set socket timeout to 0; |
|
|
at->waitResponse(); |
|
|
at->waitResponse(); |
|
|
at->writeChanges(); |
|
|
at->writeChanges(); |
|
|
at->exitCommand(); |
|
|
at->exitCommand(); |
|
|
at->modemSend("", 1, mux); |
|
|
at->modemSend("", 1, mux); |
|
|
at->streamClear(); // Empty anything remaining in the buffer; |
|
|
|
|
|
at->commandMode(); |
|
|
at->commandMode(); |
|
|
at->sendAT(GF("TM64")); // Set socket timeout back to 10seconds; |
|
|
at->sendAT(GF("TM64")); // Set socket timeout back to 10seconds; |
|
|
at->waitResponse(); |
|
|
at->waitResponse(); |
|
@ -124,7 +124,8 @@ public: |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
virtual int read(uint8_t *buf, size_t size) { |
|
|
virtual int read(uint8_t *buf, size_t size) { |
|
|
return available(); |
|
|
|
|
|
|
|
|
TINY_GSM_YIELD(); |
|
|
|
|
|
return at->stream.readBytes(buf, size); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
virtual int read() { |
|
|
virtual int read() { |
|
@ -155,6 +156,35 @@ private: |
|
|
bool sock_connected; |
|
|
bool sock_connected; |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
class GsmClientSecure : public GsmClient |
|
|
|
|
|
{ |
|
|
|
|
|
public: |
|
|
|
|
|
GsmClientSecure() {} |
|
|
|
|
|
|
|
|
|
|
|
GsmClientSecure(TinyGsm& modem, uint8_t mux = 1) |
|
|
|
|
|
: GsmClient(modem, mux) |
|
|
|
|
|
{} |
|
|
|
|
|
|
|
|
|
|
|
public: |
|
|
|
|
|
virtual int connect(const char *host, uint16_t port) { |
|
|
|
|
|
at->streamClear(); // Empty anything remaining in the buffer; |
|
|
|
|
|
at->commandMode(); |
|
|
|
|
|
sock_connected = at->modemConnect(host, port, mux, true); |
|
|
|
|
|
at->writeChanges(); |
|
|
|
|
|
at->exitCommand(); |
|
|
|
|
|
return sock_connected; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
virtual int connect(IPAddress ip, uint16_t port) { |
|
|
|
|
|
at->streamClear(); // Empty anything remaining in the buffer; |
|
|
|
|
|
at->commandMode(); |
|
|
|
|
|
sock_connected = at->modemConnect(ip, port, mux, true); |
|
|
|
|
|
at->writeChanges(); |
|
|
|
|
|
at->exitCommand(); |
|
|
|
|
|
return sock_connected; |
|
|
|
|
|
} |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
public: |
|
|
public: |
|
|
|
|
|
|
|
|
TinyGsm(Stream& stream) |
|
|
TinyGsm(Stream& stream) |
|
@ -190,7 +220,18 @@ public: |
|
|
return true; |
|
|
return true; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
bool testAT(unsigned long timeout = 10000L) { // not supported |
|
|
|
|
|
|
|
|
bool testAT(unsigned long timeout = 10000L) { |
|
|
|
|
|
for (unsigned long start = millis(); millis() - start < timeout; ) { |
|
|
|
|
|
if (commandMode()) |
|
|
|
|
|
{ |
|
|
|
|
|
sendAT(); |
|
|
|
|
|
if (waitResponse(200) == 1) { |
|
|
|
|
|
return true; |
|
|
|
|
|
} |
|
|
|
|
|
exitCommand(); |
|
|
|
|
|
} |
|
|
|
|
|
delay(100); |
|
|
|
|
|
} |
|
|
return false; |
|
|
return false; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -205,6 +246,11 @@ public: |
|
|
return ret_val; |
|
|
return ret_val; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool hasSSL() { |
|
|
|
|
|
if (beeType == S6B) return false; |
|
|
|
|
|
else return true; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
/* |
|
|
/* |
|
|
* Power functions |
|
|
* Power functions |
|
|
*/ |
|
|
*/ |
|
@ -230,8 +276,10 @@ public: |
|
|
commandMode(); |
|
|
commandMode(); |
|
|
sendAT(GF("SM"),1); |
|
|
sendAT(GF("SM"),1); |
|
|
waitResponse(); |
|
|
waitResponse(); |
|
|
sendAT(GF("SO"),200); |
|
|
|
|
|
waitResponse(); |
|
|
|
|
|
|
|
|
if (beeType == S6B) { |
|
|
|
|
|
sendAT(GF("SO"),200); |
|
|
|
|
|
waitResponse(); |
|
|
|
|
|
} |
|
|
writeChanges(); |
|
|
writeChanges(); |
|
|
exitCommand(); |
|
|
exitCommand(); |
|
|
} |
|
|
} |
|
@ -272,8 +320,7 @@ public: |
|
|
|
|
|
|
|
|
RegStatus getRegistrationStatus() { |
|
|
RegStatus getRegistrationStatus() { |
|
|
commandMode(); |
|
|
commandMode(); |
|
|
if (beeType == S6B) sendAT(GF("AI")); |
|
|
|
|
|
else sendAT(GF("CI")); |
|
|
|
|
|
|
|
|
sendAT(GF("AI")); |
|
|
// wait for the response |
|
|
// wait for the response |
|
|
unsigned long startMillis = millis(); |
|
|
unsigned long startMillis = millis(); |
|
|
while (!stream.available() && millis() - startMillis < 1000) {}; |
|
|
while (!stream.available() && millis() - startMillis < 1000) {}; |
|
@ -290,7 +337,7 @@ public: |
|
|
res == GF("40") || res == GF("41") || res == GF("42")) |
|
|
res == GF("40") || res == GF("41") || res == GF("42")) |
|
|
return REG_SEARCHING; |
|
|
return REG_SEARCHING; |
|
|
|
|
|
|
|
|
else if(res == GF("24")) |
|
|
|
|
|
|
|
|
else if(res == GF("24") || res == GF("25") || res == GF("27")) |
|
|
return REG_DENIED; |
|
|
return REG_DENIED; |
|
|
|
|
|
|
|
|
else return REG_UNKNOWN; |
|
|
else return REG_UNKNOWN; |
|
@ -314,34 +361,31 @@ public: |
|
|
int getSignalQuality() { |
|
|
int getSignalQuality() { |
|
|
commandMode(); |
|
|
commandMode(); |
|
|
if (beeType == S6B) sendAT(GF("LM")); // ask for the "link margin" - the dB above sensitivity |
|
|
if (beeType == S6B) sendAT(GF("LM")); // ask for the "link margin" - the dB above sensitivity |
|
|
else sendAT(GF("DB")); // ask for the cell strenght in dBm |
|
|
|
|
|
|
|
|
else sendAT(GF("DB")); // ask for the cell strength in dBm |
|
|
// wait for the response |
|
|
// wait for the response |
|
|
unsigned long startMillis = millis(); |
|
|
unsigned long startMillis = millis(); |
|
|
while (!stream.available() && millis() - startMillis < 1000) {}; |
|
|
while (!stream.available() && millis() - startMillis < 1000) {}; |
|
|
char buf[2] = {0}; // Set up buffer for response |
|
|
char buf[2] = {0}; // Set up buffer for response |
|
|
buf[0] = streamRead(); |
|
|
buf[0] = streamRead(); |
|
|
buf[1] = streamRead(); |
|
|
buf[1] = streamRead(); |
|
|
DBG(buf[0], buf[1], "\n"); |
|
|
|
|
|
|
|
|
// DBG(buf[0], buf[1], "\n"); |
|
|
exitCommand(); |
|
|
exitCommand(); |
|
|
int intr = strtol(buf, 0, 16); |
|
|
int intr = strtol(buf, 0, 16); |
|
|
if (beeType == S6B) return -93 + intr; // the maximum sensitivity is -93dBm |
|
|
if (beeType == S6B) return -93 + intr; // the maximum sensitivity is -93dBm |
|
|
else return -1*intr; // need to convert to negative number |
|
|
else return -1*intr; // need to convert to negative number |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool isNetworkConnected() { |
|
|
|
|
|
RegStatus s = getRegistrationStatus(); |
|
|
|
|
|
return (s == REG_OK_HOME || s == REG_OK_ROAMING); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
bool waitForNetwork(unsigned long timeout = 60000L) { |
|
|
bool waitForNetwork(unsigned long timeout = 60000L) { |
|
|
for (unsigned long start = millis(); millis() - start < timeout; ) { |
|
|
for (unsigned long start = millis(); millis() - start < timeout; ) { |
|
|
commandMode(); |
|
|
|
|
|
if (beeType == S6B) sendAT(GF("AI")); |
|
|
|
|
|
else sendAT(GF("CI")); |
|
|
|
|
|
// wait for the response |
|
|
|
|
|
unsigned long startMillis = millis(); |
|
|
|
|
|
while (!stream.available() && millis() - startMillis < 1000) {}; |
|
|
|
|
|
String res = streamReadUntil('\r'); // Does not send an OK, just the result |
|
|
|
|
|
exitCommand(); |
|
|
|
|
|
if (res == GF("0")) { |
|
|
|
|
|
|
|
|
if (isNetworkConnected()) { |
|
|
return true; |
|
|
return true; |
|
|
} |
|
|
} |
|
|
delay(1000); |
|
|
|
|
|
|
|
|
delay(250); |
|
|
} |
|
|
} |
|
|
return false; |
|
|
return false; |
|
|
} |
|
|
} |
|
@ -439,7 +483,7 @@ fail: |
|
|
|
|
|
|
|
|
private: |
|
|
private: |
|
|
|
|
|
|
|
|
int modemConnect(const char* host, uint16_t port, uint8_t mux = 0) { |
|
|
|
|
|
|
|
|
int modemConnect(const char* host, uint16_t port, uint8_t mux = 0, bool ssl = false) { |
|
|
sendAT(GF("LA"), host); |
|
|
sendAT(GF("LA"), host); |
|
|
String strIP; strIP.reserve(16); |
|
|
String strIP; strIP.reserve(16); |
|
|
// wait for the response |
|
|
// wait for the response |
|
@ -447,10 +491,10 @@ private: |
|
|
while (stream.available() < 8 && millis() - startMillis < 30000) {}; |
|
|
while (stream.available() < 8 && millis() - startMillis < 30000) {}; |
|
|
strIP = streamReadUntil('\r'); // read result |
|
|
strIP = streamReadUntil('\r'); // read result |
|
|
IPAddress ip = TinyGsmIpFromString(strIP); |
|
|
IPAddress ip = TinyGsmIpFromString(strIP); |
|
|
return modemConnect(ip, port); |
|
|
|
|
|
|
|
|
return modemConnect(ip, port, mux, ssl); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
int modemConnect(IPAddress ip, uint16_t port, uint8_t mux = 0) { |
|
|
|
|
|
|
|
|
int modemConnect(IPAddress ip, uint16_t port, uint8_t mux = 0, bool ssl = false) { |
|
|
String host; host.reserve(16); |
|
|
String host; host.reserve(16); |
|
|
host += ip[0]; |
|
|
host += ip[0]; |
|
|
host += "."; |
|
|
host += "."; |
|
@ -459,8 +503,13 @@ private: |
|
|
host += ip[2]; |
|
|
host += ip[2]; |
|
|
host += "."; |
|
|
host += "."; |
|
|
host += ip[3]; |
|
|
host += ip[3]; |
|
|
sendAT(GF("IP"), 1); // Put in TCP mode |
|
|
|
|
|
waitResponse(); |
|
|
|
|
|
|
|
|
if (ssl) { |
|
|
|
|
|
sendAT(GF("IP"), 4); // Put in TCP mode |
|
|
|
|
|
waitResponse(); |
|
|
|
|
|
} else { |
|
|
|
|
|
sendAT(GF("IP"), 1); // Put in TCP mode |
|
|
|
|
|
waitResponse(); |
|
|
|
|
|
} |
|
|
sendAT(GF("DL"), host); // Set the "Destination Address Low" |
|
|
sendAT(GF("DL"), host); // Set the "Destination Address Low" |
|
|
waitResponse(); |
|
|
waitResponse(); |
|
|
sendAT(GF("DE"), String(port, HEX)); // Set the destination port |
|
|
sendAT(GF("DE"), String(port, HEX)); // Set the destination port |
|
@ -476,8 +525,7 @@ private: |
|
|
|
|
|
|
|
|
bool modemGetConnected(uint8_t mux = 0) { |
|
|
bool modemGetConnected(uint8_t mux = 0) { |
|
|
commandMode(); |
|
|
commandMode(); |
|
|
if (beeType == S6B) sendAT(GF("AI")); |
|
|
|
|
|
else sendAT(GF("CI")); |
|
|
|
|
|
|
|
|
sendAT(GF("AI")); |
|
|
int res = waitResponse(GF("0")); |
|
|
int res = waitResponse(GF("0")); |
|
|
exitCommand(); |
|
|
exitCommand(); |
|
|
return 1 == res; |
|
|
return 1 == res; |
|
@ -504,9 +552,7 @@ public: |
|
|
TINY_GSM_YIELD(); |
|
|
TINY_GSM_YIELD(); |
|
|
String return_string = stream.readStringUntil(c); |
|
|
String return_string = stream.readStringUntil(c); |
|
|
return_string.trim(); |
|
|
return_string.trim(); |
|
|
if (String(c) == GSM_NL) { |
|
|
|
|
|
DBG(return_string, "\r\n"); |
|
|
|
|
|
} else DBG(return_string, c); |
|
|
|
|
|
|
|
|
// DBG(return_string, c); |
|
|
return return_string; |
|
|
return return_string; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -517,7 +563,7 @@ public: |
|
|
bool commandMode(void) { |
|
|
bool commandMode(void) { |
|
|
delay(guardTime); // 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 |
|
|
streamWrite(GF("+++")); // enter command mode |
|
|
DBG("\r\n+++\r\n"); |
|
|
|
|
|
|
|
|
// DBG("\r\n+++\r\n"); |
|
|
return 1 == waitResponse(guardTime*2); |
|
|
return 1 == waitResponse(guardTime*2); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -594,7 +640,7 @@ finish: |
|
|
data.replace(GSM_NL GSM_NL, GSM_NL); |
|
|
data.replace(GSM_NL GSM_NL, GSM_NL); |
|
|
data.replace(GSM_NL, "\r\n "); |
|
|
data.replace(GSM_NL, "\r\n "); |
|
|
if (data.length()) { |
|
|
if (data.length()) { |
|
|
DBG("<<< ", data, "\r\n"); |
|
|
|
|
|
|
|
|
// DBG("<<< ", data); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
return index; |
|
|
return index; |