forked from netadmindave/FamiLAB-Warehouse-Thermostat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
thermostat.ino
308 lines (262 loc) · 8.83 KB
/
thermostat.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
//----------------------------EEPROM---------------------
#include <EEPROM.h>
const int eepromAddress = 0;
//----------------------------define pins----------------------------
const int startButton = D3;
const int stopButton = D4;
const int wallFan = D7;
const int wallCompressor = D6;
const int buildingFan = D5;
const int buildingCompressor = D0;
//------------------------------i2c LCD-------------------------------
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
//---------------------------------HTU21D---------------------------------
#include <Wire.h>
#include "Adafruit_HTU21DF.h"
Adafruit_HTU21DF htu = Adafruit_HTU21DF();
//----------------------------Global Variables----------------------------
//temp related
float temp;
float rel_hum;
int targetTemp = 78;
//status variables
int fanStatus = 0;
int compressorStatus = 0;
int airConditionerStatus = 0;
//timers
int countdown;
const int countdownStart = 7200; // in seconds
int delayTimer = 300; //in seconds
int fanTime = 0; //for countdown to shutoff the fan
int compressorTime = 0; //for countdown to change state on the compressor
int compressorDelay = 300; // in seconds
int fanDelay = 300; // in seconds
long totalRunTime; //running total of seconds the fan and compresssor is running
const int loopTime = 854; //in milliseconds
int writeTimer = 0; //count between writes to the eeprom
long storedRunTime; //value from eeprom
int writeTriggerTime = 400; //delay between eeprom writes
int endCountdownWrite = 0; //make sure the eeprom is updated during shutdown
//----------------------------interupt functions-------------------------
//esp8266 requires this for interupts
void ICACHE_RAM_ATTR stopState();
void stopState() {
countdown = 0;
}
//esp8266 requires this for interupts
void ICACHE_RAM_ATTR startState();
void startState() {
countdown = countdownStart;
}
//----------------------------LCD Display-------------------------
void lcdDisplay() {
if(countdown == 7200 && delayTimer == 0){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("SET: ");
lcd.setCursor(4,0);
lcd.print(targetTemp);
lcd.setCursor(7,0);
lcd.print("NOW: ");
lcd.setCursor(11,0);
lcd.print(temp,1);
lcd.setCursor(0,1);
lcd.print("REMAINING: ");
lcd.setCursor(11,1);
lcd.print(countdown);
lcd.setCursor(15,1);
lcd.print("s");
}
if(countdown < 7200 && countdown > 0 && delayTimer == 0){
lcd.setCursor(11,0);
lcd.print(" ");
lcd.setCursor(11,0);
lcd.print(temp,1);
lcd.setCursor(11,1);
lcd.print(" ");
lcd.setCursor(11,1);
lcd.print(countdown);
}
if(countdown == 0 || delayTimer > 0){
lcd.setCursor(0,0);
lcd.print("TEMP: HUMID: ");
lcd.setCursor(5,0);
lcd.print(temp,0);
lcd.setCursor(14,0);
lcd.print(rel_hum,0);
if(compressorTime == 0 && delayTimer == 0){
lcd.setCursor(0,1);
lcd.print("AC OFF RT: ");
}
if(compressorTime > 0){
lcd.setCursor(0,1);
lcd.print("DELAY: RT: ");
lcd.setCursor(6,1);
lcd.print(compressorTime);
}
if(delayTimer > 0){
lcd.setCursor(0,1);
lcd.print("DELAY: RT: ");
lcd.setCursor(6,1);
lcd.print(delayTimer);
}
if(totalRunTime/3600 < 16){
lcd.setCursor(12,1);
lcd.print(totalRunTime/60);
lcd.setCursor(15,1);
lcd.print("M");
}else{
lcd.setCursor(12,1);
lcd.print(totalRunTime/3600);
lcd.setCursor(15,1);
lcd.print("H");
}
}
}
//--------------------------------Setup-----------------------------------
void setup() {
//serial
Serial.begin(115200);
Serial.println("Start");
//temp sensor
if (!htu.begin()) {
Serial.println("Couldn't find sensor!");
while (1){
delay(1000);
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(2,0);
lcd.print("TEMP SENSOR");
lcd.setCursor(3,1);
lcd.print("NOT FOUND");
}
}
//buttons and interupts
pinMode(stopButton, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(stopButton), stopState, CHANGE);
pinMode(startButton, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(startButton), startState, CHANGE);
//lcd
lcd.init();
lcd.backlight();
lcd.clear();
//Set pin mode
pinMode(wallFan, OUTPUT);
pinMode(wallCompressor, OUTPUT);
pinMode(buildingFan, OUTPUT);
pinMode(buildingCompressor, OUTPUT);
//turn off all relays
digitalWrite(wallFan, HIGH);
digitalWrite(wallCompressor, HIGH);
digitalWrite(buildingFan, HIGH);
digitalWrite(buildingCompressor, HIGH);
//EEPROM
EEPROM.begin(512);
EEPROM.get(eepromAddress, storedRunTime);
if(storedRunTime <= 0){
EEPROM.put(eepromAddress, 10);
EEPROM.commit();
Serial.println("wrote to eeprom");
}
totalRunTime = storedRunTime;
compressorTime = delayTimer;
}
//---------------------------------Main Loop--------------------------------
void loop() {
delay(loopTime);
//---------Read Temp-------
temp = (((htu.readTemperature()*9)/5)+32);
rel_hum = htu.readHumidity();
//--------Display Status on LCD-----------
lcdDisplay();
//decrement fan time
if (fanTime > 0){
fanTime--;
}
//decrement compressor time
if (compressorTime > 0){
compressorTime--;
}
//decrement delay time
if (delayTimer > 0){
delayTimer--;
return;
}
//decrement countdown
if(countdown > 0 && delayTimer == 0){
countdown--;
}
//compare temperature
if (targetTemp < temp){
if (countdown > 0 && delayTimer == 0){
airConditionerStatus = 1;
}
}else{
airConditionerStatus = 0;
}
//turn on the air conditioner
if (airConditionerStatus == 1 && countdown > 0 && compressorTime == 0 && compressorStatus == 0){
digitalWrite(wallFan, LOW);
digitalWrite(wallCompressor, LOW);
digitalWrite(buildingFan, LOW);
digitalWrite(buildingCompressor, LOW);
compressorTime = (compressorDelay);
fanStatus = 1;
compressorStatus = 1;
Serial.println("AC ON");
}
//turn off the compressor and start the timer
if (compressorTime == 0 && compressorStatus == 1 && airConditionerStatus == 0){
digitalWrite(wallCompressor, HIGH);
digitalWrite(buildingCompressor, HIGH);
compressorStatus = 0;
compressorTime = (compressorDelay);
fanTime = (fanDelay);
Serial.println("Compressor OFF");
}
//turn off the fan
if (compressorStatus == 0 && fanTime == 0 && fanStatus == 1 && airConditionerStatus == 0){
digitalWrite(wallFan, HIGH);
digitalWrite(buildingFan, HIGH);
fanStatus = 0;
Serial.println("Fan OFF");
if(endCountdownWrite == 1){
writeTimer = 0;
endCountdownWrite = 0;
EEPROM.put(eepromAddress, totalRunTime);
EEPROM.commit();
}
}
//start the shutdown process when the 2 hours is over
if (countdown == 0){
airConditionerStatus = 0;
endCountdownWrite = 1;
Serial.println("Out of Time. Please Insert Coin to Continue");
}
//add runtime to EEPROM
if(fanStatus == 1){
writeTimer++;
totalRunTime++;
if(writeTimer == writeTriggerTime){
writeTimer = 0;
EEPROM.put(eepromAddress, totalRunTime);
EEPROM.commit();
}
}
EEPROM.get(eepromAddress, storedRunTime);
//display all variables
Serial.print("Temp: "); Serial.print(temp); Serial.println("F");
Serial.print("Humidity: "); Serial.print(rel_hum); Serial.println("%");
Serial.print("Countdown: ");Serial.print(countdown);Serial.println("s");
Serial.print("AC Status: ");Serial.println(airConditionerStatus);
Serial.print("Compressor Status: ");Serial.println(compressorStatus);
Serial.print("Compressor Time: ");Serial.print(compressorTime);Serial.println("s");
Serial.print("Fan Status: ");Serial.println(fanStatus);
Serial.print("Fan Time: ");Serial.print(fanTime);Serial.println("s");
Serial.print("Total Run Time: ");Serial.print(totalRunTime);Serial.print("s or ");Serial.print(totalRunTime/3600);Serial.println("h");
Serial.print("Write Timer: ");Serial.print(writeTimer);Serial.println("s");
Serial.print("EEPROM Value: ");Serial.println(storedRunTime);
Serial.println("-----------------------------------------------");
}