| 1. | "text-[#c62828]">//Arduino WebPage circuit control 2018 |
| 2. | "text-[#c62828]">//Author: Dirty Dail |
| 3. | |
| 4. | #include <SPI.h> |
| 5. | #include <Ethernet.h> |
| 6. | #include <SD.h> |
| 7. | |
| 8. | #define REQ_BUF_SZ 40 "text-[#c62828]">// size of buffer used to capture HTTP requests |
| 9. | |
| 10. | byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; |
| 11. | #define IP IPAddress(192,168,0,12) |
| 12. | |
| 13. | EthernetServer server(80); "text-[#c62828]">// create a server at port 80 |
| 14. | File webFile; "text-[#c62828]">// handle to files on SD card |
| 15. | char HTTP_req[REQ_BUF_SZ] = {0}; "text-[#c62828]">// buffered HTTP request stored as null terminated string |
| 16. | byte req_index = 0; "text-[#c62828]">// index into HTTP_req buffer |
| 17. | bool root = false; "text-[#c62828]">// login indicator |
| 18. | |
| 19. | "text-[#c62828]">//RGB LED Pins |
| 20. | #define RED 3 |
| 21. | #define GREEN 5 |
| 22. | #define BLUE 6 |
| 23. | #define PWR 2 |
| 24. | |
| 25. | #define SENSOR A0 |
| 26. | |
| 27. | "text-[#c62828]">// sets every element of str to 0 (clears array) |
| 28. | void StrClear(char *str, char len) |
| 29. | { |
| 30. | for (int i = 0; i < len; i++) |
| 31. | str[i] = 0; |
| 32. | } |
| 33. | |
| 34. | "text-[#c62828]">// searches for the string sfind in the string str |
| 35. | "text-[#c62828]">// returns index if found, 0 if not found |
| 36. | byte StrContains(char *str, char *sfind) |
| 37. | { |
| 38. | #define LEN strlen(str) |
| 39. | byte found = 0; |
| 40. | byte index = 0; |
| 41. | if (strlen(sfind) > LEN) return 0; |
| 42. | while (index < LEN) |
| 43. | { |
| 44. | if (str[index] == sfind[found]) |
| 45. | { |
| 46. | found++; |
| 47. | if (strlen(sfind) == found) return index; |
| 48. | } |
| 49. | else found = 0; |
| 50. | index++; |
| 51. | } |
| 52. | return 0; |
| 53. | } |
| 54. | |
| 55. | void controlHandler(byte output) "text-[#c62828]">// byte = 00001111 (0000rgbv) |
| 56. | { |
| 57. | digitalWrite(PWR, output % 2); |
| 58. | digitalWrite(BLUE, (output / 2) % 2); |
| 59. | digitalWrite(GREEN, (output / 4) % 2); |
| 60. | digitalWrite(RED, (output / 8) % 2); |
| 61. | } |
| 62. | |
| 63. | void setup() |
| 64. | { |
| 65. | pinMode(RED, OUTPUT); |
| 66. | pinMode(GREEN, OUTPUT); |
| 67. | pinMode(BLUE, OUTPUT); |
| 68. | pinMode(PWR, OUTPUT); |
| 69. | digitalWrite(PWR, LOW); |
| 70. | digitalWrite(RED, HIGH); |
| 71. | digitalWrite(GREEN, HIGH); |
| 72. | digitalWrite(BLUE, HIGH); |
| 73. | |
| 74. | Serial.begin(9600); |
| 75. | Serial.println("Initializing SD card..."); |
| 76. | if (!SD.begin(4)) |
| 77. | { |
| 78. | Serial.println("ERROR - SD card initialization failed!"); |
| 79. | return; |
| 80. | } |
| 81. | Serial.println("SUCCESS - SD card initialized."); |
| 82. | Ethernet.begin(mac, IP); |
| 83. | server.begin(); |
| 84. | } |
| 85. | |
| 86. | void loop() |
| 87. | { |
| 88. | EthernetClient client = server.available(); |
| 89. | |
| 90. | if (client) |
| 91. | { |
| 92. | bool currentLineIsBlank = true; |
| 93. | while (client.connected()) |
| 94. | { |
| 95. | if (client.available()) |
| 96. | { |
| 97. | char c = client.read(); |
| 98. | if (req_index < (REQ_BUF_SZ - 1)) |
| 99. | { |
| 100. | HTTP_req[req_index] = c; |
| 101. | req_index++; |
| 102. | } |
| 103. | Serial.print(c); |
| 104. | if (c == '\n' && currentLineIsBlank) |
| 105. | { |
| 106. | if (root) "text-[#c62828]">// client authorized? |
| 107. | { |
| 108. | if (StrContains(HTTP_req, "GET / ") || StrContains(HTTP_req, "GET /login.htm")) |
| 109. | { |
| 110. | root = false; |
| 111. | client.println("HTTP/1.1 200 OK"); |
| 112. | client.println("Content-Type: text/html"); |
| 113. | client.println("Connnection: close"); |
| 114. | client.println(); |
| 115. | webFile = SD.open("login.htm"); |
| 116. | } |
| 117. | else if (StrContains(HTTP_req, "control.htm")) |
| 118. | { |
| 119. | client.println("HTTP/1.1 200 OK"); |
| 120. | client.println("Content-Type: text/html"); |
| 121. | client.println("Connnection: close"); |
| 122. | client.println(); |
| 123. | webFile = SD.open("control.htm"); |
| 124. | } |
| 125. | else if (StrContains(HTTP_req, "main.htm")) |
| 126. | { |
| 127. | client.println("HTTP/1.1 200 OK"); |
| 128. | client.println("Content-Type: text/html"); |
| 129. | client.println("Connnection: close"); |
| 130. | client.println(); |
| 131. | webFile = SD.open("main.htm"); |
| 132. | } |
| 133. | else if (StrContains(HTTP_req, "elec.jpg")) |
| 134. | { |
| 135. | client.println("HTTP/1.1 200 OK"); |
| 136. | client.println(); |
| 137. | webFile = SD.open("elec.jpg"); |
| 138. | } |
| 139. | else if (StrContains(HTTP_req, "style.css")) |
| 140. | { |
| 141. | client.println("HTTP/1.1 200 OK"); |
| 142. | client.println("Content-Type: text/css"); |
| 143. | client.println("Connnection: close"); |
| 144. | client.println(); |
| 145. | webFile = SD.open("style.css"); |
| 146. | } |
| 147. | else if (StrContains(HTTP_req, "click?")) |
| 148. | { |
| 149. | client.println("HTTP/1.1 200 OK"); |
| 150. | client.println(); |
| 151. | byte i = StrContains(HTTP_req, "click?") + 2; |
| 152. | byte result = 0; |
| 153. | byte dig = 0; |
| 154. | for (; HTTP_req[i] != 'x'; i++) |
| 155. | { |
| 156. | dig++; |
| 157. | if (dig >= 2) result *= 10; |
| 158. | result += (HTTP_req[i] - '0'); |
| 159. | } |
| 160. | controlHandler(result); |
| 161. | } |
| 162. | else |
| 163. | { |
| 164. | client.println("HTTP/4.5 404 Not Found"); |
| 165. | client.println("Content-Type: text/html"); |
| 166. | client.println("Connnection: close"); |
| 167. | client.println(); |
| 168. | webFile = SD.open("404.htm"); |
| 169. | } |
| 170. | } |
| 171. | else |
| 172. | { |
| 173. | if (StrContains(HTTP_req, "GET / ") || StrContains(HTTP_req, "GET /login.htm")) |
| 174. | { |
| 175. | client.println("HTTP/1.1 200 OK"); |
| 176. | client.println("Content-Type: text/html"); |
| 177. | client.println("Connnection: close"); |
| 178. | client.println(); |
| 179. | webFile = SD.open("login.htm"); |
| 180. | } |
| 181. | else if (StrContains(HTTP_req, "user=root&pass=1234")) |
| 182. | { |
| 183. | root = true; |
| 184. | client.println("HTTP/1.1 200 OK"); |
| 185. | client.println("Content-Type: text/html"); |
| 186. | client.println("Connnection: close"); |
| 187. | client.println(); |
| 188. | webFile = SD.open("main.htm"); |
| 189. | } |
| 190. | else if (StrContains(HTTP_req, "style.css")) |
| 191. | { |
| 192. | client.println("HTTP/1.1 200 OK"); |
| 193. | client.println("Content-Type: text/css"); |
| 194. | client.println("Connnection: close"); |
| 195. | client.println(); |
| 196. | webFile = SD.open("style.css"); |
| 197. | } |
| 198. | else |
| 199. | { |
| 200. | client.println("HTTP/4.2 401 Unauthorized"); |
| 201. | client.println("Content-Type: text/html"); |
| 202. | client.println("Connnection: close"); |
| 203. | client.println(); |
| 204. | webFile = SD.open("401.htm"); |
| 205. | } |
| 206. | } |
| 207. | if (webFile) |
| 208. | { |
| 209. | while (webFile.available()) |
| 210. | client.write(webFile.read()); |
| 211. | webFile.close(); |
| 212. | } |
| 213. | req_index = 0; |
| 214. | StrClear(HTTP_req, REQ_BUF_SZ); |
| 215. | break; |
| 216. | } |
| 217. | if (c == '\n') |
| 218. | currentLineIsBlank = true; |
| 219. | else if (c != '\r') |
| 220. | currentLineIsBlank = false; |
| 221. | } |
| 222. | } |
| 223. | delay(1); |
| 224. | client.stop(); |
| 225. | } |
| 226. | } |