/* * Web Server Tower Beacon * Need a Ethernet Shield / Arduino Uno. * */ #include #include #include #include /********************************************************************************************************************** * MAC address and IP address. ***********************************************************************************************************************/ byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; byte ip[] = { 192, 168, 1, 230 }; /********************************************************************************************************************** * Memory space for string management and setup WebServer service ***********************************************************************************************************************/ // For sending HTML to the client #define STRING_BUFFER_SIZE 128 char buffer[STRING_BUFFER_SIZE]; // to store data from HTTP request #define STRING_LOAD_SIZE 128 char load[STRING_LOAD_SIZE]; // POST and GET variables #define STRING_VARS_SIZE 128 char vars[STRING_VARS_SIZE]; /********************************************************************************************************************** * Strings stored in flash of the HTML we will be transmitting ***********************************************************************************************************************/ PROGMEM prog_int16_t Analog5 = 19; //Analog pin 5 ***** PROGMEM prog_int16_t Analog4 = 18; //Analog pin 4 **** PROGMEM prog_int16_t Analog3 = 17; //Analog pin 3 *** PROGMEM prog_int16_t Analog2 = 16; //Analog pin 2 *****Declare***** PROGMEM prog_int16_t Analog1 = 15; //Analog pin 1 ***Output Pins*** PROGMEM prog_int16_t Analog0 = 14; //Analog pin 0 *** PROGMEM prog_int16_t Digital8 = 8; //Digital pin 8 **** PROGMEM prog_int16_t Digital7 = 7; //Digital pin 7 ***** #define NUM_PAGES 5 // HTTP Request message PROGMEM prog_char content_404[] = "HTTP/1.1 404 Not Found\nServer: arduino\nContent-Type: text/html\n\nArduino Web Server Sending Confirmation
Single Light ControlMultiLight ControlMonitorDemo

Stage Complete!

"; PGM_P page_404[] PROGMEM = { content_404 }; // table with 404 page // HTML Header for pages PROGMEM prog_char content_main_header[] = "HTTP/1.0 200 OK\nServer: arduino\nCache-Control: no-store, no-cache, must-revalidate\nPragma: no-cache\nConnection: close\nContent-Type: text/html\n"; PROGMEM prog_char content_main_top[] = "Tower Beacon

i-Lines

"; PROGMEM prog_char content_main_menu[] = "
Single Light ControlMonitor
"; PROGMEM prog_char content_main_footer[] = ""; PGM_P contents_main[] PROGMEM = { content_main_header, content_main_top, content_main_menu, content_main_footer }; // table with 404 page #define CONT_HEADER 0 #define CONT_TOP 1 #define CONT_MENU 2 #define CONT_FOOTER 3 #define valll "test"; // Page 1 PROGMEM prog_char http_uri1[] = "/"; PROGMEM prog_char content_title1[] = "

Single Light Controls

"; PROGMEM prog_char content_page1[] = "

If multiple people are logged in at the same time, the most current command sender will be the leader.


" "" "" "" "" "" "" "" "" "
" "

NE""

xxxxx
A5=OFF
" "
" "

N

""A4=ON""
A4=OFF
" "
" "

NW

A3=ON
A3=OFF
" "
" "

W

A2=ON
A2=OFF
" "
" "

SW

A1=ON
A1=OFF
" "
" "

S

A0=ON
A0=OFF
" "
" "

SE

D8=ON
D8=OFF
" "
" "

E

D7=ON
D7=OFF
" //"
" "
" "" "" "
" "

ALL-Lights

ALL=ON
ALL=OFF
" "
" "

Lighthouse

Lighthouse=ON
" /*"Lighthouse=OFF"*/ "
" "
"; // Page 2 PROGMEM prog_char http_uri2[] = "/page2"; PROGMEM prog_char content_title2[] = "

MultiLight Control

"; PROGMEM prog_char content_page2[] = "

If multiple people are logged in at the same time, the most current command sender will be the leader.


" "" "" "
" "

ALL-Lights

ALL=ON
ALL=OFF
" "
" "

Lighthouse

Lighthouse=ON
" /*"Lighthouse=OFF"*/ "
" "
"; // Page 3 //PROGMEM prog_int8_t AnaChan3 = 3; PROGMEM prog_char http_uri3[] = "/page3"; PROGMEM prog_char content_title3[] = "

Monitor

"; char* mSTO[] = {"yes"}; PROGMEM prog_char content_page3[] = mSTO[0]; // Page 4 PROGMEM prog_char http_uri4[] = "/page4"; PROGMEM prog_char content_title4[] = "

Demo

"; PROGMEM prog_char content_page4[] = "

..5-0..

If multiple people are logged in at the same time, the most current command sender will be the leader." "" "
" "Demo=ON
Demo=OFF
" "

"; // Page 5 PROGMEM prog_char http_uri5[] = "/login"; PROGMEM prog_char content_title5[] = "

POST Page 5

"; PROGMEM prog_char content_page5[] = "

Content of Page 5

received a POST request

"; // declare tables for the pages PGM_P contents_titles[] PROGMEM = { content_title1, content_title2, content_title3, content_title4, content_title5 }; // titles PGM_P http_uris[] PROGMEM = { http_uri1, http_uri2, http_uri3, http_uri4, http_uri5 }; // URIs PGM_P contents_pages[] PROGMEM = { content_page1, content_page2, content_page3, content_page4, content_page5 }; // real content /********************************************************************************************************************** * define HTTP return structure ID for parsing HTTP header request ***********************************************************************************************************************/ struct HTTP_DEF { int pages; char vars[STRING_VARS_SIZE]; //size needs to match the global variable 'vars' otherwise program crashes due to index overrun } ; /********************************************************************************************************************** * Shared variables ***********************************************************************************************************************/ EthernetServer server = EthernetServer(80); //port 80 void setup() { Serial.begin(9600);//Initiate Serial Monitor for Debugging purposes //Ethernet.begin(mac); //HINT: for DHCP Setup //Ethernet.begin(mac, ip, gateway, subnet); //HINT: for manual setup Ethernet.begin(mac, ip); server.begin(); //Serial.println(Ethernet.localIP()); //Open the Arduino Serial Monitor (Top right corner) to view what DHCP IP Address has been pulled so you can navigate to the web page. pinMode(Analog5, OUTPUT); pinMode(Analog4, OUTPUT); pinMode(Analog3, OUTPUT); pinMode(Analog2, OUTPUT); pinMode(Analog1, OUTPUT); pinMode(Analog0, OUTPUT); pinMode(Digital8, OUTPUT); pinMode(Digital7, OUTPUT); } /********************************************************************************************************************** * Main loop ***********************************************************************************************************************/ void loop() { EthernetClient client = server.available(); if (client) { // now client is connected to arduino // read HTTP header request... so select what page client are looking for HTTP_DEF http_def = readHTTPRequest(client); if (http_def.pages > 0) { sendPage(client,http_def); } else { contentPrinter(client,(char*)pgm_read_word(&(page_404[0]))); } singleLightControls(); multiLightControls(); //monitor(); //demo(); // give the web browser time to receive the data delay(1); client.stop(); } } /********************************************************************************************************************** * Method for read HTTP Header Request from web client ***********************************************************************************************************************/ struct HTTP_DEF readHTTPRequest(EthernetClient client) { char c; int i; // use buffer, pay attention! int bufindex = 0; // reset buffer int loadindex = 0; // reset load int contentLength = 0; // reset POST content length char compare[50]; // page comparison (URI selection) HTTP_DEF http_def; // use the structure for multiple returns http_def.pages = 1; // default page selection... error // reading all rows of header if (client.connected() && client.available()) { // read a row buffer[0] = client.read(); buffer[1] = client.read(); bufindex = 2; // read the first line to determinate the request page while (buffer[bufindex-2] != '\r' && buffer[bufindex-1] != '\n') { // read full row and save it in buffer c = client.read(); if (bufindex"); // send POST variables client.print(vars); // send footer contentPrinter(client,(char*)pgm_read_word(&(contents_main[CONT_FOOTER]))); } /********************************************************************************************************************** * Single Light Controls ***********************************************************************************************************************/ void PROGMEM singleLightControls() { /*****/int i; if(strstr(buffer,"/?A5=1")!=0) { //look for the command to turn the Analog5 on digitalWrite(Analog5, 1); //turn the Analog5 on /*PROGMEM prog_char content_title1[14] = "X"; for (i = 0; i < sizeof(content_title1) - 1; i++){ Serial.print(i, DEC); Serial.print(" = "); Serial.write(content_title1[i]); Serial.println();}*/ } else if(strstr(buffer,"/?A5=0")!=0) { //look for command to turn Analog5 off. Note: If Analog5 is on, it will stay on until command is given to turn it off, even if multiple computers are on the site digitalWrite(Analog5, 0); //turn Analog5 off PROGMEM prog_char content_title1[14] = "V"; } else if(strstr(buffer,"/?A4=1")!=0) { //look for the command to turn the Analog4 on digitalWrite(Analog4, 1); //turn the Analog4 on } else if(strstr(buffer,"/?A4=0")!=0) { //look for command to turn Analog4 off. Note: If Analog4 is on, it will stay on until command is given to turn it off, even if multiple computers are on the site digitalWrite(Analog4, 0); //turn Analog4 off } else if(strstr(buffer,"/?A3=1")!=0) { //look for the command to turn the Analog3 on digitalWrite(Analog3, 1); //turn the Analog4 on } else if(strstr(buffer,"/?A3=0")!=0) { //look for command to turn Analog3 off. Note: If Analog3 is on, it will stay on until command is given to turn it off, even if multiple computers are on the site digitalWrite(Analog3, 0); //turn Analog3 off } else if(strstr(buffer,"/?A2=1")!=0) { //look for the command to turn the Analog2 on digitalWrite(Analog2, 1); //turn the Analog2 on } else if(strstr(buffer,"/?A2=0")!=0) { //look for command to turn Analog2 off. Note: If Analog2 is on, it will stay on until command is given to turn it off, even if multiple computers are on the site digitalWrite(Analog2, 0); //turn Analog2 off } else if(strstr(buffer,"/?A1=1")!=0) { //look for the command to turn the Analog1 on digitalWrite(Analog1, 1); //turn the Analog1 on } else if(strstr(buffer,"/?A1=0")!=0) { //look for command to turn Analog1 off. Note: If Analog1 is on, it will stay on until command is given to turn it off, even if multiple computers are on the site digitalWrite(Analog1, 0); //turn Analog1 off } else if(strstr(buffer,"/?A0=1")!=0) { //look for the command to turn the Analog0 on digitalWrite(Analog0, 1); //turn the Analog0 on } else if(strstr(buffer,"/?A0=0")!=0) { //look for command to turn Analog0 off. Note: If Analog0 is on, it will stay on until command is given to turn it off, even if multiple computers are on the site digitalWrite(Analog0, 0); //turn Analog0 off } else if(strstr(buffer,"/?D8=1")!=0) { //look for the command to turn the Digital8 on digitalWrite(Digital8, 1); //turn the Digital8 on } else if(strstr(buffer,"/?D8=0")!=0) { //look for command to turn Digital8 off. Note: If Digital8 is on, it will stay on until command is given to turn it off, even if multiple computers are on the site digitalWrite(Digital8, 0); //turn Digital8 off } else if(strstr(buffer,"/?D7=1")!=0) { //look for the command to turn the Digital7 on digitalWrite(Digital7, 1); //turn the Digital7 on } else if(strstr(buffer,"/?D7=0")!=0) { //look for command to turn Digital7 off. Note: If Digital7 is on, it will stay on until command is given to turn it off, even if multiple computers are on the site digitalWrite(Digital7, 0); //turn Digital7 off } /*****/ } /********************************************************************************************************************** * Multiple Light Controls ***********************************************************************************************************************/ void PROGMEM multiLightControls() {//ALL ON/OFF PROGMEM prog_char ON = 1; PROGMEM prog_char OFF = 0; if(strstr(buffer,"/?ALL=1")!=0) { //look for the command to turn the Digital7 on digitalWrite(Analog5, ON), digitalWrite(Analog4, ON), digitalWrite(Analog3, ON), digitalWrite(Analog2, ON), digitalWrite(Analog1, ON), digitalWrite(Analog0, ON), digitalWrite(Digital8, ON), digitalWrite(Digital7, ON); //turn the ALL on } else if(strstr(buffer,"/?ALL=0")!=0) { //look for command to turn All-off. Note: If All is on, it will stay on until command is given to turn it off, even if multiple computers are on the site digitalWrite(Analog5, OFF), digitalWrite(Analog4, OFF), digitalWrite(Analog3, OFF), digitalWrite(Analog2, OFF), digitalWrite(Analog1, OFF), digitalWrite(Analog0, OFF), digitalWrite(Digital8, OFF), digitalWrite(Digital7, OFF); //turn ALL off } while (strstr(buffer,"/?Lighthouse=1")!=0){//Lighthouse digitalWrite(Analog5, OFF), /////////////// digitalWrite(Analog4, OFF), // digitalWrite(Analog3, OFF), // digitalWrite(Analog2, OFF), //This turns all digitalWrite(Analog1, OFF), //lights off first digitalWrite(Analog0, OFF), // digitalWrite(Digital8, OFF), // digitalWrite(Digital7, OFF); /////////////// delay(200), digitalWrite(Analog5, 1), /////////////// delay(200), digitalWrite(Analog4, 1), digitalWrite(Analog5, 0), delay(200), digitalWrite(Analog3, 1), digitalWrite(Analog4, 0), delay(200), digitalWrite(Analog2, 1), digitalWrite(Analog3, 0), delay(200), digitalWrite(Analog1, 1), //Sequence #1 digitalWrite(Analog2, 0), delay(200), digitalWrite(Analog0, 1), digitalWrite(Analog1, 0), delay(200), digitalWrite(Digital8, 1), digitalWrite(Analog0, 0), delay(200), digitalWrite(Digital7, 1), digitalWrite(Digital8, 0), delay(200), digitalWrite(Digital7, 0); /////////////// digitalWrite(Analog5, 1), /////////////// delay(200), digitalWrite(Analog4, 1), digitalWrite(Analog5, 0), delay(200), digitalWrite(Analog3, 1), digitalWrite(Analog4, 0), delay(200), digitalWrite(Analog2, 1), digitalWrite(Analog3, 0), delay(200), digitalWrite(Analog1, 1), //Sequence #2 digitalWrite(Analog2, 0), delay(200), digitalWrite(Analog0, 1), digitalWrite(Analog1, 0), delay(200), digitalWrite(Digital8, 1), digitalWrite(Analog0, 0), delay(200), digitalWrite(Digital7, 1), digitalWrite(Digital8, 0), delay(200), digitalWrite(Digital7, 0); /////////////// break; } } /********************************************************************************************************************** * Monitor (Analog and Digital channel checks) ***********************************************************************************************************************/ void PROGMEM monitor() { //EthernetClient client; char* mSTO[] = {"yes"}; //PROGMEM prog_char myStorage[] = mSTO[]; //char* theWord = 0; /*if(analogRead(5)<500) //965 = on, 0 = off { PROGMEM prog_char content_page3[] = NE; } else { PROGMEM prog_char content_page3[] = NE; }*/ /*if(analogRead(5)<500) //965 = on, 0 = off { //*///theWord = "test"; /*} else { client.print(" On "); } client.println("
");*/ } /********************************************************************************************************************** * Send content split by buffer size ***********************************************************************************************************************/ void contentPrinter(EthernetClient client, char *realword) { int total = 0; int start = 0; char buffer[STRING_BUFFER_SIZE]; int realLen = strlen_P(realword); memset(buffer,0,STRING_BUFFER_SIZE); while (total <= realLen) { // print content strncpy_P(buffer, realword+start, STRING_BUFFER_SIZE-1); client.print(buffer); // more content to print? total = start + STRING_BUFFER_SIZE-1; start = start + STRING_BUFFER_SIZE-1; } } /********************************************************************************************************************** * Send real page content ***********************************************************************************************************************/ void sendContent(EthernetClient client, int pageId) { contentPrinter(client,(char*)pgm_read_word(&(contents_pages[pageId]))); } /********************************************************************************************************************** * END OF CODE! WELL DONE! ***********************************************************************************************************************/