I have this issue where my code works when the loop is short enough around to let the wdt_reset kick in on time. But when I add code with long delays such as the SampleUVAlarm() function which takes about 10 seconds at current, the avr is reset because the pat doesnt come in under 8S. So Im thinking I need to extend my wdt timeout from 8S to perhaps 16s.
Is my thinking correct?
Here is my sketch:
#include <TimeAlarms.h>
#include <WiFlyHQ.h>
#include <SoftwareSerial.h>
#include <avr/wdt.h>
boolean initialiseSensorFlag;
unsigned long initialiseSensorTimestamp;
long previousMillis = 0;
long interval = 1000;
SoftwareSerial wifiSerial(2,3); //WIFI SHIELD CLIENT
#define DEBUG 1 //UV Sensor
const int pinRx = 8; //UV Sensor
const int pinTx = 7; //UV Sensor
SoftwareSerial sensor(pinTx,pinRx); //UV Sensor
float uvindex;
float mq2ratio;
WiFly wifly; //WIFI
String data; //WIFI
const char mySSID[] = "myssid";
const char myPassword[] = "mywifikey";
const char site[] = "myserver.com";
void terminal();
void setup(){
Serial.begin(9600);
Alarm.timerRepeat(1800, MainAlarm); //Every 30 mins
Serial.println("Alarms set!");
[color=red]wdt_enable(WDTO_8S);[/color]
}
void loop(){
Alarm.delay(10); // wait one second between clock display
if (wifly.moduleCrashed==true){
Serial.println("moduleCrashed");
[color=red]wdt_enable(WDTO_8S);
while (true);[/color]
}
[color=red]wdt_reset();[/color]
if(initialiseSensorFlag == true && millis() - initialiseSensorTimestamp > 3000){
initialiseSensorFlag = false;
SampleUVAlarm(); // calls and executes UV Sampling
}
}
void wifiStuff(){
char buf[32];
data = "";
Serial.print("Free memory: ");
Serial.println(wifly.getFreeMemory(),DEC);
wifiSerial.begin(9600);
if (!wifly.begin(&wifiSerial, &Serial)) {
Serial.println("Failed to start wifly");
terminal();
}
/* Join wifi network if not already associated */
if (!wifly.isAssociated()) {
/* Setup the WiFly to connect to a wifi network */
Serial.println("Joining network");
wifly.setSSID(mySSID);
wifly.setPassphrase(myPassword);
wifly.enableDHCP();
if (wifly.join()) {
Serial.println("Joined wifi network");
} else {
Serial.println("Failed to join wifi network");
terminal();
}
} else {
Serial.println("Already joined network");
}
wifly.setDeviceID("Wifly-WebClient2");
Serial.print("DeviceID: ");
Serial.println(wifly.getDeviceID(buf, sizeof(buf)));
if (wifly.isConnected()) {
Serial.println("Old connection active. Closing");
wifly.close();
}
reportToCloud();
}
void initialiseSensor(){
Serial.begin(9600);
Serial.println("********************************************************");
}
void MainAlarm(){
Serial.println("Main Alarm...");
Serial.println(wifly.moduleCrashed);
initialiseSensor();
initialiseSensorFlag = true;
initialiseSensorTimestamp = millis();
}
void reportToCloud() {
Serial.println("Reporting to cloud...");
if (wifly.available() > 0) {
char ch = wifly.read();
Serial.write(ch);
if (ch == '\n') {
/* add a carriage return */
Serial.write('\r');
}
}
if (wifly.open(site, 80)) {
Serial.print("Connected to ");
Serial.println(site);
// Set data to send
static char outstr[15];
static char outstr2[15];
String dataString = dtostrf(uvindex, 8, 2, outstr);
String dataString2 = dtostrf(mq2ratio, 8, 2, outstr2); //UNCOMMENT
data = String("name=" + dataString2 + "&age=" + dataString);//UNCOMMENT
Serial.print(data);
/* Send the request */
wifly.println("POST /arduino/data_post_.php HTTP/1.0");
wifly.println("Host: www.myserver.com"); // SERVER ADDRESS HERE TOO
wifly.println("Content-Type: application/x-www-form-urlencoded" );
wifly.print("Content-Length: ");
wifly.println(data.length());
wifly.println();
wifly.print(data);
Serial.println("Posted successfully");
} else {
Serial.println(">>Failed to connect");
}
if (Serial.available() > 0) {
wifly.write(Serial.read());
}
wifly.close();
}
void SampleUVAlarm (){
delay(10000);
}