How to tell the state from api to know the photon is trying to connect the cloud, wifi?

You can use WiFi.connecting() to check if a WiFi connection in progress, WiFi.ready() to check if the device has an IP address on the network and Spark.connected() to check if the cloud connection is complete.

The device will always try to connect to the cloud when the WiFi is connected unless you call Spark.disconnect(), so there’s no Spark.connecting() API. (In case you are using different system modes, it’s a little more complicated. You can read up on system modes here).