-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharduino_code
219 lines (187 loc) · 5.39 KB
/
arduino_code
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
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <DHT.h>
#include <Button.h>
//temperature monitor
#define DHTPIN D7
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
//WiFi info
const char* ssid = "ssid";
const char* password = "password";
//MQTT info
const char* mqtt_server = "mqtt_server.example.org";
WiFiClient espClient;
PubSubClient client(espClient);
//Assign pins to relays and buttons
int wallFan = D3;
int wallCompressor = D4;
int buildingFan = D1;
int buildingCompressor = D2;
int ledIndicator = D0;
Button startButton = Button(D6,PULLDOWN);
Button stopButton = Button(D5,PULLDOWN);
//assign variables
int startupDelay = 300; //in seconds
int targetTemp = 78;
int actualTemp;
int tempReading;
int fanTime = 0;
int compressorTime = 0;
int fanStatus = 0;
int compressorStatus = 0;
int airConditionerStatus = 0;
int ledIndicatorStatus = 0;
int maxRunTime = 2; // In hours
int runTime = 0;
int startButtonStatus;
int stopButtonStatus;
int compressorDelay = 5; // In minutes
int fanDelay = 5; // In minutes
void setup() {
//start serial
Serial.begin(115200);
//start Wifi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
//Set pin mode
pinMode(wallFan, OUTPUT);
pinMode(wallCompressor, OUTPUT);
pinMode(buildingFan, OUTPUT);
pinMode(buildingCompressor, OUTPUT);
pinMode(ledIndicator, OUTPUT);
pinMode(D8, OUTPUT);
//temperature monitor
dht.begin();
//turn off all relays
digitalWrite(wallFan, HIGH);
digitalWrite(wallCompressor, HIGH);
digitalWrite(buildingFan, HIGH);
digitalWrite(buildingCompressor, HIGH);
digitalWrite(ledIndicator, LOW);
digitalWrite(D8, HIGH);
Serial.println("start delay");
//delay to not kill compressor on start up
for (startupDelay; startupDelay > 0; startupDelay--) {
Serial.println(startupDelay);
delay(1000);
}
}
void loop() {
//get temperature
tempReading = dht.readTemperature(true);
if (tempReading < 115){
if (tempReading > 50){
actualTemp = tempReading;
}
}
Serial.println("Actual Temp: ");
Serial.println(actualTemp);
//check buttons
if(startButton.isPressed()){
startButtonStatus = 1;
}else{
startButtonStatus = 0;
}
if(stopButton.isPressed()){
stopButtonStatus = 0;
}else{
stopButtonStatus = 1;
}
//decriment runtime and set led status
if (runTime > 0){
runTime--;
ledIndicatorStatus = 1;
}
//decrement fan time
if (fanTime > 0){
fanTime--;
}
//decrement compressor time
if (compressorTime > 0){
compressorTime--;
}
//turn off the compressor and start the timer
if (0 == compressorTime && 1 == compressorStatus && 0 == airConditionerStatus){
digitalWrite(wallCompressor, HIGH);
digitalWrite(buildingCompressor, HIGH);
compressorStatus = 0;
compressorTime = (compressorDelay * 30);
fanTime = (fanDelay *30);
Serial.println("Compressor OFF");
}
//turn off the fan
if (0 == compressorStatus && 0 == fanTime && 1 == fanStatus && 0 == airConditionerStatus){
digitalWrite(wallFan, HIGH);
digitalWrite(buildingFan, HIGH);
fanStatus = 0;
Serial.println("Fan OFF");
}
//start the shutdown process when the 2 hours is over
if (0 == runTime){
ledIndicatorStatus = 0;
airConditionerStatus = 0;
Serial.println("Out of Time. Please Insert Coin to Continue");
}
//turn on the air conditioner
if (1 == airConditionerStatus && runTime > 0 && 0 == compressorTime){
digitalWrite(wallFan, LOW);
digitalWrite(wallCompressor, LOW);
digitalWrite(buildingFan, LOW);
digitalWrite(buildingCompressor, LOW);
compressorTime = (compressorDelay * 30);
fanStatus = 1;
compressorStatus = 1;
Serial.println("AC ON");
}
//compare temperature
if (targetTemp < actualTemp){
if (runTime > 0){
airConditionerStatus = 1;
}
}else{
airConditionerStatus = 0;
}
//check startbutton
if (1 == startButtonStatus){
runTime = (maxRunTime * 1800);
}
//check stopbutton
if (1 == stopButtonStatus){
runTime = 0;
}
//LED indicator
if (1 == ledIndicatorStatus){
digitalWrite(ledIndicator, HIGH);
}else{
digitalWrite(ledIndicator, LOW);
}
Serial.println("Run Time: ");
Serial.println(runTime);
Serial.println("AC Status: ");
Serial.println(airConditionerStatus);
Serial.println("Compressor Status: ");
Serial.println(compressorStatus);
Serial.println("Compressor Time: ");
Serial.println(compressorTime);
Serial.println("Fan Status: ");
Serial.println(fanStatus);
Serial.println("Fan Time: ");
Serial.println(fanTime);
Serial.println("LED: ");
Serial.println(ledIndicatorStatus);
Serial.println("Stop Button: ");
Serial.println(stopButton.isPressed());
Serial.println("Start Button: ");
Serial.println(startButton.isPressed());
Serial.println("-----------------------------------------------");
//Delay each loop
delay(2000);
}