I have the following code:
Which works great and stable, when there is internet on the router (the wan cable is plugged), but when I disconnect the WAN cable from the router, the photon starts to crash after 5-10 sec (red blinks), and then restarts, then again works for 5-10 sec, then restarts.
Why is that?
SYSTEM_THREAD(ENABLED);
TCPServer server = TCPServer(3456);
TCPClient client;
unsigned long last_send = 0;
unsigned long last_blink = 0;
unsigned long blink_interval = 100;
bool blink_state = false;
int led = D7;
void setup() {
pinMode(led, OUTPUT);
server.begin();
Serial.begin(115200);
print_data();
}
void loop() {
if (millis() - last_blink > blink_interval) {
last_blink = millis();
blink_state = !blink_state;
digitalWrite(led, blink_state);
}
if (Serial.available()) {
Serial.println(millis());
while (Serial.available()) {
Serial.read();
}
print_data();
Serial.println(millis());
}
if (millis() - last_send >= 100) {
IPAddress myAddr = WiFi.localIP();
if (myAddr[0] == 192 && myAddr[1] == 168 && myAddr[2] == 1 && myAddr[3] == 111) {
// photon is connected to the router and got the right IP
blink_interval = 200;
} else {
// not connected
blink_interval = 1000;
}
IPAddress clientIP = client.remoteIP();
//Serial.println(clientIP);
if (clientIP[0] == 0 && clientIP[1] == 0 && clientIP[2] == 0 && clientIP[3] == 0) {
} else {
// a client is connected to photon
blink_interval = 20;
}
unsigned long temp = millis();
last_send = temp / 100 * 100;
server.printf("%lu: {test}", temp);
}
if (client.connected()) {
if (client.available()) {
Serial.print(millis());
Serial.print(": ");
while (client.available()) {
Serial.write(client.read());
}
Serial.println();
}
} else {
client = server.available();
}
}
void print_data() {
Serial.println(WiFi.localIP());
Serial.println(WiFi.subnetMask());
Serial.println(WiFi.gatewayIP());
Serial.println(WiFi.SSID());
byte mac[6];
WiFi.macAddress(mac);
for (int i = 0; i < 6; i++) {
Serial.printf("%02x%s", mac[i], i != 5 ? ":" : "");
}
}
Since you are using default AUTOMATIC mode the device OS will keep trying to reestablish the cloud connection which has to fail without WAN and may interfere with your TCP connection.
Try SYSTEM_MODE(SEMI_AUTOMATIC) or SYSTEM_MODE(MANUAL) and make sure to call Particle.disconnect() when your WAN goes away.
You should also not use client without making sure it was initialised.
And if a previously valid client becomes disconnected you should call client.stop() before acquiring a new socket.
"Try SYSTEM_MODE(SEMI_AUTOMATIC) or SYSTEM_MODE(MANUAL) and make sure to call Particle.disconnect() when your WAN goes away."
I would like to keep automatic, and if there is no internet, the system can keep trying the connect again and again, but not crashing the system. This is how it is done normalliy only crashes with tcp server code...
Can you please show as a safe and proper code for that? I have used the codes from particles examples... But in the other hand I do no understand, how is it possible that a system/firmware crashes on an an external factor event. This error should be handled in firmware level I guess..
"You should also not use client without making sure it was initialised."
That is also clear, but I have used the example from particles site. So what is the proper way?
"And if a previously valid client becomes disconnected you should call client.stop() before acquiring a new socket."
As my understanding I modified my code: Is it ok like that?
if (client.connected()) {
client_connected = true;
if (client.available()) {
Serial.print(millis());
Serial.print(": ");
while (client.available()) {
Serial.write(client.read());
}
Serial.println();
}
} else {
if (client_connected == true) {
client_connected = false;
client.stop();
}
client = server.available();
}
Anyone else can help me?
I have choose particle for my project because it is easy to use could solution, but does not work the cloud and tcpserver stable enough (at the same time)ā¦
When you are in automatic mode, the device OS will try as hard as it can to reconnect to the Particle cloud to the point where it will reset the device the eventually if it cannot connect. You do not seem to want to that behavior and the only way to avoid it is using semi-automatic mode or manual mode.
Thank you for the answers. Yes, I tried with SEMI_AUTOMATIC or even with MANUAL.
But the photon restarts if there is no WAN.
I have got the recommendation that I have to check if the router has connected to the WAN and has internet or not, but how can I do that?
Are there any other hidden requirements that photon needs to work properly? Like I can place only in a blue box? or has to be an Ikea chair at least 43 cm from the photon?
I just do not get the point why a micro-controller restarts every 10 sec, because of an external factor on which we do not have any control ofā¦
Thank for your answer.
I am sorry if I hurt your feelings, I did not meant as a sarcasm, I meant more like criticism which can help to make the product better.
Unfortunately two of your statements are misleading/half true/false
1.
"When you are in automatic mode, the device OS will try as hard as it can to reconnect to the Particle cloud to the point where it will reset the device the eventually if it cannot connect."
The device restarts with SOS blink pattern, which is the signal of the crash of the firmware, and not the firmware has restarted the photon on purpose because of no cloud connection, but because of the crash
2.
"All I can tell is many other people have gotten this to work fine."
This is probably because the āmany other peopleā have not tested enough, and after 2 minutes of testing they just made a conclusion that is working stable,
and everybody else just looser.
I have tried with a very basic code:
SYSTEM_THREAD(ENABLED);
TCPServer server = TCPServer(3456);
TCPClient client;
void setup()
{
server.begin();
Serial.begin(9600);
Serial.println(WiFi.localIP());
Serial.println(WiFi.subnetMask());
Serial.println(WiFi.gatewayIP());
Serial.println(WiFi.SSID());
}
void loop()
{
if (client.connected()) {
// echo all available bytes back to the client
while (client.available()) {
server.write(client.read());
}
} else {
// if no client is yet connected, check for a new connection
client = server.available();
}
}
scenario:
There is internet AND client is connected to the server: WORKS
There is NO internet AND client is NOT connected to the server: WORKS
There is NO internet AND client is connected to the server: CRASHES every 10 sec
If I comment out the SYSTEM_THREAD(ENABLED); then working again without crash.
So, the conclusion is that if you run out of Christmas tree lights all you have to do is grab a photon, run the following code, connect to the server and disconnect wan from the routerā¦
However, I really appreciate your and ScruffR inputs! I hope Particles team will fix this random feature soonā¦
I too had problems with stability when using TCPServer on an connection with intermittent WiFi or WAN.
At the very end of the thread thereās discussion of disabling SYSTEM_FLAG_RESET_NETWORK_ON_CLOUD_ERRORS and that seems to be the ticket for me (with my app running on 0.7.0).