SYSTEM_THREAD(ENABLED);
SYSTEM_THREAD is a wonderful mode that particle introduced a few months ago that moves all the wifi connection activity to a separate thread, so that can happen in the background. Cubetube automatically adds this line to the top of every sketch.
Once you have this in, get rid of SYSTEM_MODE(SEMI_AUTOMATIC) and any reference to the online/offline switch on the cube — just keep the photon in its mode basic mode where it tries to connect to wifi immediately at boot. If there is wifi available, it’ll connect. If there’s no wifi, it’ll start running your program immediately, and the wifi thread will keep looking for available wifi and periodically trying to connect in the background.
Particle documentation on SYSTEM_THREAD is
up here.
—alex