I figured code would be the best way to demonstrate this problem.
This example uses the Phant library on the IDE. If you setup a local listener on a system in your environment you will see that after a while the Spark will fail to send the complete message. It will appear to have send the entire message but in reality is stops part way thru and will only recover after two more send messages are written.
Here is the example code:
#include "application.h"
#include "phant.h"
Phant::Stream stream1("192.168.1.140", "4JwJOg8xCw63f8wGM8Zp", "b5y58rrFo07xYmyY0rEl", 1010, PHANT_POST_METHOD);
int _ret;
int _retry;
int _loopcount;
unsigned long _ms;
float _f;
void setup() {
Serial.begin(9600);
while(!Serial.available()) {
Serial.println("Press any key to begin");
delay(1000);
}
Serial.println("PHANT test");
//Phant intitialization
_ms = millis();
stream1.begin();
//clearing previous stream values
while (_retry < 5 && !(_ret = stream1.clearStream())) { delay(500); _retry++; }
_f = (millis() - _ms) / 1000.0;
if (_ret)
Serial.print("Stream successfully cleared");
else
Serial.print("Stream could not be cleared (connection failed)");
if (_retry) {
Serial.print(" Retries = "); Serial.print(_retry);
}
Serial.print(" - time = "); Serial.print(_f, 1); Serial.println("s");
}
void loop() {
//Adding a int field to the stream
stream1.add("loop",++_loopcount);
_retry = 0;
_ret = 0;
_ms = millis();
while (_retry < 5 && !(_ret = stream1.sendData())) { delay(500); _retry++; }
_f = (millis() - _ms) / 1000.0;
Serial.print("Post ["); Serial.print(_loopcount); Serial.print("] ");
if (_ret)
Serial.print("successfully sent");
else
Serial.print("could not be sent (connection failed)");
if (_retry) {
Serial.print(" retries = "); Serial.print(_retry);
}
Serial.print(" - time = "); Serial.print(_f, 1); Serial.println("s");
delay(1000);
}
Here is the serial output -
Press any key to begin
Press any key to begin
PHANT test
Stream successfully cleared - time = 0.4s
Post [1] successfully sent - time = 0.6s
Post [2] successfully sent - time = 0.6s
Post [3] successfully sent - time = 0.6s
Post [4] successfully sent - time = 0.6s
Post [5] successfully sent - time = 0.6s
Post [6] successfully sent - time = 0.6s
Post [7] successfully sent - time = 0.6s
Post [8] successfully sent - time = 0.6s
Post [9] successfully sent - time = 0.6s
Post [10] successfully sent - time = 0.5s
Post [11] successfully sent - time = 0.6s
Post [12] successfully sent - time = 0.6s
Post [13] successfully sent - time = 0.6s
Post [14] successfully sent - time = 0.6s
Post [15] successfully sent - time = 0.6s
Post [16] successfully sent - time = 0.6s
Post [17] successfully sent - time = 0.6s
Post [18] successfully sent - time = 14.5s
Post [19] successfully sent - time = 0.6s
Post [20] successfully sent - time = 0.6s
Post [21] successfully sent - time = 0.6s
Post [22] successfully sent - time = 0.5s
Post [23] successfully sent - time = 0.6s
Post [24] successfully sent - time = 0.6s
Post [25] successfully sent - time = 0.6s
Post [26] successfully sent - time = 0.6s
Post [27] successfully sent - time = 0.6s
Post 18, the one with the large time is the one that fails. Post 18 never completed and Post 19 is the beginning of a good post.
Here is the detail of what was received on the other end -
POST /input/4JwJOg8xV9CLG4wGM8Zp HTTP/1.1
Host: 192.168.1.140
Connection: close
Phant-Private-Key: b5y58raboNC5YmyY0rEl
Content-Type: application/x-www-form-urlencoded
Content-Length: 7
loop=17
POST /input/4JwJOg8xV9CLG4wGM8Zp HTTP/1.1
HoPOST /input/4JwJOg8xV9CLG4wGM8Zp HTTP/1.1
Host: 192.168.1.140
Connection: close
Phant-Private-Key: b5y58raboNC5YmyY0rEl
Content-Type: application/x-www-form-urlencoded
Content-Length: 7
loop=19
The Phant library phant.cpp and phant.h are available in the IDE Here is the actual code that does the sending -
int Phant::Stream::sendData() {
int length = _host.length()+1;
char charBuffer[length];
_host.toCharArray(charBuffer,length);
if(_client.connect(charBuffer,_port)) {
if (_method == PHANT_POST_METHOD)
_client.println(post());
else //if(_method == PHANT_GET_METHOD)
_client.println(get());
delay(150);
_client.flush();
_client.stop();
_params = "";
return 1;
}
return 0;
}
Well… I just noticed this library has changed since I ran this test. The new updated library works for this small example, so I will try and use the updated library in my larger test.
If this works in the larger test, then I will delete this post.