AT+CIPSEND unexpected response

Hello all,
it has been a few months I’m working with SIM800L attached to my microprocessor, an MSP430F5529. I’ve managed to implement those (let me say) awful AT commands by using a simple logic: when I execute a command I always wait a response and check whether the response is valid or not (using a strcmp).
The code I made is simple and works well: I reset the modem, establish a TCP connection to a remote MQTT broker, send an MQTT CONNECT, PUBLISH and DISCONNECT packets, and close the TCP connection. I do this every 5 seconds and I’m able to listen packets coming from the broker.

However there’s an issue I can’t understand: after a few cycles the AT+CIPSEND command, that I use to send TCP data, stops giving me the response “SEND OK” and just give me “OK”. This way my firmware interprets the string as an unexpected response and halts the execution.

For debug purposes I created a string made of the latest AT commands executed and related responses and I print it when an error occurs. In this case that string has the value:

Screenshot%20from%202019-05-05%2020-21-33

Is there a reason why am I receiving that “OK” instead of “SEND OK”? What’s the best way to implement AT commands on a microprocessor?

You have to implement timeouts to exit wait loop after a few seconds. And add OK as a message sent response as well if the module sends OK more often.
Make sure data is received on the broker when the module responds OK and not SEND OK.
Also check for the MQTT acknowledgement packets coming to the module. Like PUB ACK or CONN ACK. These are two byte values and can be quite helpful