Sketch/board issues after power outage

Hi,
I don't know programming.
I have a working webserver sketch (found on internet) for an arduino uno + ethernet shield, and a 9V DC adapter. The problem is that when the mains power is down (power outage) and then back on, the webserver is not working anymore. I have to unplug the adapter from the mains then plug it back, for a few times, for the webserver to start running again and be able to access the webpage it serves.

Is it possible to run some self check code and if webserver is not running then reset the board until it works? What would be the code for that?

This is the sketch I'm using:

#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xA2, 0xDA, 0x00, 0x2B, 0x7D };    // MAC address

IPAddress ip(10,77,80,15);                               // IP address
EthernetServer server(80);

char char_in = 0;    // used to store a character from the Ethernet port
String HTTP_req;     // stores the received HTTP request

const byte LED_pins[] = {2, 3};          // Arduino pins used as ouputs for LEDs
byte LED[sizeof(LED_pins)] = {0};        // the state of each LED

void setup()
{
    Ethernet.begin(mac, ip);
    server.begin();
    
    // set up the LED pins as outputs
    for (byte index = 0; index < sizeof(LED_pins); index++) {
        pinMode(LED_pins[index], OUTPUT);
    }
}

void loop()
{
    EthernetClient client = server.available();

    if (client) {
        while (client.connected()) {
            if (client.available()) {
                char_in = client.read();  // get a single character from the Ethernet port
                HTTP_req += char_in;      // build a string out of the received characters

                // answer first HTTP request immediately
                if (char_in == '\n') {
                    // respond to HTTP request
                    client.println("HTTP/1.1 200 OK");
                    client.println("Content-Type: text/html");
                    client.println();
                    CheckLEDs();              // get state of LED checkboxes from web page
                    DisplayLEDs(&client);     // display checkboxes on web & write to LEDs
                    HTTP_req = "";      // delete string contents
                    client.stop();
                } // end if (char_in == '\n')
            } // end if (client.available()
        } // end while (client.connected())
    } // end if (client)
}

// get the state of the LED checkboxes from the HTTP request
void CheckLEDs()
{
    for (byte led_num = 0; led_num < sizeof(LED_pins); led_num++) {
        if ((HTTP_req.charAt(9) == (LED_pins[led_num] + '0')) &&
                    (HTTP_req.charAt(16) == (LED_pins[led_num] + '0'))) {  // LED box is checked
            LED[led_num] = 1;
        }
        else if (HTTP_req.charAt(9) == (LED_pins[led_num] + '0')) {        // LED box is unchecked
            LED[led_num] = 0;
        }
    }
}

// write the HTML that includes the state of the LED checkboxes for displaying on the web browser
void DisplayLEDs(Client *client)
{
    // some CSS in the HTML to change colours and position of the box containing the LED checkboxes
    client->print("<div style=\"background:white; color:green; position: absolute; margin:20px; border: grey solid 2px; padding:0 10px 4px 10px;\">");
    client->print("<h1 style=\"font: normal 20px/150% Verdana, Arial, Sans-serif;\">TEST</h1>");
    // send each LED checkbox in turn
    for (byte led_num = 0; led_num < sizeof(LED_pins); led_num++) {
        // use hidden checkbox so that unchecking a checkbox sends a value to the Arduino
        // if only a normal checkbox is used, nothing is sent when it is unchecked
        // both hidden and normal checkboxes are produced here for each LED
        client->print("<form> <input type=\"hidden\" name=\"LED");
        client->print(LED_pins[led_num], DEC);
        client->print("\" value=\"0\"> <input type=\"checkbox\" name=\"LED");
        client->print(LED_pins[led_num], DEC);
        client->print("\" value=\"");
        client->print(LED_pins[led_num], DEC);
        client->print("\"");
        // write to the LED connected to the Arduino board
        if (LED[led_num]) {
            client->print(" checked ");             // part of HTML if checkbox is to be displayed checked
            digitalWrite(LED_pins[led_num], HIGH);  // switch LED on
        }
        else {
            digitalWrite(LED_pins[led_num], LOW);  // switch LED off
        }
        client->print(" onclick=\"submit();\">LED");
        client->print(LED_pins[led_num], DEC);
        client->print(" </form>");                 // end of HTML for 1 LED's form
    }
    client->print("</div>");                       // end of box containing LEDs
}

then reset the board

Reset which board? The Arduino? No, it can't reset itself. The Ethernet board? That should be reset when the Arduino is, since it is powered by the Arduino.

You are wasting huge amounts of SRAM. I suspect that THAT is the real problem.

client.println(F("HTTP/1.1 200 OK"));

Ok, thanks, I've edited the sketch and added the F() on the line you've specified, then uploaded it to my board. I'll test it by unplugging the power adaptor for a few seconds and see if that problem is still reproducing. Thank you, again :slight_smile:

Delta_G:
Don't just add the F macro on that line. Add it on every line with constant strings in print statements. Better yet, google and read (gasp) about the F macro and what it does before you blindly add it to your code.

"Global variables use 255 bytes (12%) of dynamic memory"... instead of 29% when I wasn't using the F macro.

There was a power outage, again (there's 1 per week, at least) and my arduino is now not reachable from the internet. Those 3 green leds, next to the red power led (on ethernet shield), are down. So same problem.

Is there a way to delay the running of the code on my arduino?
I suspect that this is happening because at the time the arduino (and the code for the websever) boots, the router in not yet up.

Is there a way to delay the running of the code on my arduino?
I suspect that this is happening because at the time the arduino (and the code for the websever) boots, the router in not yet up.

void setup()
{
   delay(120000); // Wait two minutes for the router...