OK on the docs re machine.
machine.sleep()
Note
This function is deprecated, use lightsleep() instead with no arguments.
machine.lightsleep([time_ms])
machine.deepsleep([time_ms])
Not sure if deprecated means that you can't even call machine.sleep() or you get an error if you do or it just reverts to lightsleep().
What is the objective of that bit of code? It looks like you want to go to sleep for fixed periods of time taking into account how long it takes to send a message.
I'll go and look at your gateway code. I am a beginner when it comes to sleep modes.