mirror of
https://github.com/KevinMidboe/TinyGSM.git
synced 2025-10-29 18:00:18 +00:00
Fewer trips in and out of command mode with XBee
This commit is contained in:
@@ -262,6 +262,7 @@ public:
|
|||||||
resetPin = -1;
|
resetPin = -1;
|
||||||
savedIP = IPAddress(0,0,0,0);
|
savedIP = IPAddress(0,0,0,0);
|
||||||
savedHost = "";
|
savedHost = "";
|
||||||
|
inCommandMode = false;
|
||||||
memset(sockets, 0, sizeof(sockets));
|
memset(sockets, 0, sizeof(sockets));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -273,6 +274,7 @@ public:
|
|||||||
this->resetPin = resetPin;
|
this->resetPin = resetPin;
|
||||||
savedIP = IPAddress(0,0,0,0);
|
savedIP = IPAddress(0,0,0,0);
|
||||||
savedHost = "";
|
savedHost = "";
|
||||||
|
inCommandMode = false;
|
||||||
memset(sockets, 0, sizeof(sockets));
|
memset(sockets, 0, sizeof(sockets));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -335,27 +337,36 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool testAT(unsigned long timeout = 10000L) {
|
bool testAT(unsigned long timeout = 10000L) {
|
||||||
for (unsigned long start = millis(); millis() - start < timeout; ) {
|
unsigned long start = millis();
|
||||||
if (commandMode())
|
bool success = false;
|
||||||
{
|
while (!success && millis() - start < timeout) {
|
||||||
|
if (!inCommandMode) {
|
||||||
|
success = commandMode();
|
||||||
|
if (success) exitCommand();
|
||||||
|
}
|
||||||
|
else {
|
||||||
sendAT();
|
sendAT();
|
||||||
if (waitResponse(200) == 1) {
|
if (waitResponse(200) == 1) {
|
||||||
|
success = true;
|
||||||
exitCommand();
|
exitCommand();
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
// if we didn't respond to the AT, must not be in command mode
|
||||||
|
else inCommandMode = false;
|
||||||
}
|
}
|
||||||
delay(100);
|
delay(250);
|
||||||
}
|
}
|
||||||
return false;
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
void maintain() {
|
void maintain() {
|
||||||
// this only happens OUTSIDE command mode, so if we're getting characters
|
// this only happens OUTSIDE command mode, so if we're getting characters
|
||||||
// they should be data received from the TCP connection
|
// they should be data received from the TCP connection
|
||||||
// TINY_GSM_YIELD();
|
// TINY_GSM_YIELD();
|
||||||
// while (stream.available()) {
|
// if (!inCommandMode) {
|
||||||
// char c = stream.read();
|
// while (stream.available()) {
|
||||||
// if (c > 0) sockets[0]->rx.put(c);
|
// char c = stream.read();
|
||||||
|
// if (c > 0) sockets[0]->rx.put(c);
|
||||||
|
// }
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -521,7 +532,13 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
RegStatus getRegistrationStatus() {
|
RegStatus getRegistrationStatus() {
|
||||||
if (!commandMode()) return REG_UNKNOWN; // Return immediately
|
|
||||||
|
bool wasInCommandMode = inCommandMode;
|
||||||
|
if (!wasInCommandMode) { // don't re-enter command mode if already in it
|
||||||
|
commandMode();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!inCommandMode) return REG_UNKNOWN; // Return immediately
|
||||||
|
|
||||||
if (beeType == XBEE_UNKNOWN) getSeries(); // Need to know the bee type to interpret response
|
if (beeType == XBEE_UNKNOWN) getSeries(); // Need to know the bee type to interpret response
|
||||||
|
|
||||||
@@ -598,7 +615,9 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
exitCommand();
|
if (!wasInCommandMode) { // only exit if we weren't in command mode
|
||||||
|
exitCommand();
|
||||||
|
}
|
||||||
return stat;
|
return stat;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -632,12 +651,17 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool waitForNetwork(unsigned long timeout = 60000L) {
|
bool waitForNetwork(unsigned long timeout = 60000L) {
|
||||||
|
// Enter command mode at the start so we don't have to go in and out of it
|
||||||
|
// The cellular Bee's often freeze up and won't respond when attempting
|
||||||
|
// to enter command mode too many times.
|
||||||
|
commandMode();
|
||||||
for (unsigned long start = millis(); millis() - start < timeout; ) {
|
for (unsigned long start = millis(); millis() - start < timeout; ) {
|
||||||
if (isNetworkConnected()) {
|
if (isNetworkConnected()) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
delay(250); // per Neil H. - more stable with delay
|
delay(250); // per Neil H. - more stable with delay
|
||||||
}
|
}
|
||||||
|
exitCommand();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -985,28 +1009,30 @@ finish:
|
|||||||
return waitResponse(1000, r1, r2, r3, r4, r5);
|
return waitResponse(1000, r1, r2, r3, r4, r5);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool commandMode(uint8_t retries = 3) {
|
bool commandMode(uint8_t retries = 5) {
|
||||||
uint8_t triesMade = 0;
|
uint8_t triesMade = 0;
|
||||||
uint8_t triesUntilReset = 2; // only reset after 2 failures
|
uint8_t triesUntilReset = 4; // only reset after 4 failures
|
||||||
bool success = false;
|
bool success = false;
|
||||||
streamClear(); // Empty everything in the buffer before starting
|
streamClear(); // Empty everything in the buffer before starting
|
||||||
while (!success and triesMade < retries) {
|
while (!success and triesMade < retries) {
|
||||||
// Cannot send anything for 1 "guard time" before entering command mode
|
// Cannot send anything for 1 "guard time" before entering command mode
|
||||||
// Default guard time is 1s, but the init fxn decreases it to 100 ms
|
// Default guard time is 1s, but the init fxn decreases it to 100 ms
|
||||||
delay(guardTime + 50);
|
delay(guardTime + 10);
|
||||||
streamWrite(GF("+++")); // enter command mode
|
streamWrite(GF("+++")); // enter command mode
|
||||||
int res = waitResponse(guardTime*2);
|
int res = waitResponse(guardTime*2);
|
||||||
success = (1 == res);
|
success = (1 == res);
|
||||||
if (0 == res) {
|
if (0 == res) {
|
||||||
triesUntilReset--;
|
triesUntilReset--;
|
||||||
if (triesUntilReset == 0) {
|
if (triesUntilReset == 0) {
|
||||||
triesUntilReset = 2;
|
triesUntilReset = 4;
|
||||||
pinReset(); // if it's unresponsive, reset
|
pinReset(); // if it's unresponsive, reset
|
||||||
delay(250); // a short delay to allow it to come back up TODO-optimize this
|
delay(250); // a short delay to allow it to come back up
|
||||||
|
// TODO-optimize this
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
triesMade ++;
|
triesMade ++;
|
||||||
}
|
}
|
||||||
|
if (success) inCommandMode = true;
|
||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1021,6 +1047,7 @@ finish:
|
|||||||
void exitCommand(void) {
|
void exitCommand(void) {
|
||||||
sendAT(GF("CN")); // Exit command mode
|
sendAT(GF("CN")); // Exit command mode
|
||||||
waitResponse();
|
waitResponse();
|
||||||
|
inCommandMode = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool exitAndFail(void) {
|
bool exitAndFail(void) {
|
||||||
@@ -1061,6 +1088,7 @@ protected:
|
|||||||
XBeeType beeType;
|
XBeeType beeType;
|
||||||
IPAddress savedIP;
|
IPAddress savedIP;
|
||||||
String savedHost;
|
String savedHost;
|
||||||
|
bool inCommandMode;
|
||||||
GsmClient* sockets[TINY_GSM_MUX_COUNT];
|
GsmClient* sockets[TINY_GSM_MUX_COUNT];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user