It’d be great to see some code snippets and or a clone link to your app.
Have you considered using scheduled tasks instead of an infinite loop?
If you’re using uplink perhaps you could add a global flag in the server module.
automatic_mode = True
You could check this flag at each iteration and this would kill the loop if the flag was False.
You then have a button that calls a function that sets this global flag.