1. /*
  2. Magic orb project
  3. A simple web server used to show a message on an LCD display
  4. */
  5. #include <SPI.h>
  6. #include <Ethernet.h>
  7. #include <LiquidCrystal.h>
  8. LiquidCrystal lcd(12, 11, 9, 5, 4, 3, 2);
  9. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  10. EthernetServer server(80);
  11. void writeDisplay(char* text)
  12. {
  13. lcd.clear();
  14. unsigned int len = (unsigned int)strlen(text);
  15. int lines = len / 20 + 1;
  16. for(int i = 0; i < lines; i++) {
  17. lcd.setCursor(0,i);
  18. char line[21];
  19. memset(line, ' ', 20);
  20. line[20] = '\0';
  21. strncpy(line, &text[i*20],20);
  22. lcd.print(line);
  23. }
  24. }
  25. void writeDisplay(String text)
  26. {
  27. char tmp[256];
  28. text.toCharArray(tmp, 256);
  29. writeDisplay(tmp);
  30. }
  31. void setup() {
  32. Serial.begin(9600);
  33. lcd.begin(20,4);
  34. lcd.clear();
  35. writeDisplay("Webserver Running. Awaiting text!");
  36. Ethernet.begin(mac);
  37. // print your local IP address:
  38. Serial.print("My IP address: ");
  39. for (byte thisByte = 0; thisByte < 4; thisByte++) {
  40. // print the value of each byte of the IP address:
  41. Serial.print(Ethernet.localIP()[thisByte], DEC);
  42. Serial.print(".");
  43. }
  44. Serial.println();
  45. }
  46. void loop() {
  47. // listen for incoming clients
  48. EthernetClient client = server.available();
  49. if (client) {
  50. // an http request ends with a blank line
  51. boolean gettingUrl = false;
  52. int urlCounter = 0;
  53. char url[256];
  54. char previousChars[6];
  55. memset(previousChars, '\0', 6);
  56. memset(url, '\0', 256);
  57. boolean currentLineIsBlank = true;
  58. while (client.connected()) {
  59. if (client.available()) {
  60. char c = client.read();
  61. if (c == '\n' && currentLineIsBlank) {
  62. // send a standard http response header
  63. String text(url);
  64. text.replace("%20", " ");
  65. //
  66. if(text != "favicon.ico")
  67. writeDisplay(text);
  68. client.println("HTTP/1.1 200 OK");
  69. client.println("Content-Type: text/html");
  70. client.println();
  71. client.print("Wrote to display: ");
  72. client.print(text);
  73. break;
  74. }
  75. if (c == '\n') {
  76. // you're starting a new line
  77. currentLineIsBlank = true;
  78. }
  79. else if (c != '\r') {
  80. if(c == ' ' && gettingUrl) {
  81. gettingUrl = false;
  82. }
  83. if(gettingUrl) {
  84. url[urlCounter++] = c;
  85. }
  86. // you've gotten a character on the current line
  87. //GET /
  88. for(int i = 0; i < 4; i++)
  89. previousChars[i] = previousChars[i+1];
  90. previousChars[4] = c;
  91. if(strcmp("GET /", previousChars) == 0) {
  92. gettingUrl = true;
  93. }
  94. currentLineIsBlank = false;
  95. }
  96. }
  97. }
  98. // give the web browser time to receive the data
  99. delay(1);
  100. // close the connection:
  101. client.stop();
  102. }
  103. }