Go Down

Topic: Server haulting after 4 days (Read 2 times) previous topic - next topic

RobDrizzle

Sep 28, 2012, 05:18 am Last Edit: Sep 28, 2012, 05:48 am by RobDrizzle Reason: 1
I have a garage door opener server that works for 4 or so days and then stops (Firefox times out and says that it took too long). This is not installed at my house, so I thought it was the internet connection. I can ping the address without any loss. The odd thing is that I was not able to access the site a few tries, then it worked but would not reload (or when the meta refresh happened, it would time out) and then I could not access it anymore.
The sketch size is 24188 bytes, is it possible that its running out of memory?

Board is a Uno R2
Sheild is a Yourduino Ethernet shield (Wiznet W5100)
IDE is 22

Here is the code, thanks for any help!

Code: [Select]
#include <Ethernet.h>
#include <TextFinder.h>
#include "DHT.h"
#include <avr/pgmspace.h> // for progmem
#include <Dhcp.h>
#include <RF24Network.h>
#include <RF24.h>
#include <SPI.h>


#define DHTPIN 5
#define DHTTYPE DHT22
//-----------------------------------------------------------------------

// nRF24L01(+) radio attached to SPI and pins 8 & 9
RF24 radio(8,9);
// Network uses that radio
RF24Network network(radio);
// Address of our node
const uint16_t this_node = 1;
// Address of the other node
const uint16_t other_node = 0;

//-----------------------------------------------------------------------
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0xE3, 0xCA };//= { 0xC5, 0xA1, 0xDA, 0x00, 0xE3, 0xA3 };
//byte ip[] = { 192, 168, 1, 66 };
//byte gateway[] = { 108, 89, xx, xxx };// 108.89.xx.xxx
char buffer[8]; // buffer holding the requested page name
Server server(80);
int relayPin = 3;
int switchPin = 2;
int ledPin = 4;
int val5 = 0;
int val = 0;
int val2 = 0;
String val3 = 0;
String doorCommand;
int stat = 0;
int doorStat;
int doorStat2;
int doorStat3;
int doorNum;
int door3;
int humid;
int t;
int temp;
int t2 = 0;
int h2 = 0;
const unsigned long interval = 2000;
unsigned long last_sent;

DHT dht(DHTPIN, DHTTYPE);
//--------------------------------------------------------------------------

struct message_t
{
 uint16_t temp_reading;
 uint16_t humid_reading;
 uint16_t doorStat_reading;
 message_t(void): temp_reading(), humid_reading(), doorStat_reading() {}
};

struct door_t
{
 uint16_t door_reading;
 door_t(void): door_reading(0) {}
};
//--------------------------------------------------------------------------

void setup()
{
Serial.begin(9600);
//Ethernet.begin(mac, ip);
 while (Dhcp.beginWithDHCP(mac) != 1)
 {
   Serial.println("Error getting IP address via DHCP, trying again...");
   delay(15000);
 }    
 delay(1000);
 
Serial.println("connecting...");
Serial.println("Begin Ethernet");
server.begin();
Serial.println("Begin Server");
dht.begin();
Serial.println("Begin DHT22");
SPI.begin();
Serial.println("Begin PrintF");
radio.begin();
Serial.println("Begin NRF Radio");
network.begin(/*channel*/ 90, /*node address*/ this_node);
Serial.println("Begin Network");
delay(3000);
Serial.println("Ready");
pinMode(relayPin, OUTPUT);
pinMode(switchPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(relayPin, HIGH);
digitalWrite(switchPin, HIGH);
//digitalWrite(ledPin, HIGH);

}

void loop(){
//-------------------------------------------
 humid = dht.readHumidity();
 t = dht.readTemperature();
 temp = (t * 1.8 + 32);
 
//----------------------------------------------  
 // Pump the network regularly
 network.update();

 // If it's time to send a message, send it!
 unsigned long now = millis();
 if ( now - last_sent > interval  )
 {
   last_sent = now;

   Serial.print("Sending...");

   RF24NetworkHeader header(/*to node*/ other_node);
   door_t door;
   door.door_reading += relayPin;
   bool ok = network.write(header,&door,sizeof(door));
   if (ok)
     Serial.println("ok.");
   else
     Serial.println("failed.");
     relayPin = 3;
 }
 
 
 while ( network.available() )
 {        
   message_t message;
   RF24NetworkHeader header;
   network.read(header,&message,sizeof(message));
   Serial.print("Received: ");
   t2 = (message.temp_reading * 1.8 + 32);
   h2 = (message.humid_reading);
   stat = (message.doorStat_reading);
   
   Serial.print(t2);
   Serial.print(" , ");
   Serial.println(h2);
   delay(1000);
 }
//----------------------------------------------  
 
Client client = server.available();
if (client) {
TextFinder finder(client );
int type = 0;
while (client.connected()) {
if (client.available()) {
// GET, POST, or HEAD
if(finder.getString("","/", buffer,sizeof(buffer))){
if(strcmp(buffer,"POST ") == 0){
finder.find("\n\r"); // skip to the body
// find string starting with "pin", stop on first blank line
// the POST parameters expected in the form pinDx=Y
// where x is the pin number and Y is 0 for LOW and 1 for HIGH

//------------------------------------------------
while(finder.findUntil("pinD", "\n\r")){
relayPin = finder.getValue(); // the pin number
val = finder.getValue(); // 0 or 1
digitalWrite(relayPin, val);
delay(1000);
digitalWrite(relayPin, HIGH);
}
//------------------------------------------------
//while(finder.findUntil("door", "\n\r")){
//doorNum = finder.getValue(); // the pin number
//val2 = finder.getValue(); // 0 or 1
//}
//-------------------------------------------------

Serial.println(relayPin);
Serial.println(val);
//Serial.println(val3);
//doorCommand = val2 + val3;
//Serial.println(doorCommand);



//------------------------------------------------------------
}

//client.print("<meta http-equiv=\"refresh\" content=\"10\">");
sendHeader(client,"Votto Vines Warehouse");
client.println("<h1><center>Votto Vines</center></h1>");
client.print("<center><IMG SRC='http://24.34.89.0:85/VVwh.jpg' ALT='Image'></center>");
client.println("<br />");
client.println("Warehouse 1 Temperature = "); client.print(temp); client.print("*F");
client.println(", Humidity = "); client.print(humid); client.print("% RH");
client.println("<br />");
client.println("Warehouse 2 Temperature = "); client.print(t2); client.print("*F");
client.println(", Humidity = "); client.print(h2); client.print("% RH");
//-------------------------------------------------------------------------
//Door 1
client.println("<h3>Warehouse 1</h3>");
client.print("<form action='/' method='POST'><p><input type='hidden'name='pinD3'");
client.print(" value='0'><input type='submit' value='Operate Door'/></form>");

client.print("Door is ");
if(doorStat == 1){
client.println("OPEN!");
}
else{
client.println("CLOSED");
}
//client.println("<br />");
//client.println("Warehouse 1 Temperature = "); client.print(temp); client.print("*F");
//client.println(", Humidity = "); client.print(humid); client.print("% RH");
client.println("<br />");

//--------------------------------------------------------------------------------
//Door 2

client.println("<h3>Warehouse 2</h3>");
client.print("<form action='/' method='POST'><p><input type='hidden'name='pinD4'");
client.print(" value='0'><input type='submit' value='Operate Door'/></form>");

client.print("Door is ");
if(doorStat2 == 1){
client.println("OPEN!");
}
else{
client.println("CLOSED");
}
client.println("<br />");

//--------------------------------------------------------------------------------
//Door 3

client.println("<h3>Warehouse 3</h3>");
client.print("<form action='/' method='POST'><p><input type='hidden'name='pinD5'");
client.print(" value='0'><input type='submit' value='Operate Door'/></form>");

client.print("Door is ");
if(doorStat3 == 1){
client.println("OPEN!");
}
else{
client.println("CLOSED");
}
client.println("<br />");

//--------------------------------------------------------------------------------
client.println("</body></html>");
client.stop();
}
break;
}
}
// give the web browser time to receive the data
delay(1);
client.stop();
}
//Serial.println(temp);
//Serial.println(humid);
//Serial.println(val5);
Serial.println(t2);
Serial.println(h2);


val5 = digitalRead(switchPin);   // read the input pin
digitalWrite(ledPin, val5);    // sets the LED to the button's value
if(val5 == LOW){
 doorStat = 0; // low = closed = 1
}
 else {
   doorStat = 1;
}
//------------------------------------------------------------


//----------------------------------------------

switch(stat) // If node dump reads "1" through blah is node. Need to assign Attic, Outdoor, etc.
{
case 0: // no "0" because we will hard code it in.
doorStat2 = 0;
doorStat3 = 0;
break;
case 10:
doorStat2 = 1;
doorStat3 = 0;
break;
case 01:
doorStat2 = 0;
doorStat3 = 1;
break;
case 11:
doorStat2 = 1;
doorStat3 = 1;
break;
default :
break;
}

}

//----------------------------------------------------------------

void sendHeader(Client client, char *title){
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.print("<meta http-equiv=\"refresh\" content=\"10\">");
client.print("<html><head><title>");
client.print(title);
client.println("</title><body>");
}

zoomkat

You can use the progmem macro F() to put static text in program memory like below.

Code: [Select]
client.println(F("<HTML><HEAD><TITLE>Arduino GET test page</TITLE></HEAD><BODY><H1>Zoomkat's simple Arduino button</H1><a href='/?on''>ON</a>&nbsp;<a href='/?off''>OFF</a></BODY></HTML>"));
Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)

Riva

Code: [Select]
String val3 = 0;
String doorCommand;

String is known to cause SRAM memory fragmentation and can lead to crashes, try coding to use char[] instead.

zoomkat's idea is also very good to help free up valuable SRAM. The F() also works with Serial.Print strings
Code: [Select]
Serial.println(F("Error getting IP address via DHCP, trying again..."));

SurferTim

#3
Sep 28, 2012, 01:22 pm Last Edit: Sep 28, 2012, 02:03 pm by SurferTim Reason: 1
The "F()" function is not available in V0022.  :(

Maybe install v1.0.1? You can run both versions if you don't like the new version.

edit: Note I said "install", not "upgrade".


RobDrizzle

Thanks for the help gentlemen. I can't use 1.0.1 without porting the libraries for the nRFnetwork and driver.

I will trying to cut down the use of String and see if that will make a difference first, before diving into any porting.

Go Up