Advertisement
Guest User

aeaw

a guest
Oct 22nd, 2014
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.94 KB | None | 0 0
  1. /*--------------------------------------------------------------
  2.   Program:      eth_websrv_SD_Ajax_in_out
  3.  
  4.   Description:  Arduino web server that displays 4 analog inputs,
  5.                 the state of 3 switches and controls 4 outputs,
  6.                 2 using checkboxes and 2 using buttons.
  7.                 The web page is stored on the micro SD card.
  8.  
  9.   Hardware:     Arduino Uno and official Arduino Ethernet
  10.                 shield. Should work with other Arduinos and
  11.                 compatible Ethernet shields.
  12.                 2Gb micro SD card formatted FAT16.
  13.                 A2 to A4 analog inputs, pins 2, 3 and 5 for
  14.                 the switches, pins 6 to 9 as outputs (LEDs).
  15.                
  16.   Software:     Developed using Arduino 1.0.5 software
  17.                 Should be compatible with Arduino 1.0 +
  18.                 SD card contains web page called index.htm
  19.  
  20.   References:   - WebServer example by David A. Mellis and
  21.                   modified by Tom Igoe
  22.                 - SD card examples by David A. Mellis and
  23.                   Tom Igoe
  24.                 - Ethernet library documentation:
  25.                   http://arduino.cc/en/Reference/Ethernet
  26.                 - SD Card library documentation:
  27.                   http://arduino.cc/en/Reference/SD
  28.  
  29.   Date:         4 April 2013
  30.   Modified:     19 June 2013
  31.                 - removed use of the String class
  32.  
  33.   Author:       W.A. Smith, http://startingelectronics.com
  34. --------------------------------------------------------------*/
  35.  
  36. #include <SPI.h>
  37. #include <Ethernet.h>
  38. #include <IRremote.h>
  39. #include <SD.h>
  40. #include <touchLib.h>
  41. // size of buffer used to capture HTTP requests
  42. #define REQ_BUF_SZ   60
  43.  
  44. // MAC address from Ethernet shield sticker under board
  45. byte mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02};
  46. EthernetServer server(81);  // create a server at port 80
  47. File webFile;               // the web page file on the SD card
  48. char HTTP_req[REQ_BUF_SZ] = {0}; // buffered HTTP request stored as null terminated string
  49. char req_index = 0;              // index into HTTP_req buffer
  50. boolean LED_state[4] = {0}; // stores the states of the LEDs
  51.  
  52. //CapSenseLib
  53. int capSensePin1 = 2;
  54. int capSensePin2 = 3;
  55. int touchedCutoff = 26;
  56.  
  57. //IR Library
  58. //IRsend irsend;
  59.  
  60. void setup()
  61. {
  62.     // disable Ethernet chip
  63.     pinMode(10, OUTPUT);
  64.     digitalWrite(10, HIGH);
  65.    
  66.     Serial.begin(9600);       // for debugging
  67.    
  68.     // initialize SD card
  69.     Serial.println("Initializing SD card...");
  70.     if (!SD.begin(4)) {
  71.         Serial.println("ERROR - SD card initialization failed!");
  72.         return;    // init failed
  73.     }
  74.     Serial.println("SUCCESS - SD card initialized.");
  75.     // check for index.htm file
  76.     if (!SD.exists("index.htm")) {
  77.         Serial.println("ERROR - Can't find index.htm file!");
  78.         return;  // can't find index file
  79.     }
  80.     Serial.println("SUCCESS - Found index.htm file.");
  81.     // LEDs
  82.     pinMode(6, OUTPUT);
  83.     pinMode(7, OUTPUT);
  84.     pinMode(8, OUTPUT);
  85.     pinMode(9, OUTPUT);
  86.    
  87.     Ethernet.begin(mac);  // initialize Ethernet device
  88.     server.begin();           // start to listen for clients
  89.    
  90. }
  91.  
  92. void loop()
  93. {
  94.     EthernetClient client = server.available();  // try to get client
  95.  
  96.     if (client) {  // got client?
  97.         boolean currentLineIsBlank = true;
  98.         while (client.connected()) {
  99.             if (client.available()) {   // client data available to read
  100.                 char c = client.read(); // read 1 byte (character) from client
  101.                 // limit the size of the stored received HTTP request
  102.                 // buffer first part of HTTP request in HTTP_req array (string)
  103.                 // leave last element in array as 0 to null terminate string (REQ_BUF_SZ - 1)
  104.                 if (req_index < (REQ_BUF_SZ - 1)) {
  105.                     HTTP_req[req_index] = c;          // save HTTP request character
  106.                     req_index++;
  107.                 }
  108.                 // last line of client request is blank and ends with \n
  109.                 // respond to client only after last line received
  110.                 if (c == '\n' && currentLineIsBlank) {
  111.                     // send a standard http response header
  112.                     client.println("HTTP/1.1 200 OK");
  113.                     // remainder of header follows below, depending on if
  114.                     // web page or XML page is requested
  115.                     // Ajax request - send XML file
  116.                     if (StrContains(HTTP_req, "ajax_inputs")) {
  117.                         // send rest of HTTP header
  118.                         client.println("Content-Type: text/xml");
  119.                         client.println("Connection: keep-alive");
  120.                         client.println();
  121.                         SetLEDs();
  122.                         //TV();
  123.                         // send XML file containing input states
  124.                         XML_response(client);
  125.                     }
  126.                     else {  // web page request
  127.                         // send rest of HTTP header
  128.                         client.println("Content-Type: text/html");
  129.                         client.println("Connection: keep-alive");
  130.                         client.println();
  131.                         // send web page
  132.                         webFile = SD.open("index.htm");        // open web page file
  133.                         if (webFile) {
  134.                             while(webFile.available()) {
  135.                                 client.write(webFile.read()); // send web page to client
  136.                             }
  137.                             webFile.close();
  138.                         }
  139.                     }
  140.                     // display received HTTP request on serial port
  141.                     Serial.print(HTTP_req);
  142.                     // reset buffer index and all buffer elements to 0
  143.                     req_index = 0;
  144.                     StrClear(HTTP_req, REQ_BUF_SZ);
  145.                     break;
  146.                 }
  147.                 // every line of text received from the client ends with \r\n
  148.                 if (c == '\n') {
  149.                     // last character on line of received text
  150.                     // starting new line with next character read
  151.                     currentLineIsBlank = true;
  152.                 }
  153.                 else if (c != '\r') {
  154.                     // a text character was received from client
  155.                     currentLineIsBlank = false;
  156.                 }
  157.             } // end if (client.available())
  158.         } // end while (client.connected())
  159.         delay(1);      // give the web browser time to receive the data
  160.         client.stop(); // close the connection
  161.     } // end if (client)
  162.    
  163.     touchFunc();
  164. }
  165.  
  166. // checks if received HTTP request is switching on/off LEDs
  167. // also saves the state of the LEDs
  168. void SetLEDs(void)
  169. {
  170.     // LED 1 (pin 6)
  171.     if (StrContains(HTTP_req, "LED1=1")) {
  172.         LED_state[0] = 1;  // save LED state
  173.         digitalWrite(6, HIGH);
  174.     }
  175.     else if (StrContains(HTTP_req, "LED1=0")) {
  176.         LED_state[0] = 0;  // save LED state
  177.         digitalWrite(6, LOW);
  178.     }
  179.     // LED 2 (pin 7)
  180.     if (StrContains(HTTP_req, "LED2=1")) {
  181.         LED_state[1] = 1;  // save LED state
  182.         digitalWrite(7, HIGH);
  183.     }
  184.     else if (StrContains(HTTP_req, "LED2=0")) {
  185.         LED_state[1] = 0;  // save LED state
  186.         digitalWrite(7, LOW);
  187.     }
  188.     // LED 3 (pin 8)
  189.     if (StrContains(HTTP_req, "LED3=1")) {
  190.         LED_state[2] = 1;  // save LED state
  191.         digitalWrite(8, HIGH);
  192.     }
  193.     else if (StrContains(HTTP_req, "LED3=0")) {
  194.         LED_state[2] = 0;  // save LED state
  195.         digitalWrite(8, LOW);
  196.     }
  197.     // LED 4 (pin 9)
  198.     if (StrContains(HTTP_req, "LED4=1")) {
  199.         LED_state[3] = 1;  // save LED state
  200.         digitalWrite(9, HIGH);
  201.     }
  202.     else if (StrContains(HTTP_req, "LED4=0")) {
  203.         LED_state[3] = 0;  // save LED state
  204.         digitalWrite(9, LOW);
  205.     }
  206. }
  207.  
  208. // send the XML file with analog values, switch status
  209. //  and LED status
  210. void XML_response(EthernetClient cl)
  211. {
  212.     int analog_val;            // stores value read from analog inputs
  213.     int count;                 // used by 'for' loops
  214.    
  215.     cl.print("<?xml version = \"1.0\" ?>");
  216.     cl.print("<inputs>");
  217.     // read analog inputs
  218.     for (count = 2; count <= 5; count++) { // A2 to A5
  219.         analog_val = analogRead(count);
  220.         cl.print("<analog>");
  221.         cl.print(analog_val);
  222.         cl.println("</analog>");
  223.     }
  224.     // checkbox LED states
  225.     // LED1
  226.     cl.print("<LED>");
  227.     if (LED_state[0]) {
  228.         cl.print("checked");
  229.     }
  230.     else {
  231.         cl.print("unchecked");
  232.     }
  233.     cl.println("</LED>");
  234.     // LED2
  235.     cl.print("<LED>");
  236.     if (LED_state[1]) {
  237.         cl.print("checked");
  238.     }
  239.     else {
  240.         cl.print("unchecked");
  241.     }
  242.      cl.println("</LED>");
  243.     // button LED states
  244.     // LED3
  245.     cl.print("<LED>");
  246.     if (LED_state[2]) {
  247.         cl.print("on");
  248.     }
  249.     else {
  250.         cl.print("off");
  251.     }
  252.     cl.println("</LED>");
  253.     // LED4
  254.     cl.print("<LED>");
  255.     if (LED_state[3]) {
  256.         cl.print("on");
  257.     }
  258.     else {
  259.         cl.print("off");
  260.     }
  261.     cl.println("</LED>");
  262.    
  263.     cl.print("</inputs>");
  264. }
  265.  
  266. // sets every element of str to 0 (clears array)
  267. void StrClear(char *str, char length)
  268. {
  269.     for (int i = 0; i < length; i++) {
  270.         str[i] = 0;
  271.     }
  272. }
  273.  
  274. // searches for the string sfind in the string str
  275. // returns 1 if string found
  276. // returns 0 if string not found
  277. char StrContains(char *str, char *sfind)
  278. {
  279.     char found = 0;
  280.     char index = 0;
  281.     char len;
  282.  
  283.     len = strlen(str);
  284.    
  285.     if (strlen(sfind) > len) {
  286.         return 0;
  287.     }
  288.     while (index < len) {
  289.         if (str[index] == sfind[found]) {
  290.             found++;
  291.             if (strlen(sfind) == found) {
  292.                 return 1;
  293.             }
  294.         }
  295.         else {
  296.             found = 0;
  297.         }
  298.         index++;
  299.     }
  300.  
  301.     return 0;
  302. }
  303.  
  304. void touchFunc()
  305. {
  306.   /*************************************************
  307.  * Touch Code
  308.  *************************************************/
  309.   if (readCapacitivePin(capSensePin1) > touchedCutoff) {
  310.     if(LED_state[1] == 0){
  311.     LED_state[1] = 1;
  312.     digitalWrite(7, HIGH);
  313.     } else{
  314.       LED_state[1] = 0;
  315.       digitalWrite(7, LOW);
  316.     }
  317.   }
  318.   /*if (readCapacitivePin(capSensePin2) > touchedCutoff) {
  319.     Serial.println("CapSensePin2: OK");
  320.   }
  321.   else {
  322.     Serial.println("CapSensePin2: OFF");
  323.   }*/
  324.  
  325.   // Every 500 ms, print the value of the capacitive sensor
  326.   // DEBUG ONLY, UNCOMMENT THIS LINES
  327.   /*if ( (millis() % 500) == 0){
  328.     Serial.print("Capacitive Sensor on Pin 2 reads: ");
  329.     Serial.println(readCapacitivePin(capSensePin1));
  330.    // Serial.print("Capacitive Sensor on Pin 7 reads: ");
  331.    // Serial.println(readCapacitivePin(capSensePin2));
  332.   }*/
  333. }
  334.  
  335. void TV(void)
  336. {
  337.       if (StrContains(HTTP_req, "TV=L")) {
  338.            //irsend.sendNEC(0xAF5E817, 32);
  339.            //delay(100);
  340.     }
  341. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement