I have been working on code relating to detecting the state of the cloud connection based on debug strings read back from the SerialCloud. One thing I have observed is that if you call a deep sleep function, the cloud connection is not shut down. This means that it is not in as deep a sleep as it could be.
I have tried using SerialCloud.end() before the sleep and calling SerialCloud.begin() again after the sleep. In this case there is no detection of the cloud opening up.
I have attached code below that can demonstrate this by setting SHUT_DOWN_CLOUD to either true or false.
-
How can the cloud connection be shut down and restarted to reach an even lower power state?
-
Is there a better way to detect the state of the cloud connection and message transmission?
Any assistance would be appreciated.
#define SHUT_DOWN_CLOUD false
const String connectKeyword = "Modem is now connected";
char connectKeywordEnd;
int connectKeywordLength = 0;
const String sentKeyword = "Modem event: CLOSE";
char sentKeywordEnd;
int sentKeywordLength = 0;
// wait for the network to be connected
void waitForNetworkConnected( void ) {
bool found = false;
char cloudCurrentChar;
String receivedFromCloud;
found = false;
while(!found) {
if (SerialCloud.available()) {
cloudCurrentChar = (char)SerialCloud.read();
// Keep our compare buffer to the keyword length
if (receivedFromCloud.length() >= connectKeywordLength) {
receivedFromCloud.remove(0, 1);
}
receivedFromCloud.concat(cloudCurrentChar);
// Watch for then end of strings
if ((cloudCurrentChar == connectKeywordEnd) && (receivedFromCloud == connectKeyword)) {
receivedFromCloud = "";
found = true;
}
}
}
}
// Wait for message to be sent
void waitForMessageSent( void ) {
bool found = false;
char cloudCurrentChar;
String receivedFromCloud;
found = false;
while(!found) {
if (SerialCloud.available()) {
cloudCurrentChar = (char)SerialCloud.read();
// Keep our compare buffer to the keyword length
if (receivedFromCloud.length() >= sentKeywordLength) {
receivedFromCloud.remove(0, 1);
}
receivedFromCloud.concat(cloudCurrentChar);
// Watch for then end of strings
if ((cloudCurrentChar == sentKeywordEnd) && (receivedFromCloud == sentKeyword)) {
receivedFromCloud = "";
found = true;
}
}
}
}
void setup() {
Dash.begin();
// put your setup code here, to run once:
connectKeywordLength = connectKeyword.length();
connectKeywordEnd = connectKeyword.charAt( connectKeywordLength - 1 );
sentKeywordLength = sentKeyword.length();
sentKeywordEnd = sentKeyword.charAt( sentKeywordLength - 1 );
}
void loop() {
Dash.begin();
Dash.onLED();
// Open the cloud
SerialCloud.begin(115200);
// Wait for the cloud to be connected
waitForNetworkConnected();
Dash.pulseLED(500, 100);
SerialCloud.println("before sleep 1");
// Wait for the message to be sent
waitForMessageSent();
Dash.pulseLED(100, 500);
SerialCloud.println("before sleep 2");
// Wait for the message to be sent
waitForMessageSent();
if (SHUT_DOWN_CLOUD) {
// Shut down the cloud before sleeping
SerialCloud.end();
}
Dash.offLED();
Dash.deepSleepAtMostMin( 1 );
Dash.onLED();
if (SHUT_DOWN_CLOUD) {
// Reopen the cloud
SerialCloud.begin(115200);
// Wait for the cloud to be connected
waitForNetworkConnected();
Dash.pulseLED(500, 100);
}
SerialCloud.println("after sleep");
// Wait for the message to be sent
waitForMessageSent();
Dash.offLED();
while (true) ;
}