Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
11 years, 6 months ago.
detect wifly disconnect?
code:
int main() { wifly.init(); //Use DHCP while (!wifly.connect()); printf("IP Address is %s\n\r", wifly.getIPAddress()); Websocket ws("ws://192.168.0.5:8080/"); while (!ws.connect()); while (1) { ws.send("WebSocket Hello World over Wifly"); wait(0.5); //attempting to detect wifly disconnect while (!wifly.connect()) { wifly.connect(); ws.connect(); } } }
I am attempting to reconnect after detecting a disconnect; however, while (!wifly.connect()) does not seem to work as I expected. I created a websocket server using nodejs and node-webkit which seems to function properly until approximately a day later the wifly seems to disconnect unexpectedly creating an error in node-webkit(which I have already dealt with). Since I do not know why the wifly disconnects, I assume checking for a disconnect to reconnect may fix my issue?
2 Answers
11 years, 1 month ago.
Hi Richard, I have seen in my own work that Wifly was working well - while I watched, but overnight it may decide to drop the link and not reestablish it. there are still a lot of Wifly commands I haven't worked through, which may hold the key to maintaining/reconnecting reliably.
As at least a means to be aware of the event, there is a pin on the Wifly module that indicates the state of connection - so if you have that wired in, you could monitor for a change in state there.
11 years, 1 month ago.
Hi Richard and David,
I added this function to Wifly.cpp
bool Wifly::is_attached()
{ char res[20]; res[0] = 0; for (int ict = 0; ((ict < 5) && (res[0] == 0)); ict++) { sendCommand("show c\r\n", "NO", res, 150); } flush(); flush(); if (res[2] == '3') { either 8630, f630, 8631 or f631 return true; attached } else return false; }
I'm not sure whether those flush(); commands are doing anything. If the wifly disconnects, the 3rd character in the response to the 'show c' command returns something other than 3 (maybe 0, but I'm not sure). Make sure the wifly is in the CMD mode before calling this. I have been checking at intervals, say 1/2 hr, and reconnecting if this function reports a disconnect and it has been very stable. The allocation for the result string can be at the top of the Wifly.cpp file, instead of here and used in other functions.
All the best.