Hi,
I’ve been struggling with my spark cores trying to avoid them resetting, assumed to be generally if the connection to the cloud is lost with a simple client / server connection to my pc on the same network.
My goal was for the pc server to regularly (minutes / seconds) ‘poll’ the spark core (‘writing’ a predefined ascii string) and for the core to reply with some status (I had an idea of creating a remote temperature / humidity sensor for my caravan).
I got the DHT11 sensor working, but to rule that out, ive stripped that code from the software, just leaving the client / server basics.
Prior to this, even connecting using the local serial port, the core would still reset if connection to the cloud was lost, so I added code to turn off the cloud connection after a few minutes. Resetting the core would re enable the connection to allow me to download new code. Not ideal, but a workaround. This sorted this issue.
However, with reference to the code example below, this still regularly resets, and the server looses the connection to the core, the only way to reset it is to disable and restart the connection on the server and to reset the core. This does not happen in a regular way like when the DNS on the router is renewed. In fact, my router has a setting to fix the DHCP address given to a device, and to not renew it. So thats ruled out.
I took on advise from various threads on the core resetting and added these to my code, but to no avail.
Note, the counters and other variables were placed in the code to try and work out at what point it resets, and what happens. It appears the entire core resets as the counter starts at 0 (if that makes sense).
Can anyone help with this?
The pseudo code is:
Start
loop
check for server connected and data available
receive incoming string and check against defined value
If incoming string matches, send other strings, variables and data back to the server
repeat
The actual code:
char inbyte[2];
int counter = 0;
int simulate_temp = 21;
unsigned long lastTime = millis();
long disconnect_counter = 0;
int Disconnect_Coms = 0;
// telnet defaults to port 23
TCPServer server = TCPServer(23);
IPAddress localIP = WiFi.localIP();
TCPClient client;
void setup()
{
server.begin();
counter == counter + 10;
//simulate_temp = 30;
}
void loop()
{
if (client.connected())
{
while( client.available()==0 && millis()-lastTime<1000)
{ //3 second timeout
} //do nothing
lastTime = millis();
while( client.available() && millis()-lastTime<1000 )
{ //3 second timeout
if (client.available())
{
inbyte[1] = client.read();
if (inbyte[1] == 'x')
{
server.print(WiFi.localIP());
server.print(",");
server.print(counter);
server.print(",");
server.print(Disconnect_Coms);
server.print(",");
server.print(simulate_temp);
server.print(",");
server.print(3);
server.print(",");
server.print(4);
server.println("/e");
counter = counter + 1;
}
lastTime = millis(); //reset the timeout only if there was data
}
else delay(10); //slow it down a tad, we are waiting anyway
}
}
else
{
client = server.available();
}
disconnect_counter = disconnect_counter + 1;
if (disconnect_counter >= 20000)
{
if (Disconnect_Coms == 0)
{
Spark.disconnect();
//WiFi.disconnect();
//WiFi.off();
Disconnect_Coms = 1;
}
disconnect_counter = 20000;
}
}