Author Topic: Wifi hangs on startup  (Read 5542 times)

mikkelg

  • Newbie
  • *
  • Posts: 10
Wifi hangs on startup
« on: February 23, 2014, 12:37:18 pm »
Hello

I am having trouble with the wifi module halting my program when starting up from an external power source but not when i have just programmed it from the IDE. kevin posted a problem similar to mine, but i am having trouble understanding exactly what fixed the problem for him (i am not exactly a hardcore programmer :) ). I made a simple sketch to try and identify the problem where i start the wifi module in server mode in the setup function and in the loop function i just cycle the LED on and off to have visual indication that the program is running. After each cycle of the LED any server requests are handled.
When programmed from the IDE the the LED cycles on and off as expected (also answers server requests), but when i unplug all power and then plug the power back in, the LED never starts blinking at all (no answers to server requests as well). However, when the DigiX is in this state, i can still ping it from the command prompt, so i guess it is connected to the wifi?

By commenting out the section of the code that checks for wifi.ready() right after wifi.begin() i have determined that this section is the problem. It seems that wifi.ready() hangs indefinetely and never returns a value, thereby halting the program. A dirty fix is of course to just leave the section out and insert a small delay instead, but in some cases that could be problematic if the wifi is not done connecting (correct me if i am wrong). I am a little interested in what causes this behaviour since i would like to use it for an application that requires reasonable reliability from it. It seems strange to me that the Wifi module seems to be connected (answers pings from the network) but wifi.ready() doesnt return any value but instead hangs indefinetely. I let the program run for over 30 minutes to see if there was some timeout to the function, but that doesnt seem to be the case.  I recently updated my digifi library so i dont think that is the problem. Any idea what might be the reason for this?

Code: [Select]
#include <DigiFi.h>
DigiFi wifi;

int led = 13;

void setup() {
  pinMode(led, OUTPUT);
 
  wifi.begin(9600);
  while (wifi.ready() != 1)
  {
    Serial.println("Connecting to network...");
    delay(1000);
  }
  delay(1000);
  Serial.print("Server running at: ");
  String address = wifi.server(8080);//sets up server and returns IP
  Serial.println(address);
}

void loop() {
  digitalWrite(led, HIGH);
  delay(500);
  digitalWrite(led, LOW);
  delay(500);
 
    if ( wifi.serverRequest()){
        wifi_handleRequest();
      } 
     delay(10);
}


I also have another question regarding the reliability of the DigiX for being turned on for longer times. I have set up the simple server example and let i run a week, checking at regular intervals to see that the server was still up and running. It was going fine until after a few days where the DigiX stopped responding to server requests again. I could still ping it, so it was still connected to wifi, but the program seemed to have halted, possibly due to a similar problem as with the wifi.ready() function. Any idea what could be causing this? If for some reason the wifi looses connection it would be easy to just reset the connection and start over, but it is a problem that the entire problem seems to crash when this happens, since i want to use the DigiX to automate some processes while i am not around to just restart it myself :)

Any help would be greatly appreciated, thanks :)

mikkelg

  • Newbie
  • *
  • Posts: 10
Re: Wifi hangs on startup
« Reply #1 on: February 25, 2014, 01:15:43 pm »
in fact i have experienced problems with the wifi.serverRequest() function as well... looking in the library it seems as though the readResponse function might be stuck indefinetely, trying to read response from the wifi module but never finishing. Any ideas why this might be happening?

digistump

  • Administrator
  • Hero Member
  • *****
  • Posts: 1465
Re: Wifi hangs on startup
« Reply #2 on: February 26, 2014, 04:54:35 pm »
only on external power?

sounds like the external power is having trouble with the 300ma quick demand when the wifi module broadcasts - a big capacitor between win and ground might help - I'd think around 300uf or more.

mikkelg

  • Newbie
  • *
  • Posts: 10
Re: Wifi hangs on startup
« Reply #3 on: February 27, 2014, 02:33:24 pm »
After further testing i determined the problem isnt just restricted to external power, it also happens while debugging from the IDE. And as i added to the original post it is not just restricted to the startup either, it seems to happen at random when the server gets a request.
Either way I tried your idea and connected a 420uF capacitor (actuallly one 220 and two 100 in parrallel) between Vin and ground. At first i thought it had remedied the problem because the server was responding well to requests, but after a couple of minutes of sending GET request with the Postman chrome extension the same thing happened and the DigiX buckled under. I retried a couple of times but with no great success, every time it was just a matter of time before the program would lock up. So too much current draw alone might not be enough to explain it... it is also strange to me that it only happens sometimes instead of all the time, and also that it had not happened a single time in the first month or so while i was playing with the DigiX in server mode. Can we completely rule out that the unit has become defective?

Anywho i did further debugging and i found out the server would finish sending the reply every time, and only locks up when the next GET request is recieved. When this happened the program would not get past the wifi.serverRequest() function.
I also noticed another perculiar thing, which was errors in the path returned by wifi.serverrequestpath() such that when sending a request like this from the postman extension:
/?dose_Mg=2&user=xxx&password=xxx&dose_Ca=5&dose_kH=21&command=setDosing
It would sometimes (and only sometimes) be recieved incorrectly, for example like this:
/?dose_Mg=2&user=xxx&password=xxx&dose_Ca=5&dose_keWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36

At first it appeared to me that the first part of the request was correct but then turned into gibberish, but upon closer inspection it seems like the HTML header has been included in the request path by accident. Each time the error happens a new part of the header is included in the requestpath.

Next i found the "EthernetCompatible" Webserver example that is included for the DigiX. Here the entire request including header is printed over serial connection. I again tried to bombard the DigiX with requests to test if i could replicate the same error again (i was hopeful, because this example doesnt include the wifi.serverRequest() function which appeared to give me problems before) and i was not able to make the program crash so far.... even after many more tries, although i will keep stress testing it over the coming days to make sure it is not too good to be true. I also noticed that the request path seemed to be correct every time while testing, something which had not been the case for the code posted in my original post.

I then started digging a little into the DigiFi library to identify problem which allowed the program to hang. I noticed that both ready() and serverRequest() make calls to  readResponse() which seems to be the only place the program can get stuck (it contains a while loop). In the library readResponse reads as follows:
Code: [Select]
String DigiFi::readResponse(int contentLength) //0 = cmd, 1 = header, 2=body
{
    String stringBuffer;
    char inByte;
    int rCount = 0;
    int nCount = 0;
    int curLength = 0;
    bool end = false;
    Serial1.flush();

    while (!end)
    {
        //look for this to be four bytes in a row
        if (Serial1.available())
        {
            inByte = Serial1.read();
            curLength++;
            debugWrite(inByte);

            if(contentLength == 0){
                if (inByte == '\n' && rCount == 2 && nCount == 1)
                {
                    end = true;
                    int strLength = stringBuffer.length()-3;
                    stringBuffer = stringBuffer.substring(0,strLength);
                }
                else if (inByte == '\r')
                    rCount++;
                else if (inByte == '\n')
                    nCount++;
                else{
                    rCount = 0;
                    nCount = 0;
                }
            }
            else if(curLength>=contentLength)
                end = true;
           
            stringBuffer += inByte;
        }
    }

Could it be that somehow a mishap happens in this function that prevents "end" from being set as true and thus continues the loop forever?
If so, could it be and idea to add some kind of timeout to the readResponse function so that if something goes wrong, then the whole program doesnt hang?
I will dig a little more when i get time... I dont fully understand the problem yet i think :)

mikkelg

  • Newbie
  • *
  • Posts: 10
Re: Wifi hangs on startup
« Reply #4 on: March 06, 2014, 12:51:32 pm »
Unfortunately still getting this error... i just tried exchanging the power supply for a new 12V 1A i just recieved, but still getting the error at random times after starting the system (I still have 320 uF capacitor connected as well). I re-wrote my sketch to completely avoid the serverRequest() and ready() functions, and i even added a 2 second timeout for recieving requests in case some error would prevent the loop from finishing. All in all it works great until a certain point were a request is sent and the system completely crashes.
All my program does is listen for requests and respond depending on the request, so i am quite perplexed as to what might be going wrong here. I might be forced to add a watchdog timer to the setup to ensure that the program can run without permanent breakdowns, but i would really like to know what could cause this since i feel this error happens quite a lot (within 24 hours after starting the sketch is my experience).

Would it help to completely reset wifi after each request or once an hour maybe? not just the reset() command but through the reset pin?

hope someone can help as i really like the board but this obstacle is giving me a headache :)

digistump

  • Administrator
  • Hero Member
  • *****
  • Posts: 1465
Re: Wifi hangs on startup
« Reply #5 on: March 07, 2014, 02:27:04 pm »
Are you using the chunked response code? If not can you try sending your responses using that method - see the chunked response example in the latest release. My idea is that the TCP connections are staying open and that is causing the board to crash.

mikkelg

  • Newbie
  • *
  • Posts: 10
Re: Wifi hangs on startup
« Reply #6 on: March 18, 2014, 05:22:29 am »
I have been debugging further now to solve this problem and i am now ready to declare it almost 100% solved. It turns out that the problem actually consisted of several smaller problems, which was probably why it was so hard to identify the source of the crashes. It am quite sure that the problems were:

  • Insufficient power. The DigiX seems to crash during startup more frequently while only on USB power
  • Calls to wifi.ready() and wifi.server() seems to crash the DigiX once in a while. After looking a little in the library functions i suspect that the culprit is the "String DigiFi::readResponse(int contentLength)" function in the library, where the code gets caught in an endless loop when recieving. I suggest maybe introducing a timeout in this function to prevent the code from getting stuck here.
  • The last problem was also the worst, and luckily not related to the DigiX. I was using the digiX to return data for an app that is to interact with the. I wanted the data to be returned in JSON format in order to more easily interpret data on the android side of things. Unfortunately, I had not properly freed memory after building the JSON array (the aJSON library uses malloc()), which over time would cause some sort of memory leak, crashing the board entirely. Stupid me, could have saved many frustrations, but i guess i will not forget this lesson anytime soon :)

So this only leaves the wifi library functions getting stuck. The upside being that this could probably be solved by implementing a watchdog timer. What i experience now is that the code will halt during the initial part of the program when calling wifi.server(). I have completely removed any calls to wifi.ready() due to these problems, so i guess i am just counting on the wifi to be ready now. Instead i have a short delay in the beginning of the sketch to allow the wifi to connect.
I have also observed another peculiar behavior from the wifi.server() function. Sometimes the call will take a long time to complete, but then finally complete, returning 0.0.0.0 as the server adress. However, the server is still running normally and responds to requests just like if it had started normally. I dont know if this info might help in debugging the function.

With regards to the cunked response in the updated library, no i have not tried this. So far i have always used the serverResponse function with so far no problems. So far as i could identify my problems have always been related to recieving data and not to responding. I only used the ethernet compatible example to build a function to recieve data.

I have now tested the server for over a week being constantly on, and bombarding it with HTTP requests to test responsiveness. So far everything is working out very well and it has not crashed once, so i am relieved :)
But it has happened once or twice that the timeout has run out while recieving data, which is why i think a timeout on recieving data could be implemented. For reference, my function for recieving data is this:

Code: [Select]
String wifi_getHTTPrequest() {
  boolean end_receive = false;
  String requestBuffer;
  boolean currentLineIsBlank = false;
  char c;
  int i = 0;

  wifi.flush();
  while (!end_receive && i < timeout) {
    if (wifi.available()) {
      c = wifi.read();
      //Serial.print(c);
      requestBuffer += c;

      if (c == '\n' && currentLineIsBlank) {
        //wifi.serverResponse(requestBuffer);
        return requestBuffer;
        end_receive = true;
        currentLineIsBlank = false;
      }
      else if (c == '\n') {
        currentLineIsBlank = true;
      }
      else if (c != '\r') {
        currentLineIsBlank = false;
      }
    }
    delay(1);
    i++;
  }
  requestBuffer = "fail";
  return  requestBuffer; //if we reached this point, the timeout has been exceeded
}

It could probably by more refined, but this is working for me so far without trouble :)

JeffRand

  • Newbie
  • *
  • Posts: 44
Re: Wifi hangs on startup
« Reply #7 on: March 18, 2014, 10:55:55 pm »
I solved the issue with wifi.ready. See my post here:http://digistump.com/board/index.php/topic,1359.msg6216.html#msg6216

I plan to fix the endless loop in readResponse, when I get a chance.

mikkelg

  • Newbie
  • *
  • Posts: 10
Re: Wifi hangs on startup
« Reply #8 on: March 19, 2014, 01:52:05 pm »
cool, hadnt seen this fix yet :) I'll implement it in my own sketch asap.

I think however that not all troubles are over with the ready function... looking in the library ready() makes a call to STALinkStatus(), which in turn makes a call to readResponse(). This also explains why server() can get the DigiX stuck as well.