OK, I put together a test code to prove the Spark.connected()
does not indicate that I could use the TCPClient.
I have to put the delay(5000);
otherwise it will failed. Initially I use serial before and it sometimes OK, this is because when u open the terminal, it may pass 5 seconds already.
Also, I disable the last case which use WiFi.Ping
, after this is called, all Spark Cloud access will failed. The IP is "spark.io"
Call the function during setup() will able to show the result.
void CloudConnectCheck (void)
{
TCPClient client; // Client
int i;
unsigned long t1;
t1 = millis(); // Get system time in ms pinMode(D5, INPUT); // D5 - TEST pin if (digitalRead(D5)==HIGH) { pinMode(D0,OUTPUT); // D0 pinMode(D1,OUTPUT); // D1 pinMode(D2,OUTPUT); // D1 digitalWrite(D0, LOW); // Turn OFF digitalWrite(D1, LOW); // Turn OFF digitalWrite(D2, LOW); // Turn OFF
#if 1
while (Spark.connected()==false)
{
SPARK_WLAN_Loop(); // Wait
if ((millis()-t1) > 60000) // Check for 60 seconds max
{
digitalWrite(D0, HIGH); // Turn ON
break; //
}
}
#endif
#if 1
if ((millis()-t1) < 60000) // Check for 60 seconds max
{
delay(5000); //***** Without delay does not work
if (client.connect("api.wunderground.com", 80)==true)
digitalWrite(D1, HIGH); // Turn ON - OK
else
digitalWrite(D2, HIGH); // Turn ON
client.flush(); client.stop(); }
**#endif
#if 0
//
if ((millis()-t1) < 60000) // Check for 60 seconds max
{
IPAddress remoteIP={62,116,130,8};
i = WiFi.ping(remoteIP); // TCP check
if (i) // Yes
digitalWrite(D1, HIGH); // Turn ON - OK
else
digitalWrite(D2, HIGH); // Turn ON
}
#endif
}
}