Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "TemperatureSwitch.h"
- void handleButtonPressed() {
- // Exit if already pressed
- if (buttonPressed) return;
- //startMillis = millis();
- // Set flag
- buttonPressed = true;
- waitingTime = false;
- // Log
- if (debug) Serial.println("Button pressed!");
- // Flag to store actual time
- getTime = true;
- }
- void checkTemperature() {
- float actualTemperature = temperatureSensor.readTemperatureC();
- /*if (!step) {
- actualTemperature -= 1;
- if (actualTemperature < 16) step = true;
- } else {
- actualTemperature += 1;
- if (actualTemperature > 22) step = false;
- }
- delay(1000);*/
- if (debug) {
- Serial.print("Temperature: ");
- Serial.print(String(actualTemperature));
- Serial.println(" C");
- }
- if ((lowTemperatureReached(actualTemperature)) && (relayPin2.value() == LOW)) {
- if (debug) Serial.println("lowTemp - Low TEMP reached!");
- // Enable pin
- relayPin2.on();
- temperatureTime = actualTime;
- // Count the difference, where to stop
- if ((temperatureTime.minute + minutesToRun) >= 60) {
- stopMinute = (temperatureTime.minute + minutesToRun) - 60;
- if (stopMinute < 0) stopMinute = 0;
- } else {
- stopMinute = temperatureTime.minute + minutesToRun;
- }
- } else if ((highTempeartureReached(actualTemperature)) && (relayPin2.value() == HIGH)) {
- if (debug) Serial.println("lowTemp - High TEMP reached!");
- // Disable pin
- relayPin2.off();
- waitTime = actualTime;
- waitingTime = true;
- if ((waitTime.minute + minutesToRun) >= 60) {
- stopMinute = (waitTime.minute + minutesToRun) - 60;
- if (stopMinute < 0) stopMinute = 0;
- } else {
- stopMinute = waitTime.minute + minutesToRun;
- }
- }
- // Stop after 10 minutes
- if ((relayPin2.value() == HIGH) && (actualTime.minute == stopMinute)) {
- if (debug) Serial.print("lowTemp - TIME reached, stopping...");
- relayPin2.off();
- waitTime = actualTime;
- waitingTime = true;
- if ((waitTime.minute + minutesToRun) >= 60) {
- stopMinute = (waitTime.minute + minutesToRun) - 60;
- if (stopMinute < 0) stopMinute = 0;
- } else {
- stopMinute = waitTime.minute + minutesToRun;
- }
- }
- }
- void getDateTime() {
- if (!Rtc.IsDateTimeValid()) {
- // Common Cuases:
- // 1) the battery on the device is low or even missing and the power line was disconnected
- Serial.println("RTC lost confidence in the DateTime!");
- }
- RtcDateTime now = Rtc.GetDateTime();
- actualTime.hour = now.Hour();
- actualTime.minute = now.Minute();
- actualTime.second = now.Second();
- /*if (debug) {
- Serial.print("Time: ");
- Serial.print(String(actualTime.hour));
- Serial.print(":");
- Serial.print(String(actualTime.minute));
- Serial.print(":");
- Serial.println(String(actualTime.second));
- }*/
- }
- bool highTempeartureReached(float temperature) {
- if (temperature >= highTemperature) {
- return true;
- }
- return false;
- }
- bool lowTemperatureReached(float temperature) {
- if (temperature <= lowTemperature) {
- return true;
- }
- return false;
- }
- void setupPins() {
- if (debug) Serial.println("Initializing pins...");
- // Disable led pin
- ledPin.setOutput(false);
- // Disable relay Pins
- relayPin1.setOutput(false);
- relayPin2.setOutput(false);
- // Prepare interrupt pin
- pinMode(buttonPin, INPUT_PULLUP);
- attachInterrupt(buttonInterruptPin, handleButtonPressed, FALLING);
- }
- void setupRTC() {
- Serial.println("Initializing RTC...");
- //--------RTC SETUP ------------
- // if you are using ESP-01 then uncomment the line below to reset the pins to
- // the available pins for SDA, SCL
- // Wire.begin(0, 2); // due to limited pins, use pin 0 and 2 for SDA, SCL
- Rtc.Begin();
- RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
- if (!Rtc.IsDateTimeValid()) {
- // Common Cuases:
- // 1) first time you ran and the device wasn't running yet
- // 2) the battery on the device is low or even missing
- Serial.println("RTC lost confidence in the DateTime!");
- // following line sets the RTC to the date & time this sketch was compiled
- // it will also reset the valid flag internally unless the Rtc device is
- // having an issue
- Rtc.SetDateTime(compiled);
- }
- if (!Rtc.GetIsRunning()) {
- Serial.println("RTC was not actively running, starting now");
- Rtc.SetIsRunning(true);
- }
- RtcDateTime now = Rtc.GetDateTime();
- if (now < compiled) {
- Serial.println("RTC is older than compile time! (Updating DateTime)");
- Rtc.SetDateTime(compiled);
- } else if (now > compiled) {
- Serial.println("RTC is newer than compile time. (this is expected)");
- } else if (now == compiled) {
- Serial.println("RTC is the same as compile time! (not expected but all is fine)");
- }
- // never assume the Rtc was last configured by you, so
- // just clear them to your needed state
- Rtc.SetSquareWavePin(DS1307SquareWaveOut_Low);
- }
- void setup() {
- // Set serial console
- Serial.begin(115200);
- //while (!Serial);
- // Setup pins
- setupPins();
- // Setup RTC
- setupRTC();
- }
- void loop() {
- // Get actual time
- getDateTime();
- if (!buttonPressed) {
- if ((actualTime.hour >= startHour) || (actualTime.hour < endHour)) {
- if (!waitingTime) {
- if (debug) {
- Serial.print("ActualMinute: ");
- Serial.print(String(actualTime.minute));
- Serial.print("/ StopMinute: ");
- Serial.println(String(stopMinute));
- }
- checkTemperature();
- } else {
- // Stop after 10 minutes
- /*if (debug) {
- Serial.print("waiting: ");
- Serial.print(String(actualTime.minute));
- Serial.print("/");
- Serial.println(String(stopMinute));
- Serial.println(String(relayPin2.value()));
- }*/
- if ((relayPin2.value() == LOW) && (actualTime.minute == stopMinute)) {
- waitingTime = false;
- stopMinute = -1;
- }
- }
- } else {
- //Whatever
- }
- } else {
- if (getTime) {
- // Set button time
- buttonTime = actualTime;
- if (debug) {
- Serial.print(String(buttonTime.hour));
- Serial.print(":");
- Serial.print(String(buttonTime.minute));
- Serial.println(" - Button Time");
- }
- // Reset flag
- getTime = false;
- // Set pin
- relayPin2.on();
- }
- if ((buttonTime.minute + minutesToRun) >= 60) {
- stopMinute = (buttonTime.minute + minutesToRun) - 60;
- if (stopMinute < 0) stopMinute = 0;
- } else {
- stopMinute = buttonTime.minute + minutesToRun;
- }
- if (debug) {
- Serial.print("button: ");
- Serial.print(String(actualTime.minute));
- Serial.print(" - Stop minute: ");
- Serial.println(String(stopMinute));
- Serial.print("Temperature ");
- Serial.print(String(temperatureSensor.readTemperatureC()));
- Serial.println(" C");
- }
- if ((actualTime.minute == stopMinute) || (highTempeartureReached(temperatureSensor.readTemperatureC()))) {
- // Disable
- buttonPressed = false;
- // Disable relay
- relayPin2.off();
- waitTime = actualTime;
- waitingTime = true;
- if ((waitTime.minute + minutesToRun) >= 60) {
- stopMinute = (waitTime.minute + minutesToRun) - 60;
- if (stopMinute < 0) stopMinute = 0;
- } else {
- stopMinute = waitTime.minute + minutesToRun;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement