auto-awning-close/main/main.ino

217 lines
4.6 KiB
Arduino
Raw Normal View History

2021-05-03 20:40:50 +02:00
#include <RCSwitch.h>
2021-06-06 00:20:53 +02:00
#include <Wire.h>
#include <Rtc_Pcf8563.h>
2021-05-03 20:40:50 +02:00
RCSwitch mySwitch = RCSwitch();
2021-06-06 00:20:53 +02:00
Rtc_Pcf8563 rtc;
2021-05-03 20:40:50 +02:00
2021-05-22 15:06:38 +02:00
// defining used pins
2021-05-03 20:40:50 +02:00
int wind = 8;
2021-06-06 00:20:53 +02:00
int rain = 12;
2021-05-03 20:40:50 +02:00
int receiver = 9;
2021-05-22 15:06:38 +02:00
int relay1 = 7;
int relay2 = 6;
int relay3 = 5;
int relay4 = 4;
2021-05-03 20:40:50 +02:00
2021-05-22 15:06:38 +02:00
// codes from remote controller
2021-06-06 18:29:25 +02:00
const int recRelay4 = 3696136;
const int recRelay3 = 16736114;
const int recRelay2 = 16736120;
2021-05-22 15:06:38 +02:00
const int recRelay1 = 16736113;
2021-05-03 20:40:50 +02:00
2021-06-06 18:29:25 +02:00
int delayTime = 500;
2021-06-19 15:35:40 +02:00
// status of roleta
bool isOpen = false;
2021-05-03 20:40:50 +02:00
2021-05-22 15:06:38 +02:00
void setup()
{
2021-05-03 20:40:50 +02:00
Serial.begin(9600);
2021-05-22 15:06:38 +02:00
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(relay4, OUTPUT);
pinMode(wind, INPUT);
2021-06-06 00:20:53 +02:00
pinMode(rain, INPUT);
2021-05-03 20:40:50 +02:00
2021-05-22 15:06:38 +02:00
mySwitch.enableReceive(0); // Receiver on interrupt 0 => that is pin #2
2021-06-06 00:20:53 +02:00
//clear out the registers
2021-06-06 18:29:25 +02:00
///rtc.initClock();
2021-06-06 00:20:53 +02:00
//set a time to start with.
//day, weekday, month, century(1=1900, 0=2000), year(0-99)
2021-06-19 15:35:40 +02:00
///rtc.setDate(18, 6, 6, 0, 21);
2021-06-06 00:20:53 +02:00
//hr, min, sec
2021-06-19 15:35:40 +02:00
///rtc.setTime(18, 44, 0);
2021-06-06 00:20:53 +02:00
}
void updateRTC()
{
/*
function to update RTC time using user input
*/
// ask user to enter new date and time
const char txt[6][15] = { "year [4-digit]", "month [1~12]", "day [1~31]",
"hours [0~23]", "minutes [0~59]", "seconds [0~59]"};
String str = "";
long newDate[6];
while (Serial.available()) {
Serial.read(); // clear serial buffer
}
for (int i = 0; i < 6; i++) {
Serial.print("Enter ");
Serial.print(txt[i]);
Serial.print(": ");
while (!Serial.available()) {
; // wait for user input
}
str = Serial.readString(); // read user input
newDate[i] = str.toInt(); // convert user input to number and save to array
Serial.println(newDate[i]); // show user input
}
// update RTC
//rtc.adjust(DateTime(2021, newDate[1], newDate[2], newDate[3], newDate[4], newDate[5]));
rtc.setDate(newDate[2], 6, newDate[1], 0, 2020);
Serial.println("RTC Updated!");
2021-05-03 20:40:50 +02:00
}
2021-06-19 15:35:40 +02:00
void relayOn(int pin, bool controller = false)
2021-05-03 20:40:50 +02:00
{
2021-06-19 15:35:40 +02:00
Serial.print("realy ");
Serial.print(pin);
Serial.print("\n");
if(controller == false)
{
Serial.println("jsemtu");
if(pin == relay4)
{
isOpen = true;
}
if(pin == relay1)
{
isOpen = false;
}
}
2021-05-22 15:06:38 +02:00
2021-05-03 20:40:50 +02:00
digitalWrite(pin, HIGH);
delay(delayTime);
2021-06-06 18:29:25 +02:00
if(pin == relay3) {
// delay(1500);
}
2021-05-03 20:40:50 +02:00
digitalWrite(pin, LOW);
2021-05-22 15:06:38 +02:00
delay(delayTime);
2021-05-03 20:40:50 +02:00
}
void loop()
{
2021-06-19 15:35:40 +02:00
//formatted strings are at the current time/date.
Serial.print(rtc.formatTime());
2021-06-06 00:20:53 +02:00
Serial.print("\r\n");
2021-06-19 15:35:40 +02:00
Serial.print(rtc.formatDate());
Serial.print("\r\n");
delay(1000);
// assign day and month into variable
char h1 = rtc.formatTime(RTCC_TIME_HM)[0];
char h2 = rtc.formatTime(RTCC_TIME_HM)[1];
int onlyHour = ( 10 * (h1 - '0') ) + h2 - '0';
char m1 = rtc.formatTime(RTCC_TIME_HM)[3];
char m2 = rtc.formatTime(RTCC_TIME_HM)[4];
int onlyMinutes = ( 10 * (m1 - '0') ) + m2 - '0';
char M1 = rtc.formatDate()[0];
char M2 = rtc.formatDate()[1];
int onlyMonth = ( 10 * (M1 - '0') ) + M2 - '0';
2021-06-06 18:29:25 +02:00
2021-06-19 15:35:40 +02:00
Serial.println(onlyHour);
Serial.println(onlyMinutes);
Serial.println(onlyMonth);
Serial.println(isOpen);
// setting up date and time from serial monitor
2021-06-06 00:20:53 +02:00
if (Serial.available())
{
char input = Serial.read();
if (input == 'u') updateRTC(); // update RTC time
}
2021-06-19 15:35:40 +02:00
// time actions
if(onlyMonth >= 5 && onlyMonth <= 9)
{
if(onlyHour >= 7 && onlyHour <= 14)
{
if(isOpen == false)
{
2021-07-26 22:37:34 +02:00
Serial.println("cas otevira");
2021-06-19 15:35:40 +02:00
relayOn(relay4);
}
}
else
{
if(isOpen == true)
{
2021-07-26 22:37:34 +02:00
Serial.println("cas zavira");
2021-06-19 15:35:40 +02:00
relayOn(relay1);
}
}
}
// rain actions
2021-06-06 00:20:53 +02:00
if(digitalRead(rain) != HIGH)
{
2021-07-26 22:37:34 +02:00
Serial.println("voda zavira");
2021-06-19 15:57:31 +02:00
relayOn(relay1, true);
2021-06-06 00:20:53 +02:00
}
2021-05-22 15:06:38 +02:00
// wind actions
if (digitalRead(wind) == HIGH)
2021-05-03 20:40:50 +02:00
{
2021-07-26 22:37:34 +02:00
Serial.println("vitr zavira");
//relayOn(relay1, true);
2021-05-03 20:40:50 +02:00
}
else
{
2021-06-19 15:35:40 +02:00
//digitalWrite(relay1, LOW); -> probably useless
2021-05-03 20:40:50 +02:00
}
2021-05-22 15:06:38 +02:00
// remote controller actions
2021-05-03 20:40:50 +02:00
if (mySwitch.available())
{
2021-05-22 15:06:38 +02:00
int received = mySwitch.getReceivedValue();
Serial.println("Received " + received);
2021-05-03 20:40:50 +02:00
2021-05-22 15:06:38 +02:00
if (received == recRelay1)
{
2021-06-19 15:35:40 +02:00
relayOn(relay1, true);
2021-05-22 15:06:38 +02:00
}
if (received == recRelay2)
2021-05-03 20:40:50 +02:00
{
2021-06-19 15:35:40 +02:00
relayOn(relay2, true);
2021-05-22 15:06:38 +02:00
}
if (received == recRelay3)
{
2021-06-19 15:35:40 +02:00
relayOn(relay3, true);
2021-05-22 15:06:38 +02:00
}
if (received == recRelay4)
{
2021-06-19 15:35:40 +02:00
relayOn(relay4, true);
2021-05-03 20:40:50 +02:00
}
mySwitch.resetAvailable();
}
2021-05-22 15:06:38 +02:00
}