Advertisement
bld

Untitled

bld
Dec 26th, 2011
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.84 KB | None | 0 0
  1. //SD card stuff
  2. #include <SD.h>
  3. File myFile;
  4.  
  5. //Ethernet stuff
  6. #include <SPI.h>
  7. #include <Ethernet.h>
  8. byte mac[] = {
  9.   0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEB };
  10. byte ip[] = {
  11.   192,168,1, 87 };
  12. Server server(80);
  13.  
  14. //EEPROM stuff
  15. #include <EEPROM.h>
  16.  
  17. /*
  18. 0   = Trigger humidity
  19.  1   = Minimum run time
  20.  2   = Air quality
  21.  3-4 = Schedule Start
  22.  5-6 = Schedule Stop
  23.  7   = Schedule Using fan 1
  24.  8   = Schedule Using fan 2
  25.  9   = PWM min fan 1
  26.  10  = PWM max fan 1
  27.  11  = PWM min fan 2
  28.  12  = PWM max fan 2
  29.  13  = Manual fan 1
  30.  14  = Manual fan 2
  31.  */
  32.  
  33.  
  34. //Stuff used to process the page and url
  35. String inString = String(150);
  36. boolean process = false;
  37.  
  38. void setup()
  39. {
  40.   Ethernet.begin(mac, ip);
  41.   server.begin();
  42.  
  43.   pinMode(10, OUTPUT); //Must be set to output for SD to work
  44.  
  45.  
  46.   Serial.begin(9600);
  47.  
  48.   if (!SD.begin(4))
  49.   {
  50.     /*
  51.     client.println("Please insert SD card.");
  52.      break;
  53.      */
  54.   }
  55. }
  56.  
  57. void loop()
  58. {
  59.   // listen for incoming clients
  60.   Client client = server.available();
  61.   if (client)
  62.   {
  63.     // an http request ends with a blank line
  64.     boolean currentLineIsBlank = true;
  65.  
  66.     while (client.connected())
  67.     {
  68.       if (client.available())
  69.       {
  70.         char c = client.read();
  71.  
  72.         if (inString.length() < 150)
  73.         {
  74.           inString += c;
  75.         }
  76.  
  77.         if (c == '\n' && currentLineIsBlank)
  78.         {
  79.           // send a standard http response header
  80.           client.println("HTTP/1.1 200 OK");
  81.           client.println("Content-Type: text/html");
  82.           client.println();
  83.           break;
  84.         }
  85.         if (c == '\n')
  86.         {
  87.           // you're starting a new line
  88.           currentLineIsBlank = true;
  89.         }
  90.         else if (c != '\r')
  91.         {
  92.           // you've gotten a character on the current line
  93.           currentLineIsBlank = false;
  94.         }
  95.       }
  96.     }
  97.  
  98.     if (inString.indexOf('GET /favicon.ico') == 10) //Do nothing about the .ico request
  99.     {
  100.       client.stop();
  101.       inString = "";
  102.       return;
  103.     }
  104.  
  105.     char* filename;
  106.  
  107.     if (inString.indexOf("GET / ") != -1)
  108.     {
  109.       Serial.println("Using default file");
  110.       filename = "index.htm";
  111.     }
  112.     else if (inString.indexOf("GET /dynamic.data") != -1)
  113.     {
  114.       client.print(analogRead(A0));
  115.       client.print(",");
  116.       client.print(analogRead(A1));
  117.       client.print(",");
  118.       client.print(analogRead(A2));
  119.       client.print(",");
  120.       client.print(analogRead(A3));
  121.       client.print(",");
  122.       client.println(analogRead(A4));
  123.       delay(1);
  124.       client.stop();
  125.       inString = "";
  126.       return;
  127.     }
  128.     else if (inString.indexOf("Save=Save") != -1)
  129.     {
  130.       /*
  131.       0   = Trigger humidity
  132.        1   = Minimum run time
  133.        2   = Air quality
  134.        3-4 = Schedule Start
  135.        5-6 = Schedule Stop
  136.        7   = Schedule Using fan 1
  137.        8   = Schedule Using fan 2
  138.        9   = PWM min fan 1
  139.        10  = PWM max fan 1
  140.        11  = PWM min fan 2
  141.        12  = PWM max fan 2
  142.        13  = Manual fan 1
  143.        14  = Manual fan 2
  144.        */
  145.       eeWrite(0, getFunc("humidityTarget"));
  146.       eeWrite(1, getFunc("minRunTime"));
  147.       eeWrite(2, getFunc("airQuality"));
  148.     }
  149.  
  150.     if (strlen(filename) <= 1)
  151.     {
  152.       filename = "index.htm";
  153.     }
  154.  
  155.     myFile = SD.open(filename);
  156.     if (myFile)
  157.     {
  158.       char temp[5];
  159.       String output;
  160.       int16_t c;
  161.       while ((c = myFile.read()) > 0)
  162.       {
  163.         itoa(c, (char*)temp, 10);
  164.         output += temp;
  165.        
  166.         if (c == '\r' || c == '\n')
  167.         {
  168.           client.print(output);
  169.           output = "";
  170.         }
  171.  
  172.         //client.print((char)c);
  173.       }
  174.       myFile.close();            
  175.     }
  176.     else
  177.     {
  178.       // if the file didn't open, print an error:
  179.       client.println("Could not find file \"");
  180.       client.println(filename);
  181.       client.println("\"");
  182.     }
  183.  
  184.     // give the web browser time to receive the data
  185.     delay(1);
  186.     // close the connection:
  187.     client.stop();
  188.  
  189.     inString = "";
  190.   }
  191. }
  192.  
  193. int getFunc(char* func)
  194. {
  195.   char* output;
  196.  
  197.   String temp = String(35);
  198.  
  199.   if(inString.indexOf(func) != -1)
  200.   {
  201.     int cStart = inString.indexOf("humidityTarget");
  202.     temp = inString.substring(cStart+14);
  203.     int cStop = temp.indexOf("&");
  204.  
  205.     String getPinDel = temp.substring(1, cStop);
  206.     char tempNum[10];
  207.     tempNum[getPinDel.length() + 1]; // Temp save pin value
  208.     getPinDel.toCharArray(tempNum, 10);
  209.  
  210.     return atoi(tempNum);
  211.   }
  212.   else
  213.   {
  214.     return -1;
  215.   }
  216. }
  217.  
  218. int eeRead(int id)
  219. {
  220.   EEPROM.read(id);
  221. }
  222.  
  223. void eeWrite(int id, int value)
  224. {
  225.   if (EEPROM.read(id) != value && value >= 0) //Check if it is needed to update the current value, if value is less than zero, it means it was not found in the url, and should not be attempted saved
  226.   {
  227.     EEPROM.write(id, value);
  228.   }
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement