Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <MySensor.h>
- #include <Wire.h>
- #include <Adafruit_BMP085.h>
- #include <DHT.h>
- #include <Time.h>
- #include <DS3232RTC.h>
- Adafruit_BMP085 bmp = Adafruit_BMP085(); // Digital Pressure Sensor
- MySensor gw;
- DHT dht_out;
- DHT dht_in;
- float t_in;
- float t_in2;
- float h_in;
- float t_out;
- float h_out;
- int pressure;
- int y = 0;
- unsigned long dht_out_time = 0;
- unsigned long dht_in_time = 0;
- unsigned long bmp_time = 0;
- unsigned long last_forecast = 0;
- unsigned long last_display_update = 0;
- bool dht_in_ok = false;
- bool dht_out_ok = false;
- boolean timeReceived = false;
- unsigned long lastUpdate=0, lastRequest=0;
- short int forecast = 5;
- unsigned long last_radio_check = 0;
- static char *weather[] = {"stable","sunny","cloudy","unstable","thunderstorm","unknown"};
- MyMessage tempMsg(1, V_TEMP);
- MyMessage pressureMsg(0, V_PRESSURE);
- MyMessage forecastMsg(0, V_FORECAST);
- MyMessage msgHum_in(2, V_HUM);
- MyMessage msgTemp_in(3, V_TEMP);
- MyMessage msgHum_out(4, V_HUM);
- MyMessage msgTemp_out(5, V_TEMP);
- void setup()
- {
- gw.begin();
- // Send the sketch version information to the gateway and Controller
- gw.sendSketchInfo("Weather Sensor", "1.0");
- setSyncProvider(RTC.get);
- gw.requestTime(receiveTime);
- if (!bmp.begin())
- {
- Serial.println(F("Could not find a valid BMP085 sensor, check wiring!"));
- while (1) { }
- }
- dht_out.setup(4);
- dht_in.setup(8);
- // Register sensors to gw (they will be created as child devices)
- //gw.present(0, S_BARO);
- gw.present(0, S_BARO);
- gw.present(1, S_TEMP);
- gw.present(2, S_HUM);
- gw.present(3, S_TEMP);
- gw.present(4, S_HUM);
- gw.present(5, S_TEMP);
- pinMode(3, OUTPUT);
- pinMode(5, OUTPUT);
- pinMode(6, OUTPUT);
- digitalWrite(3, 1);
- digitalWrite(5, 1);
- digitalWrite(6, 1);
- }
- void loop()
- {
- gw.process();
- //Display
- if (millis() > last_display_update + 3000)
- {
- tmElements_t tm;
- RTC.read(tm);
- Serial.print(tm.Day);
- Serial.print("/");
- Serial.print(tm.Month);
- Serial.print(" ");
- Serial.print(tm.Hour);
- Serial.print(":");
- Serial.print(tm.Minute);
- Serial.print(":");
- Serial.println(tm.Second);
- last_display_update = millis();
- }
- if (millis() > bmp_time + 3000)
- {
- pressure = bmp.readSealevelPressure(369)/100;
- t_in = bmp.readTemperature();
- Serial.print(F("T IN:"));
- Serial.print(t_in);
- Serial.print(F("P IN:"));
- Serial.print(pressure);
- Serial.print(F(" HPa "));
- Serial.println(weather[forecast]);
- bmp_time = millis();
- }
- //Time
- if ((!timeReceived && millis()-lastRequest > 10*1000)
- || (timeReceived && millis()-lastRequest > 60*1000*60))
- {
- Serial.println(F("requesting time"));
- gw.requestTime(receiveTime);
- lastRequest = millis();
- }
- //DHT
- if (millis() > dht_out_time + dht_out.getMinimumSamplingPeriod())
- {
- Serial.print(F("DHT OUT: "));
- t_out = dht_out.getTemperature();
- if (isnan(t_out))
- {
- Serial.println(F("Failed reading from DHT OUT"));
- dht_out_ok = false;
- }
- else
- {
- dht_out_ok = true;
- }
- Serial.print(F("T:"));
- Serial.print(t_out);
- h_out = dht_out.getHumidity();
- if (isnan(h_out))
- {
- Serial.println(F("Failed reading from DHT OUT"));
- dht_out_ok = false;
- }
- else dht_out_ok = true;
- Serial.print(F(" H:"));
- Serial.println(h_out);
- dht_out_time = millis();
- }
- if (millis() > dht_in_time + dht_in.getMinimumSamplingPeriod())
- {
- Serial.print(F("DHT IN: "));
- t_in2 = dht_in.getTemperature();
- if (isnan(t_in2))
- {
- Serial.println(F("Failed reading from DHT IN"));
- dht_in_ok = false;
- }
- else
- {
- dht_in_ok = true;
- }
- Serial.print(F("T:"));
- Serial.print(t_in2);
- h_in = dht_in.getHumidity();
- if (isnan(h_in))
- {
- Serial.println(F("Failed reading from DHT IN"));
- dht_in_ok = false;
- }
- else
- {
- dht_in_ok = true;
- }
- Serial.print(F(" H:"));
- Serial.println(h_in);
- dht_in_time = millis();
- }
- //forecast
- /*
- DP/Dt explanation
- 0 = "Stable Weather Pattern"
- 1 = "Slowly rising Good Weather", "Clear/Sunny "
- 2 = "Slowly falling L-Pressure ", "Cloudy/Rain "
- 3 = "Quickly rising H-Press", "Not Stable"
- 4 = "Quickly falling L-Press", "Thunderstorm"
- 5 = "Unknown (More Time needed)
- */
- if (millis() >= last_forecast + 6000)
- {
- y++; //for testing
- forecast = sample(y);
- last_forecast = millis();
- }
- //RADIO
- if (millis() > last_radio_check + 10000)
- {
- Serial.println(F("Sending information"));
- last_radio_check = millis();
- gw.send(tempMsg.set(t_in,1));
- gw.send(pressureMsg.set(pressure, 0));
- gw.send(msgHum_in.set(h_in, 2));
- gw.send(msgTemp_in.set(t_in2, 3));
- gw.send(msgHum_out.set(h_out, 4));
- gw.send(msgTemp_out.set(t_out, 5));
- gw.send(forecastMsg.set(weather[forecast]));
- }
- }
- int sample(int pressure)
- {
- // Algorithm found here
- // http://www.freescale.com/files/sensors/doc/app_note/AN3914.pdf
- static int minuteCount;
- static int hoursCount;
- static int cycleCount;
- static float pressureAvgAllTime;
- static float minPressureAvg;
- static float maxPressureAvg;
- static int pressureSamples[4];
- static int dP_dt;
- static bool forecast_debug = true;
- static float lastHoursPressureAvg[12];
- static float pressureAvg[24][3];
- if (forecast_debug == true)
- {
- Serial.print(F("Forecast update, minute:"));
- Serial.print(minuteCount);
- Serial.print(F(", hoursCount:"));
- Serial.print(hoursCount);
- Serial.print(F(", cycleCount:"));
- Serial.print(cycleCount);
- Serial.print(F(", dP_dt:"));
- Serial.print(dP_dt);
- Serial.print(F(", pressureAvgAllTime:"));
- Serial.print(pressureAvgAllTime);
- Serial.print(F(", minPressureAvg:"));
- Serial.print(minPressureAvg);
- Serial.print(F(", maxPressureAvg:"));
- Serial.print(maxPressureAvg);
- Serial.print(F(", sample:"));
- Serial.print(pressure);
- Serial.print(F(", lastHoursPressureAvg"));
- Serial.print(lastHoursPressureAvg[((minuteCount + 1)/5) - 1]);
- Serial.print(F(" uptime:"));
- Serial.println(millis());
- int h = 0;
- int i = 0;
- while (h != 25)
- {
- Serial.print(" pressureAvg");
- Serial.print(h);
- Serial.print("_");
- Serial.print(i);
- Serial.print(":");
- Serial.print(pressureAvg[h][i]);
- i++;
- if (i == 4)
- {
- i = 0;
- h++;
- Serial.println("");
- }
- }
- }
- pressureSamples[cycleCount] = pressure;
- if (cycleCount == 4)
- {
- cycleCount = -1;
- lastHoursPressureAvg[((minuteCount + 1)/5) - 1] = (pressureSamples[0] + pressureSamples[1] + pressureSamples[2] + pressureSamples[3] + pressureSamples[4]) / 5;
- Serial.print(((minuteCount + 1)/5) - 1);
- Serial.print(F("pressureAvg last 5 min: "));
- Serial.println(lastHoursPressureAvg[((minuteCount + 1)/5) - 1]);
- if (hoursCount == 0 && minuteCount == 4)
- {
- pressureAvgAllTime = lastHoursPressureAvg[0];
- maxPressureAvg = lastHoursPressureAvg[0];
- minPressureAvg = lastHoursPressureAvg[0];
- pressureAvg[0][0] = lastHoursPressureAvg[0];
- }
- else
- {
- pressureAvgAllTime = (pressureAvgAllTime + lastHoursPressureAvg[((minuteCount + 1)/5) - 1]) / 2;
- if (lastHoursPressureAvg[((minuteCount + 1)/5) - 1] < minPressureAvg) minPressureAvg = lastHoursPressureAvg[((minuteCount + 1)/5) - 1];
- if (lastHoursPressureAvg[((minuteCount + 1)/5) - 1] > maxPressureAvg) maxPressureAvg = lastHoursPressureAvg[((minuteCount + 1)/5) - 1];
- }
- if ((minuteCount + 1) % 15 == 0)
- {
- pressureAvg[hoursCount][((minuteCount + 1) / 15) -1 ] = (lastHoursPressureAvg[((minuteCount + 1)/5) - 1] + lastHoursPressureAvg[((minuteCount + 1)/5) - 2] + lastHoursPressureAvg[((minuteCount + 1)/5) - 3]) / 3;
- }
- }
- if (minuteCount + 1 == 60)
- {
- minuteCount = -1;
- hoursCount++;
- }
- minuteCount++;
- cycleCount++;
- if (hoursCount == 0 && minuteCount < 30) return 5;
- return 5;
- }
- void receiveTime(unsigned long controllerTime)
- {
- // Ok, set incoming time
- controllerTime = controllerTime + 3600;
- Serial.print("Time value received: ");
- Serial.println(controllerTime);
- RTC.set(controllerTime); // this sets the RTC to the time from controller - which we do want periodically
- timeReceived = true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement