Advertisement
Guest User

Arduino Ethernetshield SHT11 Home monitor

a guest
Apr 17th, 2010
647
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.90 KB | None | 0 0
  1. /*
  2. * Web Server
  3. * based on the original webserver example.
  4. * processes the GET command from the http client.
  5. * supports two pages, one readable for humans /info.html other in CSV for computers /csv.html
  6. */
  7. /*
  8. *==== Typical HTTP request ====
  9. *GET / HTTP/1.0[CRLF]
  10. *Host: www.google.com[CRLF]
  11. *Connection: close[CRLF]
  12. *User-Agent: Web-sniffer/1.0.29 (+http://web-sniffer.net/)[CRLF]
  13. *Accept-Charset: ISO-8859-1,UTF-8;q=0.7,*;q=0.7[CRLF]
  14. *Cache-Control: no[CRLF]
  15. *Accept-Language: de,en;q=0.7,en-us;q=0.3[CRLF]
  16. *Referer: http://web-sniffer.net/[CRLF]
  17. *[CRLF]
  18. *==== Typical HTTP request ====
  19. */
  20. //#define DEBUG
  21.  
  22. #include <Ethernet.h>
  23. #include <Sensirion.h>
  24.  
  25. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  26. byte ip[] = { 192, 168, 2, 253 };
  27. //These were not needed, i.e. without them the ethernet shield works ok with my router
  28. //byte gateway[] = { 192, 168, 2, 254 };
  29. //byte subnet[] = { 255, 255, 255, 0 };
  30. Server server(80);        /* instance of server */
  31.  
  32. Sensirion tempSensor = Sensirion(5,6); /* instance of sensor */
  33. // Sensor Variables
  34. float temperature;
  35. float humidity;
  36. float dewpoint;
  37. // integer values of temperature, humidity and light
  38. int tmprtr,hmdty,lght;  
  39.  
  40. char cmmnd;  /* received command = {G|P} for GET or PUT */
  41. char args[20];  /* requested file,arguments */
  42. char host[20];  /* host ip address*/
  43.  
  44. void setup()
  45. {
  46.   Ethernet.begin(mac, ip);
  47.   server.begin();
  48.   pinMode(7, OUTPUT);
  49.   pinMode(4, OUTPUT);
  50.   pinMode(3, OUTPUT);
  51.   pinMode(2, OUTPUT);
  52.   digitalWrite(4,LOW);
  53.   digitalWrite(7,HIGH);    // could shut down between conversions...
  54.  
  55. #ifdef DEBUG
  56.   Serial.begin(9600);      // serial port for debug
  57. #endif
  58. }
  59. //
  60. // Get and convert analog input values
  61. //
  62. void get_values(void)
  63. {
  64.   tempSensor.measure(&temperature, &humidity, &dewpoint);
  65.   tmprtr=int(temperature * 10.0);
  66.   hmdty=int(humidity * 10.0);
  67.   lght = analogRead(0);
  68. }
  69.  
  70. //
  71. // Get and convert analog input values
  72. //
  73. void beep(void)
  74. {
  75.   for (int i=0;i<10;i++){
  76.     digitalWrite(2,LOW);
  77.     digitalWrite(3,HIGH);
  78.     delay(1);
  79.     digitalWrite(3,LOW);
  80.     digitalWrite(2,HIGH);
  81.     delay(1);
  82.   }
  83.   digitalWrite(2,LOW);
  84. }
  85.  
  86. //
  87. // Begin of HTML page, fixed page title
  88. //
  89. void http_head(Client & client)
  90. {
  91.   client.println("HTTP/1.1 200 OK");
  92.   client.println("Content-Type: text/html");
  93.   client.println();
  94.   client.println("<html>");
  95.   client.println("<title>Arduino Home monitor</title>");
  96.   client.println("<body>");
  97. }
  98.  
  99. //
  100. // End of HTML page
  101. //
  102. void http_end(Client & client)
  103. {
  104.   client.println("</html>");
  105.   client.println("</body>");
  106. }
  107. //
  108. // reply to root request or /info.html
  109. //
  110. void http_root(Client & client)
  111. {
  112.   http_head(client);
  113.   client.print("Temperature is ");
  114.   client.print(tmprtr / 10);
  115.   client.print(".");
  116.   client.print(tmprtr % 10);
  117.   client.print("&deg C");
  118.   client.println("<br />");
  119.   client.print("Relative Humidity is ");
  120.   client.print(hmdty / 10);
  121.   client.print(".");
  122.   client.print(hmdty % 10);
  123.   client.print("%");
  124.   client.println("<br />");
  125.   client.print("Light is ");
  126.   client.print(lght);
  127.   client.println("<br />");
  128.   http_end(client);
  129. }
  130. //
  131. // reply to /csv.html
  132. //
  133. void http_csv(Client & client)
  134. {
  135.   client.println("HTTP/1.1 200 OK");
  136.   client.println("Content-Type: text/csv");
  137.   client.println();
  138.   client.print(tmprtr / 10);
  139.   client.print(".");
  140.   client.print(tmprtr % 10);
  141.   client.print(",");
  142.   client.print(hmdty / 10);
  143.   client.print(".");
  144.   client.print(hmdty % 10);
  145.   client.print(",");
  146.   client.print(lght);
  147.   client.println();
  148. }
  149. //
  150. // Work on a http reply, check request file, produce output
  151. //
  152. void http_reply(Client & client)
  153. {
  154.   #ifdef DEBUG
  155.   Serial.println("==== debug out ===");
  156.   Serial.println(cmmnd);
  157.   Serial.println(args);
  158.   Serial.println(host);
  159.   Serial.println("==== debug eot ===");
  160.   #endif
  161.   get_values();
  162.   beep();
  163.   if ((args[1]=='c') && (args[2]=='s') && (args[3]=='v')) http_csv(client);
  164.   else http_root(client);
  165. }
  166.  
  167. void loop()
  168. {
  169.   Client client = server.available();
  170.   #define LN_BUF 40
  171.   char inp_ln[LN_BUF]; /* input line, crop at 40 chars */
  172.   char chr;        /* input char */
  173.   byte inp_ln_ptr,i;
  174.  
  175.   if (client) {
  176.   inp_ln_ptr=0; /* line is empty */
  177.  
  178.   while (client.connected()) { /* while client is connected process lines */
  179.    if (client.available()) { /* is there a char available */
  180.      chr = client.read(); /* get it */
  181.        if (chr == '\n' && inp_ln_ptr<2) {
  182.           http_reply(client); /* received a blank line, create return page */
  183.           break;        /* exit while, !! find a more logical way to do this */
  184.         }
  185.         if (chr == '\n') { /* end of line */
  186.           inp_ln[inp_ln_ptr]=0; /* end of line */
  187.           Serial.println(inp_ln);
  188.           if ((inp_ln[0]=='G') && (inp_ln[1]=='E') && (inp_ln[2]=='T')) {
  189.             cmmnd='G';
  190.             // get arguments          
  191.             for (i=0;i<sizeof(args);i++) {
  192.               args[i]=inp_ln[4+i];
  193.               if (args[i] == ' ') break;
  194.             }
  195.           }
  196.          
  197.           if ((inp_ln[0]=='P') && (inp_ln[1]=='U') && (inp_ln[2]=='T')) {
  198.             cmmnd='P';
  199.             // get arguments          
  200.             for (i=0;i<sizeof(args);i++) {
  201.               args[i]=inp_ln[4+i];
  202.               if (args[i] == ' ') break;
  203.             }
  204.           }
  205.           if ((inp_ln[0]=='H') && (inp_ln[1]=='o') && (inp_ln[2]=='s') && (inp_ln[3]=='t')) {
  206.             // get arguments          
  207.             for (i=0;i<sizeof(host);i++) {
  208.               host[i]=inp_ln[5+i];
  209.               if ((host[i] == ' ') || (host[i] =='\n') ||(host[i] =='\r')) {
  210.                 host[i]='\0';
  211.                 break;
  212.               }
  213.             }
  214.           }
  215.           inp_ln_ptr=0;  /* ptr ready for next line */
  216.         }
  217.         else if (inp_ln_ptr<LN_BUF-1) inp_ln[inp_ln_ptr++]=chr;
  218.       }
  219.     }
  220.     // give the web browser time to receive the data
  221.     delay(1);
  222.     client.stop();
  223.   }
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement