Arduino Uno + Ethernet Shield Web Server freezes after an amount of time

Hi.

I got this working code that works fine for around a few hours but when I check the next day the webserver is unresponsive and I need to restart the device to get it working again.

I was wondering if anyone can help me get it to automatically restart every 30 minutes or so?

The program & Hardware:

Arduino uno connected to Ethernet shield + a radio transmitter and a few LEDs

When the device is powered on it hosts a website with clickable buttons, each clickable button transmits a radio command to some remote power plugs I have in the house.

What I need help with:

A way to restart the device every 30 minutes or so without the solution stopping the webserver. So I would really want 2 loops running at the same time, one for the web server and one for the restarting code (watchdog etc), but from my understanding the arduino uno does not support multiple loops so I am wondering how I can implement a restart without stopping the webserver.

My only solution so far is to use another arduino uno just to restart the other one, but seems a bit silly....

Working code:

#include "SPI.h"
#include "Ethernet.h"
#include <NewRemoteTransmitter.h>


const unsigned long TRANSMITTER_ADDRESS = 27612394; //My personal transmitter address
const int PIN_TRANSMITTER = 8;

NewRemoteTransmitter transmitter(TRANSMITTER_ADDRESS, PIN_TRANSMITTER, 260, 3);

uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // MAC Address
uint8_t ip[] = { 192,168,1,50 };                        // IP Address
EthernetServer server(80);                           // Server Port 80

// RemoteTransmitter configuration
// Connect Transmitter to Pin 11 and receiver to pin 2 (all digital)
// FAN 1 ON/OFF:
// Addr 27612394 unit 0 on, period: 267us.
// Addr 27612394 unit 0 off, period: 267us.
// FAN 2 ON/OFF:
// Addr 27612394 unit 1 on, period: 266us.
// Addr 27612394 unit 2 off, period: 267us.
// FAN 3 ON/OFF:
// Addr 27612394 unit 2 on, period: 267us.
// Addr 27612394 unit 1 off, period: 266us.
// ALL POWER OFF:
// Addr 27612394 group off, period: 267us.

String controlString; // Captures out URI querystring;;
int blueLEDPin = 2; // pin where our blue LED is connected
 
void setup(){
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  digitalWrite(3, HIGH);
  Ethernet.begin(mac, ip);
  server.begin();
  pinMode(PIN_TRANSMITTER, OUTPUT);
  Serial.begin(9600);
  Serial.print("Serial Write turned on");

}
 
void loop(){ 
    // Create a client connection 
    EthernetClient client = server.available(); 
    Serial.print("Launching Ethernet WebServer");
    if (client) { 
        while (client.connected()) { 
            if (client.available()) { 
                char c = client.read();
 
                //read the HTTP request 
                if (controlString.length() < 100) {
 
                    // write characters to string 
                    controlString += c; 
                }
 
                //if HTTP request has ended– 0x0D is Carriage Return \n ASCII 
                if (c == 0x0D) {
                    digitalWrite(4, HIGH);
                    httpResponseHome(client);
                    Serial.print("Website is up");
                    //stopping client 
                    client.stop();
                    digitalWrite(4, LOW);
 
                    // control arduino pin 
                    if(controlString.indexOf("?1-on") > -1) //checks for switchon 
                    { 
                        transmitter.sendGroup(true);
                        httpResponseRedirect(client);
                    } 
                    else 
                        if(controlString.indexOf("?1-off") > -1) //checks for switchoff 
                        { 
                        transmitter.sendGroup(false);
                        httpResponseRedirect(client);
                        } 
                    
                    else 
                        if(controlString.indexOf("?2-on") > -1) //checks for switchon
                        { 
                        transmitter.sendUnit(0, true);
                        httpResponseRedirect(client);
                        } 
                     
                    else 
                        if(controlString.indexOf("?2-off") > -1) //checks for switchoff 
                        { 
                        transmitter.sendUnit(0, false);
                        httpResponseRedirect(client);
                        } 
                     
                    else 
                        if(controlString.indexOf("?3-on") > -1) //checks for switchon
                        { 
                        transmitter.sendUnit(1, true);
                        httpResponseRedirect(client);
                        } 
                     
                    else 
                        if(controlString.indexOf("?3-off") > -1) //checks for switchoff 
                        { 
                        transmitter.sendUnit(1, false);
                        httpResponseRedirect(client);
                        } 
                     
                    else 
                        if(controlString.indexOf("?4-on") > -1) //checks for switchon
                        { 
                        transmitter.sendUnit(2, true);
                        httpResponseRedirect(client);
                        } 
                     
                    else 
                        if(controlString.indexOf("?4-off") > -1) //checks for switchoff 
                        { 
                        transmitter.sendUnit(2, false);
                        httpResponseRedirect(client);
                        } 
                      
                    //clearing string for next read 
                    controlString="";
 
                } 
            } 
        } 
    }
}

void httpResponseHome(EthernetClient c) {
  c.println("HTTP/1.1 200 OK");
  c.println("Content-Type: text/html");
  c.println();
  c.println("<html>");
  c.println("<head>");
  c.println(    "<title>MPC NEXA Webserver</title>");
  c.println(    "<style>");
  c.println(        "body { font-family: Arial, sans-serif; font-size:12px; }");
  c.println(    "</style>");
  c.println("</head>");
  c.println("<body>");
  c.println(    "<h1>MPC NEXA Webserver</h1>");
  c.println(    "<ul>");
  c.println(        "<li><a href=\"./?1-on\">Turn on power</a></li>");
  c.println(        "<li><a href=\"./?1-off\">Turn off power</a></li>");
  c.println(    "</ul>");
  c.println(    "<ul>");
  c.println(        "<li><a href=\"./?2-on\">Turn on power 1</a></li>");
  c.println(        "<li><a href=\"./?2-off\">turn off power 1</a></li>");
  c.println(    "</ul>");
  c.println(    "<ul>");
  c.println(        "<li><a href=\"./?3-on\">Turn on power 2</a></li>");
  c.println(        "<li><a href=\"./?3-off\">turn off power 2</a></li>");
  c.println(    "</ul>");
  c.println(    "<ul>");
  c.println(        "<li><a href=\"./?4-on\">Turn on power 3</a></li>");
  c.println(        "<li><a href=\"./?4-off\">turn off power 3</a></li>");
  c.println(    "</ul>");
  c.println(    "<hr>");
  c.println("</body>");
  c.println("</html>");
}

void httpResponseRedirect(EthernetClient c) {
  c.println("HTTP/1.1 301 Found");
  c.println("Location: /");
  c.println();
}

I did briefly look at this sort of thing and had similar problems - checking for and restarting the connection in loop helped .

You can cheat and add a watch dog timer to restart it ( yes a real bodge.. but sorting these problem out can be very difficult)

So if I just add a watchdog timer, that resets every 8 seconds with a delay (8000) that should work?

I just thought that if I add "delay (8000)" code the webserver itself might stop working, since I think it needs to run non stop in a loop?

No the idea is if the program hangs for more than 8 seconds the processor resets . If it doesn’t hang , then nothing happens .
In use the program loop should reset the watch dog timer each time it loops , if it hangs up in a loop , then the watch dog would then fire after the set time .

Google for some examples . There is a library for it too

.. it is a “ non ideal “ solution as a general fix , but it’s not a bad idea for things outside your control which might crash the sketch , wghich you can’t guard against .

I see the magic word "String" in your code. This is known to cause the exact problem you describe - it crashes after several hours of operation due to failure of the "garbage collection" of Strings in the heap.

Evil magic! :crazy_face:

1 Like

Ahhh, I'm not really a programmer and I just copy examples around to create something that works :stuck_out_tongue: Any idea on what I can do to fix this? Except for manually restarting / trying to get the watchdog to reset it all the time? :stuck_out_tongue:

Well, you need to remove the "String" functions and use "string"s instead.

Sorry, it is a programming task - someone else may chip in here.

Oki!

Well so far I have implemented the watchdog feature with a 4 second timer, and its been running for 2 days so far without a hickup.

So if I can bodge it to work, I probably do not need to fix the string in the heap issue :stuck_out_tongue:

Your band-aid may be working...

but the correct path going forward is to LEARN the differences between String vs string

1 Like

Jepp. Just got to find the time to learn, that is the hard part :joy:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.