#include #include #include #define SENSOR_PIN1 32 Preferences preferences; OneWire oneWire(SENSOR_PIN1); DallasTemperature DS18B20(&oneWire); const long interval = 600000; // 10 minutes in milliseconds const int numReadings = 144; // Number of readings in 24 hours float incrementAvg1 = 0; // will hold 10 min avg for sensor 1 float incrementAvg2 = 0; // will hold 10 min avg for sensor 2 float recurringIndex = 0; //this will count the number of records made in the 10 min period, and be used for 10 min avg float readings1[numReadings]; //keeps track of all 10 minute averages for both sensors float readings2[numReadings]; int readingIndex = 0; // indicate which index is currently being read long lastReadingTime = 0; // indicate the last time a 10 minute average occured float numberRead = 0; // indicates the number of measuremens taken in order to calculate the average //initializes variables to hold highest, lowest, and overall temp readings for sensor 1 and 2 float highestValue1 = -9999; float lowestValue1 = 9999; float overallAverage1 = 0; float highestValue2 = -9999; float lowestValue2 = 9999; float overallAverage2 = 0; float temp1F; float temp2F; void setup() { Serial.begin(115200); DS18B20.begin(); preferences.begin("my-app", false); incrementAvg1 = preferences.getFloat("incrementAvg1", 0.0f); // sets default value to zero, if key is not retrieved incrementAvg2 = preferences.getFloat("incrementAvg2", 0.0f); readingIndex = preferences.getInt("readingIndex", 0); lowestValue1 = preferences.getFloat("lowestValue1", 9999.0f); lowestValue2 = preferences.getFloat("lowestValue2", 9999.0f); highestValue2 = preferences.getFloat("highestValue2", -9999.0f); highestValue1 = preferences.getFloat("highestValue1", -9999.0f); overallAverage1 = preferences.getFloat("overallAverage1", 0.0f); overallAverage2 = preferences.getFloat("overallAverage2", 0.0f); preferences.getBytes("readings1", readings1, sizeof(readings1)); preferences.getBytes("readings2", readings2, sizeof(readings2)); recurringIndex = preferences.getInt("recurringIndex", 0); preferences.end();// close preferences } void loop() { preferences.begin("my-app", false); //retrieve live temperature from sensors DS18B20.requestTemperatures(); temp1F = DS18B20.getTempCByIndex(0); temp2F = DS18B20.getTempCByIndex(1); // convert temp to F temp1F = temp1F * 9 / 5 + 32; temp2F = temp2F * 9 / 5 + 32; // Stream live temp data to serial port float liveValue1 = temp1F; float liveValue2 = temp2F; Serial.print("Sensor1 "); Serial.print(liveValue1); Serial.print(" "); Serial.print("Sensor2 "); Serial.println(liveValue2); // Record data every 10 minutes if (millis() - lastReadingTime >= interval) { lastReadingTime = millis(); // sets current time to last read time //calculate the average for this 10 minute period incrementAvg1 = incrementAvg1 / recurringIndex; incrementAvg2 = incrementAvg2 / recurringIndex; //The following code calculates an incremental average based on the previous overall average, and the number of averages already recorded // Update overall average for sensor 1 overallAverage1 = (overallAverage1 * (readingIndex) + incrementAvg1) / (readingIndex + 1); // Update overall average for sensor 2 overallAverage2 = (overallAverage2 * (readingIndex) + incrementAvg2) / (readingIndex + 1); //record 10 min avg in their respective arrays readings1[readingIndex] = incrementAvg1; readings2[readingIndex] = incrementAvg2; readingIndex++; // if all the 10 minute periods for 24 hours have already occured if (readingIndex >= numReadings) { readingIndex = 0; } recurringIndex = 0; }// end 10 min recording // Update highest and lowest values for sensor 1 if (liveValue1 > highestValue1) { highestValue1 = liveValue1; } if (liveValue1 < lowestValue1) { lowestValue1 = liveValue1; } // Update highest and lowest values for sensor 2 if (liveValue2 > highestValue2) { highestValue2 = liveValue2; } if (liveValue2 < lowestValue2) { lowestValue2 = liveValue2; } incrementAvg1 += liveValue1; incrementAvg2 += liveValue2; recurringIndex++; // Check for command from serial port if (Serial.available() > 0) { char command = Serial.read(); if (command == 'p') { printSummary(); } } // update the values of each variable, stored in preferences preferences.putFloat("incrementAvg1", incrementAvg1); preferences.putFloat("incrementAvg2", incrementAvg2); preferences.putInt("readingIndex", readingIndex); preferences.putFloat("lowestValue1", lowestValue1); preferences.putFloat("lowestValue2", lowestValue2); preferences.putFloat("highestValue2", highestValue2); preferences.putFloat("highestValue1", highestValue1); preferences.putFloat("overallAverage1", overallAverage1); preferences.putFloat("overallAverage2", overallAverage2); preferences.putBytes("readings1", readings1, sizeof(readings1)); preferences.putBytes("readings2", readings2, sizeof(readings2)); preferences.getInt("recurringIndex", recurringIndex); preferences.end(); delay(1000); }// end loop() void printSummary() { Serial.println("Sensor #1"); Serial.println("10 minute averages:"); for (int i = 0; i < numReadings; i++) { Serial.println(readings1[i]); } Serial.print("Highest value: "); Serial.println(highestValue1); Serial.print("Lowest value: "); Serial.println(lowestValue1); Serial.print("Overall average: "); Serial.println(overallAverage1); Serial.println("Sensor #2"); Serial.println("10 minute averages:"); for (int i = 0; i < numReadings; i++) { Serial.println(readings2[i]); } Serial.print("Highest value: "); Serial.println(highestValue2); Serial.print("Lowest value: "); Serial.println(lowestValue2); Serial.print("Overall average: "); Serial.println(overallAverage2); }