/* sketch, webserver without sd for use on mega + ethernet shield, for in my pumphouse, controling electric dogfence, electric fences, irrigation and pumps (4 subjects) making 4 subjects with buttons to show contents of the subject (and an INDEX button to go back), hiding the other subjects. with checkboxes to see some activity contents consist of switching relais, reading switches and analog inputs with different colors for on and off, etc also use of colored boxes and buttons showing turn on when it is turned off etc */ #include #include // Enter a MAC address and IP address for your controller below. // The IP address will be dependent on your local network: byte mac[] = { 0xDE, 0xAD, 0xFE, 0xED, 0xBE, 0xEF }; IPAddress ip(192, 168, 8, 16); //!!!!Make sure the line in void:!!!! //"client.println("");" //!!!!gets the same address!!!! EthernetServer server(80); //Initial state on server/webpage String hideshow1State = "ON"; String hideshow2State = "ON"; String hideshow3State = "ON"; String hideshow4State = "ON"; String relay1State = "

ON

"; String relay2State = "

ON

"; String relay3State = "

OFF

"; String relay4State = "

OFF

"; String relay5State = "

OFF

"; String relay6State = "

CLOSED

"; String relay7State = "

CLOSED

"; String relay8State = "

CLOSED

"; String relay9State = "

CLOSED

"; String relay10State = "

CLOSED

"; String relay11State = "

OFF

"; String relay12State = "

OFF

"; String relay13State = "

OFF

"; String relay14State = "

OFF

"; String switch1State = "

OFF

"; String switch2State = "

OFF

"; String switch3State = "

OFF

"; String switch4State = "

OFF

"; String switch5State = "

OFF

"; String switch6State = "

OFF

"; String switch7State = "

OFF

"; String switch8State = "

OFF

"; String switch9State = "

OFF

"; String switch10State = "

OFF

"; String switch11State = "

OFF

"; String switch12State = "

OFF

"; String switch13State = "

OFF

"; String switch14State = "

OFF

"; //pin layout const int relayA = 23; const int relayB = 25; const int relayC = 27; const int relayD = 29; const int relayE = 31; const int relayF = 33; const int relayG = 35; const int relayH = 37; const int relayI = 39; const int relayJ = 41; const int relayK = 43; const int relayL = 45; const int relayM = 47; const int relayN = 49; const int switchA = 19; const int switchB = 18; const int switchC = 17; const int switchD = 16; const int switchE = 15; const int switchF = 14; const int switchG = 2; const int switchH = 3; const int switchI = 4; const int switchJ = 5; const int switchK = 6; const int switchL = 7; const int switchM = 8; const int switchN = 9; // Client variables char linebuf[80]; int charcount = 0; void setup() { // disable sd card pinMode(4, OUTPUT); digitalWrite(4, HIGH); // Relay module prepared pinMode(relayA, OUTPUT); pinMode(relayB, OUTPUT); pinMode(relayC, OUTPUT); pinMode(relayD, OUTPUT); pinMode(relayE, OUTPUT); pinMode(relayF, OUTPUT); pinMode(relayG, OUTPUT); pinMode(relayH, OUTPUT); pinMode(relayI, OUTPUT); pinMode(relayJ, OUTPUT); pinMode(relayK, OUTPUT); pinMode(relayL, OUTPUT); pinMode(relayM, OUTPUT); pinMode(relayN, OUTPUT); //state of relais at start digitalWrite(relayA, LOW); digitalWrite(relayB, LOW); digitalWrite(relayC, HIGH); digitalWrite(relayD, HIGH); digitalWrite(relayE, HIGH); digitalWrite(relayF, HIGH); digitalWrite(relayG, HIGH); digitalWrite(relayH, HIGH); digitalWrite(relayI, HIGH); digitalWrite(relayJ, HIGH); digitalWrite(relayK, HIGH); digitalWrite(relayL, HIGH); digitalWrite(relayM, HIGH); digitalWrite(relayN, HIGH); // input pin for switch pinMode(switchA, INPUT); pinMode(switchB, INPUT); pinMode(switchC, INPUT); pinMode(switchD, INPUT); pinMode(switchE, INPUT); pinMode(switchF, INPUT); pinMode(switchG, INPUT); pinMode(switchH, INPUT); pinMode(switchI, INPUT); pinMode(switchJ, INPUT); pinMode(switchK, INPUT); pinMode(switchL, INPUT); pinMode(switchM, INPUT); pinMode(switchN, INPUT); // Open serial communication at a baud rate of 9600 Serial.begin(9600); // start the Ethernet connection and the server: Ethernet.begin(mac, ip); server.begin(); Serial.print(F("server is at ")); Serial.println(Ethernet.localIP()); } // Display dashboard page with on/off buttons for showing several "pages" where to control relays and more void dashboardPage(EthernetClient &client) { client.println(F("")); client.println(F("")); //refreshes the page without keeping the commands so its possible to control with several clients at the same time client.println(F("")); //make it fit for mobile client.println(F("")); client.println(); client.println(F("")); client.println(F("Pump House")); client.println(F("")); client.println(F("")); client.println(F("")); client.println(F("
")); client.println("

Pump House

"); client.println("
"); client.println(F("")); //to show all subjects if (hideshow2State == "ON") { if (hideshow3State == "ON") { if (hideshow4State == "ON") { client.println("

Dogfence

"); //showing content of subject //HideShow 1 client.println(""); if (hideshow1State == "OFF") { client.println("
"); client.println("
"); // Generates buttons to control the relay //Relais 1 client.println("

Relais State: " + relay1State + "

"); // If relay is off, it shows the button to turn the output on if (relay1State == "

OFF

") { client.println(""); } // If relay is on, it shows the button to turn the output off else if (relay1State == "

ON

") { client.println(""); } //-Relais 1 client.println("
"); client.println("
"); //read ledstate 1 of dogfence box client.println("

- Power: --

"); if (analogRead(2) > 100 ) { client.println("

FAIL

"); } if (analogRead(2) < 100 ) { client.println("

GOOD

"); } //-read ledstate 1 of dogfence box //read ledstate 2 of dogfence box client.println("

-- Loop: ---

"); if (analogRead(3) < 100 ) { client.println("

BROKE

"); } if (analogRead(3) > 100 ) { client.println("

GOOD

"); } //-read ledstate 2 of dogfence box client.println("
"); client.println("
"); client.println("

"); //button to go to initial "page" client.println(""); client.println("

"); } //hiding content of subject else if (hideshow1State == "ON") { client.print(""); //-HideShow 1 //checkbox to see from initial "page" if stuf's ok if (relay1State == "

OFF

") { client.print(""); } else { client.print(""); } //-checkbox client.println("

"); } // to hide subjects other than current } else if (hideshow4State == "OFF") { } } else if (hideshow3State == "OFF") { } } else if (hideshow2State == "OFF") { } if (hideshow1State == "ON") { if (hideshow3State == "ON") { if (hideshow4State == "ON") { client.println("

Electric fence

"); //HideShow 2 client.println(""); if (hideshow2State == "OFF") { client.println("
"); client.println("
"); //Relais 2 //MAIN Relais to power Electric Fence Device client.println("

MAIN:--------- " + relay2State + "

"); if (relay2State == "

OFF

") { client.println(""); } else if (relay2State == "

ON

") { client.println(""); } //-Relais 2 client.println("

--(also Chicken Run)

"); client.println("

"); client.println("

"); //read ledstate 1 of electric fence box client.println("

- Power: --

"); if (analogRead(3) > 100 ) { client.println("

FAIL

"); } if (analogRead(3) < 100 ) { client.println("

GOOD

"); } //-read ledstate 1 of electric fence box //read ledstate 2 of electric fence box client.println("

-- Pulse: ---

"); if (analogRead(4) < 100 ) { client.println("

FAIL

"); } if (analogRead(4) > 100 ) { client.println("

GOOD

"); } //-read ledstate 2 of electric fence box client.println("
"); client.println("
"); //only show a button when MAIN (relay 2) is on //Relais 3 client.println("

Meadow 1: " + relay3State + "

"); if (relay2State == "

ON

" && relay3State == "

OFF

") { client.println(""); } else if (relay3State == "

ON

") { client.println(""); } //-Relais 3 //Relais 4 client.println("

Meadow 2: " + relay4State + "

"); if (relay2State == "

ON

" && relay4State == "

OFF

") { client.println(""); } else if (relay4State == "

ON

") { client.println(""); } //-Relais 4 //Relais 5 client.println("

Meadow 3: " + relay5State + "

"); if (relay2State == "

ON

" && relay5State == "

OFF

") { client.println(""); } else if (relay5State == "

ON

") { client.println(""); } //-Relais 5 //show text only when MAIN (relay 2) is off else if (relay2State = "

OFF

") { client.println("--------------------------"); client.println("

-buttons only visible

"); client.println("

-- if -MAIN- is - ON

"); } client.println("
"); client.println("
"); client.println("

"); client.println(""); client.println("

"); } else if (hideshow2State == "ON") { client.println(""); //-HideShow 2 //checkbox if (relay2State == "

OFF

") { client.print(""); } else { client.print(""); } //-checkbox client.println("

"); } } else if (hideshow4State == "OFF") { } } else if (hideshow3State == "OFF") { } } else if (hideshow1State == "OFF") { } if (hideshow1State == "ON") { if (hideshow2State == "ON") { if (hideshow4State == "ON") { client.println("

Irrigation

"); //HideShow 3 client.println(""); if (hideshow3State == "OFF") { client.println("
"); client.println("
"); //Relais 6 client.println("

Fruit 1 a : " + relay6State + "

"); if (relay6State == "

CLOSED

") { client.println(""); } else if (relay6State == "OPEN") { client.println(""); } //-Relais 6 //Relais 7 client.println("

Fruit 1 b : " + relay7State + "

"); if (relay7State == "

CLOSED

") { client.println(""); } else if (relay7State == "OPEN") { client.println(""); } //-Relais 7 //Relais 8 client.println("

Veggie 1: " + relay8State + "

"); if (relay8State == "

CLOSED

") { client.println(""); } else if (relay8State == "OPEN") { client.println(""); } //-Relais 8 //Relais 9 client.println("

Veggie 2: " + relay9State + "

"); if (relay9State == "

CLOSED

") { client.println(""); } else if (relay9State == "OPEN") { client.println(""); } //-Relais 9 //Relais 10 client.println("

Tap State: " + relay10State + "

"); if (relay10State == "

CLOSED

") { client.println(""); } else if (relay10State == "OPEN") { client.println(""); } //-Relais 10 client.println("
"); client.println("
"); //switch4 client.println("

Switch 4 state: " + switch4State + "

"); //-switch4 //switch5 client.println("

Switch 5 state: " + switch5State + "

"); //-switch5 //switch6 client.println("

Switch 6 state: " + switch6State + "

"); //-switch6 //switch7 client.println("

Switch 7 state: " + switch7State + "

"); //-switch7 //switch8 client.println("

Switch 8 state: " + switch8State + "

"); //-switch8 //switch9 client.println("

Switch 9 state: " + switch9State + "

"); //-switch9 int analog_val; //analog 3 analog_val = analogRead(4); client.print("

Analog A3:

"); client.print("
"); client.print(analog_val); client.print("
"); //-analog 3 //analog 4 analog_val = analogRead(5); client.print("

Analog A4:

"); client.print("
"); client.print(analog_val); client.print("
"); //-analog 4 //analog 5 analog_val = analogRead(6); client.print("

Analog A5:

"); client.print("
"); client.print(analog_val); client.print("
"); //-analog 5 //analog 6 analog_val = analogRead(7); client.print("

Analog A6:

"); client.print("
"); client.print(analog_val); client.print("
"); //-analog 6 //analog 7 analog_val = analogRead(8); client.print("

Analog A7:

"); client.print("
"); client.print(analog_val); client.print("
"); //-analog 7 client.println("
"); client.println("
"); client.println("

"); client.println(""); client.println("

"); } else if (hideshow3State == "ON") { client.println(""); //checkbox to see from initial "page" if stuf's ok if (relay6State == "

CLOSED

" && relay7State == "

CLOSED

" && relay8State == "

CLOSED

" && relay9State == "

CLOSED

" && relay10State == "

CLOSED

") { client.print(""); } else { client.print(""); } //-checkbox client.println("

"); } //-HideShow 3 } else if (hideshow4State == "OFF") { } } else if (hideshow2State == "OFF") { } } else if (hideshow1State == "OFF") { } if (hideshow1State == "ON") { if (hideshow2State == "ON") { if (hideshow3State == "ON") { client.println("

Pumps

"); //HideShow 4 client.println(""); if (hideshow4State == "OFF") { client.println("
"); client.println("
"); //Relais 11 client.println("

Relais State: " + relay11State + "

"); // If relay is off, it shows the button to turn the output on if (relay11State == "

OFF

") { client.println(""); } // If relay is on, it shows the button to turn the output off else if (relay11State == "

ON

") { client.println(""); } //-Relais 11 //Relais 12 client.println("

Relais State: " + relay12State + "

"); // If relay is off, it shows the button to turn the output on if (relay12State == "

OFF

") { client.println(""); } // If relay is on, it shows the button to turn the output off else if (relay12State == "

ON

") { client.println(""); } //-Relais 12 //Relais 13 client.println("

Relais State: " + relay13State + "

"); // If relay is off, it shows the button to turn the output on if (relay13State == "

OFF

") { client.println(""); } // If relay is on, it shows the button to turn the output off else if (relay13State == "

ON

") { client.println(""); } //-Relais 13 client.println("
"); client.println("
"); //read ledstate 4 client.println("

- Power: --

"); if (analogRead(4) > 100 ) { client.println("

FAIL

"); } if (analogRead(4) < 100 ) { client.println("

GOOD

"); } //-read ledstate 4 //read ledstate 5 client.println("

-- Loop: ---

"); if (analogRead(5) < 100 ) { client.println("

BROKE

"); } if (analogRead(5) > 100 ) { client.println("

GOOD

"); } //-read ledstate 5 //switch10 client.println("

Switch 10 state: " + switch10State + "

"); //-switch10 //switch11 client.println("

Switch 11 state: " + switch11State + "

"); //-switch11 //switch12 client.println("

Switch 12 state: " + switch12State + "

"); //-switch12 //switch13 client.println("

Switch 13 state: " + switch13State + "

"); //-switch13 //switch14 client.println("

Switch 14 state: " + switch14State + "

"); //-switch14 client.println("
"); client.println("
"); client.println("

"); //button to go to initial "page" client.println(""); client.println("

"); } //hiding content of subject else if (hideshow4State == "ON") { client.println(""); //checkbox to see from initial "page" if stuf's ok if (relay11State == "

OFF

" && relay12State == "

OFF

" && relay13State == "

OFF

" && switch11State == "

OFF

") { client.print(""); } else { client.print(""); } //-checkbox client.println("

"); } //-HideShow 4 } else if (hideshow3State == "OFF") { } } else if (hideshow2State == "OFF") { } } else if (hideshow1State == "OFF") { } client.println("
compiled and written by XXXXXXX at gmail com 5/6-2019
"); client.println(F("")); } void loop() { // listen for incoming clients EthernetClient client = server.available(); if (client) { Serial.println(F("new client")); memset(linebuf, 0, sizeof(linebuf)); charcount = 0; // an http request ends with a blank line boolean currentLineIsBlank = true; while (client.connected()) { if (client.available()) { char c = client.read(); //read char by char HTTP request linebuf[charcount] = c; if (charcount < sizeof(linebuf) - 1) charcount++; // if you've gotten to the end of the line (received a newline // character) and the line is blank, the http request has ended, // so you can send a reply if (c == '\n' && currentLineIsBlank) { dashboardPage(client); break; } if (c == '\n') { //Relais 1 if (strstr(linebuf, "GET /relay1off") > 0) { digitalWrite(relayA, HIGH); relay1State = "

OFF

"; } else if (strstr(linebuf, "GET /relay1on") > 0) { digitalWrite(relayA, LOW); relay1State = "

ON

"; } //-Relais 1 //if relay 2 goes off, so does 3,4and5 //Relais 2 if (strstr(linebuf, "GET /relay2off") > 0) { digitalWrite(relayB, HIGH); digitalWrite(relayC, HIGH); digitalWrite(relayD, HIGH); digitalWrite(relayE, HIGH); relay2State = "

OFF

"; relay3State = "

OFF

"; relay4State = "

OFF

"; relay5State = "

OFF

"; } else if (strstr(linebuf, "GET /relay2on") > 0) { digitalWrite(relayB, LOW); relay2State = "

ON

"; } //-Relais 2 //Relais 3 if (strstr(linebuf, "GET /relay3off") > 0) { digitalWrite(relayC, HIGH); relay3State = "

OFF

"; } else if (strstr(linebuf, "GET /relay3on") > 0) { digitalWrite(relayC, LOW); relay3State = "

ON

"; } //-Relais 3 //Relais 4 if (strstr(linebuf, "GET /relay4off") > 0) { digitalWrite(relayD, HIGH); relay4State = "

OFF

"; } else if (strstr(linebuf, "GET /relay4on") > 0) { digitalWrite(relayD, LOW); relay4State = "

ON

"; } //-Relais 4 //Relais 5 if (strstr(linebuf, "GET /relay5off") > 0) { digitalWrite(relayE, HIGH); relay5State = "

OFF

"; } else if (strstr(linebuf, "GET /relay5on") > 0) { digitalWrite(relayE, LOW); relay5State = "

ON

"; } //-Relais 5 //Relais 6 if (strstr(linebuf, "GET /relay6off") > 0) { digitalWrite(relayF, HIGH); relay6State = "

CLOSED

"; } else if (strstr(linebuf, "GET /relay6on") > 0) { digitalWrite(relayF, LOW); relay6State = "OPEN"; } //-Relais 6 //Relais 7 if (strstr(linebuf, "GET /relay7off") > 0) { digitalWrite(relayG, HIGH); relay7State = "

CLOSED

"; } else if (strstr(linebuf, "GET /relay7on") > 0) { digitalWrite(relayG, LOW); relay7State = "OPEN"; } //-Relais 7 //Relais 8 if (strstr(linebuf, "GET /relay8off") > 0) { digitalWrite(relayH, HIGH); relay8State = "

CLOSED

"; } else if (strstr(linebuf, "GET /relay8on") > 0) { digitalWrite(relayH, LOW); relay8State = "OPEN"; } //-Relais 8 //Relais 9 if (strstr(linebuf, "GET /relay9off") > 0) { digitalWrite(relayI, HIGH); relay9State = "

CLOSED

"; } else if (strstr(linebuf, "GET /relay9on") > 0) { digitalWrite(relayI, LOW); relay9State = "OPEN"; } //-Relais 9 //Relais 10 if (strstr(linebuf, "GET /relay10off") > 0) { digitalWrite(relayJ, HIGH); relay10State = "

CLOSED

"; } else if (strstr(linebuf, "GET /relay10on") > 0) { digitalWrite(relayJ, LOW); relay10State = "OPEN"; } //-Relais 10 ///Relais 11 if (strstr(linebuf, "GET /relay11off") > 0) { digitalWrite(relayK, HIGH); relay11State = "

OFF

"; } else if (strstr(linebuf, "GET /relay11on") > 0) { digitalWrite(relayK, LOW); relay11State = "

ON

"; } //-Relais 11 //Relais 12 if (strstr(linebuf, "GET /relay12off") > 0) { digitalWrite(relayL, HIGH); relay12State = "

OFF

"; } else if (strstr(linebuf, "GET /relay12on") > 0) { digitalWrite(relayL, LOW); relay12State = "

ON

"; } //-Relais 12 //Relais 13 if (strstr(linebuf, "GET /relay13off") > 0) { digitalWrite(relayM, HIGH); relay13State = "

OFF

"; } else if (strstr(linebuf, "GET /relay13on") > 0) { digitalWrite(relayM, LOW); relay13State = "

ON

"; } //-Relais 13 //switch4 if (digitalRead(switchD) == LOW) { switch4State = "

OFF

"; } else { switch4State = "

ON

"; } //-switch4 //switch5 if (digitalRead(switchE) == LOW) { switch5State = "

OFF

"; } else { switch5State = "

ON

"; } //-switch5 //switch6 if (digitalRead(switchF) == LOW) { switch6State = "

OFF

"; } else { switch6State = "

ON

"; } //-switch6 //switch7 if (digitalRead(switchG) == LOW) { switch7State = "

OFF

"; } else { switch7State = "

ON

"; } //-switch7 //switch8 if (digitalRead(switchH) == LOW) { switch8State = "

OFF

"; } else { switch8State = "

ON

"; } //-switch8 //switch9 if (digitalRead(switchI) == LOW) { switch9State = "

OFF

"; } else { switch9State = "

ON

"; } //-switch9 //switch10 if (digitalRead(switchJ) == LOW) { switch10State = "

OFF

"; } else { switch10State = "

ON

"; } //-switch10 //switch11 if (digitalRead(switchK) == LOW) { switch11State = "

OFF

"; } else { switch11State = "

ON

"; } //-switch11 //switch12 if (digitalRead(switchL) == LOW) { switch12State = "

OFF

"; } else { switch12State = "

ON

"; } //-switch12 //switch13 if (digitalRead(switchM) == LOW) { switch13State = "

OFF

"; } else { switch13State = "

ON

"; } //-switch13 //switch14 if (digitalRead(switchN) == LOW) { switch14State = "

OFF

"; } else { switch14State = "

ON

"; } //-switch14 //HideShow 1 if (strstr(linebuf, "GET /hideshow1off") > 0) { hideshow1State = "OFF"; } else if (strstr(linebuf, "GET /hideshow1on") > 0) { hideshow1State = "ON"; } //-HideShow 1 //HideShow 2 if (strstr(linebuf, "GET /hideshow2off") > 0) { hideshow2State = "OFF"; } else if (strstr(linebuf, "GET /hideshow2on") > 0) { hideshow2State = "ON"; } //-HideShow 2 //HideShow 3 if (strstr(linebuf, "GET /hideshow3off") > 0) { hideshow3State = "OFF"; } else if (strstr(linebuf, "GET /hideshow3on") > 0) { hideshow3State = "ON"; } //-HideShow 3 //HideShow 4 if (strstr(linebuf, "GET /hideshow4off") > 0) { hideshow4State = "OFF"; } else if (strstr(linebuf, "GET /hideshow4on") > 0) { hideshow4State = "ON"; } //-HideShow 4 // you're starting a new line currentLineIsBlank = true; memset(linebuf, 0, sizeof(linebuf)); charcount = 0; } else if (c != '\r') { // you've gotten a character on the current line currentLineIsBlank = false; } } } // give the web browser time to receive the data delay(1); // close the connection: client.stop(); Serial.println(F("client disonnected")); } }