-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hot_water_heater.ino
155 lines (136 loc) · 3.54 KB
/
Hot_water_heater.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/* Hot water automation written for Arduino by Chase
*
* Licenced under the GNU GPL Version 3.
* Contact: [email protected]
* www.doin.science
* https://github.com/tan-tan-san
*
*/
#include <Wire.h>
#include "Sodaq_DS3231.h"
#include <SPI.h>
#include <Servo.h>
//Define temp and time vars
char timeChar[100];
char dateChar[50];
String dateString;
int minuteNow=0;
int minutePrevious=0;
uint32_t old_ts;
// Define manual override pushbuttons
const int buttonPinOn = 2;
const int buttonPinOff = 3;
//servo and LED vars
Servo ServoMotor;
int Redpin = 13;
int Greenpin = 12;
//Foward declaring functions
void HeatWater(int heat);
void setup () {
Serial.begin(57600);
Wire.begin();
rtc.begin();
ServoMotor.attach(11);
pinMode(Redpin, OUTPUT);
pinMode(Greenpin, OUTPUT);
pinMode(buttonPinOn, INPUT);
pinMode(buttonPinOff, INPUT);
//setRTCTime(); //Uncomment and run this once to set the time on your RTC
}
void loop () {
delay(1000);
DateTime now = rtc.now();
int n = now.hour();
if ( (n >= '5' && n <= '9') || (n >= '17' && n <= '21' ) )
{
HeatWater(1);
} else {
HeatWater(0);
}
uint32_t ts = now.getEpoch();
String hours = String(now.hour());
//manual override with button press with flashing LED
if (buttonPinOn, HIGH) {
digitalWrite(Greenpin, HIGH);
delay(100);
digitalWrite(Greenpin, LOW);
delay(100);
digitalWrite(Greenpin, HIGH);
delay(100);
digitalWrite(Greenpin, LOW);
delay(100);
digitalWrite(Greenpin, HIGH);
HeatWater(1);
Serial.println("Manual Override ON, Water heater on HIGH");
}
else if (buttonPinOff, HIGH) {
digitalWrite(Redpin, HIGH);
delay(100);
digitalWrite(Redpin, LOW);
delay(100);
digitalWrite(Redpin, HIGH);
delay(100);
digitalWrite(Redpin, LOW);
delay(100);
digitalWrite(Redpin, HIGH);
ServoMotor.write(90);
Serial.println("Manual Override ON, Water heater on HIGH");
}
if (old_ts == 0 || old_ts != ts) {
old_ts = ts;
minuteNow = now.minute();
if(minuteNow!=minutePrevious)
{
dateString = getDayOfWeek(now.dayOfWeek())+", ";
dateString = dateString+String(now.date())+"/"+String(now.month());
dateString= dateString+"/"+ String(now.year());
minutePrevious = minuteNow;
if(now.minute()<10)
{
hours = hours+":0"+String(now.minute());
}
else {
hours = hours+":"+String(now.minute());
}
hours.toCharArray(timeChar,100);
dateString.toCharArray(dateChar,50);
}
}
}
String getDayOfWeek(int i) //possibly forward declare
{
switch(i)
{
case 1: return "Monday";break;
case 2: return "Tuesday";break;
case 3: return "Wednesday";break;
case 4: return "Thursday";break;
case 5: return "Friday";break;
case 6: return "Saturday";break;
case 7: return "Sunday";break;
default: return "Monday";break;
}
}
float Celcius2Fahrenheit(float celsius)
{
return 1.8 * celsius + 32;
}
void setRTCTime()
{
DateTime dt(2015, 8, 27, 9, 35, 30, 4); //Year, Month, Day, Hour, Minutes, Seconds, Day of the Week
rtc.setDateTime(dt); //Adjust date-time as defined 'dt' above
}
void HeatWater(int heat) { // servo to move the temp gauge between LOW and HIGH.
if (heat) {
digitalWrite(Redpin, LOW);
digitalWrite(Greenpin, HIGH);
ServoMotor.write(90);
Serial.println("Water heater on HIGH");
}
else {
digitalWrite(Redpin, HIGH); //timeofday = dont need water heated
digitalWrite(Greenpin, LOW);
ServoMotor.write(11);
Serial.println("Water heater on LOW");
}
}