Advertisement
Guest User

TempearureSwitch.cpp

a guest
Jun 25th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.47 KB | None | 0 0
  1. #include "TemperatureSwitch.h"
  2.  
  3. void handleButtonPressed() {
  4.   // Exit if already pressed
  5.   if (buttonPressed) return;
  6.  
  7.   //startMillis = millis();
  8.  
  9.   // Set flag
  10.   buttonPressed = true;
  11.   waitingTime = false;
  12.  
  13.   // Log
  14.   if (debug) Serial.println("Button pressed!");
  15.  
  16.   // Flag to store actual time
  17.   getTime = true;
  18. }
  19.  
  20. void checkTemperature() {
  21.   float actualTemperature = temperatureSensor.readTemperatureC();
  22.   /*if (!step) {
  23.     actualTemperature -= 1;
  24.     if (actualTemperature < 16) step = true;
  25.   } else {
  26.     actualTemperature += 1;
  27.     if (actualTemperature > 22) step = false;
  28.   }
  29.   delay(1000);*/
  30.   if (debug) {
  31.     Serial.print("Temperature: ");
  32.     Serial.print(String(actualTemperature));
  33.     Serial.println(" C");
  34.   }
  35.  
  36.   if ((lowTemperatureReached(actualTemperature)) && (relayPin2.value() == LOW)) {
  37.     if (debug) Serial.println("lowTemp - Low TEMP reached!");
  38.  
  39.     // Enable pin
  40.     relayPin2.on();
  41.     temperatureTime = actualTime;
  42.  
  43.     // Count the difference, where to stop
  44.     if ((temperatureTime.minute + minutesToRun) >= 60) {
  45.       stopMinute = (temperatureTime.minute + minutesToRun) - 60;
  46.       if (stopMinute < 0) stopMinute = 0;
  47.     } else {
  48.       stopMinute = temperatureTime.minute + minutesToRun;
  49.     }
  50.   } else if ((highTempeartureReached(actualTemperature)) && (relayPin2.value() == HIGH)) {
  51.     if (debug) Serial.println("lowTemp - High TEMP reached!");
  52.  
  53.     // Disable pin
  54.     relayPin2.off();
  55.  
  56.     waitTime = actualTime;
  57.     waitingTime = true;
  58.     if ((waitTime.minute + minutesToRun) >= 60) {
  59.       stopMinute = (waitTime.minute + minutesToRun) - 60;
  60.       if (stopMinute < 0) stopMinute = 0;
  61.     } else {
  62.       stopMinute = waitTime.minute + minutesToRun;
  63.     }
  64.   }
  65.  
  66.   // Stop after 10 minutes
  67.   if ((relayPin2.value() == HIGH) && (actualTime.minute == stopMinute)) {
  68.     if (debug) Serial.print("lowTemp - TIME reached, stopping...");
  69.     relayPin2.off();
  70.  
  71.     waitTime = actualTime;
  72.     waitingTime = true;
  73.     if ((waitTime.minute + minutesToRun) >= 60) {
  74.       stopMinute = (waitTime.minute + minutesToRun) - 60;
  75.       if (stopMinute < 0) stopMinute = 0;
  76.     } else {
  77.       stopMinute = waitTime.minute + minutesToRun;
  78.     }
  79.   }
  80. }
  81.  
  82. void getDateTime() {
  83.   if (!Rtc.IsDateTimeValid()) {
  84.     // Common Cuases:
  85.     //    1) the battery on the device is low or even missing and the power line was disconnected
  86.     Serial.println("RTC lost confidence in the DateTime!");
  87.   }
  88.  
  89.   RtcDateTime now = Rtc.GetDateTime();
  90.  
  91.   actualTime.hour = now.Hour();
  92.   actualTime.minute = now.Minute();
  93.   actualTime.second = now.Second();
  94.  
  95.   /*if (debug) {
  96.     Serial.print("Time: ");
  97.     Serial.print(String(actualTime.hour));
  98.     Serial.print(":");
  99.     Serial.print(String(actualTime.minute));
  100.     Serial.print(":");
  101.     Serial.println(String(actualTime.second));
  102.   }*/
  103. }
  104.  
  105. bool highTempeartureReached(float temperature) {
  106.   if (temperature >= highTemperature) {
  107.     return true;
  108.   }
  109.  
  110.   return false;
  111. }
  112.  
  113. bool lowTemperatureReached(float temperature) {
  114.   if (temperature <= lowTemperature) {
  115.     return true;
  116.   }
  117.  
  118.   return false;
  119. }
  120.  
  121. void setupPins() {
  122.   if (debug) Serial.println("Initializing pins...");
  123.  
  124.   // Disable led pin
  125.   ledPin.setOutput(false);
  126.  
  127.   // Disable relay Pins
  128.   relayPin1.setOutput(false);
  129.   relayPin2.setOutput(false);
  130.  
  131.   // Prepare interrupt pin
  132.   pinMode(buttonPin, INPUT_PULLUP);
  133.   attachInterrupt(buttonInterruptPin, handleButtonPressed, FALLING);
  134. }
  135.  
  136. void setupRTC() {
  137.   Serial.println("Initializing RTC...");
  138.   //--------RTC SETUP ------------
  139.   // if you are using ESP-01 then uncomment the line below to reset the pins to
  140.   // the available pins for SDA, SCL
  141.   // Wire.begin(0, 2); // due to limited pins, use pin 0 and 2 for SDA, SCL
  142.   Rtc.Begin();
  143.  
  144.   RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
  145.  
  146.   if (!Rtc.IsDateTimeValid()) {
  147.     // Common Cuases:
  148.     //    1) first time you ran and the device wasn't running yet
  149.     //    2) the battery on the device is low or even missing
  150.  
  151.     Serial.println("RTC lost confidence in the DateTime!");
  152.  
  153.     // following line sets the RTC to the date & time this sketch was compiled
  154.     // it will also reset the valid flag internally unless the Rtc device is
  155.     // having an issue
  156.  
  157.     Rtc.SetDateTime(compiled);
  158.   }
  159.  
  160.   if (!Rtc.GetIsRunning()) {
  161.     Serial.println("RTC was not actively running, starting now");
  162.     Rtc.SetIsRunning(true);
  163.   }
  164.  
  165.   RtcDateTime now = Rtc.GetDateTime();
  166.   if (now < compiled) {
  167.     Serial.println("RTC is older than compile time!  (Updating DateTime)");
  168.     Rtc.SetDateTime(compiled);
  169.   } else if (now > compiled)  {
  170.     Serial.println("RTC is newer than compile time. (this is expected)");
  171.   } else if (now == compiled) {
  172.     Serial.println("RTC is the same as compile time! (not expected but all is fine)");
  173.   }
  174.  
  175.   // never assume the Rtc was last configured by you, so
  176.   // just clear them to your needed state
  177.   Rtc.SetSquareWavePin(DS1307SquareWaveOut_Low);
  178. }
  179.  
  180. void setup() {
  181.   // Set serial console
  182.   Serial.begin(115200);
  183.   //while (!Serial);
  184.  
  185.   // Setup pins
  186.   setupPins();
  187.  
  188.   // Setup RTC
  189.   setupRTC();
  190. }
  191.  
  192. void loop() {
  193.   // Get actual time
  194.   getDateTime();
  195.  
  196.   if (!buttonPressed) {
  197.     if ((actualTime.hour >= startHour) || (actualTime.hour < endHour)) {
  198.       if (!waitingTime) {
  199.         if (debug) {
  200.           Serial.print("ActualMinute: ");
  201.           Serial.print(String(actualTime.minute));
  202.           Serial.print("/ StopMinute: ");
  203.           Serial.println(String(stopMinute));
  204.         }
  205.  
  206.         checkTemperature();
  207.       } else {
  208.         // Stop after 10 minutes
  209.         /*if (debug) {
  210.           Serial.print("waiting: ");
  211.           Serial.print(String(actualTime.minute));
  212.           Serial.print("/");
  213.           Serial.println(String(stopMinute));
  214.           Serial.println(String(relayPin2.value()));
  215.         }*/
  216.         if ((relayPin2.value() == LOW) && (actualTime.minute == stopMinute)) {
  217.           waitingTime = false;
  218.           stopMinute = -1;
  219.         }
  220.       }
  221.     } else {
  222.       //Whatever
  223.     }
  224.   } else {
  225.     if (getTime) {
  226.       // Set button time
  227.       buttonTime = actualTime;
  228.  
  229.       if (debug) {
  230.         Serial.print(String(buttonTime.hour));
  231.         Serial.print(":");
  232.         Serial.print(String(buttonTime.minute));
  233.         Serial.println(" - Button Time");
  234.       }
  235.  
  236.       // Reset flag
  237.       getTime = false;
  238.  
  239.       // Set pin
  240.       relayPin2.on();
  241.     }
  242.  
  243.     if ((buttonTime.minute + minutesToRun) >= 60) {
  244.       stopMinute = (buttonTime.minute + minutesToRun) - 60;
  245.       if (stopMinute < 0) stopMinute = 0;
  246.     } else {
  247.       stopMinute = buttonTime.minute + minutesToRun;
  248.     }
  249.  
  250.     if (debug) {
  251.       Serial.print("button: ");
  252.       Serial.print(String(actualTime.minute));
  253.       Serial.print(" - Stop minute: ");
  254.       Serial.println(String(stopMinute));
  255.  
  256.       Serial.print("Temperature ");
  257.       Serial.print(String(temperatureSensor.readTemperatureC()));
  258.       Serial.println(" C");
  259.     }
  260.  
  261.     if ((actualTime.minute == stopMinute) || (highTempeartureReached(temperatureSensor.readTemperatureC()))) {
  262.       // Disable
  263.       buttonPressed = false;
  264.  
  265.        // Disable relay
  266.       relayPin2.off();
  267.  
  268.       waitTime = actualTime;
  269.       waitingTime = true;
  270.       if ((waitTime.minute + minutesToRun) >= 60) {
  271.         stopMinute = (waitTime.minute + minutesToRun) - 60;
  272.         if (stopMinute < 0) stopMinute = 0;
  273.       } else {
  274.         stopMinute = waitTime.minute + minutesToRun;
  275.       }
  276.     }
  277.   }
  278. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement