Arduino stops working correctly after long periods of activity

Hello

I’m having trouble to get my arduino to work and I would be very happy if anyone has an Idea what the problem might be :slight_smile:

I want my arduino to check a web server every 2 minutes. The Website contains only one word which is either status1 or status0. The arduino parses all the code sent, and if it reads a 1 at the end of “status”, it makes coffee by sending a signal via a 433Mhz wireless module to a remote controlled power socket. It then resets the status by using a specific URL which makes the server reset status to 0. To send the signal to the power sockets I use the “rcswitch” library.

The problem is that after a couple of hours of activity, it stops working correctly, as it does not react to the web server saying “status1”.

However it works perfectly for the first hour or so. So Hardware is not the problem… Pressing the reset button does not seem to solve the problem. But it seems to change something as the arduino does reset the status to 0, but the power socket does still not get switched on. (It may be that the wireless module wasn’t plugged in correctly, but I strongly doubt it).

Removing the power supply and reattaching it works. The arduino does everything correctly then

Thanks for your help

Here is the code (if it helps):

#include <SPI.h>
#include <Ethernet.h>
#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char server[] = "www.inc.comxa.com";
IPAddress ip(192,168,178,147);

EthernetClient client;
String currentline="";

int status = 0;




void setup() {
  mySwitch.enableTransmit(5);
 currentline.reserve(256);
 Serial.begin(9600); 
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip);
  }
  delay(1000);
  Serial.print("Var god dröj");
}
void loop(){
  
  //Check, dvs get status
  
  delay(120000);//////////////////////////////////////////////////////////////
  Serial.println("connecting...");
  while (!client.connect(server, 80)){}
 
  
    Serial.println("connected");
    client.println("GET /StarWarsCoffee%20%20v1.0%20.php?whatiwantisthis=check HTTP/1.0");
    client.println("Host: inc.comxa.com\r\n");
    client.println("Connection: close");
    client.println();
    
    delay(3000);
    
    while (client.available()) {
      Serial.print("A");
    char c = client.read();
    Serial.print(c);
    currentline+=c;
    
    if(c=='\n'){
      currentline = "";
    }
    
    if(currentline.endsWith("status")){
      status= client.read();
      Serial.print(status);
      currentline= "";
     }
     
    
 
    }
     while(client.connected()){};
     if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
}


//done med check, kolla vad man ska göra med status

if(status==49){
  makecoffee();
  delay(4000);
  
  
  ////nu reset
  
  Serial.println("connecting...");
  while (!client.connect(server, 80)){}
 
  
    Serial.println("connected");
    client.println("GET /StarWarsCoffee%20%20v1.0%20.php?whatiwantisthis=reset HTTP/1.0");
    client.println("Host: inc.comxa.com\r\n");
    client.println("Connection: close");
    client.println();
    
    delay(3000);
    while (client.available()) {
    char c = client.read();
    
}
while(client.connected()){};
     if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    }
  

}
}

void makecoffee(){
Serial.print("making coffee");
mySwitch.switchOn("11111", "00010");
mySwitch.switchOn("11111", "00010");
mySwitch.switchOn("11111", "00010");
delay(100);
mySwitch.switchOn("11111", "00010");
delay(300000);
mySwitch.switchOff("11111", "00010");
mySwitch.switchOff("11111", "00010");
mySwitch.switchOff("11111", "00010");
delay(100);
mySwitch.switchOff("11111", "00010");
}

What do you think the problem could be?

You are using the String class in a way that fragments memory and causes it to run out of memory.

Try to remove the String. Try to process the characters coming in one at a time, without storing the whole thing.

If you have enough memory to store a complete message then the technique in Serial Input Basics should work. It uses a char array (or string, small s) to store the data rather than a String (capital S).

...R

Just out of curiosity, what information does status1 convey that 1 does not? What does status0 convey that 0 does not? The smaller the string, the easier it is to parse.

I used the php echo function to print 1, and I was to lazy to look up if echo used the standard HTML text tags, so I just added a start marker for the "1" of my own (which was "status").

By the way: I have seemingly solved the Problem. It seems to be working just fine after around 12 h of activity. (which is unusually long).

My solution was the simplest, if not the most elegant possible: I just added this function and call it at the end of void loop: void(* resetFunc) (void) = 0;

bestanamnetnogonsin:
My solution was the simplest, if not the most elegant possible: I just added this function and call it at the end of void loop: void(* resetFunc) (void) = 0;

The old "whack him in the mouth to stop him crying" trick. :slight_smile:

...R