Guest User

Untitled

a guest
Jan 1st, 2015
200
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <SPI.h>
  2. #include <MySensor.h>
  3. #include <Wire.h>
  4. #include <Adafruit_BMP085.h>
  5. #include <DHT.h>
  6. #include <Time.h>
  7. #include <DS3232RTC.h>
  8.  
  9. Adafruit_BMP085 bmp = Adafruit_BMP085(); // Digital Pressure Sensor
  10. MySensor gw;
  11. DHT dht_out;
  12. DHT dht_in;
  13. float t_in;
  14. float t_in2;
  15. float h_in;
  16. float t_out;
  17. float h_out;
  18. int pressure;
  19. int y = 0;
  20. unsigned long dht_out_time = 0;
  21. unsigned long dht_in_time = 0;
  22. unsigned long bmp_time = 0;
  23. unsigned long last_forecast = 0;
  24. unsigned long last_display_update = 0;
  25. bool dht_in_ok = false;
  26. bool dht_out_ok = false;
  27. boolean timeReceived = false;
  28. unsigned long lastUpdate=0, lastRequest=0;
  29.  
  30. short int forecast = 5;
  31.  
  32. unsigned long last_radio_check = 0;
  33.  
  34. static char *weather[] = {"stable","sunny","cloudy","unstable","thunderstorm","unknown"};
  35.  
  36.  
  37. MyMessage tempMsg(1, V_TEMP);
  38. MyMessage pressureMsg(0, V_PRESSURE);
  39. MyMessage forecastMsg(0, V_FORECAST);
  40. MyMessage msgHum_in(2, V_HUM);
  41. MyMessage msgTemp_in(3, V_TEMP);
  42. MyMessage msgHum_out(4, V_HUM);
  43. MyMessage msgTemp_out(5, V_TEMP);
  44.  
  45.  
  46. void setup()
  47. {
  48. gw.begin();
  49.  
  50. // Send the sketch version information to the gateway and Controller
  51. gw.sendSketchInfo("Weather Sensor", "1.0");
  52. setSyncProvider(RTC.get);
  53. gw.requestTime(receiveTime);
  54. if (!bmp.begin())
  55. {
  56. Serial.println(F("Could not find a valid BMP085 sensor, check wiring!"));
  57. while (1) { }
  58. }
  59. dht_out.setup(4);
  60. dht_in.setup(8);
  61. // Register sensors to gw (they will be created as child devices)
  62. //gw.present(0, S_BARO);
  63. gw.present(0, S_BARO);
  64. gw.present(1, S_TEMP);
  65. gw.present(2, S_HUM);
  66. gw.present(3, S_TEMP);
  67. gw.present(4, S_HUM);
  68. gw.present(5, S_TEMP);
  69.  
  70. pinMode(3, OUTPUT);
  71. pinMode(5, OUTPUT);
  72. pinMode(6, OUTPUT);
  73. digitalWrite(3, 1);
  74. digitalWrite(5, 1);
  75. digitalWrite(6, 1);
  76.  
  77.  
  78.  
  79. }
  80.  
  81. void loop()
  82. {
  83. gw.process();
  84.  
  85. //Display
  86. if (millis() > last_display_update + 3000)
  87. {
  88.  
  89. tmElements_t tm;
  90. RTC.read(tm);
  91. Serial.print(tm.Day);
  92. Serial.print("/");
  93. Serial.print(tm.Month);
  94. Serial.print(" ");
  95. Serial.print(tm.Hour);
  96. Serial.print(":");
  97. Serial.print(tm.Minute);
  98. Serial.print(":");
  99. Serial.println(tm.Second);
  100. last_display_update = millis();
  101. }
  102.  
  103.  
  104. if (millis() > bmp_time + 3000)
  105. {
  106. pressure = bmp.readSealevelPressure(369)/100;
  107. t_in = bmp.readTemperature();
  108.  
  109. Serial.print(F("T IN:"));
  110. Serial.print(t_in);
  111. Serial.print(F("P IN:"));
  112. Serial.print(pressure);
  113. Serial.print(F(" HPa "));
  114. Serial.println(weather[forecast]);
  115. bmp_time = millis();
  116. }
  117.  
  118. //Time
  119. if ((!timeReceived && millis()-lastRequest > 10*1000)
  120. || (timeReceived && millis()-lastRequest > 60*1000*60))
  121. {
  122. Serial.println(F("requesting time"));
  123. gw.requestTime(receiveTime);
  124. lastRequest = millis();
  125. }
  126. //DHT
  127. if (millis() > dht_out_time + dht_out.getMinimumSamplingPeriod())
  128. {
  129. Serial.print(F("DHT OUT: "));
  130. t_out = dht_out.getTemperature();
  131. if (isnan(t_out))
  132. {
  133. Serial.println(F("Failed reading from DHT OUT"));
  134. dht_out_ok = false;
  135. }
  136. else
  137. {
  138. dht_out_ok = true;
  139. }
  140. Serial.print(F("T:"));
  141. Serial.print(t_out);
  142.  
  143. h_out = dht_out.getHumidity();
  144. if (isnan(h_out))
  145. {
  146. Serial.println(F("Failed reading from DHT OUT"));
  147. dht_out_ok = false;
  148. }
  149. else dht_out_ok = true;
  150.  
  151. Serial.print(F(" H:"));
  152. Serial.println(h_out);
  153. dht_out_time = millis();
  154. }
  155. if (millis() > dht_in_time + dht_in.getMinimumSamplingPeriod())
  156. {
  157. Serial.print(F("DHT IN: "));
  158. t_in2 = dht_in.getTemperature();
  159. if (isnan(t_in2))
  160. {
  161. Serial.println(F("Failed reading from DHT IN"));
  162. dht_in_ok = false;
  163. }
  164. else
  165. {
  166.  
  167. dht_in_ok = true;
  168. }
  169. Serial.print(F("T:"));
  170. Serial.print(t_in2);
  171.  
  172. h_in = dht_in.getHumidity();
  173. if (isnan(h_in))
  174. {
  175. Serial.println(F("Failed reading from DHT IN"));
  176. dht_in_ok = false;
  177. }
  178. else
  179. {
  180.  
  181. dht_in_ok = true;
  182. }
  183. Serial.print(F(" H:"));
  184. Serial.println(h_in);
  185. dht_in_time = millis();
  186. }
  187.  
  188.  
  189.  
  190. //forecast
  191. /*
  192. DP/Dt explanation
  193.  
  194. 0 = "Stable Weather Pattern"
  195. 1 = "Slowly rising Good Weather", "Clear/Sunny "
  196. 2 = "Slowly falling L-Pressure ", "Cloudy/Rain "
  197. 3 = "Quickly rising H-Press", "Not Stable"
  198. 4 = "Quickly falling L-Press", "Thunderstorm"
  199. 5 = "Unknown (More Time needed)
  200. */
  201.  
  202. if (millis() >= last_forecast + 6000)
  203. {
  204.  
  205. y++; //for testing
  206. forecast = sample(y);
  207.  
  208. last_forecast = millis();
  209. }
  210.  
  211. //RADIO
  212. if (millis() > last_radio_check + 10000)
  213. {
  214. Serial.println(F("Sending information"));
  215. last_radio_check = millis();
  216. gw.send(tempMsg.set(t_in,1));
  217. gw.send(pressureMsg.set(pressure, 0));
  218. gw.send(msgHum_in.set(h_in, 2));
  219. gw.send(msgTemp_in.set(t_in2, 3));
  220. gw.send(msgHum_out.set(h_out, 4));
  221. gw.send(msgTemp_out.set(t_out, 5));
  222. gw.send(forecastMsg.set(weather[forecast]));
  223. }
  224. }
  225.  
  226. int sample(int pressure)
  227. {
  228. // Algorithm found here
  229. // http://www.freescale.com/files/sensors/doc/app_note/AN3914.pdf
  230.  
  231. static int minuteCount;
  232. static int hoursCount;
  233. static int cycleCount;
  234. static float pressureAvgAllTime;
  235. static float minPressureAvg;
  236. static float maxPressureAvg;
  237. static int pressureSamples[4];
  238. static int dP_dt;
  239. static bool forecast_debug = true;
  240. static float lastHoursPressureAvg[12];
  241. static float pressureAvg[24][3];
  242. if (forecast_debug == true)
  243. {
  244. Serial.print(F("Forecast update, minute:"));
  245. Serial.print(minuteCount);
  246. Serial.print(F(", hoursCount:"));
  247. Serial.print(hoursCount);
  248. Serial.print(F(", cycleCount:"));
  249. Serial.print(cycleCount);
  250. Serial.print(F(", dP_dt:"));
  251. Serial.print(dP_dt);
  252. Serial.print(F(", pressureAvgAllTime:"));
  253. Serial.print(pressureAvgAllTime);
  254. Serial.print(F(", minPressureAvg:"));
  255. Serial.print(minPressureAvg);
  256. Serial.print(F(", maxPressureAvg:"));
  257. Serial.print(maxPressureAvg);
  258. Serial.print(F(", sample:"));
  259. Serial.print(pressure);
  260. Serial.print(F(", lastHoursPressureAvg"));
  261. Serial.print(lastHoursPressureAvg[((minuteCount + 1)/5) - 1]);
  262. Serial.print(F(" uptime:"));
  263.  
  264. Serial.println(millis());
  265.  
  266. int h = 0;
  267. int i = 0;
  268.  
  269. while (h != 25)
  270. {
  271. Serial.print(" pressureAvg");
  272. Serial.print(h);
  273. Serial.print("_");
  274. Serial.print(i);
  275. Serial.print(":");
  276. Serial.print(pressureAvg[h][i]);
  277. i++;
  278. if (i == 4)
  279. {
  280. i = 0;
  281. h++;
  282. Serial.println("");
  283. }
  284. }
  285.  
  286.  
  287. }
  288.  
  289. pressureSamples[cycleCount] = pressure;
  290.  
  291. if (cycleCount == 4)
  292. {
  293. cycleCount = -1;
  294. lastHoursPressureAvg[((minuteCount + 1)/5) - 1] = (pressureSamples[0] + pressureSamples[1] + pressureSamples[2] + pressureSamples[3] + pressureSamples[4]) / 5;
  295. Serial.print(((minuteCount + 1)/5) - 1);
  296. Serial.print(F("pressureAvg last 5 min: "));
  297. Serial.println(lastHoursPressureAvg[((minuteCount + 1)/5) - 1]);
  298.  
  299. if (hoursCount == 0 && minuteCount == 4)
  300. {
  301. pressureAvgAllTime = lastHoursPressureAvg[0];
  302. maxPressureAvg = lastHoursPressureAvg[0];
  303. minPressureAvg = lastHoursPressureAvg[0];
  304. pressureAvg[0][0] = lastHoursPressureAvg[0];
  305. }
  306. else
  307. {
  308. pressureAvgAllTime = (pressureAvgAllTime + lastHoursPressureAvg[((minuteCount + 1)/5) - 1]) / 2;
  309. if (lastHoursPressureAvg[((minuteCount + 1)/5) - 1] < minPressureAvg) minPressureAvg = lastHoursPressureAvg[((minuteCount + 1)/5) - 1];
  310. if (lastHoursPressureAvg[((minuteCount + 1)/5) - 1] > maxPressureAvg) maxPressureAvg = lastHoursPressureAvg[((minuteCount + 1)/5) - 1];
  311. }
  312.  
  313. if ((minuteCount + 1) % 15 == 0)
  314. {
  315. pressureAvg[hoursCount][((minuteCount + 1) / 15) -1 ] = (lastHoursPressureAvg[((minuteCount + 1)/5) - 1] + lastHoursPressureAvg[((minuteCount + 1)/5) - 2] + lastHoursPressureAvg[((minuteCount + 1)/5) - 3]) / 3;
  316. }
  317.  
  318.  
  319. }
  320.  
  321. if (minuteCount + 1 == 60)
  322. {
  323. minuteCount = -1;
  324. hoursCount++;
  325. }
  326.  
  327. minuteCount++;
  328. cycleCount++;
  329. if (hoursCount == 0 && minuteCount < 30) return 5;
  330. return 5;
  331.  
  332. }
  333.  
  334. void receiveTime(unsigned long controllerTime)
  335. {
  336. // Ok, set incoming time
  337. controllerTime = controllerTime + 3600;
  338. Serial.print("Time value received: ");
  339. Serial.println(controllerTime);
  340. RTC.set(controllerTime); // this sets the RTC to the time from controller - which we do want periodically
  341. timeReceived = true;
  342. }
RAW Paste Data