Advertisement
Guest User

Arduino

a guest
Sep 1st, 2013
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.02 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <Ethernet.h>
  3. #include <SD.h>
  4. #include <OneWire.h>
  5. #include <DallasTemperature.h>
  6. #include "DHT.h"
  7.  
  8. // size of buffer used to capture HTTP requests
  9. #define REQ_BUF_SZ   20
  10. #define BUFSIZ 100
  11.  
  12. // MAC address from Ethernet shield sticker under board
  13. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  14. IPAddress ip(192, 168, 0, 200);   // IP address, may need to change depending on network
  15. EthernetServer server(80);       // create a server at port 80
  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. //Temperatures variables
  20. float waterInTemp;
  21. float waterOutTemp;
  22. float airInTemp;
  23. float airOutTemp;
  24. float airInHumidity;
  25. float airOutHumidity;
  26. long lastTime=0;
  27.  
  28. #define TEMP_DELAY 5000 //Delay between two temperatures in ms
  29.  
  30. #define WATER_IN_BUS 2
  31. #define WATER_OUT_BUS 3
  32. #define AIR_IN_BUS 5
  33. #define AIR_OUT_BUS 6
  34. #define DHTTYPE DHT22
  35. // Setup oneWire instance to communicate with devices
  36. OneWire waterInWire(WATER_IN_BUS);
  37. OneWire waterOutWire(WATER_OUT_BUS);
  38. // Pass oneWire reference to Dallas Temperature.
  39. DallasTemperature sensorWaterIn(&waterInWire);
  40. DallasTemperature sensorWaterOut(&waterOutWire);
  41. DHT sensorAirIn(AIR_IN_BUS, DHTTYPE);
  42. DHT sensorAirOut(AIR_OUT_BUS, DHTTYPE);
  43.  
  44.  
  45. void setup()
  46. {
  47.     // disable Ethernet chip
  48.     pinMode(10, OUTPUT);
  49.     digitalWrite(10, HIGH);
  50.    
  51.     Serial.begin(9600);       // for debugging
  52.    
  53.     // initialize SD card
  54.     Serial.println("Initializing SD card...");
  55.     if (!SD.begin(4)) {
  56.         Serial.println("ERROR - SD card initialization failed!");
  57.         return;    // init failed
  58.     }
  59.     Serial.println("SUCCESS - SD card initialized.");
  60.  
  61.     //Initialize Ethernet
  62.     Ethernet.begin(mac, ip);  // initialize Ethernet device
  63.     server.begin();           // start to listen for clients
  64.    
  65.     //Initialize Sensors
  66.     sensorWaterIn.begin();
  67.     sensorWaterOut.begin();
  68.     sensorAirIn.begin();
  69.     sensorAirOut.begin();
  70. }
  71.  
  72. void loop()
  73. {
  74.   if(TempTimer())
  75.   {
  76.       GetTemperatures();
  77.       WriteTemperatures();
  78.   }
  79.   Internet();
  80. }
  81.  
  82. //Get temperatures
  83. void GetTemperatures()
  84. {
  85.     sensorWaterIn.requestTemperatures();
  86.     waterInTemp = (sensorWaterIn.getTempCByIndex(0));
  87.  
  88.     sensorWaterOut.requestTemperatures();
  89.     waterOutTemp = (sensorWaterOut.getTempCByIndex(0));
  90.  
  91.     airInHumidity = sensorAirIn.readHumidity();
  92.     airInTemp = sensorAirIn.readTemperature();
  93.  
  94.     airOutHumidity = sensorAirOut.readHumidity();
  95.     airOutTemp = sensorAirOut.readTemperature();
  96. }
  97.  
  98. //Write temperatures
  99. void WriteTemperatures()
  100. {
  101.     File f = SD.open("logh.txt", FILE_WRITE);
  102.     f.print(millis());
  103.     f.print(",");
  104.     f.print(waterInTemp);
  105.     f.print(",");
  106.     f.print(waterOutTemp);
  107.     f.print(",");
  108.     f.print(airInTemp);
  109.     f.print(",");
  110.     f.print(airOutTemp);
  111.     f.print(",");
  112.     f.print(airInHumidity);
  113.     f.print(",");
  114.     f.println(airOutHumidity);
  115.     f.close();
  116. }
  117.  
  118. //Ethernet
  119. void Internet()
  120. {
  121.     char clientline[BUFSIZ];
  122.     int index = 0;
  123.  
  124.     EthernetClient client = server.available();  // try to get client
  125.     if (client) {  // got client?
  126.         boolean currentLineIsBlank = true;
  127.         index = 0;
  128.             while (client.connected()) {
  129.                 if (client.available()) {   // client data available to read      
  130.                     char c = client.read();
  131.                     // If it isn't a new line, add the character to the buffer
  132.                     if (c != '\n' && c != '\r') {
  133.                         clientline[index] = c;
  134.                         index++;
  135.                
  136.                         // are we too big for the buffer? start tossing out data
  137.                         if (index >= BUFSIZ)
  138.                             index = BUFSIZ -1;
  139.                         // continue to read more data!
  140.                         continue;
  141.                     }
  142.              
  143.                     // got a \n or \r new line, which means the string is done
  144.                     clientline[index] = 0;
  145.              
  146.                     // Print it out for debugging
  147.                     Serial.print("clientline : ");
  148.                     Serial.println(clientline);
  149.              
  150.              
  151.                     ///////////////////////
  152.                     // If we want to go to ROOT
  153.                     ///////////////////////
  154.                     if (strstr(clientline, "GET / ") != 0) {
  155.                         client.println("HTTP/1.1 200 OK");
  156.                         client.println("Content-Type: text/html");
  157.                         client.println();
  158.                         client.println("<h2>Please go <a href='index.htm'>here</a></h2>");
  159.                     }
  160.              
  161.                     ///////////////////////
  162.                     // If we want a specific file
  163.                     ///////////////////////
  164.                     else if (strstr(clientline, "GET /") != 0) {
  165.                         char *filename;
  166.                         filename = clientline + 5; // look after the "GET /" (5 chars)
  167.                         // a little trick, look for the " HTTP/1.1" string and
  168.                         // turn the first character of the substring into a 0 to clear it out.
  169.                         (strstr(clientline, " HTTP"))[0] = 0;
  170.                  
  171.                         File file = SD.open(filename);
  172.                         if (!file) {
  173.                             Serial.println("Error : file not found !");
  174.                             client.println("HTTP/1.1 404 Not Found");
  175.                             client.println("Content-Type: text/html");
  176.                             client.println();
  177.                             client.println("<h2>File Not Found 1!</h2>");
  178.                             file.close();
  179.                             break;
  180.                         }
  181.                         else {
  182.                             Serial.println("Opened!");
  183.                             client.println("HTTP/1.1 200 OK");
  184.                    
  185.                             //Set appropriate mime type based on file extension
  186.                             String fname = String(filename);
  187.                             int dotat = fname.lastIndexOf('.');
  188.                             String extension = String("");
  189.                             if(dotat > -1){
  190.                                 extension = String(fname.substring(dotat+1));
  191.                             }
  192.                             //client.println("Access-Control-Allow-Origin: *");
  193.                             if(extension == "js"){
  194.                                 client.println("Content-Type: application/x-javascript");
  195.                             }
  196.                             /*else if (extension == "txt"){
  197.                                 client.println("Content-Type: text/plain");
  198.                             }*/
  199.                             else {
  200.                                 client.println("Content-Type: text/html");
  201.                             }
  202.                             client.println();
  203.                    
  204.                             if (file) {
  205.                                 while(file.available()) {
  206.                                     client.write(file.read());
  207.                                 }
  208.                                 file.close();
  209.                             }
  210.                             break;
  211.                         }
  212.                     }
  213.                 ///////////////////////
  214.                 // Everything else is a 404
  215.                 ///////////////////////
  216.                 else {
  217.                     // everything else is a 404
  218.                     client.println("HTTP/1.1 404 Not Found");
  219.                     client.println("Content-Type: text/html");
  220.                     client.println();
  221.                     client.println("<h2>File Not Found 2!</h2>");
  222.                 }
  223.                 break;
  224.             } // end "if (client.available())"
  225.         } // end while (client.connected())
  226.         delay(1);      // give the web browser time to receive the data
  227.         client.stop(); // close the connection
  228.     } // end "if (client)"
  229. }
  230.  
  231.  
  232. boolean TempTimer()
  233. {
  234.     if (millis()-lastTime >= TEMP_DELAY)
  235.     {
  236.         lastTime=millis();
  237.         return true;
  238.     }
  239.     return false;
  240. }
  241. // sets every element of str to 0 (clears array)
  242. void StrClear(char *str, char length)
  243. {
  244.     for (int i = 0; i < length; i++) {
  245.         str[i] = 0;
  246.     }
  247. }
  248.  
  249. // searches for the string sfind in the string str; returns 1 if string found; return 0 if not found
  250. char StrContains(char *str, char *sfind)
  251. {
  252.     char found = 0;
  253.     char index = 0;
  254.     char len;
  255.  
  256.     len = strlen(str);
  257.    
  258.     if (strlen(sfind) > len) {
  259.         return 0;
  260.     }
  261.     while (index < len) {
  262.         if (str[index] == sfind[found]) {
  263.             found++;
  264.             if (strlen(sfind) == found) {
  265.                 return 1;
  266.             }
  267.         }
  268.         else {
  269.             found = 0;
  270.         }
  271.         index++;
  272.     }
  273.  
  274.     return 0;
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement