I have restructured my code as suggested by @descartes (taking inspiration by his code - thanks a lot for sharing!), so now the loop function looks like this:
void loop() {
bool send_flag = false;
os_runloop_once();
// If enough sleep cycles have been completed and the TX interval has been exceeded
if (sleep_cycles_done * sleep_subperiod >= TX_interval) {
send_flag = true;
}
if (send_flag) {
send_flag = false;
sleep_flag = false;
sleep_cycles_done = 0;
do_send(&sendjob);
}
if (sleep_flag) {
goToSleep(sleep_subperiod);
sleep_cycles_done++;
}
}
and in the TX_COMPLETE event i simply put the sleep_flag to true.
Anyway, I tried to run this code on a Feather: the message after the first sleep period arrived after 1h 1’ 10’‘, but all the following three messages took more than 1h 3’ to arrive, demonstrating an even worse performance that the original code. Therefore I think that as @cslorabox said probably the issue is in the timing of the library’s scheduler; I tried to look through issues of the library but until now I haven’t found anything that could help me solve the problem - I’ll keep searching in there.
As per your last message @cslorabox, I didn’t fully understand this part:
I (briefly) looked into the code of the LMiC library and I didn’t manage to find that “wait-until-next-task code”; anyway what you’re suggesting would be to (find it and) put the sleep “mini-cycles” in there?