Guest User

Untitled

a guest
Jun 30th, 2016
157
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <SPI.h>
  2. #include <Ethernet.h>
  3. #include <SD.h>
  4. #define REQ_BUF_SZ 60
  5.  
  6.  
  7. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  8. IPAddress ip(192, 168, 0, 20);
  9. EthernetServer server(80);
  10. File webFile;
  11. char HTTP_req[REQ_BUF_SZ] = {0};
  12. char req_index = 0;
  13. boolean LED_state[5] = {0};
  14.  
  15. void setup()
  16. {
  17.   // disable Ethernet chip
  18.   pinMode(10, OUTPUT);
  19.   digitalWrite(10, HIGH);
  20.  
  21.   Serial.begin(9600);
  22.  
  23.   // initialize SD card
  24.   Serial.println("Initializing SD card...");
  25.   if (!SD.begin(4)) {
  26.     Serial.println("ERROR - SD card initialization failed!");
  27.     return;
  28.   }
  29.   Serial.println("SUCCESS - SD card initialized.");
  30.  
  31.   if (!SD.exists("index.htm")) {
  32.     Serial.println("ERROR - Can't find index.htm file!");
  33.     return;
  34.   }
  35.   Serial.println("SUCCESS - Found index.htm file.");
  36.   // switches
  37.   pinMode(2, INPUT);
  38.   pinMode(3, INPUT);
  39.   // LEDs
  40.   pinMode(9, OUTPUT);
  41.   pinMode(5, OUTPUT);
  42.   pinMode(6, OUTPUT);
  43.   pinMode(7, OUTPUT);
  44.   pinMode(8, OUTPUT);
  45.  
  46.   Ethernet.begin(mac, ip);
  47.   server.begin();
  48. }
  49.  
  50. void loop()
  51. {
  52.   EthernetClient client = server.available(); // try to get client
  53.  
  54.   if (client) { // got client?
  55.     boolean currentLineIsBlank = true;
  56.     while (client.connected()) {
  57.       if (client.available()) { // client data available to read
  58.         char c = client.read(); // read 1 byte (character) from client
  59.      
  60.         if (req_index < (REQ_BUF_SZ - 1)) {
  61.           HTTP_req[req_index] = c; // save HTTP request character
  62.           req_index++;
  63.         }
  64.  
  65.         if (c == '\n' && currentLineIsBlank) {
  66.  
  67.           client.println("HTTP/1.1 200 OK");
  68.          
  69.  
  70.           if (StrContains(HTTP_req, "ajax_inputs")) {
  71.          
  72.             client.println("Content-Type: text/xml");
  73.             client.println("Connection: keep-alive");
  74.             client.println();
  75.             SetLEDs();
  76.             // send XML file containing input states
  77.             XML_response(client);
  78.           }
  79.           else {
  80.            
  81.             client.println("Content-Type: text/html");
  82.             client.println("Connection: keep-alive");
  83.             client.println();
  84.          
  85.             webFile = SD.open("index.htm");
  86.             if (webFile) {
  87.               while (webFile.available()) {
  88.                 client.write(webFile.read());               }
  89.               webFile.close();
  90.             }
  91.           }
  92.    
  93.           Serial.print(HTTP_req);
  94.      
  95.           req_index = 0;
  96.           StrClear(HTTP_req, REQ_BUF_SZ);
  97.           break;
  98.         }
  99.      
  100.         if (c == '\n') {
  101.    
  102.           currentLineIsBlank = true;
  103.         }
  104.         else if (c != '\r') {
  105.           // a text character was received from client
  106.           currentLineIsBlank = false;
  107.         }
  108.       } // end if (client.available())
  109.     } // end while (client.connected())
  110.     delay(1); // give the web browser time to receive the data
  111.     client.stop(); // close the connection
  112.   } // end if (client)
  113.  
  114.   // read buttons and debounce
  115.   ButtonDebounce();
  116. }
  117.  
  118.  
  119.  
  120.  
  121. void ButtonDebounce(void)
  122. {
  123.   static byte buttonState[2] = {LOW, LOW}; // the current reading from the input pin
  124.   static byte lastButtonState[2] = {LOW, LOW}; // the previous reading from the input pin
  125.  
  126.  
  127.   static long lastDebounceTime[2] = {0}; // the last time the output pin was toggled
  128.   long debounceDelay = 50; // the debounce time; increase if the output flickers
  129.  
  130.   byte reading[2];
  131.  
  132.   reading[0] = digitalRead(2);
  133.   reading[1] = digitalRead(3);
  134.  
  135.   for (int i = 0; i < 2; i++) {
  136.     if (reading[i] != lastButtonState[i]) {
  137.       // reset the debouncing timer
  138.       lastDebounceTime[i] = millis();
  139.     }
  140.  
  141.     if ((millis() - lastDebounceTime[i]) > debounceDelay) {
  142.      
  143.  
  144.       // if button state changed
  145.       if (reading[i] != buttonState[i]) {
  146.         buttonState[i] = reading[i];
  147.  
  148.         // toggle the LED if the new button state is HIGH
  149.         if (buttonState[i] == HIGH) {
  150.           LED_state[i] = !LED_state[i];
  151.         }
  152.       }
  153.     }
  154.   } // end for() loop
  155.  
  156.   // set the LEDs
  157.   digitalWrite(9, LED_state[0]);
  158.   digitalWrite(5, LED_state[1]);
  159.   digitalWrite(6, LED_state[2]);
  160.   digitalWrite(7, LED_state[3]);
  161.   digitalWrite(8, LED_state[4]);
  162.  
  163.  
  164.   lastButtonState[0] = reading[0];
  165.   lastButtonState[1] = reading[1];
  166. }
  167.  
  168. // Setleds
  169. void SetLEDs(void)
  170. {
  171.   // Main Verlichting (pin 9)
  172.   if (StrContains(HTTP_req, "LED1=1")) {
  173.     LED_state[0] = 1; // save LED state
  174.     digitalWrite(9, HIGH);
  175.   }
  176.   else if (StrContains(HTTP_req, "LED1=0")) {
  177.     LED_state[0] = 0; // save LED state
  178.     digitalWrite(9, LOW);
  179.   }
  180.   // Sub Verlichting (pin 5)
  181.   if (StrContains(HTTP_req, "LED2=1")) {
  182.     LED_state[1] = 1; // save LED state
  183.     digitalWrite(5, HIGH);
  184.   }
  185.   else if (StrContains(HTTP_req, "LED2=0")) {
  186.     LED_state[1] = 0; // save LED state
  187.     digitalWrite(5, LOW);
  188.   }
  189.   // Alarm (pin 6)
  190.   if (StrContains(HTTP_req, "LED3=1")) {
  191.     LED_state[2] = 1; // save LED state
  192.     digitalWrite(6, HIGH);
  193.   }
  194.   else if (StrContains(HTTP_req, "LED3=0")) {
  195.     LED_state[2] = 0; // save LED state
  196.     digitalWrite(6, LOW);
  197.   }
  198.  
  199.   // Rolluik 1(pin 7)
  200.   if (StrContains(HTTP_req, "LED4=1")) {
  201.     LED_state[3] = 1; // save LED state
  202.     digitalWrite(7, HIGH);
  203.   }
  204.   else if (StrContains(HTTP_req, "LED4=0")) {
  205.     LED_state[3] = 0; // save LED state
  206.     digitalWrite(7, LOW);
  207.   }
  208.   // Rolluik 2 (pin 8)
  209.   if (StrContains(HTTP_req, "LED5=1")) {
  210.     LED_state[4] = 1; // save LED state
  211.     digitalWrite(8, HIGH);
  212.   }
  213.   else if (StrContains(HTTP_req, "LED5=0")) {
  214.     LED_state[4] = 0; // save LED state
  215.     digitalWrite(8, LOW);
  216.   }
  217. }
  218.  
  219. // send XML file w/ switch status and LED status
  220. void XML_response(EthernetClient cl)
  221. {
  222.   int analog_val;
  223.   int count;
  224.   int sw_arr[] = {2, 3};
  225.  
  226.   cl.print("<?xml version = \"1.0\" ?>");
  227.   cl.print("<inputs>");
  228.   // checkbox LED states
  229.   // LED1
  230.   cl.print("<led>");
  231.   if (LED_state[0]) {
  232.     cl.print("checked");
  233.   }
  234.   else {
  235.     cl.print("unchecked");
  236.   }
  237.   cl.println("</led>");
  238.  
  239.   // button LED states
  240.   // LED2
  241.   cl.print("<led>");
  242.   if (LED_state[1]) {
  243.     cl.print("on");
  244.   }
  245.   else {
  246.     cl.print("off");
  247.   }
  248.   cl.println("</led>");
  249.  
  250.  
  251.  
  252.   // button LED states
  253.   // LED3
  254.   cl.print("<led>");
  255.   if (LED_state[2]) {
  256.     cl.print("on");
  257.   }
  258.   else {
  259.     cl.print("off");
  260.   }
  261.   cl.println("</led>");
  262.  
  263.  
  264.  
  265.   // button LED states
  266.   // LED4
  267.   cl.print("<led>");
  268.   if (LED_state[3]) {
  269.     cl.print("on");
  270.   }
  271.   else {
  272.     cl.print("off");
  273.   }
  274.   cl.println("</led>");
  275.  
  276.  
  277.  
  278.  
  279.   // button LED states
  280.   // LED5
  281.   cl.print("<led>");
  282.   if (LED_state[4]) {
  283.     cl.print("on");
  284.   }
  285.   else {
  286.     cl.print("off");
  287.   }
  288.   cl.println("</led>");
  289.  
  290.  
  291.  
  292.   cl.print("</inputs>");
  293. }
  294.  
  295. void StrClear(char *str, char length)
  296. {
  297.   for (int i = 0; i < length; i++) {
  298.     str[i] = 0;
  299.   }
  300. }
  301.  
  302.  
  303. char StrContains(char *str, char *sfind)
  304. {
  305.   char found = 0;
  306.   char index = 0;
  307.   char len;
  308.  
  309.   len = strlen(str);
  310.  
  311.   if (strlen(sfind) > len) {
  312.     return 0;
  313.   }
  314.   while (index < len) {
  315.     if (str[index] == sfind[found]) {
  316.       found++;
  317.       if (strlen(sfind) == found) {
  318.         return 1;
  319.       }
  320.     }
  321.     else {
  322.       found = 0;
  323.     }
  324.     index++;
  325.   }
  326.  
  327.   return 0;
  328. }
RAW Paste Data