- /*
- Magic orb project
- A simple web server used to show a message on an LCD display
- */
- #include <SPI.h>
- #include <Ethernet.h>
- #include <LiquidCrystal.h>
- LiquidCrystal lcd(12, 11, 9, 5, 4, 3, 2);
- byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
- EthernetServer server(80);
- void writeDisplay(char* text)
- {
- lcd.clear();
- unsigned int len = (unsigned int)strlen(text);
- int lines = len / 20 + 1;
- for(int i = 0; i < lines; i++) {
- lcd.setCursor(0,i);
- char line[21];
- memset(line, ' ', 20);
- line[20] = '\0';
- strncpy(line, &text[i*20],20);
- lcd.print(line);
- }
- }
- void writeDisplay(String text)
- {
- char tmp[256];
- text.toCharArray(tmp, 256);
- writeDisplay(tmp);
- }
- void setup() {
- Serial.begin(9600);
- lcd.begin(20,4);
- lcd.clear();
- writeDisplay("Webserver Running. Awaiting text!");
- Ethernet.begin(mac);
- // print your local IP address:
- Serial.print("My IP address: ");
- for (byte thisByte = 0; thisByte < 4; thisByte++) {
- // print the value of each byte of the IP address:
- Serial.print(Ethernet.localIP()[thisByte], DEC);
- Serial.print(".");
- }
- Serial.println();
- }
- void loop() {
- // listen for incoming clients
- EthernetClient client = server.available();
- if (client) {
- // an http request ends with a blank line
- boolean gettingUrl = false;
- int urlCounter = 0;
- char url[256];
- char previousChars[6];
- memset(previousChars, '\0', 6);
- memset(url, '\0', 256);
- boolean currentLineIsBlank = true;
- while (client.connected()) {
- if (client.available()) {
- char c = client.read();
- if (c == '\n' && currentLineIsBlank) {
- // send a standard http response header
- String text(url);
- text.replace("%20", " ");
- //
- if(text != "favicon.ico")
- writeDisplay(text);
- client.println("HTTP/1.1 200 OK");
- client.println("Content-Type: text/html");
- client.println();
- client.print("Wrote to display: ");
- client.print(text);
- break;
- }
- if (c == '\n') {
- // you're starting a new line
- currentLineIsBlank = true;
- }
- else if (c != '\r') {
- if(c == ' ' && gettingUrl) {
- gettingUrl = false;
- }
- if(gettingUrl) {
- url[urlCounter++] = c;
- }
- // you've gotten a character on the current line
- //GET /
- for(int i = 0; i < 4; i++)
- previousChars[i] = previousChars[i+1];
- previousChars[4] = c;
- if(strcmp("GET /", previousChars) == 0) {
- gettingUrl = true;
- }
- currentLineIsBlank = false;
- }
- }
- }
- // give the web browser time to receive the data
- delay(1);
- // close the connection:
- client.stop();
- }
- }