Hi,
In my application I need to be able to turn on and off the Cellular connection. I can never get it to work reliably. So, I've narrowed the code down to a fairly small easy to understand program that should just turn Cellular on, connect, and turn it off. The problem I am having is that in the absence of a reliable cellular tower to connect to, the behavior of the Electron is uncontrollable, and hangs. That I cannot tolerate. I need it to fail gracefully, even if that failure takes many minutes, I need control back.
Below is the code I've been using for testing. It seems each time I run it by hitting the reset button, I get different behavior.
Behavior A:
The most common behavior is to get caught forever in "Cellular.connect()" The output looks like this:
Connect in 2
Connect in 1
Connect in 0
Connect
Cellular.on()
On complete in 7350
Cellular.connect()
The green light blinks forever, or at least for the hours I've left it blinking.
Behavior B:
It gets through "Cellular.connect()" and then prints some giberish like this:
Cellular.connect()
Connect complete in 39492
Ceoff
...connection lost to /dev/tty.usbmodem641 ...
The LED goes green breathing, and then it hangs and never prints out a thing. If you look at the code, the line that says "Ceoff" should say "Cellular off". Somehow it looks like memory was corrupted, or calling Cellular.off while the Serial output buffer was still being sent screwed that up. The "connection lost" line comes from my terminal emulator which noticed that the USB port was dropped by the Electron.
Behavior C:
It gets completely around the cycle, completes the .on, .connect, and .off, and will even do it 2 or 3 cycles every 20 seconds, but then it starts doing either A or B until it hangs again.
BTW: I am using version 0.5.3-rc.2, although I've used a handful of other firmware versions with equally bewildering set of behaviors that are never the same.
Thank you for any help. Please tell me what I'm doing wrong, or how to keep this program responsive. Or perhaps some other things I can try.
#include "application.h"
SYSTEM_MODE(MANUAL);
#define TRY_FREQUENCY (20UL * 1000UL)
uint32_t timeOfLastSend;
void setup()
{
Cellular.off();
Serial.begin(9600); // USB
delay(4000);
timeOfLastSend = millis() - TRY_FREQUENCY + 10000; // Start 10 seconds behind
Serial.printf("Starting celltest\n");
}
bool connect()
{
unsigned int timer;
Serial.println("Cellular.on()");
timer = millis();
Cellular.on();
Serial.printf("On complete in %d\n", millis()-timer);
delay(1000);
Serial.println("Cellular.connect()");
timer = millis();
Cellular.connect();
Serial.printf("Connect complete in %d\n", millis()-timer);
delay(1000);
Serial.println("Cellular off");
timer = millis();
Cellular.off();
Serial.printf("Off complete in %d\n", millis()-timer);
delay(1000);
}
int countdown = 0;
void loop()
{
int now = millis();
if (now - timeOfLastSend > TRY_FREQUENCY) {
Serial.println("Connect");
connect();
Serial.println("Connect complete");
timeOfLastSend = millis();
} else {
int secsLeft = (TRY_FREQUENCY - (now-timeOfLastSend)) / 1000;
if (secsLeft != countdown) {
countdown = secsLeft;
Serial.printf("Connect in %d\n", countdown);
}
}
}