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... :~