Spark running code when spark cloud connection interrupted?

You can use Spark.disconnect() However, there seems to be major bugs with the WiFi firmware, where the network will fail after a period of time and a reset is required to fix.