Advertisement
ossipee

weather Tx

Aug 23rd, 2015
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.54 KB | None | 0 0
  1.  
  2. //Including Libs
  3. #include <OneWire.h>
  4. #include <dht11.h>
  5. #include <VirtualWire.h>
  6. #include <Wire.h> //Pressure sensor
  7.  
  8. //Defining Pins
  9. dht11 DHT11;
  10.  
  11. #define DHT11PIN 2
  12. #define ONE_WIRE_BUS 10
  13.  
  14. #define BMP085_ADDRESS 0x77  // I2C address of BMP085 //Pressure sensor
  15.  
  16. #define INTEGER_MAX (pow(2,31)-1)
  17. #define E_MAX (pow(10, 7))
  18. #define E_MIN (pow(10, -6))
  19. #define EPSILON 0.000000119209
  20.  
  21. OneWire oneWire(ONE_WIRE_BUS);
  22.  
  23. int RF_TX_PIN = 12;
  24.  
  25. //Pressure sensor
  26. const unsigned char OSS = 0;  // Oversampling Setting
  27.  
  28. // Calibration values
  29. int ac1;
  30. int ac2;
  31. int ac3;
  32. unsigned int ac4;
  33. unsigned int ac5;
  34. unsigned int ac6;
  35. int b1;
  36. int b2;
  37. int mb;
  38. int mc;
  39. int md;
  40.  
  41. // b5 is calculated in bmp085GetTemperature(...), this variable is also used in bmp085GetPressure(...)
  42. // so ...Temperature(...) must be called before ...Pressure(...).
  43. long b5;
  44.  
  45. //Seting up
  46. void setup()
  47. {
  48.   //For Debug
  49.   Serial.begin(9600);
  50.   Serial.println("Setup");
  51.  
  52.   //Pressure sensor
  53.   Wire.begin();
  54.   bmp085Calibration();
  55.  
  56.   // Setup transmit pin
  57.   vw_set_tx_pin(RF_TX_PIN);
  58.   vw_setup(2000); //
  59.  
  60.   //For Debug
  61.   Serial.println("Start Pressure");
  62. }
  63.  
  64. //Main Loop
  65. void loop()
  66. {
  67.   Serial.println("\n");
  68.  
  69.   //First Sensor
  70.   //Humidity
  71.   int chk = DHT11.read(DHT11PIN);
  72.  
  73.   //Send Data #H is for Humidity
  74.   SendData("#H"+ ((String)DHT11.humidity));
  75.  
  76.  
  77.  
  78.   //Seccond Sensor
  79.   //Temp (Baro Pressure)
  80.   float temperature = bmp085GetTemperature(bmp085ReadUT()); //MUST be called first
  81.  
  82.   //Send Data #C is for Celcious
  83.   SendData("#C"+(String(temperature,2)));
  84.  
  85.  
  86.  
  87.   //Same seccond Sensor, next output
  88.   //Baro Pressure
  89.   float pressure = bmp085GetPressure(bmp085ReadUP());
  90.  
  91.   //Send Data #P is for pressure
  92.   SendData("#P"+(String(pressure/100,2)));
  93.  
  94.  
  95.   //Wait for Next Loop
  96.   delay(5000);
  97. }
  98.  
  99. void SendData(String Data)
  100. {
  101.   //Debug
  102.   Serial.println("-->"+ Data + "<-- ");
  103.  
  104.   //Making char Array of String
  105.   const char* rawdata = Data.c_str();
  106.  
  107.   digitalWrite(13, true); // Flash a light to show transmitting
  108.   vw_send((uint8_t *)rawdata, strlen(rawdata)); //Send Data
  109.   vw_wait_tx(); // Wait until the whole message is gone
  110.   digitalWrite(13, false);
  111. }
  112.  
  113. //Calabration of Barometic sensor
  114. void bmp085Calibration()
  115. {
  116.   ac1 = bmp085ReadInt(0xAA);
  117.   ac2 = bmp085ReadInt(0xAC);
  118.   ac3 = bmp085ReadInt(0xAE);
  119.   ac4 = bmp085ReadInt(0xB0);
  120.   ac5 = bmp085ReadInt(0xB2);
  121.   ac6 = bmp085ReadInt(0xB4);
  122.   b1 = bmp085ReadInt(0xB6);
  123.   b2 = bmp085ReadInt(0xB8);
  124.   mb = bmp085ReadInt(0xBA);
  125.   mc = bmp085ReadInt(0xBC);
  126.   md = bmp085ReadInt(0xBE);
  127. }
  128.  
  129. // Calculate temperature in deg C
  130. float bmp085GetTemperature(unsigned int ut){
  131.   long x1, x2;
  132.  
  133.   x1 = (((long)ut - (long)ac6)*(long)ac5) >> 15;
  134.   x2 = ((long)mc << 11)/(x1 + md);
  135.   b5 = x1 + x2;
  136.  
  137.   float temp = ((b5 + 8)>>4);
  138.   temp = temp /10;
  139.  
  140.   return temp;
  141. }
  142.  
  143. // Calculate pressure given up
  144. // calibration values must be known
  145. // b5 is also required so bmp085GetTemperature(...) must be called first.
  146. // Value returned will be pressure in units of Pa.
  147. long bmp085GetPressure(unsigned long up){
  148.   long x1, x2, x3, b3, b6, p;
  149.   unsigned long b4, b7;
  150.  
  151.   b6 = b5 - 4000;
  152.   // Calculate B3
  153.   x1 = (b2 * (b6 * b6)>>12)>>11;
  154.   x2 = (ac2 * b6)>>11;
  155.   x3 = x1 + x2;
  156.   b3 = (((((long)ac1)*4 + x3)<<OSS) + 2)>>2;
  157.  
  158.   // Calculate B4
  159.   x1 = (ac3 * b6)>>13;
  160.   x2 = (b1 * ((b6 * b6)>>12))>>16;
  161.   x3 = ((x1 + x2) + 2)>>2;
  162.   b4 = (ac4 * (unsigned long)(x3 + 32768))>>15;
  163.  
  164.   b7 = ((unsigned long)(up - b3) * (50000>>OSS));
  165.   if (b7 < 0x80000000)
  166.     p = (b7<<1)/b4;
  167.   else
  168.     p = (b7/b4)<<1;
  169.  
  170.   x1 = (p>>8) * (p>>8);
  171.   x1 = (x1 * 3038)>>16;
  172.   x2 = (-7357 * p)>>16;
  173.   p += (x1 + x2 + 3791)>>4;
  174.  
  175.   long temp = p;
  176.   return temp;
  177. }
  178.  
  179. // Read 1 byte from the BMP085 at 'address'
  180. char bmp085Read(unsigned char address)
  181. {
  182.   unsigned char data;
  183.  
  184.   Wire.beginTransmission(BMP085_ADDRESS);
  185.   Wire.write(address);
  186.   Wire.endTransmission();
  187.  
  188.   Wire.requestFrom(BMP085_ADDRESS, 1);
  189.   while(!Wire.available())
  190.     ;
  191.  
  192.   return Wire.read();
  193. }
  194.  
  195. // Read 2 bytes from the BMP085
  196. // First byte will be from 'address'
  197. // Second byte will be from 'address'+1
  198. int bmp085ReadInt(unsigned char address)
  199. {
  200.   unsigned char msb, lsb;
  201.  
  202.   Wire.beginTransmission(BMP085_ADDRESS);
  203.   Wire.write(address);
  204.   Wire.endTransmission();
  205.  
  206.   Wire.requestFrom(BMP085_ADDRESS, 2);
  207.   while(Wire.available()<2)
  208.     ;
  209.   msb = Wire.read();
  210.   lsb = Wire.read();
  211.  
  212.   return (int) msb<<8 | lsb;
  213. }
  214.  
  215. // Read the uncompensated temperature value
  216. unsigned int bmp085ReadUT(){
  217.   unsigned int ut;
  218.  
  219.   // Write 0x2E into Register 0xF4
  220.   // This requests a temperature reading
  221.   Wire.beginTransmission(BMP085_ADDRESS);
  222.   Wire.write(0xF4);
  223.   Wire.write(0x2E);
  224.   Wire.endTransmission();
  225.  
  226.   // Wait at least 4.5ms
  227.   delay(5);
  228.  
  229.   // Read two bytes from registers 0xF6 and 0xF7
  230.   ut = bmp085ReadInt(0xF6);
  231.   return ut;
  232. }
  233.  
  234. // Read the uncompensated pressure value
  235. unsigned long bmp085ReadUP(){
  236.  
  237.   unsigned char msb, lsb, xlsb;
  238.   unsigned long up = 0;
  239.  
  240.   // Write 0x34+(OSS<<6) into register 0xF4
  241.   // Request a pressure reading w/ oversampling setting
  242.   Wire.beginTransmission(BMP085_ADDRESS);
  243.   Wire.write(0xF4);
  244.   Wire.write(0x34 + (OSS<<6));
  245.   Wire.endTransmission();
  246.  
  247.   // Wait for conversion, delay time dependent on OSS
  248.   delay(2 + (3<<OSS));
  249.  
  250.   // Read register 0xF6 (MSB), 0xF7 (LSB), and 0xF8 (XLSB)
  251.   msb = bmp085Read(0xF6);
  252.   lsb = bmp085Read(0xF7);
  253.   xlsb = bmp085Read(0xF8);
  254.  
  255.   up = (((unsigned long) msb << 16) | ((unsigned long) lsb << 8) | (unsigned long) xlsb) >> (8-OSS);
  256.  
  257.   return up;
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement