So there is a few things you might be running into.
There was an issue with the uBlox chip that would block the modem - there was a recall/bullentin that you should have seen. The work around was to allow at least 30 seconds of time to pass from the connection establishment to disconnect. Since mine was doing something simple, I was having the XBee wake up, do it’s stuff, than back to sleep. This occurred within 30s, but eventually the device would become unreachable. Adding a 30s delay at the end of the program improved this.
The second thing I learned is that you should be managing the cellular connection status within your program. In other words, just don’t send the device to sleep without first disconnecting from the network. After doing this, I had more improvement. The easiest thing to do is to put the modem into airplane mode before sending it to sleep. I left a short delay between setting to airplane mode and sleep (I think about 5 seconds). This delay just gives it time (in my mind at least…) to do it’s thing. Someone that knows more than me on the hardware front could tell you how long it takes for the modem to actually run through it’s shutdown process.
So a few more questions for you: is your program hanging/freezing somewhere? If so, which operation is it hanging on? Or is it just waiting for a cellular network connection and gets stuck there in a loop waiting for a connection?
Also, inside Micropython, there is a function to check the cellular network connection. There is no need to utilize the AT commands via python for this. I believe it is in the “Network” library.
What I have been doing for our code is make the program check for a cell connection before doing anything else. If there isn’t one, it sits in a loop and waits for the connection. If there is no connection after sometime, I have the module perform a soft reset. If the issue persist, then the I have the module perform a hard reset. The soft reset will retain the variable values, so you can use that as a counter to track connection attempts. I typically just use a 5 or 10 second delay between checking for cellular connections. When everything is running “normal” the cellular connection takes seconds to establish, so stopping to check for a cell connection doesn’t cause too much of a delay.
When using the simple MQTT library, make sure you check the connection to your MQTT server as well. It contains a similar method for checking the server connection status. If something sticks, try reseting the connection to the server. Issue a disconnect, then a connect.
If you are hanging on the simple.connect() function, let me know. There is a trick to get it to throw an error. I don’t know if the forum would allow a code snippet easily, so it might be easier to find another channel to show you (i.e. email, Git, etc.).