Want more features on Pastebin? Sign Up, it's FREE!
Guest

roachmx

By: a guest on Feb 7th, 2011  |  syntax: C  |  size: 3.33 KB  |  views: 96  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <SPI.h>
  2. #include <Ethernet.h>
  3. #include <SHT1x.h>
  4.  
  5. #define dataPin  2
  6. #define clockPin 3
  7. SHT1x sht1x(dataPin, clockPin);
  8.  
  9. byte mac[] = {
  10.   0x90, 0xA2, 0xD2, 0x00, 0x17, 0xAA};
  11. byte ip[] = {
  12.   192,168,1,20};
  13. byte gateway[] = {
  14.   192,168,1,254};      
  15. byte subnet[] = {
  16.   255,255,255,0};
  17. byte server[] = {
  18.   173,203,98,29};
  19.  
  20. Client client(server, 80);
  21.  
  22. long lastConnectionTime = 0;
  23. boolean lastConnected = false;
  24. const int postingInterval = 10000;
  25.  
  26. void setup()
  27. {
  28.   Ethernet.begin(mac, ip, gateway, subnet);
  29.   Serial.begin(9600);
  30.   delay(1000);
  31.   Serial.println("Starting up");
  32. }
  33.  
  34. void loop()
  35. {
  36.   float temp_c;
  37.   float temp_f;
  38.   float humidity;
  39.  
  40.   temp_c = sht1x.readTemperatureC();
  41.   temp_f = sht1x.readTemperatureF();
  42.   humidity = sht1x.readHumidity();
  43.  
  44.   /*
  45.   Serial.print("Temperature: ");
  46.    Serial.print(temp_c, DEC);
  47.    Serial.print("C / ");
  48.    Serial.print(temp_f, DEC);
  49.    Serial.print("F. Humidity: ");
  50.    Serial.print(humidity);
  51.    Serial.println("%");
  52.    delay(2000);
  53.    */
  54.  
  55.   char centigrados[20]="";
  56.   ftoa(centigrados,temp_c,3);
  57.   char faren[20]="";
  58.   ftoa(faren,temp_f,3);
  59.   char humedad[20]="";
  60.   ftoa(humedad,humidity,3);
  61.  
  62.   String dataString = String(centigrados);
  63.   dataString += ",";
  64.   dataString += String(faren);
  65.   dataString += ",";
  66.   dataString += String(humedad);
  67.  
  68.   if (client.available()) {
  69.     char c = client.read();
  70.     //   Serial.print(c);
  71.   }
  72.   if (!client.connected() && lastConnected) {
  73.     Serial.println();
  74.     Serial.println("disconnecting.");
  75.     client.stop();
  76.   }
  77.  
  78.   // if you're not connected, and ten seconds have passed since
  79.   // your last connection, then connect again and send data:
  80.   if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
  81.     sendData(dataString);
  82.   }
  83.   // store the state of the connection for next time through
  84.   // the loop:
  85.   lastConnected = client.connected();
  86. }
  87.  
  88. void sendData(String thisData) {
  89.   // if there's a successful connection:
  90.   if (client.connect()) {
  91.     Serial.println("connecting...");
  92.     // send the HTTP PUT request.
  93.     // fill in your feed address here:
  94.     client.print("PUT /api/13310.csv HTTP/1.1\n");
  95.     client.print("Host: www.pachube.com\n");
  96.     // fill in your Pachube API key here:
  97.     client.print("X-PachubeApiKey: ____________________________________________________________ \n");
  98.     client.print("Content-Length: ");
  99.     client.println(thisData.length(), DEC);
  100.  
  101.     // last pieces of the HTTP PUT request:
  102.     client.print("Content-Type: text/csv\n");
  103.     client.println("Connection: close\n");
  104.  
  105.     // here's the actual content of the PUT request:
  106.     client.println(thisData);
  107.     Serial.println(thisData);
  108.  
  109.     // note the time that the connection was made:
  110.     lastConnectionTime = millis();
  111.   }
  112.   else {
  113.     // if you couldn't make a connection:
  114.     Serial.println("connection failed");
  115.   }
  116. }
  117. // Float support is hard on arduinos
  118. // (http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1164927646)
  119. char *ftoa(char *a, double f, int precision)
  120. {
  121.   long p[] = {
  122.     0,10,100,1000,10000,100000,1000000,10000000,100000000                    };
  123.  
  124.   char *ret = a;
  125.   long heiltal = (long)f;
  126.   itoa(heiltal, a, 10);
  127.   while (*a != '\0') a++;
  128.   *a++ = '.';
  129.   long desimal = abs((long)((f - heiltal) * p[precision]));
  130.   itoa(desimal, a, 10);
  131.   return ret;
  132. }
clone this paste RAW Paste Data