Hi,
We use the P2 as a TCP server. When a client connects it to the server of P2 and we disconnect it with a not proper way, with eg. disable the wifi of the client, the P2 freeze.
Client eg. windows telnet:
telnet IP_OF_p2 3456
(after connection leave it for a few sec to run, then disable the wifi of your PC or laptop)
Firmware on Argon és P2 verzió: 5.1.0
Tested on Argon as well, the same problem.
P2 code:
#include "Particle.h"
#include "application.h"
SYSTEM_THREAD(ENABLED);
//STARTUP(System.enableFeature(FEATURE_DISABLE_LISTENING_MODE));
STARTUP(WiFi.selectAntenna(ANT_INTERNAL));
SYSTEM_MODE(AUTOMATIC);
TCPServer server = TCPServer(3456);
TCPClient client;
boolean tablet_client_connected = false;
unsigned long client_connected_counter = 0;
unsigned long free_memory_error_millis = 0;
unsigned long last_send_tablet_data = 0;
unsigned long last_loop_wifi = 0;
byte tcp_msg[1152] = {0};
unsigned long last_blink = 0;
unsigned long blink_interval = 100;
bool blink_state = false;
int led = D7;
void setup() {
Serial.begin(115200);
WiFi.on();
WiFi.connect(WIFI_CONNECT_SKIP_LISTEN);
server.begin();
}
void loop() {
if (millis() - last_blink > blink_interval) {
last_blink = millis();
blink_state = !blink_state;
digitalWrite(led, blink_state);
Serial.printlnf("Cycle\t%lu", System.freeMemory());
}
/*
if (client.connected()) {
if (free_memory_error_millis > 0 && millis() - free_memory_error_millis > 3000) {
Serial.println("ipad disconnection detected");
client.flush();
client.stop();
client.flush();
client.stop();
free_memory_error_millis = 0;
}
}
*/
send_tcp_msg();
if (client.connected()) {
tablet_client_connected = true;
auto randomclient = server.available();
if(randomclient.connected()) {
randomclient.stop();
}
loop_wifi();
}
else {
if (tablet_client_connected == true) {
tablet_client_connected = false;
Serial.print(millis());
Serial.println(" disconnected ipad");
client.stop();
}
client = server.available();
if (client.connected()) {
tablet_client_connected = true;
Serial.print(millis());
Serial.println(" connected ipad");
client_connected_counter++;
}
}
}
void loop_wifi() {
if(millis() - last_loop_wifi >= 100) {
Serial.printlnf("a");
last_loop_wifi = millis();
if (client.connected()) {
Serial.printlnf("b");
if (client.available()) {
Serial.printlnf("c");
byte temp_buf[TCPCLIENT_BUF_MAX_SIZE] = {0};
int available_bytes = client.available();
Serial.printlnf("d");
if(available_bytes <= 0)
return;
while((available_bytes = client.available()) > 0) {
if(available_bytes > 0) {
client.read(temp_buf, available_bytes);
}
}
Serial.printlnf("e");
}
}
}
}
void send_tcp_msg() {
if(millis() - last_send_tablet_data >= 100) {
Serial.printlnf("1");
last_send_tablet_data = millis();
if(client.connected()) {
Serial.printlnf("2");
memset(tcp_msg, 'A', sizeof(tcp_msg));
Serial.printlnf("2-0");
client.write(tcp_msg, 1152, 0);
Serial.printlnf("2-1");
client.flush();
Serial.printlnf("2-2");
int tcpWriteErr = client.getWriteError();
/*if(tcpWriteErr < 0) {
Serial.print("tablet2: ");
Serial.println(tcpWriteErr);
}*/
if (tcpWriteErr != 0) {
client.flush();
if(free_memory_error_millis == 0) {
free_memory_error_millis = millis();
}
}
else {
free_memory_error_millis = 0;
}
}
Serial.printlnf("3");
}
}
Can you please help us to solve this problem?
Thanks!