Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2013
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.86 KB | None | 0 0
  1. #include <SD.h>
  2. #include <Wire.h>
  3. #include "DHT.h"
  4. #include "VirtualWire.h"
  5.  
  6. #define DHTPIN 2
  7. #define DHTTYPE DHT11   // DHT 11
  8. DHT dht(DHTPIN, DHTTYPE);
  9.  
  10.  
  11. #define BMP085_ADDRESS 0x77  // I2C address of BMP085
  12.  
  13. const unsigned char OSS = 0;  // Oversampling Setting
  14. File myFile;
  15.  
  16. char RFMessage[50];
  17.  
  18. // Calibration values for the BMP085
  19. int ac1;
  20. int ac2;
  21. int ac3;
  22. unsigned int ac4;
  23. unsigned int ac5;
  24. unsigned int ac6;
  25. int b1;
  26. int b2;
  27. int mb;
  28. int mc;
  29. int md;
  30.  
  31. long b5;
  32.  
  33. short temperature;
  34. long pressure;
  35.  
  36. // Use these for altitude conversions
  37. const float p0 = 101700;     // Pressure at sea level (Pa)
  38. float altitude;
  39.  
  40. #define DS1307_I2C_ADDRESS 0x68  // This is the I2C address
  41. // Arduino version compatibility Pre-Compiler Directives
  42. #if defined(ARDUINO) && ARDUINO >= 100   // Arduino v1.0 and newer
  43. #define I2C_WRITE Wire.send
  44. #define I2C_READ Wire.receive
  45. #else                                   // Arduino Prior to v1.0
  46. #define I2C_WRITE Wire.send
  47. #define I2C_READ Wire.receive
  48. #endif
  49. // Global Variables
  50. int command = 0;       // This is the command char, in ascii form, sent from the serial port    
  51. int i;
  52. long previousMillis = 0;        // will store last time Temp was updated
  53. byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  54. byte test;
  55. byte zero;
  56. char  *Day[] = {
  57.   "","Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
  58. char  *Mon[] = {
  59.   "","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
  60.  
  61. // Convert normal decimal numbers to binary coded decimal
  62. byte decToBcd(byte val)
  63. {
  64.   return ( (val/10*16) + (val%10) );
  65. }
  66.  
  67. // Convert binary coded decimal to normal decimal numbers
  68. byte bcdToDec(byte val)
  69. {
  70.   return ( (val/16*10) + (val%16) );
  71. }
  72.  
  73.  
  74.  
  75. // Gets the date and time from the ds1307 and prints result
  76. void getDateDs1307()
  77. {
  78.   // Reset the register pointer
  79.   Wire.beginTransmission(DS1307_I2C_ADDRESS);
  80.   I2C_WRITE(zero);
  81.   Wire.endTransmission();
  82.  
  83.   Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
  84.  
  85.   // A few of these need masks because certain bits are control bits
  86.   second     = bcdToDec(I2C_READ() & 0x7f);
  87.   minute     = bcdToDec(I2C_READ());
  88.   hour       = bcdToDec(I2C_READ() & 0x3f);  // Need to change this if 12 hour am/pm
  89.   dayOfWeek  = bcdToDec(I2C_READ());
  90.   dayOfMonth = bcdToDec(I2C_READ());
  91.   month      = bcdToDec(I2C_READ());
  92.   year       = bcdToDec(I2C_READ());
  93.  
  94.   if (hour < 10)
  95.     myFile.print("0");
  96.   myFile.print(hour, DEC);
  97.   myFile.print(":");
  98.   if (minute < 10)
  99.     myFile.print("0");
  100.   myFile.print(minute, DEC);
  101.   myFile.print(":");
  102.   if (second < 10)
  103.     myFile.print("0");
  104.   myFile.print(second, DEC);
  105.  
  106.   if (hour < 10)
  107.     Serial.print("0");
  108.   Serial.print(hour, DEC);
  109.   Serial.print(":");
  110.   if (minute < 10)
  111.     Serial.print("0");
  112.   Serial.print(minute, DEC);
  113.   Serial.print(":");
  114.   if (second < 10)
  115.     Serial.print("0");
  116.   Serial.print(second, DEC);
  117.  
  118. }
  119.  
  120. float t;
  121. float h;
  122.  
  123. void setup()
  124. {
  125.   vw_set_ptt_inverted(true); // Required for DR3100
  126.   vw_set_tx_pin(5);
  127.   vw_setup(2000);    // Bits per sec
  128.   pinMode(10, OUTPUT);
  129.   Serial.begin(9600);
  130.   Wire.begin();
  131.   bmp085Calibration();
  132.   zero=0x00;
  133.   dht.begin();
  134.   if (!SD.begin(4)) {
  135.     Serial.println("initialization failed!");
  136.   }
  137.   else{
  138.     Serial.println("initialization done.");
  139.     myFile = SD.open("balloon.txt", FILE_WRITE);
  140.     myFile.println("Bootup!");
  141.   }
  142. }
  143.  
  144. unsigned int loopcounter = 0;
  145. void loop(){
  146.   loopcounter ++;
  147.   static unsigned long PreviousMillis;
  148.   int TimeElapsed;
  149.   TimeElapsed = millis()-PreviousMillis;
  150.   if(TimeElapsed < 100) Serial.print("0");
  151.   Serial.print(TimeElapsed);
  152.   Serial.print(" ");
  153.   PreviousMillis = millis();
  154.  
  155.   getDateDs1307();
  156.   myFile.print(" ");
  157.   Serial.print(" ");
  158.  
  159.  
  160.  
  161.   temperature = bmp085GetTemperature(bmp085ReadUT());
  162.   pressure = bmp085GetPressure(bmp085ReadUP());
  163.   altitude = (float)44330 * (1 - pow(((float) pressure/p0), 0.190295));
  164.   myFile.print(temperature, DEC);
  165.   myFile.print(" ");
  166.   myFile.print(pressure, DEC);
  167.   myFile.print(" ");
  168.   myFile.print(altitude, 2);
  169.   myFile.print(" ");
  170.  
  171.   Serial.print(temperature, DEC);
  172.   Serial.print(" ");
  173.   Serial.print(pressure, DEC);
  174.   Serial.print(" ");
  175.   Serial.print(altitude, 2);
  176.   Serial.print(" ");
  177.  
  178.  sprintf(RFMessage, "%d: Altitude = %d.%02d",loopcounter, (int)altitude, (int)(100*(altitude-(int)altitude)));
  179.   vw_send((uint8_t *)RFMessage, strlen(RFMessage));
  180.     vw_wait_tx(); // Wait until the whole message is gone
  181.  
  182. static int DHTCountdown;
  183.  
  184.   if(DHTCountdown == 0){
  185.     h = dht.readHumidity();
  186.     t = dht.readTemperature();
  187.     DHTCountdown = 16;
  188.   }
  189.   // check if returns are valid, if they are NaN (not a number) then something went wrong!
  190.   if (isnan(t) || isnan(h)) {
  191.     myFile.print("FAIL");
  192.     myFile.print(" ");
  193.     myFile.print("FAIL");
  194.  
  195.     Serial.print("FAIL");
  196.     Serial.print(" ");
  197.     Serial.print("FAIL");
  198.   }
  199.   else {
  200.     myFile.print(t);
  201.     myFile.print(" ");
  202.  
  203.     myFile.print(h);
  204.  
  205.     Serial.print(t);
  206.     Serial.print(" ");
  207.     Serial.print(h);
  208.   }
  209.   DHTCountdown = DHTCountdown - 1;
  210.   myFile.println(" ");
  211.   Serial.println(" ");
  212.  
  213.   myFile.flush();
  214.  
  215.  
  216.  
  217. }
  218.  
  219. // Stores all of the bmp085's calibration values into global variables
  220. // Calibration values are required to calculate temp and pressure
  221. // This function should be called at the beginning of the program
  222. void bmp085Calibration()
  223. {
  224.   ac1 = bmp085ReadInt(0xAA);
  225.   ac2 = bmp085ReadInt(0xAC);
  226.   ac3 = bmp085ReadInt(0xAE);
  227.   ac4 = bmp085ReadInt(0xB0);
  228.   ac5 = bmp085ReadInt(0xB2);
  229.   ac6 = bmp085ReadInt(0xB4);
  230.   b1 = bmp085ReadInt(0xB6);
  231.   b2 = bmp085ReadInt(0xB8);
  232.   mb = bmp085ReadInt(0xBA);
  233.   mc = bmp085ReadInt(0xBC);
  234.   md = bmp085ReadInt(0xBE);
  235. }
  236.  
  237. // Calculate temperature given ut.
  238. // Value returned will be in units of 0.1 deg C
  239. short bmp085GetTemperature(unsigned int ut)
  240. {
  241.   long x1, x2;
  242.  
  243.   x1 = (((long)ut - (long)ac6)*(long)ac5) >> 15;
  244.   x2 = ((long)mc << 11)/(x1 + md);
  245.   b5 = x1 + x2;
  246.  
  247.   return ((b5 + 8)>>4);  
  248. }
  249.  
  250. // Calculate pressure given up
  251. // calibration values must be known
  252. // b5 is also required so bmp085GetTemperature(...) must be called first.
  253. // Value returned will be pressure in units of Pa.
  254. long bmp085GetPressure(unsigned long up)
  255. {
  256.   long x1, x2, x3, b3, b6, p;
  257.   unsigned long b4, b7;
  258.  
  259.   b6 = b5 - 4000;
  260.   // Calculate B3
  261.   x1 = (b2 * (b6 * b6)>>12)>>11;
  262.   x2 = (ac2 * b6)>>11;
  263.   x3 = x1 + x2;
  264.   b3 = (((((long)ac1)*4 + x3)<<OSS) + 2)>>2;
  265.  
  266.   // Calculate B4
  267.   x1 = (ac3 * b6)>>13;
  268.   x2 = (b1 * ((b6 * b6)>>12))>>16;
  269.   x3 = ((x1 + x2) + 2)>>2;
  270.   b4 = (ac4 * (unsigned long)(x3 + 32768))>>15;
  271.  
  272.   b7 = ((unsigned long)(up - b3) * (50000>>OSS));
  273.   if (b7 < 0x80000000)
  274.     p = (b7<<1)/b4;
  275.   else
  276.     p = (b7/b4)<<1;
  277.  
  278.   x1 = (p>>8) * (p>>8);
  279.   x1 = (x1 * 3038)>>16;
  280.   x2 = (-7357 * p)>>16;
  281.   p += (x1 + x2 + 3791)>>4;
  282.  
  283.   return p;
  284. }
  285.  
  286. // Read 1 byte from the BMP085 at 'address'
  287. char bmp085Read(unsigned char address)
  288. {
  289.   unsigned char data;
  290.  
  291.   Wire.beginTransmission(BMP085_ADDRESS);
  292.   Wire.send(address);
  293.   Wire.endTransmission();
  294.  
  295.   Wire.requestFrom(BMP085_ADDRESS, 1);
  296.   while(!Wire.available())
  297.     ;
  298.  
  299.   return Wire.receive();
  300. }
  301.  
  302. // Read 2 bytes from the BMP085
  303. // First byte will be from 'address'
  304. // Second byte will be from 'address'+1
  305. int bmp085ReadInt(unsigned char address)
  306. {
  307.   unsigned char msb, lsb;
  308.  
  309.   Wire.beginTransmission(BMP085_ADDRESS);
  310.   Wire.send(address);
  311.   Wire.endTransmission();
  312.  
  313.   Wire.requestFrom(BMP085_ADDRESS, 2);
  314.   while(Wire.available()<2)
  315.     ;
  316.   msb = Wire.receive();
  317.   lsb = Wire.receive();
  318.  
  319.   return (int) msb<<8 | lsb;
  320. }
  321.  
  322. // Read the uncompensated temperature value
  323. unsigned int bmp085ReadUT()
  324. {
  325.   unsigned int ut;
  326.  
  327.   // Write 0x2E into Register 0xF4
  328.   // This requests a temperature reading
  329.   Wire.beginTransmission(BMP085_ADDRESS);
  330.   Wire.send(0xF4);
  331.   Wire.send(0x2E);
  332.   Wire.endTransmission();
  333.  
  334.   // Wait at least 4.5ms
  335.   delay(5);
  336.  
  337.   // Read two bytes from registers 0xF6 and 0xF7
  338.   ut = bmp085ReadInt(0xF6);
  339.   return ut;
  340. }
  341.  
  342. // Read the uncompensated pressure value
  343. unsigned long bmp085ReadUP()
  344. {
  345.   unsigned char msb, lsb, xlsb;
  346.   unsigned long up = 0;
  347.  
  348.   // Write 0x34+(OSS<<6) into register 0xF4
  349.   // Request a pressure reading w/ oversampling setting
  350.   Wire.beginTransmission(BMP085_ADDRESS);
  351.   Wire.send(0xF4);
  352.   Wire.send(0x34 + (OSS<<6));
  353.   Wire.endTransmission();
  354.  
  355.   // Wait for conversion, delay time dependent on OSS
  356.   delay(2 + (3<<OSS));
  357.  
  358.   // Read register 0xF6 (MSB), 0xF7 (LSB), and 0xF8 (XLSB)
  359.   Wire.beginTransmission(BMP085_ADDRESS);
  360.   Wire.send(0xF6);
  361.   Wire.endTransmission();
  362.   Wire.requestFrom(BMP085_ADDRESS, 3);
  363.  
  364.   // Wait for data to become available
  365.   while(Wire.available() < 3)
  366.     ;
  367.   msb = Wire.receive();
  368.   lsb = Wire.receive();
  369.   xlsb = Wire.receive();
  370.  
  371.   up = (((unsigned long) msb << 16) | ((unsigned long) lsb << 8) | (unsigned long) xlsb) >> (8-OSS);
  372.  
  373.   return up;
  374. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement