Browse Source

Added yield delay and more debug prints

v_master
SRGDamia1 8 years ago
parent
commit
ed6fb6d548
2 changed files with 66 additions and 40 deletions
  1. +62
    -37
      TinyGsmClientSIM800.h
  2. +4
    -3
      TinyGsmCommon.h

+ 62
- 37
TinyGsmClientSIM800.h View File

@ -9,7 +9,7 @@
#ifndef TinyGsmClientSIM800_h #ifndef TinyGsmClientSIM800_h
#define TinyGsmClientSIM800_h #define TinyGsmClientSIM800_h
//#define TINY_GSM_DEBUG Serial
#define TINY_GSM_DEBUG Serial
//#define TINY_GSM_USE_HEX //#define TINY_GSM_USE_HEX
#if !defined(TINY_GSM_RX_BUFFER) #if !defined(TINY_GSM_RX_BUFFER)
@ -253,9 +253,8 @@ public:
if (waitResponse(GF(GSM_NL "+ICCID:")) != 1) { if (waitResponse(GF(GSM_NL "+ICCID:")) != 1) {
return ""; return "";
} }
String res = stream.readStringUntil('\n');
String res = streamReadUntil('\n');
waitResponse(); waitResponse();
res.trim();
return res; return res;
} }
@ -264,9 +263,8 @@ public:
if (waitResponse(GF(GSM_NL)) != 1) { if (waitResponse(GF(GSM_NL)) != 1) {
return ""; return "";
} }
String res = stream.readStringUntil('\n');
String res = streamReadUntil('\n');
waitResponse(); waitResponse();
res.trim();
return res; return res;
} }
@ -275,7 +273,7 @@ public:
if (waitResponse(GF(GSM_NL "+CSQ:")) != 1) { if (waitResponse(GF(GSM_NL "+CSQ:")) != 1) {
return 99; return 99;
} }
int res = stream.readStringUntil(',').toInt();
int res = streamReadUntil(',').toInt();
waitResponse(); waitResponse();
return res; return res;
} }
@ -285,9 +283,8 @@ public:
if (waitResponse(GF(GSM_NL "+CIPGSMLOC:")) != 1) { if (waitResponse(GF(GSM_NL "+CIPGSMLOC:")) != 1) {
return ""; return "";
} }
String res = stream.readStringUntil('\n');
String res = streamReadUntil('\n');
waitResponse(); waitResponse();
res.trim();
return res; return res;
} }
@ -353,7 +350,7 @@ public:
for (unsigned long start = millis(); millis() - start < timeout; ) { for (unsigned long start = millis(); millis() - start < timeout; ) {
sendAT(GF("+CPIN?")); sendAT(GF("+CPIN?"));
if (waitResponse(GF(GSM_NL "+CPIN:")) != 1) { if (waitResponse(GF(GSM_NL "+CPIN:")) != 1) {
delay(1000);
delay(250);
continue; continue;
} }
int status = waitResponse(GF("READY"), GF("SIM PIN"), GF("SIM PUK"), GF("NOT INSERTED")); int status = waitResponse(GF("READY"), GF("SIM PIN"), GF("SIM PUK"), GF("NOT INSERTED"));
@ -374,7 +371,7 @@ public:
return REG_UNKNOWN; return REG_UNKNOWN;
} }
streamSkipUntil(','); // Skip format (0) streamSkipUntil(','); // Skip format (0)
int status = stream.readStringUntil('\n').toInt();
int status = streamReadUntil('\n').toInt();
waitResponse(); waitResponse();
return (RegStatus)status; return (RegStatus)status;
} }
@ -385,22 +382,22 @@ public:
return ""; return "";
} }
streamSkipUntil('"'); // Skip mode and format streamSkipUntil('"'); // Skip mode and format
String res = stream.readStringUntil('"');
String res = streamReadUntil('"');
waitResponse(); waitResponse();
return res; return res;
} }
bool waitForNetwork(unsigned long timeout = 60000L) { bool waitForNetwork(unsigned long timeout = 60000L) {
for (unsigned long start = millis(); millis() - start < timeout; ) {
unsigned long start = millis();
bool is_registered = false;
while (millis() - start < timeout && is_registered == false){
RegStatus s = getRegistrationStatus(); RegStatus s = getRegistrationStatus();
if (s == REG_OK_HOME || s == REG_OK_ROAMING) { if (s == REG_OK_HOME || s == REG_OK_ROAMING) {
return true;
} else if (s == REG_UNREGISTERED) {
return false;
is_registered = true;
} }
delay(1000); delay(1000);
} }
return false;
return is_registered;
} }
/* /*
@ -415,11 +412,11 @@ public:
sendAT(GF("+SAPBR=3,1,\"APN\",\""), apn, '"'); sendAT(GF("+SAPBR=3,1,\"APN\",\""), apn, '"');
waitResponse(); waitResponse();
if (user) {
if (!strcmp(user, "")) {
sendAT(GF("+SAPBR=3,1,\"USER\",\""), user, '"'); sendAT(GF("+SAPBR=3,1,\"USER\",\""), user, '"');
waitResponse(); waitResponse();
} }
if (pwd) {
if (!strcmp(pwd, "")) {
sendAT(GF("+SAPBR=3,1,\"PWD\",\""), pwd, '"'); sendAT(GF("+SAPBR=3,1,\"PWD\",\""), pwd, '"');
waitResponse(); waitResponse();
} }
@ -521,7 +518,7 @@ public:
streamSkipUntil(','); // Skip streamSkipUntil(','); // Skip
streamSkipUntil(','); // Skip streamSkipUntil(','); // Skip
uint16_t res = stream.readStringUntil(',').toInt();
uint16_t res = streamReadUntil(',').toInt();
waitResponse(); waitResponse();
return res; return res;
} }
@ -542,11 +539,12 @@ private:
return -1; return -1;
} }
stream.write((uint8_t*)buff, len); stream.write((uint8_t*)buff, len);
stream.flush();
if (waitResponse(GF(GSM_NL "DATA ACCEPT:")) != 1) { if (waitResponse(GF(GSM_NL "DATA ACCEPT:")) != 1) {
return -1; return -1;
} }
streamSkipUntil(','); // Skip mux streamSkipUntil(','); // Skip mux
return stream.readStringUntil('\n').toInt();
return streamReadUntil('\n').toInt();
} }
size_t modemRead(size_t size, uint8_t mux) { size_t modemRead(size_t size, uint8_t mux) {
@ -563,19 +561,19 @@ private:
#endif #endif
streamSkipUntil(','); // Skip mode 2/3 streamSkipUntil(','); // Skip mode 2/3
streamSkipUntil(','); // Skip mux streamSkipUntil(','); // Skip mux
size_t len = stream.readStringUntil(',').toInt();
sockets[mux]->sock_available = stream.readStringUntil('\n').toInt();
size_t len = streamReadUntil(',').toInt();
sockets[mux]->sock_available = streamReadUntil('\n').toInt();
for (size_t i=0; i<len; i++) { for (size_t i=0; i<len; i++) {
#ifdef TINY_GSM_USE_HEX #ifdef TINY_GSM_USE_HEX
while (stream.available() < 2) {} while (stream.available() < 2) {}
char buf[4] = { 0, }; char buf[4] = { 0, };
buf[0] = stream.read();
buf[1] = stream.read();
buf[0] = streamRead(); DBG(buf[0]);
buf[1] = streamRead(); DBG(buf[1]);
char c = strtol(buf, NULL, 16); char c = strtol(buf, NULL, 16);
#else #else
while (!stream.available()) {} while (!stream.available()) {}
char c = stream.read();
char c = streamRead(); DBG(c);
#endif #endif
sockets[mux]->rx.put(c); sockets[mux]->rx.put(c);
} }
@ -589,7 +587,7 @@ private:
if (waitResponse(GF("+CIPRXGET:")) == 1) { if (waitResponse(GF("+CIPRXGET:")) == 1) {
streamSkipUntil(','); // Skip mode 4 streamSkipUntil(','); // Skip mode 4
streamSkipUntil(','); // Skip mux streamSkipUntil(','); // Skip mux
result = stream.readStringUntil('\n').toInt();
result = streamReadUntil('\n').toInt();
waitResponse(); waitResponse();
} }
if (!result) { if (!result) {
@ -617,15 +615,33 @@ private:
streamWrite(tail...); streamWrite(tail...);
} }
int streamRead() { return stream.read(); }
int streamRead() {
// int r = stream.read();
// if (String((char)r) == GSM_NL || String((char)r) == "\n"){
// DBG((char)r, " ");
// } else DBG((char)r);
// return r;
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;
}
bool streamSkipUntil(char c) { //TODO: timeout bool streamSkipUntil(char c) { //TODO: timeout
while (true) {
while (!stream.available()) {}
if (stream.read() == c)
return true;
}
return false;
String skipped = stream.readStringUntil(c);
skipped.trim();
if (skipped.length()) {
if (String(c) == GSM_NL || String(c) == "\n"){
DBG(skipped, c, " ");
} else DBG(skipped, c);
return true;
} else return false;
} }
template<typename... Args> template<typename... Args>
@ -633,7 +649,7 @@ private:
streamWrite("AT", cmd..., GSM_NL); streamWrite("AT", cmd..., GSM_NL);
stream.flush(); stream.flush();
TINY_GSM_YIELD(); TINY_GSM_YIELD();
//DBG("### AT:", cmd...);
DBG(GSM_NL, ">>> AT:", cmd...);
} }
// TODO: Optimize this! // TODO: Optimize this!
@ -674,9 +690,9 @@ private:
index = 5; index = 5;
goto finish; goto finish;
} else if (data.endsWith(GF(GSM_NL "+CIPRXGET:"))) { } else if (data.endsWith(GF(GSM_NL "+CIPRXGET:"))) {
String mode = stream.readStringUntil(',');
String mode = streamReadUntil(',');
if (mode.toInt() == 1) { if (mode.toInt() == 1) {
mux = stream.readStringUntil('\n').toInt();
mux = streamReadUntil('\n').toInt();
gotData = true; gotData = true;
data = ""; data = "";
} else { } else {
@ -693,7 +709,7 @@ private:
} }
} }
} while (millis() - startMillis < timeout); } while (millis() - startMillis < timeout);
finish:
finish:
if (!index) { if (!index) {
data.trim(); data.trim();
if (data.length()) { if (data.length()) {
@ -701,6 +717,15 @@ finish:
} }
data = ""; data = "";
} }
else {
data.trim();
data.replace(GSM_NL GSM_NL, GSM_NL);
data.replace(GSM_NL, GSM_NL " ");
if (data.length()) {
DBG(GSM_NL, "<<< ", data);
}
data = "";
}
if (gotData) { if (gotData) {
sockets[mux]->sock_available = modemGetAvailable(mux); sockets[mux]->sock_available = modemGetAvailable(mux);
} }


+ 4
- 3
TinyGsmCommon.h View File

@ -23,7 +23,7 @@
#include <TinyGsmFifo.h> #include <TinyGsmFifo.h>
#ifndef TINY_GSM_YIELD #ifndef TINY_GSM_YIELD
#define TINY_GSM_YIELD() { delay(0); }
#define TINY_GSM_YIELD() { delay(2); }
#endif #endif
#if defined(__AVR__) #if defined(__AVR__)
@ -42,13 +42,14 @@
namespace { namespace {
template<typename T> template<typename T>
static void DBG(T last) { static void DBG(T last) {
TINY_GSM_DEBUG.println(last);
// TINY_GSM_DEBUG.println(last);
TINY_GSM_DEBUG.print(last);
} }
template<typename T, typename... Args> template<typename T, typename... Args>
static void DBG(T head, Args... tail) { static void DBG(T head, Args... tail) {
TINY_GSM_DEBUG.print(head); TINY_GSM_DEBUG.print(head);
TINY_GSM_DEBUG.print(' ');
// TINY_GSM_DEBUG.print(' ');
DBG(tail...); DBG(tail...);
} }
} }


Loading…
Cancel
Save