Arduino Yun freezes after a few hours

Hello,

I am trying to use the arduino yun to output the number of parts a machine has produced to a google spreadsheet. With the setup I have, I am able to output data to the spreadsheet temporarily. However, after some time, the yun stops working. The red LED I have on to indicate the code is running turns off, and I can no longer see the arduino in the port list. A reset of the 32u4 chip causes the LED to turn back on, indicating the code is running, but the board still does not appear in the port menu.

I have tested it at our workshop and it has run for 7-12 hours with no problems at all. It is only when we take it on to the production floor that we start experiencing these problems. Does anyone have any idea what the issue could be? Here is the most relevant part of the code:

  int sensorValue = analogRead(A0);
  voltage = sensorValue * (VOLTAGE_MAX / VOLTAGE_MAXCOUNTS);
  
  delay(50);
  if(voltage >= 4.9 && pressFlag == 0){
     
     sensorValue = analogRead(A0);
     voltage = sensorValue * (VOLTAGE_MAX / VOLTAGE_MAXCOUNTS);
     
     if(pressFlag == 0 && voltage >= 4.9){
          
          unsigned long int intCycleTime = timeSinceLastCycle;
          timeSinceLastCycle = 0;
          digitalWrite(LED_BUILTIN, LOW);   // turn the LED on (HIGH is the voltage level)
          
          printDate();
          String pressTime = printDate();
         
          buttonCount++;
          
          pressFlag = 1;
    
         
          
          String part1 = "curl -X POST -H \"Content-Type: application/json\" -d '{\"value1\":\"";
          String timeString = pressTime;
          String part2 = "\",\"value2\":\"";
          String numParts = String(buttonCount);
          String part3 = "\",\"value3\":\"";
          String strCycleTime = String(intCycleTime/1000); // + " seconds";
          String part4 = "\"}' https://maker.ifttt.com/trigger/sendData/with/key/xxxxxxxxxx -k";
          String curlString = part1+ timeString + part2 + numParts + part3 + strCycleTime + part4;
          
         // The curl string sends data to oue excel spreadhsheet using the IFTTT web service

          sendData.runShellCommandAsynchronously(curlString);
          sendData.close();
          
          digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
          
      }

Are you running the Yun off of wifi on the production floor?

This is only based on my own experience with the Yun's I have deployed to our own facility, but I ran into issues with our wifi connection and the yun's antenna just not being good enough for reliable constant data uploads. The arduino wouldn't consistently be available in the Ports menu either.

I end up doing a lot of testing via wifi for convenience sake, but when it is time to deploy something I hardwire it into the ethernet nowadays.

Perhaps if the network connection drops momentarily the sendData process might hang?

If you are on wifi, I'd recommend running at least a temporary ethernet line to see if that's the issue.

A few other comments on the code referenced:
printDate();
String pressTime = printDate();

^ you are calling the printDate() function twice here. I don't know if the function does anything else when it runs, but if you just need to store the result in your string, then you probably don't need that first call.

sendData.runShellCommandAsynchronously(curlString);
sendData.close();

^ wouldn't this attempt to close the sendData process immediately after it has been initiated?
Granted...the process probably isn't going to take very long to execute, so you are probably getting away with it a lot of the time, but you might consider using a while loop with sendData.running() to evaluate whether the process is still running before trying to close it. You could add a variable to check the millis() that have elapsed to help close a non-responsive process after 5 seconds or something.

I am running the yun on the WiFi on the production floor. There will be ethernet ports installed on the machines in the next day or two, but I was hoping to get WiFi working before then, seeing as I was able to get it to work in the workshop.

Maybe the network drop does cause the sendData to momentarily hang. While testing it in our workshop I cut out the internet a couple of time to see how the yun would respond but it was able to get itself back up and running successfully, so I don't think that is the issue.

Thank you for your input on the code. I am running the sendDate function twice because it occasionally returns 00:00:00 as the time if only run once (usually on startup this occurs). Running it twice seemed to rectify that, even if it is a little inelegant.

In regards to the sendData.close, I initially ran the code with a while loop for that, but I was worried that that was where to yun was hanging. I was also concerned that not closing the process would create a memory leak that would build up and cause the yun to hang. Seeing as neither of these attempted fixes have worked I will put the while loop back in the code.

Thank you for your help. It's just particularly strange, it worked perfectly on the WiFi in our workshop, it only seems to be on the production floor that things seem to go haywire. It will run perfectly for 1.5 hours or so, and then just stop outputting. Was the issue you experienced when you deployed the Yuns similar to mine?

Some more information. I added a series of outputs that change the code to look like this:

Console.println(voltage);
  delay(50);
  if (voltage >= 4.9 && pressFlag == 0) {

    Console.println("Delaying");
    sensorValue = analogRead(A0);
    voltage = sensorValue * (VOLTAGE_MAX / VOLTAGE_MAXCOUNTS);

    if (pressFlag == 0 && voltage >= 4.9) {

      unsigned long int intCycleTime = timeSinceLastCycle;
      timeSinceLastCycle = 0;
      digitalWrite(LED_BUILTIN, LOW);   // turn the LED on (HIGH is the voltage level)

      printDate();
      String pressTime = printDate();

      Console.print("PressTime is ");
      Console.println(pressTime);

      buttonCount++;
      Console.println(buttonCount);

      pressFlag = 1;



      String part1 = "curl -X POST -H \"Content-Type: application/json\" -d '{\"value1\":\"";
      String timeString = pressTime;
      Console.print(timeString + " seconds");
      String part2 = "\",\"value2\":\"";
      String numParts = String(buttonCount);
      String part3 = "\",\"value3\":\"";
      String strCycleTime = String(intCycleTime / 1000); // + " seconds";
      String part4 = "\"}' https://maker.ifttt.com/trigger/arduino2Request/with/key/gL8YmxeaUChOMJvmwpdXp -k";
      //curl -X POST -H "Content-Type: application/json" -d '{"value1":"1","value2":"2","value3":"3"}' https://maker.ifttt.com/trigger/arduino2Request/with/key/gL8YmxeaUChOMJvmwpdXp

      String curlString = part1 + timeString + part2 + numParts + part3 + strCycleTime + part4;

      // The curl string sends data to oue excel spreadhsheet using the IFTTT web service

      sendData.runShellCommandAsynchronously(curlString);

      elapsedMillis breakTimer = 0;
      /*while(sendData.running()){

          if(breakTimer > 5*1000){
            break;
          }
        } */
 

      Console.print("Data Available: "); // A value of 32 indicates a successful transmission of data, 0 also works if run asynchronously.
      Console.println(sendData.available());

      digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)

    }

The arduino stalled, and the output when it did so looked like:

1.58
1.54
5.00
Delaying
PressTime is n" -d 
3

This indicates it died when trying to use sendData.runShellCommandAsynchronously (I think, may also have been trying to print the time string but this would be explained by the blank). Could runShellCommandAsynchronously have caused the program to freeze if the network were to cut out at the wrong time?