I want to know the best way to handle time-consuming processes.
I am using an external module used for AT commands via UART. It is necessary to judge the success or failure of each command based on the response received from the UART. (Depending on the command, the content of the response that can be judged as successful differs.) It is necessary to send multiple commands to execute a specific function, but depending on the command, it may take some time for the response to be received ( Up to 10 sec). The code below is simple, but I think it will be a busy loop and a bad implementation.
bool connectMqttServer() {
// send AT Command
sendCommand("AT+ACTIVATE=1");
// The process below waits for UART reception in a busy loop
if (!readAndCheckResponse("OK", 300)) {
return false;
}
sendCommand("AT+CONNECT=mqttSever.io,1883");
// It may take up to 10 seconds to receive the response of this command
if (!readAndCheckResponse("OK", 10000)) {
return false;
}
sendCommand("AT+READ=0,100");
if (!readAndCheckResponse("OK", 300)) {
return false;
}
return true;
}
void readAndCheckResponse(const char* successRes, uint32_t timeout) {
...
}
I wanted to resolve the success or failure of the command with an interrupt triggered by a UART reception event so that it would not become a busy loop. However, in this case, the implementation becomes very complicated.
I think this problem is caused by the inability to use threads and asynchronous processing rather than the SDK, but I would be grateful if you could give me some advice.