Hello All.
It appears that the Electron will always keep trying to connect if there is absolutely no signal. Is there a way to set a timeout period?
More importantly, I would like to write my own code to check how long it has been trying to connect since the modem was switched on (and stop the process after, say 2 minutes).
This is what I have tried.
I set a flag cellular_on
to true
after calling Cellular.on()
but just before calling Cellular.connect()
. I assign a variable lastTime=millis()
to get the time I set this flag.
I then hoped I would use another thread to keep track of what’s going on after turning on the modem. I followed the example here by @krvarma and studied this file he refers to.
I came up with the following function, whose job is supposedly to reset the system if 60 seconds have elapsed without a connection to the tower.
Thread* superVthread;
unsigned long lastTime = 0, now=0;
os_thread_return_t resetSystem(void* param){
for(;;){
if(cellular_on){
now = millis();
if((now-lastTime > 60000) )
{
System.reset();
}
}
}
}
And I am creating the thread at the end of setup()
like this:
superVthread = new Thread("superVthread", resetSystem, NULL);
I cannot get it to compile. The errors I get are as shown below:
Any help will be very crucial at this point.
Quick Update:
It appears cellular_on
is already in use by the Electron firmware, hence the system complaining that it was declared somewhere else. Replacing it with cellular_is_on
cleared the compile errors. Now to try uploading and see.
What about:
SYSTEM_THREAD(ENABLED);
SYSTEM_MODE(SEMI_AUTOMATIC);
long now = millis();
Cellular.connect();
while(!Cellular.connected()) {
}
long diff = millis() - now;
Note: Im posting from the phone, so there will be an error in the code.
@maleficarum
The reason why that will not work is because we’ll never get out of that while loop when the signal is completely absent.
Using your code as a second example, what I’d like to know is how long we’ve been stuck in that while loop since calling the Cellular.connect()
function.
Got it. You want to keep your electron doing’t things in the mean while ?
What about :
long now = millis();
long last = millist();
void loop() {
if(!Cellular.connected()) {
last = millis();
} else {
//There will be a flag to prevent print or handle this behaviour in each loop.
Serial.print("Now connectd and took ");
Serial.println(now - last);
now = millis();
}
//Do other things.
}
Yes. The Electron is by default in sleep and is woken up by another MCU, data is sent to it over the serial port. when a particular amount of data has been received, the modem is turned on to upload the data.
Figured it out! Here’s the code for those with the same issue. Multithreading really comes in handly. In short, just set a flag just before you call Cellular.connect()
and then, in another thread, check to see if the flag was set and start counting. The code below will reset the system if the modem fails to connect to a tower because there’s no signal. The thread checks every second. I removed my antenna to test that it works. When the antenna is on and a connection may occur, you simply need to set another flag and check it in the same thread to prevent the reset.
#include "application.h"
#include "Particle.h"
SYSTEM_MODE(MANUAL); /*do not autoconnect to particle cloud*/
STARTUP(cellular_credentials_set("orange.ug", "", "", NULL));
SYSTEM_THREAD(ENABLED);
Thread* superVthread;
unsigned long lastTime = 0, now=0;
bool cellular_is_on=false;
os_thread_return_t sysreset(void* param){
for(;;){
if(cellular_is_on){
Serial.println("Cell is on");
now = millis();
if((now-lastTime > 60000))
{
Serial.println("Going for reset");
System.reset();
}
}
delay(1000);
}
}
void setup() {
Cellular.off();
Serial.begin(38400);
while(!Serial.available());
superVthread = new Thread("superVthread", sysreset, NULL);
}
void loop() {
lastTime=millis();
Cellular.on();
cellular_is_on=true;
Cellular.connect();
while (!Cellular.ready()) {
/* code */
}
}
1 Like