Advertisement
safwan092

final-test-homeautomation-ethernet

Dec 26th, 2017
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.86 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <Ethernet.h>
  3. #include <SD.h>
  4. #include <Servo.h>
  5. #include "DHT.h"
  6.  
  7.  
  8. // size of buffer used to capture HTTP requests
  9. #define REQ_BUF_SZ   60
  10.  
  11. // MAC address from Ethernet shield sticker under board
  12. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  13. IPAddress ip(192, 168, 0, 20); // IP address, may need to change depending on network
  14. EthernetServer server(80);  // create a server at port 80
  15. File webFile;               // the web page file on the SD card
  16. char HTTP_req[REQ_BUF_SZ] = {0}; // buffered HTTP request stored as null terminated string
  17. char req_index = 0;              // index into HTTP_req buffer
  18.  
  19.  
  20. boolean LED_state[4] = {0}; // stores the states of the LEDs
  21.  
  22.  
  23.  
  24. DHT dht;
  25.  
  26. Servo servo;
  27. int angle = 10;
  28.  
  29. void setup()
  30. {
  31.  
  32.  
  33.   dht.setup(14); // data pin 2
  34.  
  35.   // disable Ethernet chip
  36.   pinMode(10, OUTPUT);
  37.   digitalWrite(10, HIGH);
  38.  
  39.   Serial.begin(9600);       // for debugging
  40.  
  41.   // initialize SD card
  42.   Serial.println("Initializing SD card...");
  43.   if (!SD.begin(4)) {
  44.     Serial.println("ERROR - SD card initialization failed!");
  45.     return;    // init failed
  46.   }
  47.   Serial.println("SUCCESS - SD card initialized.");
  48.   // check for index.htm file
  49.   if (!SD.exists("index.htm")) {
  50.     Serial.println("ERROR - Can't find index.htm file!");
  51.     return;  // can't find index file
  52.   }
  53.   Serial.println("SUCCESS - Found index.htm file.");
  54.   // switches on pins 2, 3 and 5
  55.   pinMode(2, INPUT);
  56.   pinMode(3, INPUT);
  57.   pinMode(5, INPUT);
  58.   // LEDs
  59.   pinMode(6, OUTPUT);
  60.   servo.attach(7);
  61.   servo.write(10);
  62.   //pinMode(7, OUTPUT);
  63.   pinMode(8, OUTPUT);
  64.   pinMode(9, OUTPUT);
  65.  
  66.   Ethernet.begin(mac, ip);  // initialize Ethernet device
  67.   server.begin();           // start to listen for clients
  68. }
  69.  
  70. void loop()
  71. {
  72.  
  73.   EthernetClient client = server.available();  // try to get client
  74.  
  75.   if (client) {  // got client?
  76.     boolean currentLineIsBlank = true;
  77.     while (client.connected()) {
  78.       if (client.available()) {   // client data available to read
  79.         char c = client.read(); // read 1 byte (character) from client
  80.         // limit the size of the stored received HTTP request
  81.         // buffer first part of HTTP request in HTTP_req array (string)
  82.         // leave last element in array as 0 to null terminate string (REQ_BUF_SZ - 1)
  83.         if (req_index < (REQ_BUF_SZ - 1)) {
  84.           HTTP_req[req_index] = c;          // save HTTP request character
  85.           req_index++;
  86.         }
  87.         // last line of client request is blank and ends with \n
  88.         // respond to client only after last line received
  89.         if (c == '\n' && currentLineIsBlank) {
  90.           // send a standard http response header
  91.           client.println("HTTP/1.1 200 OK");
  92.           // remainder of header follows below, depending on if
  93.           // web page or XML page is requested
  94.           // Ajax request - send XML file
  95.           if (StrContains(HTTP_req, "ajax_inputs")) {
  96.             // send rest of HTTP header
  97.             client.println("Content-Type: text/xml");
  98.             client.println("Connection: keep-alive");
  99.             client.println();
  100.             SetLEDs();
  101.             // send XML file containing input states
  102.             XML_response(client);
  103.           }
  104.           else {  // web page request
  105.             // send rest of HTTP header
  106.             client.println("Content-Type: text/html");
  107.             client.println("Connection: keep-alive");
  108.             client.println();
  109.             // send web page
  110.             webFile = SD.open("index.htm");        // open web page file
  111.             if (webFile) {
  112.               while (webFile.available()) {
  113.                 client.write(webFile.read()); // send web page to client
  114.               }
  115.               webFile.close();
  116.             }
  117.           }
  118.           // display received HTTP request on serial port
  119.           Serial.print(HTTP_req);
  120.           // reset buffer index and all buffer elements to 0
  121.           req_index = 0;
  122.           StrClear(HTTP_req, REQ_BUF_SZ);
  123.           break;
  124.         }
  125.         // every line of text received from the client ends with \r\n
  126.         if (c == '\n') {
  127.           // last character on line of received text
  128.           // starting new line with next character read
  129.           currentLineIsBlank = true;
  130.         }
  131.         else if (c != '\r') {
  132.           // a text character was received from client
  133.           currentLineIsBlank = false;
  134.         }
  135.       } // end if (client.available())
  136.     } // end while (client.connected())
  137.     delay(1);      // give the web browser time to receive the data
  138.     client.stop(); // close the connection
  139.   } // end if (client)
  140. }
  141.  
  142. // checks if received HTTP request is switching on/off LEDs
  143. // also saves the state of the LEDs
  144. void SetLEDs(void)
  145. {
  146.   // LED 1 (pin 6)
  147.   if (StrContains(HTTP_req, "LED1=1")) {
  148.     LED_state[0] = 1;  // save LED state
  149.     digitalWrite(6, HIGH);
  150.   }
  151.   else if (StrContains(HTTP_req, "LED1=0")) {
  152.     LED_state[0] = 0;  // save LED state
  153.     digitalWrite(6, LOW);
  154.   }
  155.   // LED 2 (pin 7)
  156.   if (StrContains(HTTP_req, "LED2=1")) {
  157.     LED_state[1] = 1;  // save LED state
  158.     servo.write(100);
  159.     //digitalWrite(7, HIGH);
  160.   }
  161.   else if (StrContains(HTTP_req, "LED2=0")) {
  162.     LED_state[1] = 0;  // save LED state
  163.     servo.write(10);
  164.     //digitalWrite(7, LOW);
  165.   }
  166.   // LED 3 (pin 8)
  167.   if (StrContains(HTTP_req, "LED3=1")) {
  168.     LED_state[2] = 1;  // save LED state
  169.     digitalWrite(8, HIGH);
  170.   }
  171.   else if (StrContains(HTTP_req, "LED3=0")) {
  172.     LED_state[2] = 0;  // save LED state
  173.     digitalWrite(8, LOW);
  174.   }
  175.   // LED 4 (pin 9)
  176.   if (StrContains(HTTP_req, "LED4=1")) {
  177.     LED_state[3] = 1;  // save LED state
  178.     digitalWrite(9, HIGH);
  179.   }
  180.   else if (StrContains(HTTP_req, "LED4=0")) {
  181.     LED_state[3] = 0;  // save LED state
  182.     digitalWrite(9, LOW);
  183.   }
  184. }
  185.  
  186. // send the XML file with analog values, switch status
  187. //  and LED status
  188. void XML_response(EthernetClient cl)
  189. {
  190.   int analog_val;            // stores value read from analog inputs
  191.   int count;                 // used by 'for' loops
  192.   int sw_arr[] = {2, 3, 5};  // pins interfaced to switches
  193.   int  humidity = dht.getHumidity();
  194.   int  temperature = dht.getTemperature();
  195.   //  delay(dht.getMinimumSamplingPeriod());
  196.   cl.print("<?xml version = \"1.0\" ?>");
  197.   cl.print("<inputs>");
  198.   // read analog inputs
  199.   for (count = 2; count <= 2; count++) { // A2 to A5
  200.     cl.print("<analog>");
  201.     cl.print(humidity);
  202.     cl.println("%</analog>");
  203.   }
  204.   for (count = 2; count <= 2; count++) { // A2 to A5
  205.     cl.print("<analog>");
  206.     cl.print(temperature);
  207.     cl.println("'C</analog>");
  208.   }
  209.  
  210.  
  211.   // button LED states
  212.   // LED1
  213.   cl.print("<LED>");
  214.   if (LED_state[0]) {
  215.     cl.print("on");
  216.   }
  217.   else {
  218.     cl.print("off");
  219.   }
  220.   cl.println("</LED>");
  221.   // LED2
  222.   cl.print("<LED>");
  223.   if (LED_state[1]) {
  224.     cl.print("on");
  225.   }
  226.   else {
  227.     cl.print("off");
  228.   }
  229.   cl.println("</LED>");
  230.   // LED3
  231.   cl.print("<LED>");
  232.   if (LED_state[2]) {
  233.     cl.print("on");
  234.   }
  235.   else {
  236.     cl.print("off");
  237.   }
  238.   cl.println("</LED>");
  239.   // LED4
  240.   cl.print("<LED>");
  241.   if (LED_state[3]) {
  242.     cl.print("on");
  243.   }
  244.   else {
  245.     cl.print("off");
  246.   }
  247.   cl.println("</LED>");
  248.  
  249.   cl.print("</inputs>");
  250. }
  251.  
  252. // sets every element of str to 0 (clears array)
  253. void StrClear(char *str, char length)
  254. {
  255.   for (int i = 0; i < length; i++) {
  256.     str[i] = 0;
  257.   }
  258. }
  259.  
  260. // searches for the string sfind in the string str
  261. // returns 1 if string found
  262. // returns 0 if string not found
  263. char StrContains(char *str, char *sfind)
  264. {
  265.   char found = 0;
  266.   char index = 0;
  267.   char len;
  268.  
  269.   len = strlen(str);
  270.  
  271.   if (strlen(sfind) > len) {
  272.     return 0;
  273.   }
  274.   while (index < len) {
  275.     if (str[index] == sfind[found]) {
  276.       found++;
  277.       if (strlen(sfind) == found) {
  278.         return 1;
  279.       }
  280.     }
  281.     else {
  282.       found = 0;
  283.     }
  284.     index++;
  285.   }
  286.  
  287.   return 0;
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement