Advertisement
seston

arduino easyiot bmp180 v0.1

Jan 26th, 2016
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.60 KB | None | 0 0
  1. /*
  2. V1.0 - first version
  3.  
  4. Created by Igor Jarc <igor.jarc1@gmail.com>
  5. See http://iot-playground.com for details
  6.  
  7. This program is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU General Public License
  9. version 2 as published by the Free Software Foundation.
  10. */
  11. #include <Esp8266EasyIoT.h>
  12. #include <SFE_BMP180.h>
  13. #include <Wire.h>
  14. #include <SoftwareSerial.h>
  15.  
  16. SoftwareSerial mySerial(10, 11);
  17.  
  18. #define ALTITUDE 13.0 // Altitude of my home
  19. #define ESP_RESET_PIN 12
  20.  
  21. #define MILS_IN_MIN 60000
  22.  
  23. #define CHILD_ID_TEMP 0
  24. #define CHILD_ID_BARO 1
  25.  
  26.  
  27. int minuteCount = 0;
  28. double pressureSamples[9][6];
  29. double pressureAvg[9];
  30. double dP_dt;
  31.  
  32. const char *weather[] = {
  33. "stable","sunny","cloudy","unstable","thunderstorm","unknown"};
  34.  
  35. int forecast = 5;
  36.  
  37. unsigned long startTime;
  38.  
  39. SFE_BMP180 bmp180;
  40. Esp8266EasyIoT esp;
  41.  
  42.  
  43. Esp8266EasyIoTMsg msgTemp(CHILD_ID_TEMP, V_TEMP);
  44. Esp8266EasyIoTMsg msgPress(CHILD_ID_BARO, V_PRESSURE);
  45. Esp8266EasyIoTMsg msgForec(CHILD_ID_BARO, V_FORECAST);
  46.  
  47. void setup()
  48. {
  49. mySerial.begin(9600); // ESP
  50. Serial.begin(115200); // debug
  51.  
  52. if (bmp180.begin())
  53. Serial.println("BMP180 init success");
  54. else
  55. {
  56. // Oops, something went wrong, this is usually a connection problem,
  57. // see the comments at the top of this sketch for the proper connections.
  58.  
  59. Serial.println("BMP180 init fail\n\n");
  60. while(1); // Pause forever.
  61. }
  62.  
  63. startTime = -1;
  64.  
  65. esp.begin(NULL, ESP_RESET_PIN, &mySerial, &Serial);
  66.  
  67. esp.present(CHILD_ID_TEMP, S_TEMP);
  68. esp.present(CHILD_ID_BARO, S_BARO);
  69. }
  70.  
  71.  
  72. void loop()
  73. {
  74.  
  75. for(int i =0; i<10;i++)
  76. {
  77. if (esp.process())
  78. break;
  79. }
  80.  
  81.  
  82. if (IsTimeout())
  83. {
  84. char status;
  85. double T,P,p0,a;
  86.  
  87. // Loop here getting pressure readings every 60 seconds.
  88.  
  89. // If you want sea-level-compensated pressure, as used in weather reports,
  90. // you will need to know the altitude at which your measurements are taken.
  91. // We're using a constant called ALTITUDE in this sketch:
  92.  
  93. // If you want to measure altitude, and not pressure, you will instead need
  94. // to provide a known baseline pressure. This is shown at the end of the sketch.
  95.  
  96. // You must first get a temperature measurement to perform a pressure reading.
  97.  
  98. // Start a temperature measurement:
  99. // If request is successful, the number of ms to wait is returned.
  100. // If request is unsuccessful, 0 is returned.
  101.  
  102. status = bmp180.startTemperature();
  103. if (status != 0)
  104. {
  105. // Wait for the measurement to complete:
  106. delay(status);
  107.  
  108. // Retrieve the completed temperature measurement:
  109. // Note that the measurement is stored in the variable T.
  110. // Function returns 1 if successful, 0 if failure.
  111.  
  112. status = bmp180.getTemperature(T);
  113. if (status != 0)
  114. {
  115. // Print out the measurement:
  116. Serial.print("temperature: ");
  117. Serial.print(T,2);
  118. Serial.print(" deg C, ");
  119. Serial.print((9.0/5.0)*T+32.0,2);
  120. Serial.println(" deg F");
  121.  
  122.  
  123. static int lastSendTempInt;
  124. int temp = round(T *10);
  125.  
  126. if (temp != lastSendTempInt)
  127. {
  128. lastSendTempInt = temp;
  129. esp.send(msgTemp.set((float)T, 1));
  130. }
  131.  
  132. // Start a pressure measurement:
  133. // The parameter is the oversampling setting, from 0 to 3 (highest res, longest wait).
  134. // If request is successful, the number of ms to wait is returned.
  135. // If request is unsuccessful, 0 is returned.
  136.  
  137. status = bmp180.startPressure(3);
  138. if (status != 0)
  139. {
  140. // Wait for the measurement to complete:
  141. delay(status);
  142.  
  143. // Retrieve the completed pressure measurement:
  144. // Note that the measurement is stored in the variable P.
  145. // Note also that the function requires the previous temperature measurement (T).
  146. // (If temperature is stable, you can do one temperature measurement for a number of pressure measurements.)
  147. // Function returns 1 if successful, 0 if failure.
  148.  
  149. status = bmp180.getPressure(P,T);
  150. if (status != 0)
  151. {
  152. // The pressure sensor returns abolute pressure, which varies with altitude.
  153. // To remove the effects of altitude, use the sealevel function and your current altitude.
  154. // This number is commonly used in weather reports.
  155. // Parameters: P = absolute pressure in mb, ALTITUDE = current altitude in m.
  156. // Result: p0 = sea-level compensated pressure in mb
  157.  
  158. p0 = bmp180.sealevel(P,ALTITUDE); // we're at 1655 meters (Boulder, CO)
  159. Serial.print("relative (sea-level) pressure: ");
  160. Serial.print(p0,2);
  161. Serial.print(" mb, ");
  162. Serial.print(p0*0.0295333727,2);
  163. Serial.println(" inHg");
  164.  
  165.  
  166. static int lastSendPresInt;
  167. int pres = round(p0 *10);
  168.  
  169. if (pres != lastSendPresInt)
  170. {
  171. lastSendPresInt = pres;
  172. esp.send(msgPress.set((float)p0, 1));
  173. }
  174.  
  175. forecast = calculateForecast(p0);
  176. static int lastSendForeInt = -1;
  177.  
  178.  
  179. if (forecast != lastSendForeInt)
  180. {
  181. lastSendForeInt = forecast;
  182. esp.send(msgForec.set(weather[forecast]));
  183. }
  184. }
  185. else Serial.println("error retrieving pressure measurement\n");
  186. }
  187. else Serial.println("error starting pressure measurement\n");
  188. }
  189. else Serial.println("error retrieving temperature measurement\n");
  190. }
  191. else Serial.println("error starting temperature measurement\n");
  192.  
  193. startTime = millis();
  194. }
  195.  
  196. //delay(5000); // Pause for 5 seconds.
  197. }
  198.  
  199. boolean IsTimeout()
  200. {
  201. unsigned long now = millis();
  202. if (startTime <= now)
  203. {
  204. if ( (unsigned long)(now - startTime ) < MILS_IN_MIN )
  205. return false;
  206. }
  207. else
  208. {
  209. if ( (unsigned long)(startTime - now) < MILS_IN_MIN )
  210. return false;
  211. }
  212.  
  213. return true;
  214. }
  215.  
  216.  
  217. int calculateForecast(double pressure) {
  218. //From 0 to 5 min.
  219. if (minuteCount <= 5){
  220. pressureSamples[0][minuteCount] = pressure;
  221. }
  222. //From 30 to 35 min.
  223. else if ((minuteCount >= 30) && (minuteCount <= 35)){
  224. pressureSamples[1][minuteCount - 30] = pressure;
  225. }
  226. //From 60 to 65 min.
  227. else if ((minuteCount >= 60) && (minuteCount <= 65)){
  228. pressureSamples[2][minuteCount - 60] = pressure;
  229. }
  230. //From 90 to 95 min.
  231. else if ((minuteCount >= 90) && (minuteCount <= 95)){
  232. pressureSamples[3][minuteCount - 90] = pressure;
  233. }
  234. //From 120 to 125 min.
  235. else if ((minuteCount >= 120) && (minuteCount <= 125)){
  236. pressureSamples[4][minuteCount - 120] = pressure;
  237. }
  238. //From 150 to 155 min.
  239. else if ((minuteCount >= 150) && (minuteCount <= 155)){
  240. pressureSamples[5][minuteCount - 150] = pressure;
  241. }
  242. //From 180 to 185 min.
  243. else if ((minuteCount >= 180) && (minuteCount <= 185)){
  244. pressureSamples[6][minuteCount - 180] = pressure;
  245. }
  246. //From 210 to 215 min.
  247. else if ((minuteCount >= 210) && (minuteCount <= 215)){
  248. pressureSamples[7][minuteCount - 210] = pressure;
  249. }
  250. //From 240 to 245 min.
  251. else if ((minuteCount >= 240) && (minuteCount <= 245)){
  252. pressureSamples[8][minuteCount - 240] = pressure;
  253. }
  254.  
  255.  
  256. if (minuteCount == 5) {
  257. // Avg pressure in first 5 min, value averaged from 0 to 5 min.
  258. pressureAvg[0] = ((pressureSamples[0][0] + pressureSamples[0][1]
  259. + pressureSamples[0][2] + pressureSamples[0][3]
  260. + pressureSamples[0][4] + pressureSamples[0][5]) / 6);
  261. }
  262. else if (minuteCount == 35) {
  263. // Avg pressure in 30 min, value averaged from 0 to 5 min.
  264. pressureAvg[1] = ((pressureSamples[1][0] + pressureSamples[1][1]
  265. + pressureSamples[1][2] + pressureSamples[1][3]
  266. + pressureSamples[1][4] + pressureSamples[1][5]) / 6);
  267. float change = (pressureAvg[1] - pressureAvg[0]);
  268. dP_dt = change / 5;
  269. }
  270. else if (minuteCount == 65) {
  271. // Avg pressure at end of the hour, value averaged from 0 to 5 min.
  272. pressureAvg[2] = ((pressureSamples[2][0] + pressureSamples[2][1]
  273. + pressureSamples[2][2] + pressureSamples[2][3]
  274. + pressureSamples[2][4] + pressureSamples[2][5]) / 6);
  275. float change = (pressureAvg[2] - pressureAvg[0]);
  276. dP_dt = change / 10;
  277. }
  278. else if (minuteCount == 95) {
  279. // Avg pressure at end of the hour, value averaged from 0 to 5 min.
  280. pressureAvg[3] = ((pressureSamples[3][0] + pressureSamples[3][1]
  281. + pressureSamples[3][2] + pressureSamples[3][3]
  282. + pressureSamples[3][4] + pressureSamples[3][5]) / 6);
  283. float change = (pressureAvg[3] - pressureAvg[0]);
  284. dP_dt = change / 15;
  285. }
  286. else if (minuteCount == 125) {
  287. // Avg pressure at end of the hour, value averaged from 0 to 5 min.
  288. pressureAvg[4] = ((pressureSamples[4][0] + pressureSamples[4][1]
  289. + pressureSamples[4][2] + pressureSamples[4][3]
  290. + pressureSamples[4][4] + pressureSamples[4][5]) / 6);
  291. float change = (pressureAvg[4] - pressureAvg[0]);
  292. dP_dt = change / 20;
  293. }
  294. else if (minuteCount == 155) {
  295. // Avg pressure at end of the hour, value averaged from 0 to 5 min.
  296. pressureAvg[5] = ((pressureSamples[5][0] + pressureSamples[5][1]
  297. + pressureSamples[5][2] + pressureSamples[5][3]
  298. + pressureSamples[5][4] + pressureSamples[5][5]) / 6);
  299. float change = (pressureAvg[5] - pressureAvg[0]);
  300. dP_dt = change / 25;
  301. }
  302. else if (minuteCount == 185) {
  303. // Avg pressure at end of the hour, value averaged from 0 to 5 min.
  304. pressureAvg[6] = ((pressureSamples[6][0] + pressureSamples[6][1]
  305. + pressureSamples[6][2] + pressureSamples[6][3]
  306. + pressureSamples[6][4] + pressureSamples[6][5]) / 6);
  307. float change = (pressureAvg[6] - pressureAvg[0]);
  308. dP_dt = change / 30;
  309. }
  310. else if (minuteCount == 215) {
  311. // Avg pressure at end of the hour, value averaged from 0 to 5 min.
  312. pressureAvg[7] = ((pressureSamples[7][0] + pressureSamples[7][1]
  313. + pressureSamples[7][2] + pressureSamples[7][3]
  314. + pressureSamples[7][4] + pressureSamples[7][5]) / 6);
  315. float change = (pressureAvg[7] - pressureAvg[0]);
  316. dP_dt = change / 35;
  317. }
  318. else if (minuteCount == 245) {
  319. // Avg pressure at end of the hour, value averaged from 0 to 5 min.
  320. pressureAvg[8] = ((pressureSamples[8][0] + pressureSamples[8][1]
  321. + pressureSamples[8][2] + pressureSamples[8][3]
  322. + pressureSamples[8][4] + pressureSamples[8][5]) / 6);
  323. float change = (pressureAvg[8] - pressureAvg[0]);
  324. dP_dt = change / 40; // note this is for t = 4 hour
  325.  
  326. minuteCount -= 30;
  327. pressureAvg[0] = pressureAvg[1];
  328. pressureAvg[1] = pressureAvg[2];
  329. pressureAvg[2] = pressureAvg[3];
  330. pressureAvg[3] = pressureAvg[4];
  331. pressureAvg[4] = pressureAvg[5];
  332. pressureAvg[5] = pressureAvg[6];
  333. pressureAvg[6] = pressureAvg[7];
  334. pressureAvg[7] = pressureAvg[8];
  335. }
  336.  
  337. minuteCount++;
  338.  
  339. if (minuteCount < 36) //if time is less than 35 min
  340. return 5; // Unknown, more time needed
  341. else if (dP_dt < (-0.25))
  342. return 4; // Quickly falling LP, Thunderstorm, not stable
  343. else if (dP_dt > 0.25)
  344. return 3; // Quickly rising HP, not stable weather
  345. else if ((dP_dt > (-0.25)) && (dP_dt < (-0.05)))
  346. return 2; // Slowly falling Low Pressure System, stable rainy weather
  347. else if ((dP_dt > 0.05) && (dP_dt < 0.25))
  348. return 1; // Slowly rising HP stable good weather
  349. else if ((dP_dt > (-0.05)) && (dP_dt < 0.05))
  350. return 0; // Stable weather
  351. else
  352. return 5; // Unknown
  353. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement