Nothing seems.
Im not sure where problem is.
I through was this timeout frezing program since photon stop answer ping.
Im using this code:
SYSTEM_MODE(MANUAL);
SYSTEM_THREAD(ENABLED);
#include "I2Cdev.h"
#include "ITG3200.h"
#include "ADXL345.h"
#include "HMC5843.h"
ITG3200 gyroDev;
ADXL345 accelDev;
HMC5843 magDev;
char wifiName[ ] = "XXXX";
char wifiPassword[ ] = "XXXX";
IPAddress myAddress(192, 168, 0, 99);
IPAddress netmask(255, 255, 255, 0);
IPAddress gateway(192, 168, 0, 1);
IPAddress dns(192, 168, 0, 1);
TCPClient client;
byte server[] = {192, 168, 0, 5};
int port = 8087;
#define STATUS_LED_PIN D7
String buffer;
bool wifiConnected(bool reconnect, int limit) {
if (! WiFi.ready()) {
if (reconnect) {
if (! WiFi.connecting()) {
RGB.control(true);
RGB.color(255, 255, 255);
WiFi.setCredentials(wifiName, wifiPassword, WPA2, WLAN_CIPHER_AES);
WiFi.setStaticIP(myAddress, netmask, gateway, dns);
WiFi.useStaticIP();
WiFi.connect(WIFI_CONNECT_SKIP_LISTEN);
RGB.control(false);
}
Particle.process();
if (limit > 0) {
if (waitFor(WiFi.ready, limit)) {
Particle.process();
return true;
}
} else if (WiFi.ready()) {
return true;
}
}
Particle.process();
return false;
}
Particle.process();
return true;
}
bool clientIsConnected() {
Particle.process();
if (wifiConnected(false, 0)) {
Particle.process();
if (client.connected()) {
return true;
}
}
Particle.process();
return false;
}
void setup() {
WiFi.on();
if (wifiConnected(true, 10000)) {
Particle.disconnect();
Serial.begin(9600);
pinMode(STATUS_LED_PIN, OUTPUT);
digitalWrite(STATUS_LED_PIN, HIGH);
buffer = "";
while(! Serial.available()) {
Particle.process();
delay(500);
}
delay(1000);
digitalWrite(STATUS_LED_PIN, LOW);
} else {
RGB.control(true);
RGB.color(255, 255, 0);
RGB.brightness(255);
delay(5000);
RGB.control(false);
System.reset();
}
}
void loop() {
Serial.println("LOOP");
if (wifiConnected(false, 0)) {
Serial.println("WIFI ON");
Serial.println(System.freeMemory());
if (! clientIsConnected()) {
RGB.control(true);
RGB.color(0, 0, 255);
buffer.remove(0);
buffer = "";
digitalWrite(STATUS_LED_PIN, LOW);
client.flush();
client.connect(server, port);
RGB.color(255, 255, 255);
if (client.connected()) {
RGB.color(255, 0, 0);
delay(1000);
}
delay(1000);
RGB.color(111, 111, 111);
delay(1000);
RGB.control(false);
} else {
Serial.print("Client connected ");
Serial.println(client.available());
if (clientIsConnected()) {
client.println("Initializing I2C devices...");
}
RGB.control(true);
RGB.color(255, 255, 255);
if (clientIsConnected()) {
RGB.control(true);
RGB.color(255, 255, 0);
client.println("Testing device connections...");
}
RGB.control(false);
bool retry = false;
do {
Serial.println("while");
Serial.print("Ready: ");
Serial.println(WiFi.ready());
Serial.print("Connecting: ");
Serial.println(WiFi.connecting());
Serial.print("Ping: ");
Serial.println(WiFi.ping(dns));
digitalWrite(STATUS_LED_PIN, HIGH);
Wire.begin();
gyroDev.initialize();
if (! gyroDev.testConnection()) {
retry = true;
Wire.end();
if (clientIsConnected()) {
client.println("ITG3200 connection failed, retrying...");
}
delay(1000);
} else {
if (clientIsConnected()) {
client.println("ITG3200 connection successful");
}
}
if (! retry) {
accelDev.initialize();
if (! accelDev.testConnection()) {
retry = true;
Wire.end();
if (clientIsConnected()) {
client.println("ADXL345 connection failed, retrying...");
}
delay(1000);
} else {
if (clientIsConnected()) {
client.println("ADXL345 connection successful");
}
}
}
if (! retry) {
magDev.initialize();
magDev.setMode(HMC5843_MODE_CONTINUOUS);
magDev.setDataRate(HMC5843_RATE_50);
if (! magDev.testConnection()) {
retry = true;
Wire.end();
if (clientIsConnected()) {
client.println("HMC5843 connection failed, retrying...");
}
delay(1000);
} else {
if (clientIsConnected()) {
client.println("HMC5843 connection successful");
}
}
}
//Works with this
/*while (client.available() > 0) {
char a = client.read();
} */
//Freeze photon after some ~5 seconds, photon stop answer pings, and server reports tcpclient connection dropped
client.flush();
} while (retry && clientIsConnected());
digitalWrite(STATUS_LED_PIN, LOW);
}
} else {
RGB.control(true);
RGB.color(255, 255, 0);
RGB.brightness(255);
digitalWrite(STATUS_LED_PIN, LOW);
delay(5000);
RGB.control(false);
System.reset();
}
}
Nodejs server code running at pc for test
var net = require('net');
var server = net.createServer(function(socket) {
console.log('con');
setInterval(function() {
socket.write('#D01!');
}, 1000);
socket.on('data', function(data) {
console.log('DATA ' + socket.remoteAddress + ':' + socket.remotePort + ': ' + data);
});
socket.on('close', function(data) {
console.log('CLOSED: ' + socket.remoteAddress +':'+ socket.remotePort);
});
});
server.listen(8087, '192.168.0.5');
Not sure what causing photon to stop answering pings, seems wifi connection went down, but works if I use client.read() to empty tcp client buffer. I though timeout was making this