Advertisement
yOPERO

Pachube+ds18b20

Apr 4th, 2012
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.58 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <Ethernet.h>
  3. #include <Pachube.h>
  4. #include <OneWire.h>
  5.  
  6. OneWire  ds(8);  // en el pin 8 o en el que tengas enchufado el ds18
  7.  
  8. byte mac[] = {0x90, 0xA2, 0xDA, 0x00, 0x10, 0x8f};
  9. char apiKey[] = "AQUI pones tu key";
  10. long feedId = 44942;
  11. char datastreamId[] = "aqui datastream";
  12. long prevMillis = 0;
  13. float celsius;
  14. PachubeClient client = PachubeClient(apiKey);
  15.  
  16. void setup()
  17. {
  18.   Serial.begin(9600);
  19.   client.connectWithMac(mac);
  20. }
  21.  
  22. void loop()
  23. {
  24.  
  25.  unsigned long curMillis = millis();
  26.  if(curMillis - prevMillis > 10000)  
  27.   {
  28.    
  29.      byte i;
  30.   byte present = 0;
  31.   byte type_s;
  32.   byte data[12];
  33.   byte addr[8];
  34.  
  35.  
  36.   if ( !ds.search(addr)) {
  37.    
  38.     ds.reset_search();
  39.     delay(250);
  40.     return;
  41.   }
  42.  
  43.  
  44.  
  45.  
  46.   Serial.println();
  47.  
  48.   // the first ROM byte indicates which chip
  49.   switch (addr[0]) {
  50.     case 0x10:
  51.       Serial.println("  Chip = DS18S20");  // or old DS1820
  52.       type_s = 1;
  53.       break;
  54.     case 0x28:
  55.       Serial.println("  Chip = DS18B20");
  56.       type_s = 0;
  57.       break;
  58.     case 0x22:
  59.       Serial.println("  Chip = DS1822");
  60.       type_s = 0;
  61.       break;
  62.     default:
  63.       Serial.println("Device is not a DS18x20 family device.");
  64.       return;
  65.   }
  66.  
  67.   ds.reset();
  68.   ds.select(addr);
  69.   ds.write(0x44,1);         // start conversion, with parasite power on at the end
  70.  
  71.   delay(1000);     // maybe 750ms is enough, maybe not
  72.   // we might do a ds.depower() here, but the reset will take care of it.
  73.  
  74.   present = ds.reset();
  75.   ds.select(addr);    
  76.   ds.write(0xBE);         // Read Scratchpad
  77.  
  78.  
  79.   for ( i = 0; i < 9; i++) {           // we need 9 bytes
  80.     data[i] = ds.read();
  81.    
  82.   }
  83.  
  84.  
  85.   // convert the data to actual temperature
  86.  
  87.   unsigned int raw = (data[1] << 8) | data[0];
  88.   if (type_s) {
  89.     raw = raw << 3; // 9 bit resolution default
  90.     if (data[7] == 0x10) {
  91.       // count remain gives full 12 bit resolution
  92.       raw = (raw & 0xFFF0) + 12 - data[6];
  93.     }
  94.   } else {
  95.     byte cfg = (data[4] & 0x60);
  96.     if (cfg == 0x00) raw = raw << 3;  // 9 bit resolution, 93.75 ms
  97.     else if (cfg == 0x20) raw = raw << 2; // 10 bit res, 187.5 ms
  98.     else if (cfg == 0x40) raw = raw << 1; // 11 bit res, 375 ms
  99.     // default is 12 bit resolution, 750 ms conversion time
  100.   }
  101.   celsius = (float)raw / 16.0;
  102.  
  103.   Serial.print("Temperatur = ");
  104.   Serial.print(celsius);
  105.   Serial.print(" Celsius, ");
  106.   //ende tempmessung  
  107.   prevMillis = curMillis;
  108.   }
  109.     else{
  110.     // send to the Pachube client
  111.     client.updateFeed(feedId, datastreamId, celsius);
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement