Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. #include <Client.h>
  2. #include <Dhcp.h>
  3. #include <dns.h>
  4. #include <Ethernet.h>
  5. #include <Server.h>
  6. #include <sockutil.h>
  7. #include <Udp.h>
  8. #include <util.h>
  9.  
  10. #include <SPI.h>
  11.  
  12.  
  13. #define PINOFFSET 2
  14. #define PINRANGE 5
  15.  
  16. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  17. boolean ipAcquired = false;
  18.  
  19. Server server(80);
  20.  
  21. void setup()
  22. {
  23. for (int i = 0; i < PINRANGE; i++)
  24. {
  25. pinMode(i + PINOFFSET, OUTPUT);
  26. digitalWrite(i + PINOFFSET, 0);
  27. }
  28.  
  29. for (int i = 0; i < PINRANGE; i++)
  30. {
  31. digitalWrite(i + PINOFFSET, 1);
  32. delay(100);
  33. }
  34. delay(1000);
  35.  
  36. for (int i = 0; i < PINRANGE; i++)
  37. {
  38. digitalWrite(i+ PINOFFSET, 0);
  39. }
  40.  
  41. Serial.begin(9600);
  42. // Get an IP address to use and also find out our DNS server address
  43. while (Dhcp.beginWithDHCP(mac) != 1)
  44. {
  45. Serial.println("Error getting IP address via DHCP, trying again...");
  46. delay(15000);
  47. }
  48. delay(1000);
  49.  
  50. uint8_t localIP[4];
  51. Dhcp.getLocalIp(localIP);
  52.  
  53. Serial.print((int)localIP[0]);
  54. Serial.print(".");
  55. Serial.print((int)localIP[1]);
  56. Serial.print(".");
  57. Serial.print((int)localIP[2]);
  58. Serial.print(".");
  59. Serial.println((int)localIP[3]);
  60. }
  61.  
  62.  
  63.  
  64. void loop()
  65. {
  66. Client client = server.available();
  67. if (client)
  68. {
  69. boolean currentLineIsBlank = true;
  70. char lineBuffer[255];
  71. int index = 0;
  72. for (int i = 0; i < 255; i++)
  73. lineBuffer[i] = 0;
  74.  
  75. while (client.connected())
  76. {
  77. if (client.available())
  78. {
  79. char c = client.read();
  80. lineBuffer[index++] = c;
  81.  
  82. // if you've gotten to the end of the line (received a newline
  83. // character) and the line is blank, the http request has ended,
  84. // so you can send a reply
  85. if (c == '\n' && currentLineIsBlank)
  86. {
  87. // send a standard http response header
  88. client.println("HTTP/1.1 200 OK");
  89. client.println("Content-Type: text/html");
  90. client.println();
  91. client.println("OK");
  92.  
  93. break;
  94. }
  95. if (c == '\n') {
  96. // you're starting a new line
  97. currentLineIsBlank = true;
  98. String buffer = String(lineBuffer);
  99. if (buffer.startsWith("GET"))
  100. {
  101. char led, state;
  102.  
  103. led = lineBuffer[5] - '0';
  104. state = lineBuffer[6] - '0';
  105.  
  106. Serial.println((int)led);
  107. Serial.println((int)state);
  108.  
  109. Serial.println(lineBuffer);
  110.  
  111. digitalWrite(led + PINOFFSET, state);
  112. }
  113. index = 0;
  114. for (int i = 0; i < 255; i++)
  115. lineBuffer[i] = 0;
  116.  
  117. }
  118. else if (c != '\r') {
  119. // you've gotten a character on the current line
  120. currentLineIsBlank = false;
  121. }
  122. }
  123. }
  124. // give the web browser time to receive the data
  125. delay(1);
  126. // close the connection:
  127. client.stop();
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement