Ok, now I think I’m getting closer to the real cause of the random disconnects. So far everything I’ve done above (including the use of ConnectionManager calls) have only been having a palliative effect. That is, regaining the connection quicker when it drops.
But now I have found that if I comment out certain (time consuming?) parts of my device code I can get a constant connection even right next to the router.
My code is all event driven but when a 337 char string arrives via the uart (@128000baud) I have to lightly process it then agent.send the result (160 bytes) twice a second. I’ve stripped it all down to the bare bones but somehow it’s still blocking the maintenance of the WiFi connection.
Here’s my Device test code:
`
// regularly get value from Imp’s wifi signal strength
// and send to agent so we can see device is alive
function loop() {
agent.send(“rssi”, imp.rssi()); //receipt of rssi triggers publication
imp.wakeup(4.0, loop); // Sleep imp for another 4 seconds
}
// Function triggered by receipt of a byte from uart
function readback() {
local byte = serial.read();
// Ignore initial input / no data signal
if (byte == -1) return;
if (byte == 13) {
agent.send("stringLen",inputString.len());
inputString = "";
} else {
// Add the input character to the buffer
inputString = inputString + chr(byte);
}
}
// Convert passed integer value Ascii code into a character string
function chr(asciiValue) {
if (asciiValue < 32) return “”;
return format("%c", asciiValue);
}
//extend default retry period to 120 seconds
//server.setsendtimeoutpolicy(SUSPEND_ON_ERROR, WAIT_TIL_SENT, 120)
//imp.setsendbuffersize(8096); // a bigger buffer size
serial <- hardware.uart57;
inputString <- “”;
serial.setrxfifosize(1024); //accomodate large data xfer from catbrain
serial.configure(128000, 8, PARITY_NONE, 1, NO_CTSRTS, readback);
//imp.setpowersave(true); //This drops down to around one tenth of the normal operational power, with only a small impact on incoming message latency.
loop(); //kick-off regular updates to agent
`
And the test Agent:
`
// log connection events seen by the agent
device.onconnect(function() {
server.log(“Device connection event”);
});
// log disconnection events seen by the agent
device.ondisconnect(function() {
server.log(“Device disconnection event”);
});
// log “rssi” messages from the device
device.on(“rssi”, function(rssi) {
server.log(“RSSI=” + rssi);
});
// log how many bytes device recieved via uart
device.on(“stringLen”, function(stringLen) {
server.log(“stringLen=” + stringLen);
});
// regularly report supposed connection status
function loop() {
server.log(“Current connection state=”+device.isconnected());
imp.wakeup(4.0, loop); // Sleep for another 4 seconds
}
loop();`
And a log showing a typical disconnection (at around 12:20):
2015-10-31 12:19:42 UTC+0 [Status] Device connected 2015-10-31 12:19:42 UTC+0 [Agent] Device connection event 2015-10-31 12:19:42 UTC+0 [Agent] stringLen=337 2015-10-31 12:19:42 UTC+0 [Agent] RSSI=-33 2015-10-31 12:19:42 UTC+0 [Agent] stringLen=337 2015-10-31 12:19:42 UTC+0 [Agent] stringLen=337 2015-10-31 12:19:42 UTC+0 [Agent] stringLen=337 2015-10-31 12:19:42 UTC+0 [Agent] stringLen=347 2015-10-31 12:19:43 UTC+0 [Agent] stringLen=337 2015-10-31 12:19:43 UTC+0 [Agent] stringLen=337 2015-10-31 12:19:44 UTC+0 [Agent] stringLen=337 2015-10-31 12:19:44 UTC+0 [Agent] stringLen=337 2015-10-31 12:19:44 UTC+0 [Agent] Current connection state=true 2015-10-31 12:19:45 UTC+0 [Agent] stringLen=337 2015-10-31 12:19:45 UTC+0 [Agent] stringLen=337 2015-10-31 12:19:46 UTC+0 [Agent] RSSI=-38 2015-10-31 12:19:46 UTC+0 [Agent] stringLen=337 2015-10-31 12:19:46 UTC+0 [Agent] stringLen=337 2015-10-31 12:19:47 UTC+0 [Agent] stringLen=337 2015-10-31 12:19:47 UTC+0 [Agent] stringLen=337 2015-10-31 12:19:48 UTC+0 [Agent] stringLen=337 2015-10-31 12:19:48 UTC+0 [Agent] Current connection state=true 2015-10-31 12:19:49 UTC+0 [Agent] stringLen=337 2015-10-31 12:19:49 UTC+0 [Agent] stringLen=337 2015-10-31 12:19:50 UTC+0 [Agent] stringLen=337 2015-10-31 12:19:50 UTC+0 [Agent] RSSI=-42 2015-10-31 12:19:50 UTC+0 [Agent] stringLen=337 2015-10-31 12:19:51 UTC+0 [Agent] stringLen=337 2015-10-31 12:19:51 UTC+0 [Agent] stringLen=337 2015-10-31 12:19:52 UTC+0 [Agent] stringLen=337 2015-10-31 12:19:52 UTC+0 [Agent] stringLen=337 2015-10-31 12:19:52 UTC+0 [Agent] Current connection state=true 2015-10-31 12:19:53 UTC+0 [Agent] stringLen=337 2015-10-31 12:19:53 UTC+0 [Agent] stringLen=337 2015-10-31 12:19:54 UTC+0 [Agent] RSSI=-32 2015-10-31 12:19:54 UTC+0 [Agent] stringLen=337 2015-10-31 12:19:54 UTC+0 [Agent] stringLen=337 2015-10-31 12:19:56 UTC+0 [Agent] Current connection state=true 2015-10-31 12:20:01 UTC+0 [Agent] Current connection state=true 2015-10-31 12:20:05 UTC+0 [Agent] Current connection state=true 2015-10-31 12:20:09 UTC+0 [Agent] Current connection state=true 2015-10-31 12:20:13 UTC+0 [Agent] Current connection state=true 2015-10-31 12:20:17 UTC+0 [Agent] Current connection state=true 2015-10-31 12:20:21 UTC+0 [Agent] Current connection state=true 2015-10-31 12:20:25 UTC+0 [Agent] Current connection state=true 2015-10-31 12:20:29 UTC+0 [Agent] Current connection state=true 2015-10-31 12:20:33 UTC+0 [Agent] Current connection state=true 2015-10-31 12:20:37 UTC+0 [Agent] Current connection state=true 2015-10-31 12:20:41 UTC+0 [Agent] Current connection state=true 2015-10-31 12:20:45 UTC+0 [Agent] Current connection state=true 2015-10-31 12:20:49 UTC+0 [Agent] Current connection state=true 2015-10-31 12:20:53 UTC+0 [Agent] Current connection state=true 2015-10-31 12:20:54 UTC+0 [Status] Device connected 2015-10-31 12:20:54 UTC+0 [Agent] Device disconnection event 2015-10-31 12:20:54 UTC+0 [Agent] Device connection event 2015-10-31 12:20:55 UTC+0 [Agent] stringLen=337 2015-10-31 12:20:55 UTC+0 [Agent] RSSI=-34 2015-10-31 12:20:55 UTC+0 [Agent] stringLen=337 2015-10-31 12:20:55 UTC+0 [Agent] stringLen=337 2015-10-31 12:20:55 UTC+0 [Agent] stringLen=337 2015-10-31 12:20:55 UTC+0 [Agent] stringLen=347 2015-10-31 12:20:56 UTC+0 [Agent] stringLen=337 2015-10-31 12:20:56 UTC+0 [Agent] stringLen=337 2015-10-31 12:20:57 UTC+0 [Agent] stringLen=337 2015-10-31 12:20:57 UTC+0 [Agent] Current connection state=true 2015-10-31 12:20:57 UTC+0 [Agent] stringLen=337 2015-10-31 12:20:58 UTC+0 [Agent] stringLen=337 2015-10-31 12:20:58 UTC+0 [Agent] stringLen=337 2015-10-31 12:20:59 UTC+0 [Agent] stringLen=337 2015-10-31 12:20:59 UTC+0 [Agent] RSSI=-37
Apparently it takes nearly a minute for the Agent to spot the disconnection.
As is, the connection drops every couple of minutes. If I stop the 337 chars going into the device uart, it stays connected all the time. Oddly, if I let the chars come in but comment out the line that sends the char count to the agent, that’s also fine.
I can’t figure out what’s blocking?