Hi Guys,
I am working with the scheduler class and the yield() function and I am having issues. I am working with an Arduino Due and the scheduler library is https://github.com/arduino-libraries/Scheduler. I am hoping you guys might be able to shed some light on it. I will give an explanation of my issue and post stripped down code below. I removed some commands with passwords etc. but left commands in there place that would cause the same actions to occur.
As an overview I am trying to write some code that resets and re initializes a 3G module if it has lost its connection for a long period of time. The issue is that some of the 3G module commands can take up to three minutes to reply and I need to confirm the reply before sending the next command. With all the commands I need to send to reset and re initialize the wait time would be huge and I can’t afford to delay my main code at all.
To combat this I have added a separate schedule for the reset function. Inside this schedule if retry3G is set (set in main when I need to retry) I trigger a call to a reconnect3G function. Reconnect3G calls one other function (send3gCommand) which is where I hold the code and check the serial2 waiting from a reply.
I need to use yield() in a way that I can return control to my main loop while still executing the second scheduled loop correctly but I am not sure how to do this. I think I need to place a yield() inside the send3gCommand function inside the while (timeToConstructReply <= timeout) loop but I just wanted some advice. Yield() is completely new to me and I'm finding it hard to find info on it.
Thanks for reading
//inside setup
Scheduler.startLoop(Three_G_Loop);
void Three_G_Loop()
{
if (retry3G)
{
reconnect3G();
}
yield(); //not working from here
}
void reconnect3G()
{
send3gCommand ("AT+CFUN=16", 180000, "OK",1, 0); // send3gCommand (Command to send, timeout, text to find in reply for success, attempts, delay between retrys);
send3gCommand ("ATz", 100, "ATzOK", 15, 200);
send3gCommand ("AT+CMEE=2", 200, "AT+CMEE=2OK", 1, 0);
send3gCommand ("AT+CMD", 200, "OK", 1, 0);
send3gCommand ("AT+CMD", 200, "OK", 1, 0);
send3gCommand ("AT+CMD", 200, "OK", 1, 0);
send3gCommand ("AT+CMD", 200, "OK", 1, 0);
send3gCommand ("AT+CMD", 200, "OK", 1, 0);
delay(1000);
send3gCommand ("AT+UPSDA=0,3", 180000, "AT+UPSDA=0,3OK", 1, 0);
send3gCommand ("AT+CMD", 200, "OK", 1, 0);
send3gCommand ("AT+CMD", 200, "OK", 1, 0);
send3gCommand ("AT+CMD", 200, "OK", 1, 0);
retry3G = 0;
commsModuleFailTimeout = 0;
}
bool send3gCommand (String cmd, int timeout, String text, int attempts, int delayRetry)
{
int temp =0;
int msgMax = 0;
int errorMsgMax = 0;
String errorText ="ERROR";
String commandReply = "";
elapsedMillis timeToConstructReply;
while (attempts > 0)
{
//Send command
Serial2.println(cmd);
while (timeToConstructReply <= timeout)
{
//Construct reply
if (Serial2.available() >0)
{
temp = Serial2.read();
if (temp != '\r' && temp != '\n' && temp != ' ')
{
char convertedChar = char(temp);
commandReply += convertedChar;
}
}
//Check reply for text for success once large enough
if ((commandReply.length()) >= (text.length()) )
{
msgMax = commandReply.length() - text.length();
for (int i = 0; i <= msgMax; i++)
{
if (commandReply.substring(i, (i+text.length())) == text)
{
delay(20);
clearRxSerial2Buff();
return true;
}
}
}
//Check the reply text for error for early exit
if ((commandReply.length()) >= (errorText.length()));
{
errorMsgMax = commandReply.length() - errorText.length();
for (int i = 0; i <= errorMsgMax; i++)
{
if (commandReply.substring(i,(i + (errorText.length()))) == errorText)
{
Serial.println("IN ERROR");
timeToConstructReply = timeout+1;
}
}
}
}
//All attemps failed, clear serial and reset everything we use for next attempt
clearRxSerial2Buff();
delay (delayRetry);
attempts-= 1;
commandReply = "";
timeToConstructReply=0;
}
return false;
}
void clearRxSerial2Buff()
{
while (Serial2.available()>0)
{
Serial2.read();
}
}
Edit: Added board type and link to Library used