Ethernet Relay Switch with timed based auto-off.

I’m having real trouble creating and integrating this project.

I’ve started with this code which is a ethernet web based switch for a relay on pin 4 (int led=4)

/*
 Created by Rui Santos
 Visit: http://randomnerdtutorials.com for more arduino projects

 Arduino with Ethernet Shield
 */

#include <SPI.h>
#include <Ethernet.h>
int led = 4;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };   //physical mac address
byte ip[] = { 192, 168, 1, 178 };                      // ip in lan (that's what you need to use in your browser. ("192.168.1.178")
byte gateway[] = { 192, 168, 1, 1 };                   // internet access via router
byte subnet[] = { 255, 255, 255, 0 };                  //subnet mask
EthernetServer server(80);                             //server port     
String readString;

void setup() {
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  pinMode(led, OUTPUT);
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}


void loop() {
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {   
      if (client.available()) {
        char c = client.read();
     
        //read char by char HTTP request
        if (readString.length() < 100) {
          //store characters to string
          readString += c;
          //Serial.print(c);
         }

         //if HTTP request has ended
         if (c == '\n') {          
           Serial.println(readString); //print to serial monitor for debuging
     
           client.println("HTTP/1.1 200 OK"); //send new page
           client.println("Content-Type: text/html");
           client.println();     
           client.println("<HTML>");
           client.println("<HEAD>");
           client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
           client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
           client.println("<link rel='stylesheet' type='text/css' href='http://randomnerdtutorials.com/ethernetcss.css' />");
           client.println("<TITLE>Random Nerd Tutorials Project</TITLE>");
           client.println("</HEAD>");
           client.println("<BODY>");
           client.println("<H1>Instructions in sketch near bottom</H1>");
           client.println("<hr />");
           client.println("
");  
           client.println("<H2>Arduino with Ethernet Shield</H2>");
           client.println("
");  
           client.println("<a href=\"/?button1on\"\">Turn On</a>");
           client.println("<a href=\"/?button1off\"\">Turn Off</a>
");   
           client.println("<p>Created by Rui Santos. Visit http://randomnerdtutorials.com for more projects!</p>");  
           client.println("
"); 
           client.println("</BODY>");
           client.println("</HTML>");

           delay(1);
           //stopping client
           client.stop();
           //controls the Arduino if you press the buttons

           //I want this button1on button to turn on led T=0 (or C=10), then turn off after T=10 seconds (or C=0)
           //(I will change to 1 hour later) and 
           //if pressed again, will 'reset' but max time is T=10.  Example if press at t=0 and t=1 then turn of at t=11 NOT T=20
           if (readString.indexOf("?button1on") >0){
               digitalWrite(led, HIGH);
           }
           //I want this button1off button to turn off led in all conditions.
           if (readString.indexOf("?button1off") >0){
               digitalWrite(led, LOW);
           }
            //clearing string for next read
            readString="";  
           
         }
       }
    }
}
}

It works great.

So I found a Timer based project here where the function is where you press the reset button which starts a 60 second countdown timer and switches a relay on pin 4 (depending on the state you wish) and fires a buzzer on pin 13 at the end, all the while using an LCD screen to display the output.

I made a couple of changes and merged the two sets of code to hopefully make something work.

What I did was turn off the inital state of the timer and changed the actions of the web buttons so that instead of chaniging the state of the pin/relay to high/low, it changes the state of the int timer state from 0 to 1 which I was hoping would turn the relay on (high) and start a timer that would count down and turn off the relay.

//Arduino Self-Timer
//T.K.Hareendran
//www.electroschematics.com
#include <LiquidCrystal.h>
#include <SPI.h>
#include <Ethernet.h>
LiquidCrystal lcd(7,8,9,10,11,12);
int runTimer=0; 
int runFor = 20; // time in seconds
int buzzerPin = 13;
int relayPin=4;
int data = 0;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };   //physical mac address
byte ip[] = { 192, 168, 1, 178 };                      // ip in lan (that's what you need to use in your browser. ("192.168.1.178")
byte gateway[] = { 192, 168, 1, 1 };                   // internet access via router
byte subnet[] = { 255, 255, 255, 0 };                  //subnet mask
EthernetServer server(80);                             //server port     
String readString;
 
void setup() {
   pinMode(buzzerPin, OUTPUT);
   pinMode(relayPin,OUTPUT);
   lcd.begin(16, 2);
    // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  pinMode(relayPin, OUTPUT);
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}
 
void loop() {
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {   
      if (client.available()) {
        char c = client.read();
     
        //read char by char HTTP request
        if (readString.length() < 100) {
          //store characters to string
          readString += c;
          //Serial.print(c);
         }

         //if HTTP request has ended
         if (c == '\n') {          
           Serial.println(readString); //print to serial monitor for debuging
     
           client.println("HTTP/1.1 200 OK"); //send new page
           client.println("Content-Type: text/html");
           client.println();     
           client.println("<HTML>");
           client.println("<HEAD>");
           client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
           client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
           client.println("<link rel='stylesheet' type='text/css' href='http://randomnerdtutorials.com/ethernetcss.css' />");
           client.println("<TITLE>Random Nerd Tutorials Project</TITLE>");
           client.println("</HEAD>");
           client.println("<BODY>");
           client.println("<H1>Instructions in sketch near bottom</H1>");
           client.println("<hr />");
           client.println("
");  
           client.println("<H2>Arduino with Ethernet Shield</H2>");
           client.println("
");  
           client.println("<a href=\"/?button1on\"\">Turn On</a>");
           client.println("<a href=\"/?button1off\"\">Turn Off</a>
");   
           client.println("<p>Created by Rui Santos. Visit http://randomnerdtutorials.com for more projects!</p>");  
           client.println("
"); 
           client.println("</BODY>");
           client.println("</HTML>");

           delay(1);
           //stopping client
           client.stop();
           //controls the Arduino if you press the buttons

           //Hello Fiverr  I want this button1on button to turn on led T=0 (or C=10), then turn off after T=10 seconds (or C=0)
           //(I will change to 1 hour later) and 
           //if pressed again, will 'reset' but max time is T=10.  Example if press at t=0 and t=1 then turn of at t=11 NOT T=20
           if (readString.indexOf("?button1on") >0){
               digitalWrite(runTimer, 1);
           }
           //Hello Fiverr I want this button1off button to turn off led in all conditions.
           if (readString.indexOf("?button1off") >0){
               digitalWrite(runTimer, 0);
               
           }
            //clearing string for next read
            readString="";  
           
         }
       }
    }
}
   if(runTimer == 1){ 
      digitalWrite(relayPin,HIGH); // relay is ON during countdown
      /* *change to HIGH if you want the relay to be ON while countdowning */
      lcd.clear();
      lcd.print("TIMER=");
      //Start timer
      timer(); 
   } else {
      digitalWrite(relayPin,LOW); // relay is OFF when time expired
      /* *change to LOW if you want the relay to be OFF when the time expired */
   }
   runTimer = 0;
   lcd.noDisplay();
   delay(250);
   for(int duration = 0; duration < 100; duration ++){
      digitalWrite(buzzerPin, HIGH);
      delayMicroseconds(500);
      digitalWrite(buzzerPin, LOW);
      delayMicroseconds(500);
   } 
   lcd.display();
   delay(250);
}
 
void timer() {
   for(int timer = runFor;timer > 0; --timer){
   if(timer >= 10) {
      lcd.setCursor(6,0); 
   } else {   
      lcd.setCursor(6,0);
      lcd.print("0");
      lcd.setCursor(7,0);
   }
   lcd.print(timer);
   lcd.print(" SECOND!");
   delay(1000);
   }
   lcd.setCursor(0,0);
   lcd.clear();
   lcd.print(" TIMER ALERT!");  
}

I left all the instructions for the LCD output in place since I didn’t think it’s output would interfere with the functionality.

But it doesn’t work.

I can still web interface, but the buttons don’t seem to trigger anything. The hardware works fine with the first original code, but I can’t get it working.

Here’s a third try, this time using elapsed mills. I’m using the web buttons to set the elapsedmills timer () function to either over the interval time frame (which turns it off) or at zero, which if set correctly, will run for 10 seconds (on) then over 10 seconds turns it off.

Of which. Doesn’t work. This time, there’s something within the page that seems to break elements in the displayed web page. The buttons do toggle, but nothing seems to execute.

Any help would be appreciated

/*
 Created by Rui Santos
 Visit: http://randomnerdtutorials.com for more arduino projects

 Arduino with Ethernet Shield
 */

#include <SPI.h>
#include <Ethernet.h>
#include <elapsedMillis.h>
int led = 4;
elapsedMillis timer0;
#define interval 10000
// the interval in mS 
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };   //physical mac address
byte ip[] = { 192, 168, 1, 178 };                      // ip in lan (that's what you need to use in your browser. ("192.168.1.178")
byte gateway[] = { 192, 168, 1, 1 };                   // internet access via router
byte subnet[] = { 255, 255, 255, 0 };                  //subnet mask
EthernetServer server(80);                             //server port     
String readString;

void setup() {
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  pinMode(led, OUTPUT);
  timer0 = 110000; // have the timer at a high level outside of 'on' timeframe 0-10000ms
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}


void loop() {
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {   
      if (client.available()) {
        char c = client.read();
     
        //read char by char HTTP request
        if (readString.length() < 100) {
          //store characters to string
          readString += c;
          //Serial.print(c);
         }

         //if HTTP request has ended
         if (c == '\n') {          
           Serial.println(readString); //print to serial monitor for debuging
     
           client.println("HTTP/1.1 200 OK"); //send new page
           client.println("Content-Type: text/html");
           client.println();     
           client.println("<HTML>");
           client.println("<HEAD>");
           client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
           client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
           client.println("<link rel='stylesheet' type='text/css' href='http://randomnerdtutorials.com/ethernetcss.css' />");
           client.println("<TITLE>Random Nerd Tutorials Project</TITLE>");
           client.println("</HEAD>");
           client.println("<BODY>");
           client.println("<H1>Instructions in sketch near bottom</H1>");
           client.println("<hr />");
           client.println("
");  
           client.println("<H2>Arduino with Ethernet Shield</H2>");
           client.println("
");  
           client.println("<a href=\"/?button1on\"\">Turn On</a>");
           client.println("<a href=\"/?button1off\"\">Turn Off</a>
");   
           client.println("<p>Created by Rui Santos. Visit http://randomnerdtutorials.com for more projects!</p>");  
           client.println("
"); 
           client.println("</BODY>");
           client.println("</HTML>");

           delay(1);
           //stopping client
           client.stop();
           //controls the Arduino if you press the buttons

           //This button supposedly will change the timer value to 0MS which when between 0-10000ms will turn the switch on
           if (readString.indexOf("?button1on") >0){
               digitalWrite(timer0, 1);
           }
           //This button sets the timer0 value to greater than 10000ms which will turn the switch off
           if (readString.indexOf("?button1off") >0){
               digitalWrite(timer0, 12000);
           }
            //This sets the conditions for the Led.  If it's greater than 10 seconds, it will be OFF, if under, it will be on.
            readString="";  
            if (timer0 > interval) {
               digitalWrite(led, LOW);
           }
           else  {
               digitalWrite(led, HIGH);// action B
            }
           
         }
       }
    }
}
}

My Latest Failure.

Seems to turn on the relay, but not auto off, or off at all.

Using time based zones of 0 to 10 seconds and using the buttonon/off to set the timeelapsed=0 or greater than 10.

Seems like it’s setting the hands on the clock, but no power to the clock.

Or maybe errors with the location of where I put things.

/*
 Created by Rui Santos
 Visit: http://randomnerdtutorials.com for more arduino projects

 Arduino with Ethernet Shield
 */

#include <elapsedMillis.h>
#include <SPI.h>
#include <Ethernet.h>
elapsedMillis timeElapsed; //declare global if you don't want it reset every time loop 
int led = 4;
// delay in milliseconds between blinks of the LED
unsigned int interval = 5000;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };   //physical mac address
byte ip[] = { 192, 168, 1, 178 };                      // ip in lan (that's what you need to use in your browser. ("192.168.1.178")
byte gateway[] = { 192, 168, 1, 1 };                   // internet access via router
byte subnet[] = { 255, 255, 255, 0 };                  //subnet mask
EthernetServer server(80);                             //server port     
String readString;

void setup() {

  pinMode(led, OUTPUT);
  timeElapsed = 11000;       // Sets counter above on state time frame.

  
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}


void loop() {
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {   
      if (client.available()) {
        char c = client.read();
     
        //read char by char HTTP request
        if (readString.length() < 100) {
          //store characters to string
          readString += c;
          //Serial.print(c);
         }

         //if HTTP request has ended
         if (c == '\n') {          
           Serial.println(readString); //print to serial monitor for debuging
     
           client.println("HTTP/1.1 200 OK"); //send new page
           client.println("Content-Type: text/html");
           client.println();     
           client.println("<HTML>");
           client.println("<HEAD>");
           client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
           client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
           client.println("<link rel='stylesheet' type='text/css' href='http://randomnerdtutorials.com/ethernetcss.css' />");
           client.println("<TITLE>Random Nerd Tutorials Project</TITLE>");
           client.println("</HEAD>");
           client.println("<BODY>");
           client.println("<H1>Random Nerd Tutorials Project</H1>");
           client.println("<hr />");
           client.println("
");  
           client.println("<H2>Arduino with Ethernet Shield</H2>");
           client.println("
");  
           client.println("<a href=\"/?button1on\"\">Turn On</a>");
           client.println("<a href=\"/?button1off\"\">Turn Off</a>
");   
           client.println("<p>Created by Rui Santos. Visit http://randomnerdtutorials.com for more projects!</p>");  
           client.println("
"); 
           client.println("</BODY>");
           client.println("</HTML>");

           delay(1);
           //stopping client
           client.stop();
           //controls the Arduino if you press the buttons

           //I want this button1on button to turn on led T=0 (or C=10), then turn off after T=10 seconds (or C=0)
           //(I will change to 1 hour later) and 
           //if pressed again, will 'reset' but max time is T=10.  Example if press at t=0 and t=1 then turn of at t=11 NOT T=20
           if (readString.indexOf("?button1on") >0){
               timeElapsed = 0;
           }
           //I want this button1off button to turn off led in all conditions.
           if (readString.indexOf("?button1off") >0){
               timeElapsed = 11000;
           }
            //clearing string for next read
            readString="";  
           if (timeElapsed > interval) {                    
               digitalWrite(led, LOW);
           }  else  {
               digitalWrite(led, HIGH);
  // action B
}
         }
       }
    }
}
}