Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1.  
  2. #include <SPI.h>
  3. #include <Ethernet.h>
  4. int led = 24;
  5.  
  6. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
  7. byte ip[] = { 10, 0, 0, 188 }; // ip in lan (that's what you need to use in your browser. ("192.168.1.178")
  8. byte gateway[] = { 10, 0, 0, 1 }; // internet access via router
  9. byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
  10. EthernetServer server(8087); //server port
  11. String readString;
  12.  
  13. void setup() {
  14. // Open serial communications and wait for port to open:
  15. Serial.begin(9600);
  16. while (!Serial) {
  17. ; // wait for serial port to connect. Needed for Leonardo only
  18. }
  19. pinMode(led, OUTPUT);
  20.  
  21. // start the Ethernet connection and the server:
  22. Ethernet.begin(mac, ip, gateway, subnet);
  23. server.begin();
  24. Serial.print("server is at ");
  25. Serial.println(Ethernet.localIP());
  26. }
  27.  
  28.  
  29. void loop() {
  30. // Create a client connection
  31. EthernetClient client = server.available();
  32. if (client) {
  33. while (client.connected()) {
  34. if (client.available()) {
  35. char c = client.read();
  36.  
  37. //read char by char HTTP request
  38. if (readString.length() < 100) {
  39. //store characters to string
  40. readString += c;
  41. //Serial.print(c);
  42. }
  43.  
  44. //if HTTP request has ended
  45. if (c == '\n') {
  46. Serial.println(readString); //print to serial monitor for debuging
  47.  
  48. client.println("HTTP/1.1 200 OK"); //send new page
  49. client.println("Content-Type: text/html");
  50. client.println();
  51. client.println("<HTML>");
  52. client.println("<HEAD>");
  53. client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
  54. client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
  55. client.println("<link rel='stylesheet' type='text/css' href='http://randomnerdtutorials.com/ethernetcss.css' />");
  56. client.println("<TITLE>Project</TITLE>");
  57. client.println("</HEAD>");
  58. client.println("<BODY>");
  59.  
  60. client.println("<hr />");
  61. client.println("<br />");
  62. client.println("<H2>Arduino with Ethernet Shield</H2>");
  63. client.println("<br />");
  64. client.println("<a href=\"/?button1on\"\">Turn On LED</a>");
  65. client.println("<a href=\"/?button1off\"\">Turn Off LED</a><br />");
  66.  
  67. client.println("<p>Created by Rui Santos. Visit http://randomnerdtutorials.com for more projects!</p>");
  68. client.println("<br />");
  69. client.println("</BODY>");
  70. client.println("</HTML>");
  71.  
  72. delay(1);
  73. //stopping client
  74. client.stop();
  75. //controls the Arduino if you press the buttons
  76. if (readString.indexOf("?button1on") >0){
  77. digitalWrite(led, HIGH);
  78. }
  79. if (readString.indexOf("?button1off") >0){
  80. digitalWrite(led, LOW);
  81. }
  82.  
  83. //clearing string for next read
  84. readString="";
  85.  
  86. }
  87. }
  88. }
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement