| 1. | "text-[#c62828]">// Arduino WebPage LED strip control server |
| 2. | "text-[#c62828]">// Author: Robotics & Energy |
| 3. | "text-[#c62828]">// April, 2023 |
| 4. | #include <SPI.h> |
| 5. | #include <Ethernet.h> |
| 6. | #include <SD.h> |
| 7. | #define REQ_BUF_SZ 50 |
| 8. | |
| 9. | #define RED 3 |
| 10. | #define GREEN 5 |
| 11. | #define BLUE 6 |
| 12. | #define PWR 2 |
| 13. | |
| 14. | byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; |
| 15. | #define IP IPAddress(192,168,0,12) |
| 16. | |
| 17. | EthernetServer server(80); |
| 18. | File webFile; |
| 19. | char HTTP_req[REQ_BUF_SZ] = {0}; |
| 20. | byte req_index = 0; |
| 21. | bool root = false; |
| 22. | |
| 23. | void StrClear(char *str, char len) |
| 24. | { |
| 25. | for (int i = 0; i < len; i++) |
| 26. | str[i] = 0; |
| 27. | } |
| 28. | |
| 29. | byte StrContains(char *str, char *sfind) |
| 30. | { |
| 31. | #define LEN strlen(str) |
| 32. | byte found = 0; |
| 33. | byte index = 0; |
| 34. | if (strlen(sfind) > LEN) return 0; |
| 35. | while (index < LEN) |
| 36. | { |
| 37. | if (str[index] == sfind[found]) |
| 38. | { |
| 39. | found++; |
| 40. | if (strlen(sfind) == found) return index; |
| 41. | } |
| 42. | else found = 0; |
| 43. | index++; |
| 44. | } |
| 45. | return 0; |
| 46. | } |
| 47. | |
| 48. | void controlHandler(unsigned long int dataRGB) "text-[#c62828]">// dataRGB = VRRRGGGBBB |
| 49. | { |
| 50. | byte P = (byte)((dataRGB / 1000000000) ); |
| 51. | byte R = (byte)((dataRGB / 1000000) % 1000); |
| 52. | byte G = (byte)((dataRGB / 1000) % 1000); |
| 53. | byte B = (byte)((dataRGB) % 1000); |
| 54. | |
| 55. | digitalWrite(PWR, P); |
| 56. | analogWrite(RED, R); |
| 57. | analogWrite(GREEN, G); |
| 58. | analogWrite(BLUE, B); |
| 59. | } |
| 60. | |
| 61. | void setup() |
| 62. | { |
| 63. | pinMode(13, OUTPUT); |
| 64. | pinMode(RED, OUTPUT); |
| 65. | pinMode(GREEN, OUTPUT); |
| 66. | pinMode(BLUE, OUTPUT); |
| 67. | pinMode(PWR, OUTPUT); |
| 68. | digitalWrite(13, LOW); |
| 69. | digitalWrite(PWR, LOW); |
| 70. | analogWrite(RED, 0); |
| 71. | analogWrite(GREEN, 0); |
| 72. | analogWrite(BLUE, 0); |
| 73. | |
| 74. | Serial.begin(230400); |
| 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. | if (client) |
| 90. | { |
| 91. | bool currentLineIsBlank = true; |
| 92. | while (client.connected()) |
| 93. | { |
| 94. | if (client.available()) |
| 95. | { |
| 96. | char c = client.read(); |
| 97. | if (req_index < (REQ_BUF_SZ - 1)) |
| 98. | { |
| 99. | HTTP_req[req_index] = c; |
| 100. | req_index++; |
| 101. | } |
| 102. | Serial.print(c); |
| 103. | if (c == '\n' && currentLineIsBlank) |
| 104. | { |
| 105. | if (root) |
| 106. | { |
| 107. | if (StrContains(HTTP_req, "GET / ") || StrContains(HTTP_req, "GET /login.htm")) |
| 108. | { |
| 109. | root = false; |
| 110. | client.println("HTTP/1.1 200 OK"); |
| 111. | client.println("Content-Type: text/html"); |
| 112. | client.println("Connnection: close"); |
| 113. | client.println(); |
| 114. | webFile = SD.open("login.htm"); |
| 115. | } |
| 116. | else if (StrContains(HTTP_req, "control.htm")) |
| 117. | { |
| 118. | client.println("HTTP/1.1 200 OK"); |
| 119. | client.println("Content-Type: text/html"); |
| 120. | client.println("Connnection: close"); |
| 121. | client.println(); |
| 122. | webFile = SD.open("control.htm"); |
| 123. | } |
| 124. | else if (StrContains(HTTP_req, "main.htm")) |
| 125. | { |
| 126. | client.println("HTTP/1.1 200 OK"); |
| 127. | client.println("Content-Type: text/html"); |
| 128. | client.println("Connnection: close"); |
| 129. | client.println(); |
| 130. | webFile = SD.open("main.htm"); |
| 131. | } |
| 132. | else if (StrContains(HTTP_req, "elec.jpg")) |
| 133. | { |
| 134. | client.println("HTTP/1.1 200 OK"); |
| 135. | client.println(); |
| 136. | webFile = SD.open("elec.jpg"); |
| 137. | } |
| 138. | else if (StrContains(HTTP_req, "style.css")) |
| 139. | { |
| 140. | client.println("HTTP/1.1 200 OK"); |
| 141. | client.println("Content-Type: text/css"); |
| 142. | client.println("Connnection: close"); |
| 143. | client.println(); |
| 144. | webFile = SD.open("style.css"); |
| 145. | } |
| 146. | else if (StrContains(HTTP_req, "click?")) |
| 147. | { |
| 148. | client.println("HTTP/1.1 200 OK"); |
| 149. | client.println(); |
| 150. | unsigned long int result = 0; |
| 151. | sscanf(HTTP_req, "GET /click?=%lux HTTP/1.1", &result); |
| 152. | controlHandler(result); |
| 153. | } |
| 154. | else |
| 155. | { |
| 156. | client.println("HTTP/4.5 404 Not Found"); |
| 157. | client.println("Content-Type: text/html"); |
| 158. | client.println("Connnection: close"); |
| 159. | client.println(); |
| 160. | webFile = SD.open("404.htm"); |
| 161. | } |
| 162. | } |
| 163. | else |
| 164. | { |
| 165. | if (StrContains(HTTP_req, "GET / ") || StrContains(HTTP_req, "GET /login.htm")) |
| 166. | { |
| 167. | client.println("HTTP/1.1 200 OK"); |
| 168. | client.println("Content-Type: text/html"); |
| 169. | client.println("Connnection: close"); |
| 170. | client.println(); |
| 171. | webFile = SD.open("login.htm"); |
| 172. | } |
| 173. | else if (StrContains(HTTP_req, "user=root&pass=1234")) |
| 174. | { |
| 175. | root = true; |
| 176. | client.println("HTTP/1.1 200 OK"); |
| 177. | client.println("Content-Type: text/html"); |
| 178. | client.println("Connnection: close"); |
| 179. | client.println(); |
| 180. | webFile = SD.open("main.htm"); |
| 181. | } |
| 182. | else if (StrContains(HTTP_req, "style.css")) |
| 183. | { |
| 184. | client.println("HTTP/1.1 200 OK"); |
| 185. | client.println("Content-Type: text/css"); |
| 186. | client.println("Connnection: close"); |
| 187. | client.println(); |
| 188. | webFile = SD.open("style.css"); |
| 189. | } |
| 190. | else |
| 191. | { |
| 192. | client.println("HTTP/4.2 401 Unauthorized"); |
| 193. | client.println("Content-Type: text/html"); |
| 194. | client.println("Connnection: close"); |
| 195. | client.println(); |
| 196. | webFile = SD.open("401.htm"); |
| 197. | } |
| 198. | } |
| 199. | if (webFile) |
| 200. | { |
| 201. | while (webFile.available()) |
| 202. | client.write(webFile.read()); |
| 203. | webFile.close(); |
| 204. | } |
| 205. | req_index = 0; |
| 206. | StrClear(HTTP_req, REQ_BUF_SZ); |
| 207. | break; |
| 208. | } |
| 209. | if (c == '\n') |
| 210. | currentLineIsBlank = true; |
| 211. | else if (c != '\r') |
| 212. | currentLineIsBlank = false; |
| 213. | } |
| 214. | } |
| 215. | delay(1); |
| 216. | client.stop(); |
| 217. | } |
| 218. | } |