Guest User

Untitled

a guest
Aug 6th, 2017
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1.  
  2.  
  3. /*
  4. Created by Rui Santos
  5. Visit: http://randomnerdtutorials.com for more arduino projects
  6.  
  7. Arduino with Ethernet Shield
  8. */
  9.  
  10. #include <SPI.h>
  11. #include <Ethernet.h>
  12.  
  13. int led = 4;
  14. int clock1 = 5;
  15. int clock2 = 3;
  16. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
  17. byte ip[] = { 192, 168, 1, 178 }; // ip in lan (that's what you need to use in your browser. ("192.168.1.178")
  18. byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
  19. byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
  20. EthernetServer server(80); //server port
  21. String readString;
  22.  
  23. void setup() {
  24. // Open serial communications and wait for port to open:
  25. Serial.begin(9600);
  26. while (!Serial) {
  27. ; // wait for serial port to connect. Needed for Leonardo only
  28. }
  29. pinMode(led, OUTPUT);
  30. pinMode(clock1, OUTPUT);
  31. pinMode(clock2, OUTPUT);
  32. digitalWrite(clock2, HIGH);
  33.  
  34. // start the Ethernet connection and the server:
  35. Ethernet.begin(mac, ip, gateway, subnet);
  36. server.begin();
  37. Serial.print("Server's ip is ");
  38. Serial.println(Ethernet.localIP());
  39. }
  40.  
  41.  
  42. void loop() {
  43. // Create a client connection
  44.  
  45. EthernetClient client = server.available();
  46. if (client) {
  47. while (client.connected()) {
  48. if (client.available()) {
  49. char c = client.read();
  50. //read char by char HTTP request
  51. if (readString.length() < 100) {
  52. //store characters to string
  53. readString += c;
  54. Serial.print(c);
  55. }
  56.  
  57. //if HTTP request has ended
  58. if (c == '\n') {
  59. Serial.println(readString); //print to serial monitor for debuging
  60. client.println("HTTP/1.1 200 OK"); //send new page
  61. client.println("Content-Type: text/html");
  62. client.println();
  63. client.println("<HTML>");
  64. client.println("<BODY>");
  65. client.println("<H1>TEST</H1>");
  66. client.println("<H2>CONTROLS</H2>");
  67. client.println("<br />");
  68. client.println("<a href=/?ledOn>Turn LED On</a>");
  69. client.println("<a href=/?ledOff>Turn LED Off</a>");
  70. client.println("<br />");
  71. client.println("<a href=/?on>Turn Clock On</a>");
  72. client.println("<a href=/?off>Turn Clock Off</a>");
  73. client.println("</BODY>");
  74. client.println("</HTML>");
  75. delay(1);
  76.  
  77. //stopping client
  78. client.stop();
  79. //controls the Arduino if you press the buttons
  80. if (readString.indexOf("?ledOn") >0){
  81. digitalWrite(led, HIGH);
  82. }
  83. if (readString.indexOf("?ledOff") >0){
  84. digitalWrite(led, LOW);
  85. }
  86. if (readString.indexOf("?on") >0){
  87. if (digitalRead(clock1)==0){
  88. digitalWrite(clock1, HIGH);
  89. Serial.println(digitalRead(clock1));
  90. }
  91. else {
  92. Serial.println("The clock is already on");
  93. }
  94. }
  95.  
  96. if (readString.indexOf("?off") >0){
  97. if (digitalRead(clock1)==1){
  98. digitalWrite(clock1, LOW);
  99. Serial.println(digitalRead(clock1));
  100. Serial.println("Waiting 17 seconds...");
  101. delay(7000);
  102. Serial.println("10 seconds...");
  103. delay(5000);
  104. for (int x=5; x>0;x--) {
  105. Serial.print(x);
  106. Serial.println(" seconds...");
  107. delay(1000);
  108. }
  109. Serial.println("The clock is off");
  110. }
  111. else {
  112. Serial.println("The clock is already off");
  113. }
  114. }
  115. //clearing string for next read
  116. readString="";
  117.  
  118. }
  119. }
  120. }
  121. }
  122. }
Add Comment
Please, Sign In to add comment