Advertisement
Guest User

Pachube sensor client

a guest
Jan 22nd, 2012
8,143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.27 KB | None | 0 0
  1. /*
  2.   Pachube sensor client
  3.  
  4.  This sketch connects an analog sensor to Pachube (http://www.pachube.com)
  5.  using a Wiznet Ethernet shield. You can use the Arduino Ethernet shield, or
  6.  the Adafruit Ethernet shield, either one will work, as long as it's got
  7.  a Wiznet Ethernet module on board.
  8.  
  9.  Circuit:
  10.  * Analog sensor attached to analog in 0
  11.  * Ethernet shield attached to pins 10, 11, 12, 13
  12.  
  13.  created 15 March 2010
  14.  updated 4 Sep 2010
  15.  by Tom Igoe
  16.  
  17.  http://www.tigoe.net/pcomp/code/category/arduinowiring/873
  18.  This code is in the public domain.
  19.  
  20.  */
  21.  
  22. #include <SPI.h>
  23. #include <Ethernet.h>
  24. #include <OneWire.h>
  25.  
  26. int DS18S20_Pin = 9; //DS18S20 Signal pin on digital 9
  27.  
  28. //Temperature chip i/o
  29. OneWire ds(DS18S20_Pin); // on digital pin 2
  30.  
  31. // assign a MAC address for the ethernet controller.
  32. // fill in your address here:
  33. byte mac[] = {
  34.   0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
  35. // assign an IP address for the controller:
  36. byte ip[] = {
  37.   192,169,1,20 };
  38. byte gateway[] = {
  39.   192,168,1,1};
  40. byte subnet[] = {
  41.   255, 255, 255, 0 };
  42.  
  43. //  The address of the server you want to connect to (pachube.com):
  44. byte server[] = {
  45.   209,40,205,190 };
  46.  
  47. // initialize the library instance:
  48. Client client(server, 80);
  49.  
  50. long lastConnectionTime = 0;        // last time you connected to the server, in milliseconds
  51. boolean lastConnected = false;      // state of the connection last time through the main loop
  52. const int postingInterval = 10000;  //delay between updates to Pachube.com
  53.  
  54. void setup() {
  55.   // start the ethernet connection and serial port:
  56.   Ethernet.begin(mac, ip);
  57.   Serial.begin(9600);
  58.   // give the ethernet module time to boot up:
  59.   delay(1000);
  60. }
  61.  
  62. void loop() {
  63.  
  64.   float temperature = getTemp();
  65.   Serial.println(temperature);
  66.  
  67.   // read the analog sensor:
  68.   int sensorReading = temperature;  
  69.  
  70.   // if there's incoming data from the net connection.
  71.   // send it out the serial port.  This is for debugging
  72.   // purposes only:
  73.   if (client.available()) {
  74.     char c = client.read();
  75.     Serial.print(c);
  76.   }
  77.  
  78.   // if there's no net connection, but there was one last time
  79.   // through the loop, then stop the client:
  80.   if (!client.connected() && lastConnected) {
  81.     Serial.println();
  82.     Serial.println("disconnecting.");
  83.     client.stop();
  84.   }
  85.  
  86.   // if you're not connected, and ten seconds have passed since
  87.   // your last connection, then connect again and send data:
  88.   if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
  89.     sendData(sensorReading);
  90.   }
  91.   // store the state of the connection for next time through
  92.   // the loop:
  93.   lastConnected = client.connected();
  94. }
  95.  
  96. // this method makes a HTTP connection to the server:
  97. void sendData(int thisData) {
  98.   // if there's a successful connection:
  99.   if (client.connect()) {
  100.     Serial.println("connecting...");
  101.     // send the HTTP PUT request.
  102.     // fill in your feed address here:
  103.     client.print("PUT /api/YOUR_FEED_HERE.csv HTTP/1.1\n");
  104.     client.print("Host: www.pachube.com\n");
  105.     // fill in your Pachube API key here:
  106.     client.print("X-PachubeApiKey: YOUR_KEY_HERE\n");
  107.     client.print("Content-Length: ");
  108.  
  109.     // calculate the length of the sensor reading in bytes:
  110.     int thisLength = getLength(thisData);
  111.     client.println(thisLength, DEC);
  112.  
  113.     // last pieces of the HTTP PUT request:
  114.     client.print("Content-Type: text/csv\n");
  115.     client.println("Connection: close\n");
  116.  
  117.     // here's the actual content of the PUT request:
  118.     client.println(thisData, DEC);
  119.  
  120.     // note the time that the connection was made:
  121.     lastConnectionTime = millis();
  122.   }
  123.   else {
  124.     // if you couldn't make a connection:
  125.     Serial.println("connection failed");
  126.   }
  127.  
  128.   delay(100);
  129. }
  130.  
  131.  
  132. // This method calculates the number of digits in the
  133. // sensor reading.  Since each digit of the ASCII decimal
  134. // representation is a byte, the number of digits equals
  135. // the number of bytes:
  136.  
  137. int getLength(int someValue) {
  138.   // there's at least one byte:
  139.   int digits = 1;
  140.   // continually divide the value by ten,
  141.   // adding one to the digit count for each
  142.   // time you divide, until you're at 0:
  143.   int dividend = someValue /10;
  144.   while (dividend > 0) {
  145.     dividend = dividend /10;
  146.     digits++;
  147.   }
  148.   // return the number of digits:
  149.   return digits;
  150. }
  151.  
  152. float getTemp(){
  153. //returns the temperature from one DS18S20 in DEG Celsius
  154.  
  155. byte data[12];
  156. byte addr[8];
  157.  
  158. if ( !ds.search(addr)) {
  159. //no more sensors on chain, reset search
  160.    ds.reset_search();
  161. return -1000;
  162. }
  163.  
  164. if ( OneWire::crc8( addr, 7) != addr[7]) {
  165. Serial.print("CRC is not valid!\n");
  166. return -1000;
  167. }
  168.  
  169. if ( addr[0] != 0x10 && addr[0] != 0x28) {
  170. Serial.print("Device is not recognized");
  171. return -1000;
  172. }
  173.  
  174. ds.reset();
  175. ds.select(addr);
  176. ds.write(0x44,1); // start conversion, with parasite power on at the end
  177.  
  178. byte present = ds.reset();
  179. ds.select(addr);  
  180. ds.write(0xBE); // Read Scratchpad
  181.  
  182.  
  183. for (int i = 0; i < 9; i++) { // we need 9 bytes
  184.   data[i] = ds.read();
  185. }
  186.  
  187. ds.reset_search();
  188.  
  189. byte MSB = data[1];
  190. byte LSB = data[0];
  191. float CONT_REMAIN = data[6];
  192. float CONT_PER_C = data[7];
  193.  
  194. float tempRead = ((MSB << 8) | LSB); //using two's compliment
  195. float TemperatureSum = tempRead/2-0.25+(CONT_PER_C-CONT_REMAIN)/CONT_PER_C;
  196.  
  197. return TemperatureSum; }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement