/* * Rui Santos * Complete Project Details http://randomnerdtutorials.com */ #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, 0xBE, 0xEF, 0xFE, 0xED }; IPAddress ip(192,168,1, 111); // Initialize the Ethernet server library // with the IP address and port you want to use // (port 80 is default for HTTP): EthernetServer server(80); // Relay state and pin String relay1State = "Off"; const int relay = 7; // Client variables char linebuf[80]; int charcount=0; void setup() { // Relay module prepared pinMode(relay, OUTPUT); digitalWrite(relay, HIGH); // 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("server is at "); Serial.println(Ethernet.localIP()); } // Display dashboard page with on/off button for relay // It also print Temperature in C and F void dashboardPage(EthernetClient &client) { client.println(""); client.println(""); client.println("

Arduino Web Server - Refresh

"); // Generates buttons to control the relay client.println("

Relay 1 - 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(""); } client.println(""); } void loop() { // listen for incoming clients EthernetClient client = server.available(); if (client) { Serial.println("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 0){ digitalWrite(relay, HIGH); relay1State = "Off"; } else if (strstr(linebuf,"GET /relay1on") > 0){ digitalWrite(relay, LOW); relay1State = "On"; } // 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("client disonnected"); } }