Advertisement
Guest User

Untitled

a guest
Apr 7th, 2011
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.11 KB | None | 0 0
  1. #include <SPI.h>
  2.  
  3. #include <WString.h>
  4. #include <Ethernet.h>
  5. /*
  6. Simple Ethernet Test
  7.  
  8. Arduino server outputs simple text to browser
  9.  
  10. The circuit:
  11. * Arduino Duemilanove
  12. * Arduino Ethernet shield
  13. * Basic FTDI breakout 5V
  14. *LED connected to GND and digital pin 4 via resistor
  15.  
  16. */
  17.  
  18. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
  19. byte ip[] = { 192, 168, 1,200 }; // ip in lan
  20. byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
  21. byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
  22. Server server(80); //server port
  23. byte sampledata=50; //some sample data - outputs 2 (ascii = 50 DEC)
  24. int ledPin = 4; // LED pin
  25. int ledPin2=5;
  26. int ledPin3=6;
  27. char link[]="http://www.scienceprog.com/"; //link data
  28. String readString = String(30); //string for fetching data from address
  29. boolean LEDON = false; //LED status flag
  30. boolean LED2ON=false;
  31. boolean LED3ON=false;
  32. void setup(){
  33. //start Ethernet
  34. Ethernet.begin(mac, ip, gateway, subnet);
  35. //Set pin 4 to output
  36. pinMode(ledPin, OUTPUT);
  37. pinMode(ledPin2, OUTPUT); //Set pin 5 to output
  38. pinMode(ledPin3, OUTPUT); //Set pin 6 to output
  39.  
  40. //enable serial datada print
  41. Serial.begin(9600);
  42. }
  43. void loop(){
  44. // Create a client connection
  45. Client 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() < 30)
  52. {
  53. //store characters to string
  54. readString +=(c);
  55. }
  56. //output chars to serial port
  57. Serial.print(c);
  58. //if HTTP request has ended
  59. if (c == '\n') {
  60. //lets check if LED should be lighted
  61. if(readString.indexOf("L=1")>=0)
  62. {
  63. //led has to be turned ON
  64. digitalWrite(ledPin, HIGH); // set the LED on
  65. LEDON = true;
  66. }else{
  67. //led has to be turned OFF
  68. digitalWrite(ledPin, LOW); // set the LED OFF
  69. LEDON = false;
  70. }
  71.  
  72. if(readString.indexOf("M=1")>=0)
  73. {
  74. //led has to be turned ON
  75. digitalWrite(ledPin2, HIGH); // set the LED on
  76. LED2ON = true;
  77. }else{
  78. //led has to be turned OFF
  79. digitalWrite(ledPin2, LOW); // set the LED OFF
  80. LED2ON = false;
  81. }
  82.  
  83.  
  84. if(readString.indexOf("N=1")>=0)
  85. {
  86. //led has to be turned ON
  87. digitalWrite(ledPin3, HIGH); // set the LED on
  88. LED3ON = true;
  89. }else{
  90. //led has to be turned OFF
  91. digitalWrite(ledPin3, LOW); // set the LED OFF
  92. LED3ON = false;
  93. }
  94.  
  95. // now output HTML data starting with standart header
  96. client.println("HTTP/1.1 200 OK");
  97. client.println("Content-Type: text/html");
  98. client.println();
  99. //set background to yellow
  100. client.print("<body style=background-color:Whi>");
  101. //send first heading
  102. client.println("<font color='red'><h1>Group 10 Server</font></h1>");
  103. client.println("<hr />");
  104. client.println("<hr />");
  105.  
  106.  
  107. //controlling led via checkbox
  108. client.println("<h1>LED control</h1>");
  109. //address will look like http://192.168.1.110/?L=1 when submited
  110. client.println("<form method=get name=LED><input type=checkbox name=L value=1>LED<br><input type=submit value=submit></form>");
  111. client.println("<br />");
  112. //printing LED status
  113. client.print("<font size='5'>LED status: ");
  114. if (LEDON)
  115. client.println("<font color='green' size='5'>ON");
  116. else
  117. client.println("<font color='grey' size='5'>OFF");
  118. client.println("<hr />");
  119. client.println("<hr />");
  120. client.println("</body></html>");
  121. //clearing string for next read
  122. readString="";
  123. //stopping client
  124.  
  125.  
  126.  
  127.  
  128. //LED2
  129. //added by me
  130. //controlling led via checkbox
  131. //address will look like http://192.168.1.110/?L=1 when submited
  132. client.println("<form method=get name=LED><input type=checkbox name=M value=1>LED2<br><input type=submit value=submit></form>");
  133. client.println("<br />");
  134. //printing LED status
  135. client.print("<font size='5'>LED status: ");
  136. if (LED2ON)
  137. client.println("<font color='green' size='5'>ON");
  138. else
  139. client.println("<font color='grey' size='5'>OFF");
  140. client.println("<hr />");
  141. client.println("<hr />");
  142. client.println("</body></html>");
  143. //clearing string for next read
  144. readString="";
  145. //stopping client
  146.  
  147.  
  148.  
  149.  
  150.  
  151. //LED3
  152. //added by me
  153. //controlling led via checkbox
  154. //address will look like http://192.168.1.110/?L=1 when submited
  155. client.println("<form method=get name=LED><input type=checkbox name=N value=1>LED2<br><input type=submit value=submit></form>");
  156. client.println("<br />");
  157. //printing LED status
  158. client.print("<font size='5'>LED status: ");
  159. if (LED3ON)
  160. client.println("<font color='green' size='5'>ON");
  161. else
  162. client.println("<font color='grey' size='5'>OFF");
  163. client.println("<hr />");
  164. client.println("<hr />");
  165. client.println("</body></html>");
  166. //clearing string for next read
  167. readString="";
  168. //stopping client
  169. client.stop();
  170.  
  171.  
  172. }
  173. }
  174. }
  175. }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement