Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 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;
- int clock1 = 5;
- int clock2 = 3;
- 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);
- pinMode(clock1, OUTPUT);
- pinMode(clock2, OUTPUT);
- digitalWrite(clock2, HIGH);
- // start the Ethernet connection and the server:
- Ethernet.begin(mac, ip, gateway, subnet);
- server.begin();
- Serial.print("Server's ip is ");
- 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("<BODY>");
- client.println("<H1>TEST</H1>");
- client.println("<H2>CONTROLS</H2>");
- client.println("<br />");
- client.println("<a href=/?ledOn>Turn LED On</a>");
- client.println("<a href=/?ledOff>Turn LED Off</a>");
- client.println("<br />");
- client.println("<a href=/?on>Turn Clock On</a>");
- client.println("<a href=/?off>Turn Clock Off</a>");
- client.println("</BODY>");
- client.println("</HTML>");
- delay(1);
- //stopping client
- client.stop();
- //controls the Arduino if you press the buttons
- if (readString.indexOf("?ledOn") >0){
- digitalWrite(led, HIGH);
- }
- if (readString.indexOf("?ledOff") >0){
- digitalWrite(led, LOW);
- }
- if (readString.indexOf("?on") >0){
- if (digitalRead(clock1)==0){
- digitalWrite(clock1, HIGH);
- Serial.println(digitalRead(clock1));
- }
- else {
- Serial.println("The clock is already on");
- }
- }
- if (readString.indexOf("?off") >0){
- if (digitalRead(clock1)==1){
- digitalWrite(clock1, LOW);
- Serial.println(digitalRead(clock1));
- Serial.println("Waiting 17 seconds...");
- delay(7000);
- Serial.println("10 seconds...");
- delay(5000);
- for (int x=5; x>0;x--) {
- Serial.print(x);
- Serial.println(" seconds...");
- delay(1000);
- }
- Serial.println("The clock is off");
- }
- else {
- Serial.println("The clock is already off");
- }
- }
- //clearing string for next read
- readString="";
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment