Imp continually disconnecting from WIFI

I have an Imp that is used to communicate with a 4x4 HDMI matrix from monoprice over the UART. It has been working wonderfully until recently (maybe a few months ago) I started having problems when I couldn’t communicate with the Imp.

According to the logs, the Imp continually disconnects from the Wireless every 5 minutes or so. When it’s connected, it works fine, but once it disconnects, I cannot get it to reconnect (even if I re-blink it) until it connects on its own.

Here is a snippet from my logs:

2014-11-10 08:35:33 UTC-5 [Status] Device Booting; 3.37% program storage used 2014-11-10 08:36:35 UTC-5 [Status] Device disconnected 2014-11-10 08:36:42 UTC-5 [Status] Device Booting; 3.37% program storage used 2014-11-10 08:41:29 UTC-5 [Status] Device disconnected 2014-11-10 08:41:31 UTC-5 [Status] Device Booting; 3.37% program storage used 2014-11-10 08:43:13 UTC-5 [Status] Device disconnected 2014-11-10 08:43:15 UTC-5 [Status] Device Booting; 3.37% program storage used 2014-11-10 08:52:33 UTC-5 [Status] Device disconnected 2014-11-10 08:52:38 UTC-5 [Status] Device Booting; 3.37% program storage used 2014-11-10 08:58:23 UTC-5 [Status] Device disconnected 2014-11-10 09:18:45 UTC-5 [Exit Code] imp restarted, reason: wifi outage 2014-11-10 09:18:45 UTC-5 [Status] Device Booting; 3.37% program storage used 2014-11-10 09:24:45 UTC-5 [Status] Device disconnected 2014-11-10 09:24:45 UTC-5 [Status] Device Booting; 3.37% program storage used 2014-11-10 10:09:30 UTC-5 [Agent] { "input2": "PS3", "input3": "GoogleTV", "input4": "PC", "input2On": true, "output2": "Basement", "output3": "Dan's Office", "output4": "Liz's Office", "input1On": false, "input1": "XBox", "memD": 2, "memC": 2, "memB": 4, "memA": 3, "input4On": false, "output1On": false, "output1": "Living Room", "output4On": false, "input3On": true, "output2On": true, "output3On": true, "out4": 2, "out1": 3, "out2": 2, "out3": 3 } 2014-11-10 10:24:47 UTC-5 [Status] Device disconnected

The code is simple:

`function changeMatrix(command)
{
//hardware.configure(UART_57);
hardware.uart57.configure(9600, 8, PARITY_NONE, 1, NO_CTSRTS);

if (command != 255)
{
local notCommand = 255 - command;

server.log("command: " + command + " notCommand: " + notCommand);


hardware.uart57.write(command);
imp.sleep(0.06);

hardware.uart57.write(notCommand);
imp.sleep(0.06);

hardware.uart57.write(0xD5);
imp.sleep(0.06);

hardware.uart57.write(0x7B);

}

imp.sleep(0.06);

local startbit = 0;

do {
startbit = hardware.uart57.read();
}while(startbit != 0x61)

local status = blob(9);

for (local i = 0; i < 9; i += 1)
{
status.writen(hardware.uart57.read(), ‘b’);
}

agent.send(“status”, status);

hardware.uart57.disable();
}

agent.on(“change”, changeMatrix);
`

The HDMI matrix continuously sends data over the uart, which may be a problem? Also, I have a Netgear WNR3500L, which is placed about a foot from the Imp.

Thank you for your help!

You may want to look into setting the IMP so that it can still function when it loses network connectivity. This is what I did in one of my projects, so that it would let me know how many times it had been disconnected.

`function disconnection_handler(reason)
{
if (reason != SERVER_CONNECTED) {
disconnectFlag = true;
disconnectCount++;
server.connect(disconnection_handler, 60);
} else {
server.log("-------------------------------------------------- Disconnected " + disconnectCount + " times!" );
disconnectCount = 0;
}
}

server.setsendtimeoutpolicy(RETURN_ON_ERROR, WAIT_FOR_ACK, 60);
server.onunexpecteddisconnect(disconnection_handler);`

That’s a good idea for tracking and logging the disconnects, but I think the log does that ok already. I use an app on my phone to control the matrix when I go to a new room in the house, so it has to be connected whenever I need it.

Also, if I do try to fiddle with it while its disconnected, it blinks the one long red, and three short reds like its joining the network, but it never goes green.

The log will automatically track that the device went offline, but the variable disconnectCount will track that it attempted to re-establish a connection X times during that period.

Setting the imp to “RETURN_ON_ERROR” allows it to continue with the program in the device despite the loss of connectivity. I was under the impression that the IMP is driving the output on the matrix. Does this activity stop if it loses the wifi connection?

The Imp is just selecting which HDMI input goes to which HDMI output. It is controlling this through the serial port, but the actual selection is user driven, which needs access to the network.

I just tried to re-flash it again, and it still hasn’t connected to the wifi.

Hmmm… maybe if you try re-routing the wifi power through the primary EPS taps and then apply warp power directly using a phase discriminator.

(sorry… I had to)

A couple things to try:

  1. Can you log the rssi?
  2. You have an infinite loop in your code:

do { startbit = hardware.uart57.read(); }while(startbit != 0x61)

If it takes a while for the startbit to arrive, you’re going to end up disconnecting as you’re blocking all of the other background tasks the imp needs to do. You might want to try modifying your loop to something like this:

`local timeout = 10; // try to get startbit for up to 10 seconds
local start = time();

do {
startbit = hardware.uart57.read();
} while(startbit != 0x61 && time() - start < timeout)

// if we didn’t get a start bit it means we timed out
if (startbit != 0x61) {
server.log(“Timed out waiting for for start bit”);
return;
} `

I definitely want to try that! Thank you. The only thing is that should not be reached until a request to switch comes in. The only code that should be executed on start-up is the agent.on("change", changeMatrix);, unless I’m mistaken about that? The imp disconnects without any call from the agent most of the time.

Is there any other reason the changeMatrix function could be called without a signal from the agent?

Not that I can see based on your code…

If that doesn’t resolve it you’ll need to start at other possibilities (are you having WiFi issues, is your imp rebooting because of electrical or power problems, etc)