1/**********************************************************
2 Bosch Pressure Sensor BMP085 / BMP180 readout routine
3 for the Arduino platform.
4
5 Compiled by Leo Nutz
6 www.ALTDuino.de
7**********************************************************/
8
9#include "DHT.h" // Adafruit DHTxx lib
10#include <Wire.h> // Wire lib for I2C
11#include <VirtualWire.h>
12
13#define TXPIN 12
14#define BMPPIN 0x77 // Sensor address
15#define DHTPIN 2 // Digital pin the DHT22 is connected to
16#define DHTTYPE DHT22 // Sensor type (DHT11, DHT22 etc)
17DHT dht(DHTPIN, DHTTYPE); // Create DHT sensor object
18
19int RF_TX_PIN = 12;
20
21int16_t ac1, ac2, ac3, b1, b2, mb, mc, md; // Store sensor PROM values from BMP180
22uint16_t ac4, ac5, ac6; // Store sensor PROM values from BMP180
23// Ultra Low Power OSS = 0, OSD = 5ms
24// Standard OSS = 1, OSD = 8ms
25// High OSS = 2, OSD = 14ms
26// Ultra High Resolution OSS = 3, OSD = 26ms
27const uint8_t oss = 3; // Set oversampling setting
28const uint8_t osd = 26; // with corresponding oversampling delay
29
30float T, P; // Set global variables for temperature and pressure
31
32void setup()
33{
34 Wire.begin(); // Activate I2C
35 Serial.begin(9600); // Set up serial port
36 init_SENSOR(); // Initialize baro sensor variables
37 // Setup transmit pin
38 vw_set_tx_pin(RF_TX_PIN);
39 vw_setup(2000); //
40 delay(100);
41
42}
43
44void loop()
45{
46 int32_t b5;
47
48 float h = dht.readHumidity(); // Read humidity
49 float t = dht.readTemperature(); // Read temperatre in celcius
50 float f = dht.readTemperature(true); // Read temperature in farenheit
51
52 // Check if any reads failed and exit early (to try again).
53 if (isnan(h) || isnan(t) || isnan(f)) {
54 Serial.println("Failed to read from DHT sensor!");
55 return;
56 }
57 // Compute heat index in Celsius (isFahreheit = false)
58 float hic = dht.computeHeatIndex(t, h, false);
59
60 // Print everything
61 Serial.print("Humidity: ");
62 Serial.print(h);
63 Serial.println("%");
64
65 b5 = temperature(); // Read and calculate temperature (T)
66
67 Serial.print("Temperature: ");
68 Serial.print(T, 2);
69 Serial.print(" C, ");
70 Serial.print(1.8 * T + 32.0, 2);
71 Serial.println(" F");
72
73 P = pressure(b5); // Read and calculate pressure (P)
74
75 Serial.print("Pressure: ");
76 Serial.print(P, 2);
77 Serial.print(" mbar, ");
78 Serial.print(P * 0.0295299830714, 2);
79 Serial.println(" inHg");
80 Serial.println("");
81
82 SendData("testing");
83 delay(2000); // Delay between each readout
84
85}
86
87
88void SendData(String Data)
89{
90 //Debug
91 Serial.println("-->"+ Data + "<-- ");
92
93 //Making char Array of String
94 const char* rawdata = Data.c_str();
95
96 digitalWrite(13, true); // Flash a light to show transmitting
97 vw_send((uint8_t *)rawdata, strlen(rawdata)); //Send Data
98 vw_wait_tx(); // Wait until the whole message is gone
99 digitalWrite(13, false);
100 Serial.println("Data sent");
101}
102
103/**********************************************
104 Initialize sensor variables
105**********************************************/
106void init_SENSOR()
107{
108 ac1 = read_2_bytes(0xAA);
109 ac2 = read_2_bytes(0xAC);
110 ac3 = read_2_bytes(0xAE);
111 ac4 = read_2_bytes(0xB0);
112 ac5 = read_2_bytes(0xB2);
113 ac6 = read_2_bytes(0xB4);
114 b1 = read_2_bytes(0xB6);
115 b2 = read_2_bytes(0xB8);
116 mb = read_2_bytes(0xBA);
117 mc = read_2_bytes(0xBC);
118 md = read_2_bytes(0xBE);
119
120 Serial.println("");
121 Serial.println("Sensor calibration data:");
122 Serial.print(F("AC1 = ")); Serial.println(ac1);
123 Serial.print(F("AC2 = ")); Serial.println(ac2);
124 Serial.print(F("AC3 = ")); Serial.println(ac3);
125 Serial.print(F("AC4 = ")); Serial.println(ac4);
126 Serial.print(F("AC5 = ")); Serial.println(ac5);
127 Serial.print(F("AC6 = ")); Serial.println(ac6);
128 Serial.print(F("B1 = ")); Serial.println(b1);
129 Serial.print(F("B2 = ")); Serial.println(b2);
130 Serial.print(F("MB = ")); Serial.println(mb);
131 Serial.print(F("MC = ")); Serial.println(mc);
132 Serial.print(F("MD = ")); Serial.println(md);
133 Serial.println("");
134}
135
136/**********************************************
137 Calcualte pressure readings
138**********************************************/
139float pressure(int32_t b5)
140{
141 int32_t x1, x2, x3, b3, b6, p, UP;
142 uint32_t b4, b7;
143
144 UP = read_pressure(); // Read raw pressure
145
146 b6 = b5 - 4000;
147 x1 = (b2 * (b6 * b6 >> 12)) >> 11;
148 x2 = ac2 * b6 >> 11;
149 x3 = x1 + x2;
150 b3 = (((ac1 * 4 + x3) << oss) + 2) >> 2;
151 x1 = ac3 * b6 >> 13;
152 x2 = (b1 * (b6 * b6 >> 12)) >> 16;
153 x3 = ((x1 + x2) + 2) >> 2;
154 b4 = (ac4 * (uint32_t)(x3 + 32768)) >> 15;
155 b7 = ((uint32_t)UP - b3) * (50000 >> oss);
156 if(b7 < 0x80000000) { p = (b7 << 1) / b4; } else { p = (b7 / b4) << 1; } // or p = b7 < 0x80000000 ? (b7 * 2) / b4 : (b7 / b4) * 2;
157 x1 = (p >> 8) * (p >> 8);
158 x1 = (x1 * 3038) >> 16;
159 x2 = (-7357 * p) >> 16;
160 return (p + ((x1 + x2 + 3791) >> 4)) / 100.0f; // Return pressure in mbar
161}
162
163/**********************************************
164 Read uncompensated temperature
165**********************************************/
166int32_t temperature()
167{
168 int32_t x1, x2, b5, UT;
169
170 Wire.beginTransmission(BMPPIN); // Start transmission to device
171 Wire.write(0xf4); // Sends register address
172 Wire.write(0x2e); // Write data
173 Wire.endTransmission(); // End transmission
174 delay(5); // Datasheet suggests 4.5 ms
175
176 UT = read_2_bytes(0xf6); // Read uncompensated TEMPERATURE value
177
178 // Calculate true temperature
179 x1 = (UT - (int32_t)ac6) * (int32_t)ac5 >> 15;
180 x2 = ((int32_t)mc << 11) / (x1 + (int32_t)md);
181 b5 = x1 + x2;
182 T = (b5 + 8) >> 4;
183 T = T / 10.0; // Temperature in celsius
184 return b5;
185}
186
187/**********************************************
188 Read uncompensated pressure value
189**********************************************/
190int32_t read_pressure()
191{
192 int32_t value;
193 Wire.beginTransmission(BMPPIN); // Start transmission to device
194 Wire.write(0xf4); // Sends register address to read from
195 Wire.write(0x34 + (oss << 6)); // Write data
196 Wire.endTransmission(); // SEd transmission
197 delay(osd); // Oversampling setting delay
198 Wire.beginTransmission(BMPPIN);
199 Wire.write(0xf6); // Register to read
200 Wire.endTransmission();
201 Wire.requestFrom(BMPPIN, 3); // Request three bytes
202 if(Wire.available() >= 3)
203 {
204 value = (((int32_t)Wire.read() << 16) | ((int32_t)Wire.read() << 8) | ((int32_t)Wire.read())) >> (8 - oss);
205 }
206 return value; // Return value
207}
208
209/**********************************************
210 Read 1 byte from the BMP sensor
211**********************************************/
212uint8_t read_1_byte(uint8_t code)
213{
214 uint8_t value;
215 Wire.beginTransmission(BMPPIN); // Start transmission to device
216 Wire.write(code); // Sends register address to read from
217 Wire.endTransmission(); // End transmission
218 Wire.requestFrom(BMPPIN, 1); // Request data for 1 byte to be read
219 if(Wire.available() >= 1)
220 {
221 value = Wire.read(); // Get 1 byte of data
222 }
223 return value; // Return value
224}
225
226/**********************************************
227 Read 2 bytes from the BMP sensor
228**********************************************/
229uint16_t read_2_bytes(uint8_t code)
230{
231 uint16_t value;
232 Wire.beginTransmission(BMPPIN); // Start transmission to device
233 Wire.write(code); // Sends register address to read from
234 Wire.endTransmission(); // End transmission
235 Wire.requestFrom(BMPPIN, 2); // Request 2 bytes from device
236 if(Wire.available() >= 2)
237 {
238 value = (Wire.read() << 8) | Wire.read(); // Get 2 bytes of data
239 }
240 return value; // Return value
241}