I'm using a wifly module from roving networks for wifi posting to the Web.
I've noticed that after a while of working just fine, it spits out this message:
Wifly crashed and will reboot...but never does!
If it rebooted I guess it would be great because I might get my data again. But the problem is that it doesn't. I looked into Wifly.cpp and found that after that debug message it doesn't really call for a reboot. Should I add that in manually, wifly.reboot()?
Here is my full sketch:
#include <Wire.h>
#include "RTClib.h"
#include <Time.h>
#include <TimeAlarms.h>
#include <WiFlyHQ.h>
#include <SoftwareSerial.h>
RTC_DS1307 rtc;
////////////////////////SETUP GROVE SHIELD////////////////////////////////////
int pin = 8;
unsigned long duration;
unsigned long starttime;
unsigned long sampletime_ms = 30000;//sampe 30s ;
unsigned long lowpulseoccupancy = 0;
float ratio = 0;
float concentration = 0;
long previousMillis = 0;
long interval = 1000;
SoftwareSerial wifiSerial(2,3); //WIFI SHIELD CLIENT
#define DEBUG 1 //CO2 Sensor
const int pinRx = 8; //CO2 Sensor
const int pinTx = 7; //CO2 Sensor
SoftwareSerial sensor(pinTx,pinRx); //CO2 Sensor
const unsigned char cmd_get_sensor[] =
{
0xff, 0x01, 0x86, 0x00, 0x00,
0x00, 0x00, 0x00, 0x79
};
unsigned char dataRevice[9]; //CO2 Sensor
int temperature; //CO2 Sensor
int CO2PPM; //CO2 Sensor
WiFly wifly; //WIFI
String data; //WIFI
const char mySSID[] = "linksys";
const char myPassword[] = "mykey";
const char site[] = "myserver.com";
void terminal();
void setup(){
Serial.begin(9600);
Wire.begin();
rtc.begin();
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
//rtc.adjust(DateTime(__DATE__, __TIME__));
}
DateTime now = rtc.now();
setTime(now.hour(), now.minute(), now.second(), now.month(), now.day(), now.year());
Serial.print("Time is: ");
Serial.println(now.hour());
//Alarm.alarmRepeat(dowSaturday,8,30,30,WeeklyAlarm); // 8:30:30 every Saturday
Alarm.timerRepeat(1800, MorningAlarm); // timer for every 1800 seconds (30mins)
Serial.print("Alarms set!");
}
void loop(){
Alarm.delay(10); // wait one second between clock display
}
void wifiStuff(){
///////////////////WiFi CLIENT SETUP////////////////////////////////////////////
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();
//failedToJoin = true;
//waitAndTryAgain();
}
/* 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-WebClient");
Serial.print("DeviceID: ");
Serial.println(wifly.getDeviceID(buf, sizeof(buf)));
if (wifly.isConnected()) {
Serial.println("Old connection active. Closing");
wifly.close();
}
}
void initialiseSensor(){
sensor.begin(9600);
Serial.begin(9600);
Serial.println("get a 'g', begin to read from sensor!");
Serial.println("********************************************************");
Serial.println();
}
void MorningAlarm(){
Serial.println("Morning Alarm...");
initialiseSensor();
delay(3000);
printCO2();
wifiStuff();
reportToCloud();
}
void reportToCloud() {
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];
String dataString = dtostrf(CO2PPM, 8, 2, outstr);
data = String("name=Station1&age=" + dataString);
Serial.print(data);
/* Send the request */
wifly.println("POST /arduino/process.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());
//Replaced by wifly.close() at bottom of next if
//wifly.println("Connnection: close");
wifly.println();
wifly.print(data);
} else {
Serial.println("Failed to connect");
}
if (Serial.available() > 0) {
wifly.write(Serial.read());
}
}
/* Connect the WiFly serial to the serial monitor. */
void terminal(){
while (1) {
if (wifly.available() > 0) {
Serial.write(wifly.read());
}
if (Serial.available() > 0) {
wifly.write(Serial.read());
}
}
}
void printCO2() {
Serial.println("printing co2...");
if(dataRecieve())
{
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" CO2: ");
Serial.print(CO2PPM);
Serial.println("");
}
delay(1000);
}
bool dataRecieve(void) {
byte data[9];
int i = 0;
//transmit command data
for(i=0; i<sizeof(cmd_get_sensor); i++)
{
sensor.write(cmd_get_sensor[i]);
}
delay(500);
//begin reveiceing data
if(sensor.available())
{
while(sensor.available())
{
for(int i=0;i<9; i++)
{
data[i] = sensor.read();
}
}
}
#if DEBUG
for(int j=0; j<9; j++)
{
Serial.print(data[j]);
Serial.print(" ");
}
Serial.println("");
#endif
if((i != 9) || (1 + (0xFF ^ (byte)(data[1] + data[2] + data[3]
+ data[4] + data[5] + data[6] + data[7]))) != data[8])
{
Serial.println("false returned");
return false;
}
CO2PPM = (int)data[2] * 256 + (int)data[3];
temperature = (int)data[4] - 40;
Serial.println(temperature);
return true;
}