Yun stops working after some hours, or days.

Hi all,

i'm having a very strange problem. I have a Yun that is programmed to send out sensor data to a server
every x minutes. The Yun is configurd for my WiFi network, and it has a 3G dongle . Both are working and
when there is no wifi signal anymore, the 3G takes over. Works fine;

But every now and then, the Yun hangs. I don't know exactly what causes it but obviously there's no other
option then to power it down and reboot. Obviously both sides of the Yun (arduino and linux) both are
down. In my code i added some watchdogs to see if the code is still running and the connections are made
properly, but it doesn't help a single bit. Pretty frustrating !

here's my code :

#include "Bridge.h"
#include "Process.h"
#include "dht.h"
#include "avr/wdt.h"

dht DHT;
#define DHT22_PIN 5         // attach sensor to pin 5

int reed = 2;               // connection to the reed sensor
int led =13;                // ledpin 
int counter0 = 0;           // 1 liter count  
unsigned long lastDebounce0 = 0;     // debouncing time
unsigned long debounceDelay = 500;   // Ignore bounces under 1/2 second
unsigned long lastPost = 0;
unsigned long PostDelay = 900000;    // Do a post every 15 minutes 
String processResult;       //Connection tries 
int trycount = 0;
int offset = 0 ;
boolean ledstatus=false;    // ledstatus
boolean rebooting=false;    // Busy rebooting Linux
String temp;                // temperature
String hum;                 // humidity
String cnt;                 // liter count
String IPaddress;

void setup() {
  pinMode(led, OUTPUT);
  pinMode(reed, INPUT);      
  digitalWrite(reed, HIGH);
  attachInterrupt(1, trigger, FALLING);
  offset=millis();
  Bridge.begin();
  wdt_enable(WDTO_8S);   //Enable watchdog. just in case.
}

// The loop routine runs over and over again forever:
void loop() {

  if (trycount<3)
  {
  wdt_reset();         // Reset watchdog
  }
  else 
  {
    if (rebooting=false)
    {
      doReboot;          //I've tried too many times, rebooting. while rebooting watchdog will reset arduino side ?
    }
  }
  
if((millis() - lastPost) > PostDelay)
  {
    lastPost = millis();
    int chk = DHT.read22(DHT22_PIN);
    temp = String(DHT.temperature);
    hum = String(DHT.humidity);
    cnt = String(counter0);
    postToServer(temp,hum,cnt);   
  }
}

void postToServer(String temp,String hum,String cnt) {

  digitalWrite(led, HIGH);         //Light up LED
  IPaddress="";
  Process q;                 
  q.runShellCommand("curl --connect-timeout 6 -m 6 \"http://www.xxx.xx/get_ip.php\"");
  while(q.running());
  
  while (q.available() > 0) {
    char result =q.read();
    IPaddress.concat(result);
    }

  processResult="";
  Process p;                 
  p.runShellCommand("curl --connect-timeout 6 -m 6 \"http://www.xxxx.xx/update.php?temp="+temp+"&hum="+hum+"&water="+cnt+"&trycount="+cnt+"&ip="+IPaddress+"\"");

  // do nothing until the process finishes, so you get the whole output:
  while(p.running());
  
  while (p.available() > 0) {
    char result =p.read();
    processResult.concat(result);
    }
    if (processResult.equals("OK"))               // there was no error submitting
      { 
       trycount=0;
       counter0 = 0;             // set liter count to 0
      Serial.println("Ok , posted temp "+temp+" hum "+hum);
      }
     else 
     {
      trycount++;
      Serial.println("Error posting :"+processResult);
     }
  digitalWrite(led, LOW);       // Finished uploading, turn off LED
}
  
void trigger() {
  if( (millis() - lastDebounce0) > debounceDelay){
    if (!ledstatus)
    {
      digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
      ledstatus=true;
    }else
    {
      digitalWrite(led, LOW);   // turn the LED on (HIGH is the voltage level)
      ledstatus=false;
    }
    counter0++;
    lastDebounce0 = millis();
  }
}


void doReboot() {
  rebooting=true;
  Process p;                 
  p.runShellCommand("reboot");
}

As you can see, i need to add the IP address to the database, in order to be able to connect to the Yun
over the 3G network (in case it is connected trough 3G).

Buth php scripts (get_ip and update.php) are pretty basic. The first one returns the IP address, the second
one reads the posted values trough GET and write them in a database. If that goes ok, the script returns
an 'OK' and the Yun knows that the data was processed as it should . If not, he tries 2 more times and after
that i call the 'doReboot' to reset the Linux side and then let the wdt_reset run out of time to reset the
arduino. Not sure of these are proper ways of resetting.

Nevertheless, it all doesn 't seem right, the Yun keeps hanging from time to time... :~

Does it actually reboot?

No, it doesn't . I think it just doesn't get that far.

i'm trying to build a logging system that logs everything on a SD card in order
to find the bug.

Sometimes it happens after a few hours, but the Yun rarely runs longer then 24hrs without hanging :frowning:

Question is, when i do the shellcommand 'Reboot' , is the arduino also rebooted ? I'm not sure about that.

Next to that, i see that i'm still on version 1.0 of OpenWRT. I'm going to update to 1.5.2 tomorrow and hopefully
that could give some improvement;

Yes you should definitely upgrade. The reboot only reboots the CPU side, but (with old versions of the image), if the sketch side is still doing things on the bridge, the CPU will probably not reboot, but hang in the bootloader. With the newer images there is less chance that this happens.

I did the upgrade and the system is still up and running. I'll hope this keeps going ok.

That way, how can i tell (or make the arduino reboot if not) when the cpu side is down
from the arduino side ? I should be able not to call any bridge processes when the cpu isn't
ready for it.

I don't know if it is the best way, but I regularly do a query to 'the other side'. If no response comes, I assume something is wrong.

Here is a way of checking if cpu side is responding: Add check for bridge by arve0 · Pull Request #2353 · arduino/Arduino · GitHub

Great information, thanx.

Also, normally when the CPU is booted, the white LED is shining. In my case, when the Yun hangs,
the LED is out . I suppose the only way to get it working again is reboot ?

A Bridge powered sketch may interfere with uboot (linux side bootloader). A fix has been released but it requires to follow this tutorial http://arduino.cc/en/Tutorial/YunUBootReflash: the uboot part is what you need and it requires particular care