#include <SPI.h>
#include <Ethernet.h>
#define MaxHeaderLength 47 //maximum length of http header required
#include <Wire.h>
#include <Adafruit_MCP23017.h>
#include <Adafruit_RGBLCDShield.h>
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
byte mac[] = {
0x90, 0xA2, 0xDA, 0x0D, 0x35, 0x70 }; //physical mac address
byte ip[] = {
10,0,0, 26 };
byte gateway[] = {
10,0,0, 1 };
byte subnet[] = {
255, 255, 255, 0 };
EthernetServer server(76); //arduino server port
String HttpHeader = String(MaxHeaderLength);
String displayText1 = "1";
String displayText2 = "2";
String displayText3 = "3";
void setup(){
//enable serial monitor
Serial.begin(9600);
//start Ethernet
Ethernet.begin(mac, ip, gateway, subnet);
//initialize variable
HttpHeader="";
displayText1="";
displayText2="";
displayText3="";
lcd.begin(16,2);
lcd.print("SystemOnline");
Serial.print("System Online");
}
void loop(){
// Create a client connection
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
//read MaxHeaderLength number of characters in the HTTP header
//discard the rest until \n
if (HttpHeader.length() < MaxHeaderLength){
//store characters to string
HttpHeader = HttpHeader + c; //As I understand it, it's reading the characters one by one and adding them to the string as it goes.
}
//if HTTP request has ended
if (c == '\n') {
HttpHeader.replace("HTTP/1.1"," "); //This appears at the end of the string, so removing it doesn't mess with the character count
HttpHeader.replace("+", " "); //Replacing + signs with actual spaces
HttpHeader.replace("%2B", "+"); //Replacing character codes with the actual symbols
HttpHeader.replace("%2F", "/");
HttpHeader.replace("%21", "!");
HttpHeader.replace("%3F", "?");
if (HttpHeader.startsWith("GET /?text")){
displayText1 = HttpHeader.substring(11,27); //Splitting the text among the two lines
displayText2 = HttpHeader.substring(27,43);
}
lcd.clear();
lcd.print(displayText1);
lcd.setCursor(0,1);
lcd.print(displayText2);
// show the string on the monitor
Serial.println("displayText1:" + displayText1);
Serial.println("displayText2:" + displayText2);
// start of web page
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("<html><head></head><body>");
client.println();
client.print("<form method=get>");
client.print("Enter in a 16 character string");
client.print("<input type='text' name=text><br>");
client.print("<input type=submit value=submit></form>");
client.print("</body></html>");
//clearing string for next read
HttpHeader="";
//stopping client
client.stop();
}
}
}
}
}