Browse Source

Moved modem functions to pre-processor macros

v_master
Sara Damiano 5 years ago
parent
commit
9a3d408923
13 changed files with 332 additions and 1319 deletions
  1. +10
    -98
      src/TinyGsmClientA6.h
  2. +14
    -131
      src/TinyGsmClientBG96.h
  3. +3
    -42
      src/TinyGsmClientESP8266.h
  4. +12
    -120
      src/TinyGsmClientM590.h
  5. +14
    -131
      src/TinyGsmClientM95.h
  6. +13
    -139
      src/TinyGsmClientMC60.h
  7. +12
    -129
      src/TinyGsmClientSIM7000.h
  8. +12
    -128
      src/TinyGsmClientSIM800.h
  9. +13
    -130
      src/TinyGsmClientSaraR4.h
  10. +10
    -110
      src/TinyGsmClientSequansMonarch.h
  11. +12
    -130
      src/TinyGsmClientUBLOX.h
  12. +1
    -31
      src/TinyGsmClientXBee.h
  13. +206
    -0
      src/TinyGsmCommon.h

+ 10
- 98
src/TinyGsmClientA6.h View File

@ -150,22 +150,11 @@ public:
return "AI-Thinker A6"; return "AI-Thinker A6";
} }
void setBaud(unsigned long baud) {
sendAT(GF("+IPR="), baud);
}
TINY_GSM_MODEM_SET_BAUD_IPR()
bool testAT(unsigned long timeout = 10000L) {
for (unsigned long start = millis(); millis() - start < timeout; ) {
sendAT(GF(""));
if (waitResponse(200) == 1) return true;
delay(100);
}
return false;
}
TINY_GSM_MODEM_TEST_AT()
void maintain() {
waitResponse(10, NULL, NULL);
}
TINY_GSM_MODEM_MAINTAIN_LISTEN()
bool factoryDefault() { bool factoryDefault() {
sendAT(GF("&FZE0&W")); // Factory + Reset + Echo Off + Write sendAT(GF("&FZE0&W")); // Factory + Reset + Echo Off + Write
@ -174,17 +163,7 @@ public:
return waitResponse() == 1; return waitResponse() == 1;
} }
String getModemInfo() {
sendAT(GF("I"));
String res;
if (waitResponse(1000L, res) != 1) {
return "";
}
res.replace(GSM_NL "OK" GSM_NL, "");
res.replace(GSM_NL, " ");
res.trim();
return res;
}
TINY_GSM_MODEM_GET_INFO_ATI()
bool hasSSL() { bool hasSSL() {
return false; return false;
@ -224,10 +203,7 @@ public:
* SIM card functions * SIM card functions
*/ */
bool simUnlock(const char *pin) {
sendAT(GF("+CPIN=\""), pin, GF("\""));
return waitResponse() == 1;
}
TINY_GSM_MODEM_SIM_UNLOCK_CPIN()
String getSimCCID() { String getSimCCID() {
sendAT(GF("+CCID")); sendAT(GF("+CCID"));
@ -240,16 +216,7 @@ public:
return res; return res;
} }
String getIMEI() {
sendAT(GF("+GSN"));
if (waitResponse(GF(GSM_NL)) != 1) {
return "";
}
String res = stream.readStringUntil('\n');
waitResponse();
res.trim();
return res;
}
TINY_GSM_MODEM_GET_IMEI_GSN()
SimStatus getSimStatus(unsigned long timeout = 10000L) { SimStatus getSimStatus(unsigned long timeout = 10000L) {
for (unsigned long start = millis(); millis() - start < timeout; ) { for (unsigned long start = millis(); millis() - start < timeout; ) {
@ -270,16 +237,7 @@ public:
return SIM_ERROR; return SIM_ERROR;
} }
RegStatus getRegistrationStatus() {
sendAT(GF("+CREG?"));
if (waitResponse(GF(GSM_NL "+CREG:")) != 1) {
return REG_UNKNOWN;
}
streamSkipUntil(','); // Skip format (0)
int status = stream.readStringUntil('\n').toInt();
waitResponse();
return (RegStatus)status;
}
TINY_GSM_MODEM_GET_REGISTRATION_XREG(CREG)
String getOperator() { String getOperator() {
sendAT(GF("+COPS=3,0")); // Set format sendAT(GF("+COPS=3,0")); // Set format
@ -299,30 +257,14 @@ public:
* Generic network functions * Generic network functions
*/ */
int16_t getSignalQuality() {
sendAT(GF("+CSQ"));
if (waitResponse(GF(GSM_NL "+CSQ:")) != 1) {
return 99;
}
int res = stream.readStringUntil(',').toInt();
waitResponse();
return res;
}
TINY_GSP_MODEM_GET_CSQ()
bool isNetworkConnected() { bool isNetworkConnected() {
RegStatus s = getRegistrationStatus(); RegStatus s = getRegistrationStatus();
return (s == REG_OK_HOME || s == REG_OK_ROAMING); return (s == REG_OK_HOME || s == REG_OK_ROAMING);
} }
bool waitForNetwork(unsigned long timeout = 60000L) {
for (unsigned long start = millis(); millis() - start < timeout; ) {
if (isNetworkConnected()) {
return true;
}
delay(250);
}
return false;
}
TINY_GSM_MODEM_WAIT_FOR_NETWORK()
/* /*
* GPRS functions * GPRS functions
@ -603,37 +545,7 @@ public:
Utilities Utilities
*/ */
template<typename T>
void streamWrite(T last) {
stream.print(last);
}
template<typename T, typename... Args>
void streamWrite(T head, Args... tail) {
stream.print(head);
streamWrite(tail...);
}
template<typename... Args>
void sendAT(Args... cmd) {
streamWrite("AT", cmd..., GSM_NL);
stream.flush();
TINY_GSM_YIELD();
//DBG("### AT:", cmd...);
}
bool streamSkipUntil(const char c, const unsigned long timeout = 1000L) {
unsigned long startMillis = millis();
while (millis() - startMillis < timeout) {
while (millis() - startMillis < timeout && !stream.available()) {
TINY_GSM_YIELD();
}
if (stream.read() == c) {
return true;
}
}
return false;
}
TINY_GSP_MODEM_STREAM_UTILITIES()
// TODO: Optimize this! // TODO: Optimize this!
uint8_t waitResponse(uint32_t timeout, String& data, uint8_t waitResponse(uint32_t timeout, String& data,


+ 14
- 131
src/TinyGsmClientBG96.h View File

@ -174,31 +174,11 @@ public:
return "Quectel BG96"; return "Quectel BG96";
} }
void setBaud(unsigned long baud) {
sendAT(GF("+IPR="), baud);
}
TINY_GSM_MODEM_SET_BAUD_IPR()
bool testAT(unsigned long timeout = 10000L) {
for (unsigned long start = millis(); millis() - start < timeout; ) {
sendAT(GF(""));
if (waitResponse(200) == 1) return true;
delay(100);
}
return false;
}
TINY_GSM_MODEM_TEST_AT()
void maintain() {
for (int mux = 0; mux < TINY_GSM_MUX_COUNT; mux++) {
GsmClient* sock = sockets[mux];
if (sock && sock->got_data) {
sock->got_data = false;
sock->sock_available = modemGetAvailable(mux);
}
}
while (stream.available()) {
waitResponse(10, NULL, NULL);
}
}
TINY_GSM_MODEM_MAINTAIN_CHECK_SOCKS()
bool factoryDefault() { bool factoryDefault() {
sendAT(GF("&FZE0&W")); // Factory + Reset + Echo Off + Write sendAT(GF("&FZE0&W")); // Factory + Reset + Echo Off + Write
@ -209,17 +189,7 @@ public:
return waitResponse() == 1; return waitResponse() == 1;
} }
String getModemInfo() {
sendAT(GF("I"));
String res;
if (waitResponse(1000L, res) != 1) {
return "";
}
res.replace(GSM_NL "OK" GSM_NL, "");
res.replace(GSM_NL, " ");
res.trim();
return res;
}
TINY_GSM_MODEM_GET_INFO_ATI()
bool hasSSL() { bool hasSSL() {
return false; // TODO: For now return false; // TODO: For now
@ -268,14 +238,11 @@ public:
* SIM card functions * SIM card functions
*/ */
bool simUnlock(const char *pin) {
sendAT(GF("+CPIN=\""), pin, GF("\""));
return waitResponse() == 1;
}
TINY_GSM_MODEM_SIM_UNLOCK_CPIN()
String getSimCCID() { String getSimCCID() {
sendAT(GF("+ICCID"));
if (waitResponse(GF(GSM_NL "+ICCID:")) != 1) {
sendAT(GF("+QCCID"));
if (waitResponse(GF(GSM_NL "+QCCID:")) != 1) {
return ""; return "";
} }
String res = stream.readStringUntil('\n'); String res = stream.readStringUntil('\n');
@ -284,16 +251,7 @@ public:
return res; return res;
} }
String getIMEI() {
sendAT(GF("+GSN"));
if (waitResponse(GF(GSM_NL)) != 1) {
return "";
}
String res = stream.readStringUntil('\n');
waitResponse();
res.trim();
return res;
}
TINY_GSM_MODEM_GET_IMEI_GSN()
SimStatus getSimStatus(unsigned long timeout = 10000L) { SimStatus getSimStatus(unsigned long timeout = 10000L) {
for (unsigned long start = millis(); millis() - start < timeout; ) { for (unsigned long start = millis(); millis() - start < timeout; ) {
@ -314,56 +272,22 @@ public:
return SIM_ERROR; return SIM_ERROR;
} }
RegStatus getRegistrationStatus() {
sendAT(GF("+CREG?"));
if (waitResponse(GF(GSM_NL "+CREG:")) != 1) {
return REG_UNKNOWN;
}
streamSkipUntil(','); // Skip format (0)
int status = stream.readStringUntil('\n').toInt();
waitResponse();
return (RegStatus)status;
}
TINY_GSM_MODEM_GET_REGISTRATION_XREG(CREG)
String getOperator() {
sendAT(GF("+COPS?"));
if (waitResponse(GF(GSM_NL "+COPS:")) != 1) {
return "";
}
streamSkipUntil('"'); // Skip mode and format
String res = stream.readStringUntil('"');
waitResponse();
return res;
}
TINY_GSM_MODEM_GET_OPERATOR_COPS()
/* /*
* Generic network functions * Generic network functions
*/ */
int16_t getSignalQuality() {
sendAT(GF("+CSQ"));
if (waitResponse(GF(GSM_NL "+CSQ:")) != 1) {
return 99;
}
int res = stream.readStringUntil(',').toInt();
waitResponse();
return res;
}
TINY_GSP_MODEM_GET_CSQ()
bool isNetworkConnected() { bool isNetworkConnected() {
RegStatus s = getRegistrationStatus(); RegStatus s = getRegistrationStatus();
return (s == REG_OK_HOME || s == REG_OK_ROAMING); return (s == REG_OK_HOME || s == REG_OK_ROAMING);
} }
bool waitForNetwork(unsigned long timeout = 60000L) {
for (unsigned long start = millis(); millis() - start < timeout; ) {
if (isNetworkConnected()) {
return true;
}
delay(250);
}
return false;
}
TINY_GSM_MODEM_WAIT_FOR_NETWORK()
/* /*
* GPRS functions * GPRS functions
@ -400,18 +324,7 @@ public:
return true; return true;
} }
bool isGprsConnected() {
sendAT(GF("+CGATT?"));
if (waitResponse(GF(GSM_NL "+CGATT:")) != 1) {
return false;
}
int res = stream.readStringUntil('\n').toInt();
waitResponse();
if (res != 1)
return false;
return localIP() != IPAddress(0,0,0,0);
}
TINY_GSP_MODEM_GET_GPRS_IP_CONNECTED()
/* /*
* IP Address functions * IP Address functions
@ -646,37 +559,7 @@ public:
Utilities Utilities
*/ */
template<typename T>
void streamWrite(T last) {
stream.print(last);
}
template<typename T, typename... Args>
void streamWrite(T head, Args... tail) {
stream.print(head);
streamWrite(tail...);
}
template<typename... Args>
void sendAT(Args... cmd) {
streamWrite("AT", cmd..., GSM_NL);
stream.flush();
TINY_GSM_YIELD();
//DBG("### AT:", cmd...);
}
bool streamSkipUntil(const char c, const unsigned long timeout = 1000L) {
unsigned long startMillis = millis();
while (millis() - startMillis < timeout) {
while (millis() - startMillis < timeout && !stream.available()) {
TINY_GSM_YIELD();
}
if (stream.read() == c) {
return true;
}
}
return false;
}
TINY_GSP_MODEM_STREAM_UTILITIES()
// TODO: Optimize this! // TODO: Optimize this!
uint8_t waitResponse(uint32_t timeout, String& data, uint8_t waitResponse(uint32_t timeout, String& data,


+ 3
- 42
src/TinyGsmClientESP8266.h View File

@ -173,18 +173,9 @@ public:
sendAT(GF("+UART_CUR="), baud, "8,1,0,0"); sendAT(GF("+UART_CUR="), baud, "8,1,0,0");
} }
bool testAT(unsigned long timeout = 10000L) {
for (unsigned long start = millis(); millis() - start < timeout; ) {
sendAT(GF(""));
if (waitResponse(200) == 1) return true;
delay(100);
}
return false;
}
TINY_GSM_MODEM_TEST_AT()
void maintain() {
waitResponse(10, NULL, NULL);
}
TINY_GSM_MODEM_MAINTAIN_LISTEN()
bool factoryDefault() { bool factoryDefault() {
sendAT(GF("+RESTORE")); sendAT(GF("+RESTORE"));
@ -378,37 +369,7 @@ public:
Utilities Utilities
*/ */
template<typename T>
void streamWrite(T last) {
stream.print(last);
}
template<typename T, typename... Args>
void streamWrite(T head, Args... tail) {
stream.print(head);
streamWrite(tail...);
}
template<typename... Args>
void sendAT(Args... cmd) {
streamWrite("AT", cmd..., GSM_NL);
stream.flush();
TINY_GSM_YIELD();
//DBG("### AT:", cmd...);
}
bool streamSkipUntil(const char c, const unsigned long timeout = 1000L) {
unsigned long startMillis = millis();
while (millis() - startMillis < timeout) {
while (millis() - startMillis < timeout && !stream.available()) {
TINY_GSM_YIELD();
}
if (stream.read() == c) {
return true;
}
}
return false;
}
TINY_GSP_MODEM_STREAM_UTILITIES()
// TODO: Optimize this! // TODO: Optimize this!
uint8_t waitResponse(uint32_t timeout, String& data, uint8_t waitResponse(uint32_t timeout, String& data,


+ 12
- 120
src/TinyGsmClientM590.h View File

@ -143,24 +143,11 @@ public:
return "Neoway M590"; return "Neoway M590";
} }
void setBaud(unsigned long baud) {
sendAT(GF("+IPR="), baud);
}
TINY_GSM_MODEM_SET_BAUD_IPR()
bool testAT(unsigned long timeout = 10000L) {
for (unsigned long start = millis(); millis() - start < timeout; ) {
sendAT(GF(""));
if (waitResponse(200) == 1) return true;
delay(100);
}
return false;
}
TINY_GSM_MODEM_TEST_AT()
void maintain() {
//while (stream.available()) {
waitResponse(10, NULL, NULL);
//}
}
TINY_GSM_MODEM_MAINTAIN_LISTEN()
bool factoryDefault() { bool factoryDefault() {
sendAT(GF("&FZE0&W")); // Factory + Reset + Echo Off + Write sendAT(GF("&FZE0&W")); // Factory + Reset + Echo Off + Write
@ -175,17 +162,7 @@ public:
return waitResponse() == 1; return waitResponse() == 1;
} }
String getModemInfo() {
sendAT(GF("I"));
String res;
if (waitResponse(1000L, res) != 1) {
return "";
}
res.replace(GSM_NL "OK" GSM_NL, "");
res.replace(GSM_NL, " ");
res.trim();
return res;
}
TINY_GSM_MODEM_GET_INFO_ATI()
bool hasSSL() { bool hasSSL() {
return false; return false;
@ -232,32 +209,11 @@ public:
* SIM card functions * SIM card functions
*/ */
bool simUnlock(const char *pin) {
sendAT(GF("+CPIN=\""), pin, GF("\""));
return waitResponse() == 1;
}
TINY_GSM_MODEM_SIM_UNLOCK_CPIN()
String getSimCCID() {
sendAT(GF("+CCID"));
if (waitResponse(GF(GSM_NL "+CCID:")) != 1) {
return "";
}
String res = stream.readStringUntil('\n');
waitResponse();
res.trim();
return res;
}
TINY_GSM_MODEM_GET_SIMCCID_CCID()
String getIMEI() {
sendAT(GF("+GSN"));
if (waitResponse(GF(GSM_NL)) != 1) {
return "";
}
String res = stream.readStringUntil('\n');
waitResponse();
res.trim();
return res;
}
TINY_GSM_MODEM_GET_IMEI_GSN()
SimStatus getSimStatus(unsigned long timeout = 10000L) { SimStatus getSimStatus(unsigned long timeout = 10000L) {
for (unsigned long start = millis(); millis() - start < timeout; ) { for (unsigned long start = millis(); millis() - start < timeout; ) {
@ -278,56 +234,22 @@ public:
return SIM_ERROR; return SIM_ERROR;
} }
RegStatus getRegistrationStatus() {
sendAT(GF("+CREG?"));
if (waitResponse(GF(GSM_NL "+CREG:")) != 1) {
return REG_UNKNOWN;
}
streamSkipUntil(','); // Skip format (0)
int status = stream.readStringUntil('\n').toInt();
waitResponse();
return (RegStatus)status;
}
TINY_GSM_MODEM_GET_REGISTRATION_XREG(CREG)
String getOperator() {
sendAT(GF("+COPS?"));
if (waitResponse(GF(GSM_NL "+COPS:")) != 1) {
return "";
}
streamSkipUntil('"'); // Skip mode and format
String res = stream.readStringUntil('"');
waitResponse();
return res;
}
TINY_GSM_MODEM_GET_OPERATOR_COPS()
/* /*
* Generic network functions * Generic network functions
*/ */
int16_t getSignalQuality() {
sendAT(GF("+CSQ"));
if (waitResponse(GF(GSM_NL "+CSQ:")) != 1) {
return 99;
}
int res = stream.readStringUntil(',').toInt();
waitResponse();
return res;
}
TINY_GSP_MODEM_GET_CSQ()
bool isNetworkConnected() { bool isNetworkConnected() {
RegStatus s = getRegistrationStatus(); RegStatus s = getRegistrationStatus();
return (s == REG_OK_HOME || s == REG_OK_ROAMING); return (s == REG_OK_HOME || s == REG_OK_ROAMING);
} }
bool waitForNetwork(unsigned long timeout = 60000L) {
for (unsigned long start = millis(); millis() - start < timeout; ) {
if (isNetworkConnected()) {
return true;
}
delay(250);
}
return false;
}
TINY_GSM_MODEM_WAIT_FOR_NETWORK()
/* /*
* GPRS functions * GPRS functions
@ -545,37 +467,7 @@ public:
Utilities Utilities
*/ */
template<typename T>
void streamWrite(T last) {
stream.print(last);
}
template<typename T, typename... Args>
void streamWrite(T head, Args... tail) {
stream.print(head);
streamWrite(tail...);
}
template<typename... Args>
void sendAT(Args... cmd) {
streamWrite("AT", cmd..., GSM_NL);
stream.flush();
TINY_GSM_YIELD();
//DBG("### AT:", cmd...);
}
bool streamSkipUntil(const char c, const unsigned long timeout = 1000L) {
unsigned long startMillis = millis();
while (millis() - startMillis < timeout) {
while (millis() - startMillis < timeout && !stream.available()) {
TINY_GSM_YIELD();
}
if (stream.read() == c) {
return true;
}
}
return false;
}
TINY_GSP_MODEM_STREAM_UTILITIES()
// TODO: Optimize this! // TODO: Optimize this!
uint8_t waitResponse(uint32_t timeout, String& data, uint8_t waitResponse(uint32_t timeout, String& data,


+ 14
- 131
src/TinyGsmClientM95.h View File

@ -178,31 +178,11 @@ public:
return "Quectel M95"; return "Quectel M95";
} }
void setBaud(unsigned long baud) {
sendAT(GF("+IPR="), baud);
}
TINY_GSM_MODEM_SET_BAUD_IPR()
bool testAT(unsigned long timeout = 10000L) {
for (unsigned long start = millis(); millis() - start < timeout; ) {
sendAT(GF(""));
if (waitResponse(200) == 1) return true;
delay(100);
}
return false;
}
TINY_GSM_MODEM_TEST_AT()
void maintain() {
for (int mux = 0; mux < TINY_GSM_MUX_COUNT; mux++) {
GsmClient* sock = sockets[mux];
if (sock && sock->got_data) {
sock->got_data = false;
sock->sock_available = modemGetAvailable(mux);
}
}
while (stream.available()) {
waitResponse(10, NULL, NULL);
}
}
TINY_GSM_MODEM_MAINTAIN_CHECK_SOCKS()
bool factoryDefault() { bool factoryDefault() {
sendAT(GF("&FZE0&W")); // Factory + Reset + Echo Off + Write sendAT(GF("&FZE0&W")); // Factory + Reset + Echo Off + Write
@ -213,17 +193,7 @@ public:
return waitResponse() == 1; return waitResponse() == 1;
} }
String getModemInfo() {
sendAT(GF("I"));
String res;
if (waitResponse(1000L, res) != 1) {
return "";
}
res.replace(GSM_NL "OK" GSM_NL, "");
res.replace(GSM_NL, " ");
res.trim();
return res;
}
TINY_GSM_MODEM_GET_INFO_ATI()
bool hasSSL() { bool hasSSL() {
return false; // TODO: For now return false; // TODO: For now
@ -276,14 +246,11 @@ public:
* SIM card functions * SIM card functions
*/ */
bool simUnlock(const char *pin) {
sendAT(GF("+CPIN=\""), pin, GF("\""));
return waitResponse() == 1;
}
TINY_GSM_MODEM_SIM_UNLOCK_CPIN()
String getSimCCID() { String getSimCCID() {
sendAT(GF("+ICCID"));
if (waitResponse(GF(GSM_NL "+ICCID:")) != 1) {
sendAT(GF("+QCCID"));
if (waitResponse(GF(GSM_NL "+QCCID:")) != 1) {
return ""; return "";
} }
String res = stream.readStringUntil('\n'); String res = stream.readStringUntil('\n');
@ -292,16 +259,7 @@ public:
return res; return res;
} }
String getIMEI() {
sendAT(GF("+GSN"));
if (waitResponse(GF(GSM_NL)) != 1) {
return "";
}
String res = stream.readStringUntil('\n');
waitResponse();
res.trim();
return res;
}
TINY_GSM_MODEM_GET_IMEI_GSN()
SimStatus getSimStatus(unsigned long timeout = 10000L) { SimStatus getSimStatus(unsigned long timeout = 10000L) {
for (unsigned long start = millis(); millis() - start < timeout; ) { for (unsigned long start = millis(); millis() - start < timeout; ) {
@ -322,41 +280,15 @@ public:
return SIM_ERROR; return SIM_ERROR;
} }
RegStatus getRegistrationStatus() {
sendAT(GF("+CREG?"));
if (waitResponse(GF(GSM_NL "+CREG:")) != 1) {
return REG_UNKNOWN;
}
streamSkipUntil(','); // Skip format (0)
int status = stream.readStringUntil('\n').toInt();
waitResponse();
return (RegStatus)status;
}
TINY_GSM_MODEM_GET_REGISTRATION_XREG(CREG)
String getOperator() {
sendAT(GF("+COPS?"));
if (waitResponse(GF(GSM_NL "+COPS:")) != 1) {
return "";
}
streamSkipUntil('"'); // Skip mode and format
String res = stream.readStringUntil('"');
waitResponse();
return res;
}
TINY_GSM_MODEM_GET_OPERATOR_COPS()
/* /*
* Generic network functions * Generic network functions
*/ */
int16_t getSignalQuality() {
sendAT(GF("+CSQ"));
if (waitResponse(GF(GSM_NL "+CSQ:")) != 1) {
return 99;
}
int res = stream.readStringUntil(',').toInt();
waitResponse();
return res;
}
TINY_GSP_MODEM_GET_CSQ()
bool isNetworkConnected() { bool isNetworkConnected() {
RegStatus s = getRegistrationStatus(); RegStatus s = getRegistrationStatus();
@ -372,15 +304,7 @@ public:
waitResponse(); waitResponse();
} }
bool waitForNetwork(unsigned long timeout = 60000L) {
for (unsigned long start = millis(); millis() - start < timeout; ) {
if (isNetworkConnected()) {
return true;
}
delay(250);
}
return false;
}
TINY_GSM_MODEM_WAIT_FOR_NETWORK()
/* /*
* GPRS functions * GPRS functions
@ -438,18 +362,7 @@ public:
return waitResponse(60000L, GF("DEACT OK"), GF("ERROR")) == 1; return waitResponse(60000L, GF("DEACT OK"), GF("ERROR")) == 1;
} }
bool isGprsConnected() {
sendAT(GF("+CGATT?"));
if (waitResponse(GF(GSM_NL "+CGATT:")) != 1) {
return false;
}
int res = stream.readStringUntil('\n').toInt();
waitResponse();
if (res != 1)
return false;
return localIP() != IPAddress(0,0,0,0);
}
TINY_GSP_MODEM_GET_GPRS_IP_CONNECTED()
/* /*
* IP Address functions * IP Address functions
@ -701,37 +614,7 @@ public:
Utilities Utilities
*/ */
template<typename T>
void streamWrite(T last) {
stream.print(last);
}
template<typename T, typename... Args>
void streamWrite(T head, Args... tail) {
stream.print(head);
streamWrite(tail...);
}
template<typename... Args>
void sendAT(Args... cmd) {
streamWrite("AT", cmd..., GSM_NL);
stream.flush();
TINY_GSM_YIELD();
//DBG("### AT:", cmd...);
}
bool streamSkipUntil(const char c, const unsigned long timeout = 1000L) {
unsigned long startMillis = millis();
while (millis() - startMillis < timeout) {
while (millis() - startMillis < timeout && !stream.available()) {
TINY_GSM_YIELD();
}
if (stream.read() == c) {
return true;
}
}
return false;
}
TINY_GSP_MODEM_STREAM_UTILITIES()
// TODO: Optimize this! // TODO: Optimize this!
uint8_t waitResponse(uint32_t timeout, String& data, uint8_t waitResponse(uint32_t timeout, String& data,


+ 13
- 139
src/TinyGsmClientMC60.h View File

@ -185,31 +185,11 @@ public:
return "Quectel MC60"; return "Quectel MC60";
} }
void setBaud(unsigned long baud) {
sendAT(GF("+IPR="), baud);
}
TINY_GSM_MODEM_SET_BAUD_IPR()
bool testAT(unsigned long timeout = 10000L) {
for (unsigned long start = millis(); millis() - start < timeout; ) {
sendAT(GF(""));
if (waitResponse(200) == 1) return true;
delay(100);
}
return false;
}
TINY_GSM_MODEM_TEST_AT()
void maintain() {
for (int mux = 0; mux < TINY_GSM_MUX_COUNT; mux++) {
GsmClient* sock = sockets[mux];
if (sock && sock->got_data) {
sock->got_data = false;
sock->sock_available = modemGetAvailable(mux);
}
}
while (stream.available()) {
waitResponse(10, NULL, NULL);
}
}
TINY_GSM_MODEM_MAINTAIN_CHECK_SOCKS()
bool factoryDefault() { bool factoryDefault() {
sendAT(GF("&FZE0&W")); // Factory + Reset + Echo Off + Write sendAT(GF("&FZE0&W")); // Factory + Reset + Echo Off + Write
@ -220,17 +200,7 @@ public:
return waitResponse() == 1; return waitResponse() == 1;
} }
String getModemInfo() {
sendAT(GF("I"));
String res;
if (waitResponse(1000L, res) != 1) {
return "";
}
res.replace(GSM_NL "OK" GSM_NL, "");
res.replace(GSM_NL, " ");
res.trim();
return res;
}
TINY_GSM_MODEM_GET_INFO_ATI()
/* /*
* under development * under development
@ -298,32 +268,11 @@ public:
* SIM card functions * SIM card functions
*/ */
bool simUnlock(const char *pin) {
sendAT(GF("+CPIN=\""), pin, GF("\""));
return waitResponse() == 1;
}
TINY_GSM_MODEM_SIM_UNLOCK_CPIN()
String getSimCCID() {
sendAT(GF("+ICCID"));
if (waitResponse(GF(GSM_NL "+ICCID:")) != 1) {
return "";
}
String res = stream.readStringUntil('\n');
waitResponse();
res.trim();
return res;
}
TINY_GSM_MODEM_GET_SIMCCID_CCID()
String getIMEI() {
sendAT(GF("+GSN"));
if (waitResponse(GF(GSM_NL)) != 1) {
return "";
}
String res = stream.readStringUntil('\n');
waitResponse();
res.trim();
return res;
}
TINY_GSM_MODEM_GET_IMEI_GSN()
SimStatus getSimStatus(unsigned long timeout = 10000L) { SimStatus getSimStatus(unsigned long timeout = 10000L) {
for (unsigned long start = millis(); millis() - start < timeout; ) { for (unsigned long start = millis(); millis() - start < timeout; ) {
@ -346,56 +295,22 @@ public:
return SIM_ERROR; return SIM_ERROR;
} }
RegStatus getRegistrationStatus() {
sendAT(GF("+CREG?"));
if (waitResponse(GF(GSM_NL "+CREG:")) != 1) {
return REG_UNKNOWN;
}
streamSkipUntil(','); // Skip format (0)
int status = stream.readStringUntil('\n').toInt();
waitResponse();
return (RegStatus)status;
}
TINY_GSM_MODEM_GET_REGISTRATION_XREG(CREG)
String getOperator() {
sendAT(GF("+COPS?"));
if (waitResponse(GF(GSM_NL "+COPS:")) != 1) {
return "";
}
streamSkipUntil('"'); // Skip mode and format
String res = stream.readStringUntil('"');
waitResponse();
return res;
}
TINY_GSM_MODEM_GET_OPERATOR_COPS()
/* /*
* Generic network functions * Generic network functions
*/ */
int16_t getSignalQuality() {
sendAT(GF("+CSQ"));
if (waitResponse(GF(GSM_NL "+CSQ:")) != 1) {
return 99;
}
int res = stream.readStringUntil(',').toInt();
waitResponse();
return res;
}
TINY_GSP_MODEM_GET_CSQ()
bool isNetworkConnected() { bool isNetworkConnected() {
RegStatus s = getRegistrationStatus(); RegStatus s = getRegistrationStatus();
return (s == REG_OK_HOME || s == REG_OK_ROAMING); return (s == REG_OK_HOME || s == REG_OK_ROAMING);
} }
bool waitForNetwork(unsigned long timeout = 60000L) {
for (unsigned long start = millis(); millis() - start < timeout; ) {
if (isNetworkConnected()) {
return true;
}
delay(250);
}
return false;
}
TINY_GSM_MODEM_WAIT_FOR_NETWORK()
/* /*
* GPRS functions * GPRS functions
@ -466,18 +381,7 @@ public:
return waitResponse(60000L, GF("DEACT OK"), GF("ERROR")) == 1; return waitResponse(60000L, GF("DEACT OK"), GF("ERROR")) == 1;
} }
bool isGprsConnected() {
sendAT(GF("+CGATT?"));
if (waitResponse(GF(GSM_NL "+CGATT:")) != 1) {
return false;
}
int res = stream.readStringUntil('\n').toInt();
waitResponse();
if (res != 1)
return false;
return localIP() != IPAddress(0,0,0,0);
}
TINY_GSP_MODEM_GET_GPRS_IP_CONNECTED()
/* /*
* IP Address functions * IP Address functions
@ -730,37 +634,7 @@ public:
Utilities Utilities
*/ */
template<typename T>
void streamWrite(T last) {
stream.print(last);
}
template<typename T, typename... Args>
void streamWrite(T head, Args... tail) {
stream.print(head);
streamWrite(tail...);
}
template<typename... Args>
void sendAT(Args... cmd) {
streamWrite("AT", cmd..., GSM_NL);
stream.flush();
TINY_GSM_YIELD();
//DBG("### AT:", cmd...);
}
bool streamSkipUntil(const char c, const unsigned long timeout = 1000L) {
unsigned long startMillis = millis();
while (millis() - startMillis < timeout) {
while (millis() - startMillis < timeout && !stream.available()) {
TINY_GSM_YIELD();
}
if (stream.read() == c) {
return true;
}
}
return false;
}
TINY_GSP_MODEM_STREAM_UTILITIES()
// TODO: Optimize this! // TODO: Optimize this!
uint8_t waitResponse(uint32_t timeout, String& data, uint8_t waitResponse(uint32_t timeout, String& data,


+ 12
- 129
src/TinyGsmClientSIM7000.h View File

@ -187,48 +187,17 @@ public:
return "SIMCom SIM7000"; return "SIMCom SIM7000";
} }
void setBaud(unsigned long baud) {
sendAT(GF("+IPR="), baud);
}
TINY_GSM_MODEM_SET_BAUD_IPR()
bool testAT(unsigned long timeout = 10000L) {
//streamWrite(GF("AAAAA" GSM_NL)); // TODO: extra A's to help detect the baud rate
for (unsigned long start = millis(); millis() - start < timeout; ) {
sendAT(GF(""));
if (waitResponse(200) == 1) return true;
delay(100);
}
return false;
}
TINY_GSM_MODEM_TEST_AT()
void maintain() {
for (int mux = 0; mux < TINY_GSM_MUX_COUNT; mux++) {
GsmClient* sock = sockets[mux];
if (sock && sock->got_data) {
sock->got_data = false;
sock->sock_available = modemGetAvailable(mux);
}
}
while (stream.available()) {
waitResponse(10, NULL, NULL);
}
}
TINY_GSM_MODEM_MAINTAIN_CHECK_SOCKS()
bool factoryDefault() { // these commands aren't supported bool factoryDefault() { // these commands aren't supported
return false; return false;
} }
String getModemInfo() {
sendAT(GF("I"));
String res;
if (waitResponse(1000L, res) != 1) {
return "";
}
res.replace(GSM_NL "OK" GSM_NL, "");
res.replace(GSM_NL, " ");
res.trim();
return res;
}
TINY_GSM_MODEM_GET_INFO_ATI()
bool hasSSL() { bool hasSSL() {
return false; // TODO: Module supports SSL, but not yet implemented return false; // TODO: Module supports SSL, but not yet implemented
@ -297,32 +266,11 @@ public:
* SIM card functions * SIM card functions
*/ */
bool simUnlock(const char *pin) {
sendAT(GF("+CPIN=\""), pin, GF("\""));
return waitResponse() == 1;
}
TINY_GSM_MODEM_SIM_UNLOCK_CPIN()
String getSimCCID() {
sendAT(GF("+ICCID"));
if (waitResponse(GF(GSM_NL "+ICCID:")) != 1) {
return "";
}
String res = stream.readStringUntil('\n');
waitResponse();
res.trim();
return res;
}
TINY_GSM_MODEM_GET_SIMCCID_CCID()
String getIMEI() {
sendAT(GF("+GSN"));
if (waitResponse(GF(GSM_NL)) != 1) {
return "";
}
String res = stream.readStringUntil('\n');
waitResponse();
res.trim();
return res;
}
TINY_GSM_MODEM_GET_IMEI_GSN()
SimStatus getSimStatus(unsigned long timeout = 10000L) { SimStatus getSimStatus(unsigned long timeout = 10000L) {
for (unsigned long start = millis(); millis() - start < timeout; ) { for (unsigned long start = millis(); millis() - start < timeout; ) {
@ -343,42 +291,15 @@ public:
return SIM_ERROR; return SIM_ERROR;
} }
RegStatus getRegistrationStatus() {
sendAT(GF("+CGREG?"));
// TODO: Shouldn't this be CEREG for the EPS status of this 4G module?
if (waitResponse(GF(GSM_NL "+CGREG:")) != 1) {
return REG_UNKNOWN;
}
streamSkipUntil(','); // Skip format (0)
int status = stream.readStringUntil('\n').toInt();
waitResponse();
return (RegStatus)status;
}
TINY_GSM_MODEM_GET_REGISTRATION_XREG(CGREG)
String getOperator() {
sendAT(GF("+COPS?"));
if (waitResponse(GF(GSM_NL "+COPS:")) != 1) {
return "";
}
streamSkipUntil('"'); // Skip mode and format
String res = stream.readStringUntil('"');
waitResponse();
return res;
}
TINY_GSM_MODEM_GET_OPERATOR_COPS()
/* /*
* Generic network functions * Generic network functions
*/ */
int16_t getSignalQuality() {
sendAT(GF("+CSQ"));
if (waitResponse(GF(GSM_NL "+CSQ:")) != 1) {
return 99;
}
int res = stream.readStringUntil(',').toInt();
waitResponse();
return res;
}
TINY_GSP_MODEM_GET_CSQ()
bool isNetworkConnected() { bool isNetworkConnected() {
RegStatus s = getRegistrationStatus(); RegStatus s = getRegistrationStatus();
@ -395,15 +316,7 @@ public:
return res; return res;
} }
bool waitForNetwork(unsigned long timeout = 60000L) {
for (unsigned long start = millis(); millis() - start < timeout; ) {
if (isNetworkConnected()) {
return true;
}
delay(250);
}
return false;
}
TINY_GSM_MODEM_WAIT_FOR_NETWORK()
String setNetworkMode(uint8_t mode) { String setNetworkMode(uint8_t mode) {
sendAT(GF("+CNMP="), mode); sendAT(GF("+CNMP="), mode);
@ -944,37 +857,7 @@ public:
Utilities Utilities
*/ */
template<typename T>
void streamWrite(T last) {
stream.print(last);
}
template<typename T, typename... Args>
void streamWrite(T head, Args... tail) {
stream.print(head);
streamWrite(tail...);
}
template<typename... Args>
void sendAT(Args... cmd) {
streamWrite("AT", cmd..., GSM_NL);
stream.flush();
TINY_GSM_YIELD();
//DBG("### AT:", cmd...);
}
bool streamSkipUntil(const char c, const unsigned long timeout = 1000L) {
unsigned long startMillis = millis();
while (millis() - startMillis < timeout) {
while (millis() - startMillis < timeout && !stream.available()) {
TINY_GSM_YIELD();
}
if (stream.read() == c) {
return true;
}
}
return false;
}
TINY_GSP_MODEM_STREAM_UTILITIES()
// TODO: Optimize this! // TODO: Optimize this!
uint8_t waitResponse(uint32_t timeout, String& data, uint8_t waitResponse(uint32_t timeout, String& data,


+ 12
- 128
src/TinyGsmClientSIM800.h View File

@ -195,32 +195,11 @@ public:
return "SIMCom SIM800"; return "SIMCom SIM800";
} }
void setBaud(unsigned long baud) {
sendAT(GF("+IPR="), baud);
}
TINY_GSM_MODEM_SET_BAUD_IPR()
bool testAT(unsigned long timeout = 10000L) {
//streamWrite(GF("AAAAA" GSM_NL)); // TODO: extra A's to help detect the baud rate
for (unsigned long start = millis(); millis() - start < timeout; ) {
sendAT(GF(""));
if (waitResponse(200) == 1) return true;
delay(100);
}
return false;
}
TINY_GSM_MODEM_TEST_AT()
void maintain() {
for (int mux = 0; mux < TINY_GSM_MUX_COUNT; mux++) {
GsmClient* sock = sockets[mux];
if (sock && sock->got_data) {
sock->got_data = false;
sock->sock_available = modemGetAvailable(mux);
}
}
while (stream.available()) {
waitResponse(10, NULL, NULL);
}
}
TINY_GSM_MODEM_MAINTAIN_CHECK_SOCKS()
bool factoryDefault() { bool factoryDefault() {
sendAT(GF("&FZE0&W")); // Factory + Reset + Echo Off + Write sendAT(GF("&FZE0&W")); // Factory + Reset + Echo Off + Write
@ -237,17 +216,7 @@ public:
return waitResponse() == 1; return waitResponse() == 1;
} }
String getModemInfo() {
sendAT(GF("I"));
String res;
if (waitResponse(1000L, res) != 1) {
return "";
}
res.replace(GSM_NL "OK" GSM_NL, "");
res.replace(GSM_NL, " ");
res.trim();
return res;
}
TINY_GSM_MODEM_GET_INFO_ATI()
bool hasSSL() { bool hasSSL() {
#if defined(TINY_GSM_MODEM_SIM900) #if defined(TINY_GSM_MODEM_SIM900)
@ -325,32 +294,11 @@ public:
* SIM card functions * SIM card functions
*/ */
bool simUnlock(const char *pin) {
sendAT(GF("+CPIN=\""), pin, GF("\""));
return waitResponse() == 1;
}
TINY_GSM_MODEM_SIM_UNLOCK_CPIN()
String getSimCCID() {
sendAT(GF("+ICCID"));
if (waitResponse(GF(GSM_NL "+ICCID:")) != 1) {
return "";
}
String res = stream.readStringUntil('\n');
waitResponse();
res.trim();
return res;
}
TINY_GSM_MODEM_GET_SIMCCID_CCID()
String getIMEI() {
sendAT(GF("+GSN"));
if (waitResponse(GF(GSM_NL)) != 1) {
return "";
}
String res = stream.readStringUntil('\n');
waitResponse();
res.trim();
return res;
}
TINY_GSM_MODEM_GET_IMEI_GSN()
SimStatus getSimStatus(unsigned long timeout = 10000L) { SimStatus getSimStatus(unsigned long timeout = 10000L) {
for (unsigned long start = millis(); millis() - start < timeout; ) { for (unsigned long start = millis(); millis() - start < timeout; ) {
@ -371,56 +319,22 @@ public:
return SIM_ERROR; return SIM_ERROR;
} }
RegStatus getRegistrationStatus() {
sendAT(GF("+CREG?"));
if (waitResponse(GF(GSM_NL "+CREG:")) != 1) {
return REG_UNKNOWN;
}
streamSkipUntil(','); // Skip format (0)
int status = stream.readStringUntil('\n').toInt();
waitResponse();
return (RegStatus)status;
}
TINY_GSM_MODEM_GET_REGISTRATION_XREG(CREG)
String getOperator() {
sendAT(GF("+COPS?"));
if (waitResponse(GF(GSM_NL "+COPS:")) != 1) {
return "";
}
streamSkipUntil('"'); // Skip mode and format
String res = stream.readStringUntil('"');
waitResponse();
return res;
}
TINY_GSM_MODEM_GET_OPERATOR_COPS()
/* /*
* Generic network functions * Generic network functions
*/ */
int16_t getSignalQuality() {
sendAT(GF("+CSQ"));
if (waitResponse(GF(GSM_NL "+CSQ:")) != 1) {
return 99;
}
int res = stream.readStringUntil(',').toInt();
waitResponse();
return res;
}
TINY_GSP_MODEM_GET_CSQ()
bool isNetworkConnected() { bool isNetworkConnected() {
RegStatus s = getRegistrationStatus(); RegStatus s = getRegistrationStatus();
return (s == REG_OK_HOME || s == REG_OK_ROAMING); return (s == REG_OK_HOME || s == REG_OK_ROAMING);
} }
bool waitForNetwork(unsigned long timeout = 60000L) {
for (unsigned long start = millis(); millis() - start < timeout; ) {
if (isNetworkConnected()) {
return true;
}
delay(250);
}
return false;
}
TINY_GSM_MODEM_WAIT_FOR_NETWORK()
/* /*
* GPRS functions * GPRS functions
@ -869,37 +783,7 @@ public:
Utilities Utilities
*/ */
template<typename T>
void streamWrite(T last) {
stream.print(last);
}
template<typename T, typename... Args>
void streamWrite(T head, Args... tail) {
stream.print(head);
streamWrite(tail...);
}
template<typename... Args>
void sendAT(Args... cmd) {
streamWrite("AT", cmd..., GSM_NL);
stream.flush();
TINY_GSM_YIELD();
//DBG("### AT:", cmd...);
}
bool streamSkipUntil(const char c, const unsigned long timeout = 1000L) {
unsigned long startMillis = millis();
while (millis() - startMillis < timeout) {
while (millis() - startMillis < timeout && !stream.available()) {
TINY_GSM_YIELD();
}
if (stream.read() == c) {
return true;
}
}
return false;
}
TINY_GSP_MODEM_STREAM_UTILITIES()
// TODO: Optimize this! // TODO: Optimize this!
uint8_t waitResponse(uint32_t timeout, String& data, uint8_t waitResponse(uint32_t timeout, String& data,


+ 13
- 130
src/TinyGsmClientSaraR4.h View File

@ -242,48 +242,18 @@ public:
return name; return name;
} }
void setBaud(unsigned long baud) {
sendAT(GF("+IPR="), baud);
}
TINY_GSM_MODEM_SET_BAUD_IPR()
bool testAT(unsigned long timeout = 10000L) {
for (unsigned long start = millis(); millis() - start < timeout; ) {
sendAT(GF(""));
if (waitResponse(200) == 1) return true;
delay(100);
}
return false;
}
TINY_GSM_MODEM_TEST_AT()
void maintain() {
for (int mux = 0; mux < TINY_GSM_MUX_COUNT; mux++) {
GsmClient* sock = sockets[mux];
if (sock && sock->got_data) {
sock->got_data = false;
sock->sock_available = modemGetAvailable(mux);
}
}
while (stream.available()) {
waitResponse(15, NULL, NULL);
}
}
TINY_GSM_MODEM_MAINTAIN_CHECK_SOCKS()
bool factoryDefault() { bool factoryDefault() {
sendAT(GF("&F")); // Resets the current profile, other NVM not affected sendAT(GF("&F")); // Resets the current profile, other NVM not affected
return waitResponse() == 1; return waitResponse() == 1;
} }
String getModemInfo() {
sendAT(GF("I"));
String res;
if (waitResponse(1000L, res) != 1) {
return "";
}
res.replace(GSM_NL "OK" GSM_NL, "");
res.replace(GSM_NL, " ");
res.trim();
return res;
}
TINY_GSM_MODEM_GET_INFO_ATI()
bool hasSSL() { bool hasSSL() {
return true; return true;
@ -333,21 +303,9 @@ public:
* SIM card functions * SIM card functions
*/ */
bool simUnlock(const char *pin) {
sendAT(GF("+CPIN=\""), pin, GF("\""));
return waitResponse() == 1;
}
TINY_GSM_MODEM_SIM_UNLOCK_CPIN()
String getSimCCID() {
sendAT(GF("+CCID"));
if (waitResponse(GF(GSM_NL "+CCID:")) != 1) {
return "";
}
String res = stream.readStringUntil('\n');
waitResponse();
res.trim();
return res;
}
TINY_GSM_MODEM_GET_SIMCCID_CCID()
String getIMEI() { String getIMEI() {
sendAT(GF("+CGSN")); sendAT(GF("+CGSN"));
@ -379,42 +337,16 @@ public:
return SIM_ERROR; return SIM_ERROR;
} }
RegStatus getRegistrationStatus() {
// Check EPS registration for LTE modules
sendAT(GF("+CEREG?"));
if (waitResponse(GF(GSM_NL "+CEREG:")) != 1) {
return REG_UNKNOWN;
}
streamSkipUntil(','); // Skip format (0)
int status = stream.readStringUntil('\n').toInt();
waitResponse();
return (RegStatus)status;
}
String getOperator() {
sendAT(GF("+COPS?"));
if (waitResponse(GF(GSM_NL "+COPS:")) != 1) {
return "";
}
streamSkipUntil('"'); // Skip mode and format
String res = stream.readStringUntil('"');
waitResponse();
return res;
}
TINY_GSM_MODEM_GET_REGISTRATION_XREG(CEREG)
TINY_GSM_MODEM_GET_OPERATOR_COPS()
/* /*
* Generic network functions * Generic network functions
*/ */
int16_t getSignalQuality() {
sendAT(GF("+CSQ"));
if (waitResponse(GF(GSM_NL "+CSQ:")) != 1) {
return 99;
}
int res = stream.readStringUntil(',').toInt();
waitResponse();
return res;
}
TINY_GSP_MODEM_GET_CSQ()
bool isNetworkConnected() { bool isNetworkConnected() {
RegStatus s = getRegistrationStatus(); RegStatus s = getRegistrationStatus();
@ -425,15 +357,7 @@ public:
else return false; else return false;
} }
bool waitForNetwork(unsigned long timeout = 60000L) {
for (unsigned long start = millis(); millis() - start < timeout; ) {
if (isNetworkConnected()) {
return true;
}
delay(250);
}
return false;
}
TINY_GSM_MODEM_WAIT_FOR_NETWORK()
bool setURAT( uint8_t urat ) { bool setURAT( uint8_t urat ) {
// AT+URAT=<SelectedAcT>[,<PreferredAct>[,<2ndPreferredAct>]] // AT+URAT=<SelectedAcT>[,<PreferredAct>[,<2ndPreferredAct>]]
@ -500,18 +424,7 @@ public:
return true; return true;
} }
bool isGprsConnected() {
sendAT(GF("+CGATT?"));
if (waitResponse(GF(GSM_NL "+CGATT:")) != 1) {
return false;
}
int res = stream.readStringUntil('\n').toInt();
waitResponse();
if (res != 1)
return false;
return localIP() != IPAddress(0,0,0,0);
}
TINY_GSP_MODEM_GET_GPRS_IP_CONNECTED()
/* /*
* IP Address functions * IP Address functions
@ -738,37 +651,7 @@ public:
Utilities Utilities
*/ */
template<typename T>
void streamWrite(T last) {
stream.print(last);
}
template<typename T, typename... Args>
void streamWrite(T head, Args... tail) {
stream.print(head);
streamWrite(tail...);
}
template<typename... Args>
void sendAT(Args... cmd) {
streamWrite("AT", cmd..., GSM_NL);
stream.flush();
TINY_GSM_YIELD();
//DBG("### AT:", cmd...);
}
bool streamSkipUntil(const char c, const unsigned long timeout = 1000L) {
unsigned long startMillis = millis();
while (millis() - startMillis < timeout) {
while (millis() - startMillis < timeout && !stream.available()) {
TINY_GSM_YIELD();
}
if (stream.read() == c) {
return true;
}
}
return false;
}
TINY_GSP_MODEM_STREAM_UTILITIES()
// TODO: Optimize this! // TODO: Optimize this!
uint8_t waitResponse(uint32_t timeout, String& data, uint8_t waitResponse(uint32_t timeout, String& data,


+ 10
- 110
src/TinyGsmClientSequansMonarch.h View File

@ -215,9 +215,7 @@ public:
return "Sequans Monarch"; return "Sequans Monarch";
} }
void setBaud(unsigned long baud) {
sendAT(GF("+IPR="), baud);
}
TINY_GSM_MODEM_SET_BAUD_IPR()
bool testAT(unsigned long timeout = 10000L) { bool testAT(unsigned long timeout = 10000L) {
for (unsigned long start = millis(); millis() - start < timeout; ) { for (unsigned long start = millis(); millis() - start < timeout; ) {
@ -231,18 +229,7 @@ public:
return false; return false;
} }
void maintain() {
for (int mux = 0; mux < TINY_GSM_MUX_COUNT; mux++) {
GsmClient* sock = sockets[mux];
if (sock && sock->got_data) {
sock->got_data = false;
sock->sock_available = modemGetAvailable(mux);
}
}
while (stream.available()) {
waitResponse(10, NULL, NULL);
}
}
TINY_GSM_MODEM_MAINTAIN_CHECK_SOCKS()
bool factoryDefault() { bool factoryDefault() {
sendAT(GF("&FZE0&W")); // Factory + Reset + Echo Off + Write sendAT(GF("&FZE0&W")); // Factory + Reset + Echo Off + Write
@ -259,17 +246,7 @@ public:
return waitResponse() == 1; return waitResponse() == 1;
} }
String getModemInfo() {
sendAT(GF("I"));
String res;
if (waitResponse(1000L, res) != 1) {
return "";
}
res.replace(GSM_NL "OK" GSM_NL, "");
res.replace(GSM_NL, " ");
res.trim();
return res;
}
TINY_GSM_MODEM_GET_INFO_ATI()
bool hasSSL() { bool hasSSL() {
return true; return true;
@ -325,10 +302,7 @@ public:
* SIM card functions * SIM card functions
*/ */
bool simUnlock(const char *pin) {
sendAT(GF("+CPIN=\""), pin, GF("\""));
return waitResponse() == 1;
}
TINY_GSM_MODEM_SIM_UNLOCK_CPIN()
String getSimCCID() { String getSimCCID() {
sendAT(GF("+SQNCCID")); sendAT(GF("+SQNCCID"));
@ -341,16 +315,7 @@ public:
return res; return res;
} }
String getIMEI() {
sendAT(GF("+GSN"));
if (waitResponse(GF(GSM_NL)) != 1) {
return "";
}
String res = stream.readStringUntil('\n');
waitResponse();
res.trim();
return res;
}
TINY_GSM_MODEM_GET_IMEI_GSN()
SimStatus getSimStatus(unsigned long timeout = 10000L) { SimStatus getSimStatus(unsigned long timeout = 10000L) {
for (unsigned long start = millis(); millis() - start < timeout; ) { for (unsigned long start = millis(); millis() - start < timeout; ) {
@ -371,42 +336,15 @@ public:
return SIM_ERROR; return SIM_ERROR;
} }
RegStatus getRegistrationStatus() {
//sendAT(GF("+CREG?"));
sendAT(GF("+CEREG?"));
if (waitResponse(GF(GSM_NL "+CEREG:")) != 1) {
return REG_UNKNOWN;
}
streamSkipUntil(','); // Skip format (0)
int status = stream.readStringUntil(',').toInt();
waitResponse();
return (RegStatus)status;
}
TINY_GSM_MODEM_GET_REGISTRATION_XREG(CEREG)
String getOperator() {
sendAT(GF("+COPS?"));
if (waitResponse(GF(GSM_NL "+COPS:")) != 1) {
return "";
}
streamSkipUntil('"'); // Skip mode and format
String res = stream.readStringUntil('"');
waitResponse();
return res;
}
TINY_GSM_MODEM_GET_OPERATOR_COPS()
/* /*
* Generic network functions * Generic network functions
*/ */
int16_t getSignalQuality() {
sendAT(GF("+CSQ"));
if (waitResponse(GF(GSM_NL "+CSQ:")) != 1) {
return 99;
}
int res = stream.readStringUntil(',').toInt();
waitResponse();
return res;
}
TINY_GSP_MODEM_GET_CSQ()
bool isNetworkConnected() { bool isNetworkConnected() {
RegStatus s = getRegistrationStatus(); RegStatus s = getRegistrationStatus();
@ -418,15 +356,7 @@ public:
} }
} }
bool waitForNetwork(unsigned long timeout = 60000L) {
for (unsigned long start = millis(); millis() - start < timeout; ) {
if (isNetworkConnected()) {
return true;
}
delay(250);
}
return false;
}
TINY_GSM_MODEM_WAIT_FOR_NETWORK()
/* /*
* GPRS functions * GPRS functions
@ -665,37 +595,7 @@ public:
Utilities Utilities
*/ */
template<typename T>
void streamWrite(T last) {
stream.print(last);
}
template<typename T, typename... Args>
void streamWrite(T head, Args... tail) {
stream.print(head);
streamWrite(tail...);
}
template<typename... Args>
void sendAT(Args... cmd) {
streamWrite("AT", cmd..., GSM_NL);
stream.flush();
TINY_GSM_YIELD();
//DBG("### AT:", cmd...);
}
bool streamSkipUntil(const char c, const unsigned long timeout = 1000L) {
unsigned long startMillis = millis();
while (millis() - startMillis < timeout) {
while (millis() - startMillis < timeout && !stream.available()) {
TINY_GSM_YIELD();
}
if (stream.read() == c) {
return true;
}
}
return false;
}
TINY_GSP_MODEM_STREAM_UTILITIES()
// TODO: Optimize this! // TODO: Optimize this!
uint8_t waitResponse(uint32_t timeout, String& data, uint8_t waitResponse(uint32_t timeout, String& data,


+ 12
- 130
src/TinyGsmClientUBLOX.h View File

@ -229,31 +229,11 @@ public:
return name; return name;
} }
void setBaud(unsigned long baud) {
sendAT(GF("+IPR="), baud);
}
TINY_GSM_MODEM_SET_BAUD_IPR()
bool testAT(unsigned long timeout = 10000L) {
for (unsigned long start = millis(); millis() - start < timeout; ) {
sendAT(GF(""));
if (waitResponse(200) == 1) return true;
delay(100);
}
return false;
}
TINY_GSM_MODEM_TEST_AT()
void maintain() {
for (int mux = 0; mux < TINY_GSM_MUX_COUNT; mux++) {
GsmClient* sock = sockets[mux];
if (sock && sock->got_data) {
sock->got_data = false;
sock->sock_available = modemGetAvailable(mux);
}
}
while (stream.available()) {
waitResponse(15, NULL, NULL);
}
}
TINY_GSM_MODEM_MAINTAIN_CHECK_SOCKS()
bool factoryDefault() { bool factoryDefault() {
sendAT(GF("+UFACTORY=0,1")); // No factory restore, erase NVM sendAT(GF("+UFACTORY=0,1")); // No factory restore, erase NVM
@ -262,17 +242,7 @@ public:
return waitResponse() == 1; return waitResponse() == 1;
} }
String getModemInfo() {
sendAT(GF("I"));
String res;
if (waitResponse(1000L, res) != 1) {
return "";
}
res.replace(GSM_NL "OK" GSM_NL, "");
res.replace(GSM_NL, " ");
res.trim();
return res;
}
TINY_GSM_MODEM_GET_INFO_ATI()
bool hasSSL() { bool hasSSL() {
return true; return true;
@ -322,21 +292,9 @@ public:
* SIM card functions * SIM card functions
*/ */
bool simUnlock(const char *pin) {
sendAT(GF("+CPIN=\""), pin, GF("\""));
return waitResponse() == 1;
}
TINY_GSM_MODEM_SIM_UNLOCK_CPIN()
String getSimCCID() {
sendAT(GF("+CCID"));
if (waitResponse(GF(GSM_NL "+CCID:")) != 1) {
return "";
}
String res = stream.readStringUntil('\n');
waitResponse();
res.trim();
return res;
}
TINY_GSM_MODEM_GET_SIMCCID_CCID()
String getIMEI() { String getIMEI() {
sendAT(GF("+CGSN")); sendAT(GF("+CGSN"));
@ -368,42 +326,15 @@ public:
return SIM_ERROR; return SIM_ERROR;
} }
RegStatus getRegistrationStatus() {
// Check GPRS registration
sendAT(GF("+CGREG?"));
if (waitResponse(GF(GSM_NL "+CGREG:")) != 1) {
return REG_UNKNOWN;
}
streamSkipUntil(','); // Skip format (0)
int status = stream.readStringUntil('\n').toInt();
waitResponse();
return (RegStatus)status;
}
TINY_GSM_MODEM_GET_REGISTRATION_XREG(CGREG)
String getOperator() {
sendAT(GF("+COPS?"));
if (waitResponse(GF(GSM_NL "+COPS:")) != 1) {
return "";
}
streamSkipUntil('"'); // Skip mode and format
String res = stream.readStringUntil('"');
waitResponse();
return res;
}
TINY_GSM_MODEM_GET_OPERATOR_COPS()
/* /*
* Generic network functions * Generic network functions
*/ */
int16_t getSignalQuality() {
sendAT(GF("+CSQ"));
if (waitResponse(GF(GSM_NL "+CSQ:")) != 1) {
return 99;
}
int res = stream.readStringUntil(',').toInt();
waitResponse();
return res;
}
TINY_GSP_MODEM_GET_CSQ()
bool isNetworkConnected() { bool isNetworkConnected() {
RegStatus s = getRegistrationStatus(); RegStatus s = getRegistrationStatus();
@ -414,15 +345,7 @@ public:
else return false; else return false;
} }
bool waitForNetwork(unsigned long timeout = 60000L) {
for (unsigned long start = millis(); millis() - start < timeout; ) {
if (isNetworkConnected()) {
return true;
}
delay(250);
}
return false;
}
TINY_GSM_MODEM_WAIT_FOR_NETWORK()
/* /*
* GPRS functions * GPRS functions
@ -483,18 +406,7 @@ public:
return true; return true;
} }
bool isGprsConnected() {
sendAT(GF("+CGATT?"));
if (waitResponse(GF(GSM_NL "+CGATT:")) != 1) {
return false;
}
int res = stream.readStringUntil('\n').toInt();
waitResponse();
if (res != 1)
return false;
return localIP() != IPAddress(0,0,0,0);
}
TINY_GSP_MODEM_GET_GPRS_IP_CONNECTED()
/* /*
* IP Address functions * IP Address functions
@ -724,37 +636,7 @@ public:
Utilities Utilities
*/ */
template<typename T>
void streamWrite(T last) {
stream.print(last);
}
template<typename T, typename... Args>
void streamWrite(T head, Args... tail) {
stream.print(head);
streamWrite(tail...);
}
template<typename... Args>
void sendAT(Args... cmd) {
streamWrite("AT", cmd..., GSM_NL);
stream.flush();
TINY_GSM_YIELD();
//DBG("### AT:", cmd...);
}
bool streamSkipUntil(const char c, const unsigned long timeout = 1000L) {
unsigned long startMillis = millis();
while (millis() - startMillis < timeout) {
while (millis() - startMillis < timeout && !stream.available()) {
TINY_GSM_YIELD();
}
if (stream.read() == c) {
return true;
}
}
return false;
}
TINY_GSP_MODEM_STREAM_UTILITIES()
// TODO: Optimize this! // TODO: Optimize this!
uint8_t waitResponse(uint32_t timeout, String& data, uint8_t waitResponse(uint32_t timeout, String& data,


+ 1
- 31
src/TinyGsmClientXBee.h View File

@ -933,37 +933,7 @@ public:
} }
} }
template<typename T>
void streamWrite(T last) {
stream.print(last);
}
template<typename T, typename... Args>
void streamWrite(T head, Args... tail) {
stream.print(head);
streamWrite(tail...);
}
template<typename... Args>
void sendAT(Args... cmd) {
streamWrite("AT", cmd..., GSM_NL);
stream.flush();
TINY_GSM_YIELD();
//DBG("### AT:", cmd...);
}
bool streamSkipUntil(const char c, const unsigned long timeout = 1000L) {
unsigned long startMillis = millis();
while (millis() - startMillis < timeout) {
while (millis() - startMillis < timeout && !stream.available()) {
TINY_GSM_YIELD();
}
if (stream.read() == c) {
return true;
}
}
return false;
}
TINY_GSP_MODEM_STREAM_UTILITIES()
// TODO: Optimize this! // TODO: Optimize this!
// NOTE: This function is used while INSIDE command mode, so we're only // NOTE: This function is used while INSIDE command mode, so we're only


+ 206
- 0
src/TinyGsmCommon.h View File

@ -389,6 +389,7 @@ String TinyGsmDecodeHex16bit(String &instr) {
} }
// The peek, flush, and connected functions
#define TINY_GSM_CLIENT_PEEK_FLUSH_CONNECTED() \ #define TINY_GSM_CLIENT_PEEK_FLUSH_CONNECTED() \
virtual int peek() { return -1; } /* TODO */ \ virtual int peek() { return -1; } /* TODO */ \
\ \
@ -402,4 +403,209 @@ String TinyGsmDecodeHex16bit(String &instr) {
} \ } \
virtual operator bool() { return connected(); } virtual operator bool() { return connected(); }
// Set baud rate via the V.25TER standard IPR command
#define TINY_GSM_MODEM_SET_BAUD_IPR() \
void setBaud(unsigned long baud) { \
sendAT(GF("+IPR="), baud); \
}
// Test response to AT commands
#define TINY_GSM_MODEM_TEST_AT() \
bool testAT(unsigned long timeout = 10000L) { \
for (unsigned long start = millis(); millis() - start < timeout; ) { \
sendAT(GF("")); \
if (waitResponse(200) == 1) return true; \
delay(100); \
} \
return false; \
}
// Keeps listening for modem URC's and iterates through sockets
// to see if any data is avaiable
#define TINY_GSM_MODEM_MAINTAIN_CHECK_SOCKS() \
void maintain() { \
for (int mux = 0; mux < TINY_GSM_MUX_COUNT; mux++) { \
GsmClient* sock = sockets[mux]; \
if (sock && sock->got_data) { \
sock->got_data = false; \
sock->sock_available = modemGetAvailable(mux); \
} \
} \
while (stream.available()) { \
waitResponse(15, NULL, NULL); \
} \
}
// Keeps listening for modem URC's - doesn't check socks because
// modem has no internal fifo
#define TINY_GSM_MODEM_MAINTAIN_LISTEN() \
void maintain() { \
waitResponse(10, NULL, NULL); \
}
// Asks for modem information via the V.25TER standard ATI command
// NOTE: The actual value and style of the response is quite varied
#define TINY_GSM_MODEM_GET_INFO_ATI() \
String getModemInfo() { \
sendAT(GF("I")); \
String res; \
if (waitResponse(1000L, res) != 1) { \
return ""; \
} \
res.replace(GSM_NL "OK" GSM_NL, ""); \
res.replace(GSM_NL, " "); \
res.trim(); \
return res; \
}
// Unlocks a sim via the 3GPP TS command AT+CPIN
#define TINY_GSM_MODEM_SIM_UNLOCK_CPIN() \
bool simUnlock(const char *pin) { \
sendAT(GF("+CPIN=\""), pin, GF("\"")); \
return waitResponse() == 1; \
}
// Gets the CCID of a sim card via AT+CCID
#define TINY_GSM_MODEM_GET_SIMCCID_CCID() \
String getSimCCID() { \
sendAT(GF("+CCID")); \
if (waitResponse(GF(GSM_NL "+CCID:")) != 1) { \
return ""; \
} \
String res = stream.readStringUntil('\n'); \
waitResponse(); \
res.trim(); \
return res; \
}
// Asks for TA Serial Number Identification (IMEI) via the V.25TER standard AT+GSN command
#define TINY_GSM_MODEM_GET_IMEI_GSN() \
String getIMEI() { \
sendAT(GF("+GSN")); \
if (waitResponse(GF(GSM_NL)) != 1) { \
return ""; \
} \
String res = stream.readStringUntil('\n'); \
waitResponse(); \
res.trim(); \
return res; \
}
// Gets the modem's registration status via CREG/CGREG/CEREG
// CREG = Generic network registration
// CGREG = GPRS service registration
// CEREG = EPS registration for LTE modules
#define TINY_GSM_MODEM_GET_REGISTRATION_XREG(regCommand) \
RegStatus getRegistrationStatus() { \
sendAT(GF("+" #regCommand "?")); \
if (waitResponse(GF(GSM_NL "+" #regCommand ":")) != 1) { \
return REG_UNKNOWN; \
} \
streamSkipUntil(','); /* Skip format (0) */ \
int status = stream.readStringUntil('\n').toInt(); \
waitResponse(); \
return (RegStatus)status; \
}
// Gets the current network operator via the 3GPP TS command AT+COPS
#define TINY_GSM_MODEM_GET_OPERATOR_COPS() \
String getOperator() { \
sendAT(GF("+COPS?")); \
if (waitResponse(GF(GSM_NL "+COPS:")) != 1) { \
return ""; \
} \
streamSkipUntil('"'); /* Skip mode and format */ \
String res = stream.readStringUntil('"'); \
waitResponse(); \
return res; \
}
// Waits for network attachment
#define TINY_GSM_MODEM_WAIT_FOR_NETWORK() \
bool waitForNetwork(unsigned long timeout = 60000L) { \
for (unsigned long start = millis(); millis() - start < timeout; ) { \
if (isNetworkConnected()) { \
return true; \
} \
delay(250); \
} \
return false; \
}
// Checks if current attached to GPRS/EPS service
#define TINY_GSP_MODEM_GET_GPRS_IP_CONNECTED() \
bool isGprsConnected() { \
sendAT(GF("+CGATT?")); \
if (waitResponse(GF(GSM_NL "+CGATT:")) != 1) { \
return false; \
} \
int res = stream.readStringUntil('\n').toInt(); \
waitResponse(); \
if (res != 1) \
return false; \
\
return localIP() != IPAddress(0,0,0,0); \
}
// Gets signal quality report according to 3GPP TS command AT+CSQ
#define TINY_GSP_MODEM_GET_CSQ() \
int16_t getSignalQuality() { \
sendAT(GF("+CSQ")); \
if (waitResponse(GF(GSM_NL "+CSQ:")) != 1) { \
return 99; \
} \
int res = stream.readStringUntil(',').toInt(); \
waitResponse(); \
return res; \
}
// Utility templates for writing/skipping characters on a stream
#define TINY_GSP_MODEM_STREAM_UTILITIES() \
template<typename T> \
void streamWrite(T last) { \
stream.print(last); \
} \
\
template<typename T, typename... Args> \
void streamWrite(T head, Args... tail) { \
stream.print(head); \
streamWrite(tail...); \
} \
\
template<typename... Args> \
void sendAT(Args... cmd) { \
streamWrite("AT", cmd..., GSM_NL); \
stream.flush(); \
TINY_GSM_YIELD(); \
/* DBG("### AT:", cmd...); */ \
} \
\
bool streamSkipUntil(const char c, const unsigned long timeout = 1000L) { \
unsigned long startMillis = millis(); \
while (millis() - startMillis < timeout) { \
while (millis() - startMillis < timeout && !stream.available()) { \
TINY_GSM_YIELD(); \
} \
if (stream.read() == c) { \
return true; \
} \
} \
return false; \
}
#endif #endif

Loading…
Cancel
Save