-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfireplace.ino
More file actions
296 lines (254 loc) · 8.14 KB
/
fireplace.ino
File metadata and controls
296 lines (254 loc) · 8.14 KB
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
// This #include statement was automatically added by the Particle IDE.
#include <WebServer.h>
/* Function prototypes -------------------------------------------------------*/
int tinkerDigitalRead(String pin);
int tinkerDigitalWrite(String command);
int tinkerAnalogRead(String pin);
int tinkerAnalogWrite(String command);
int fireplaceSwitch = D0;
int relaySwitch = A0;
#define PREFIX ""
WebServer webserver(PREFIX, 80);
void helloCmd(WebServer &server, WebServer::ConnectionType type, char *, bool)
{
/* this line sends the standard "we're all OK" headers back to the
browser */
server.httpSuccess();
/* if we're handling a GET or POST, we can output our data here.
For a HEAD request, we just stop after outputting headers. */
if (type != WebServer::HEAD)
{
/* this defines some HTML text in read-only memory aka PROGMEM.
* This is needed to avoid having the string copied to our limited
* amount of RAM. */
P(helloMsg) = "<h1>fireplace!</h1>";
/* this is a special form of print that outputs from PROGMEM */
server.printP(helloMsg);
}
}
/* This function is called once at start up ----------------------------------*/
void setup() {
Particle.function("rlySwitchOn",rlySwitchOn);
Particle.function("rlySwitchOff",rlySwitchOff);
Particle.function("rlySwitch15",rlySwitch15);
Particle.function("rlySwitch60",rlySwitch60);
Particle.function("rlySwitch120",rlySwitch120);
Particle.function("digitalread", tinkerDigitalRead);
Particle.function("digitalwrite", tinkerDigitalWrite);
Particle.function("analogread", tinkerAnalogRead);
Particle.function("analogwrite", tinkerAnalogWrite);
pinMode(fireplaceSwitch, INPUT_PULLDOWN);
pinMode(relaySwitch, OUTPUT);
digitalWrite(relaySwitch, HIGH);
Time.zone(-6);
int curHour = Time.hour();
char str[15];
sprintf(str, "%d", curHour);
webserver.setDefaultCommand(&helloCmd);
RGB.control(true);
RGB.brightness(0);
webserver.addCommand("index.html", &helloCmd);
}
int delayMins = 1;
unsigned long lastTime = 0;
unsigned long lastTimeCheck = 0;
bool relaySwitchNeedsToTurnOn = false;
bool relaySwitchTimedNeedsToTurnOff = false;
bool fireplaceSwitchNeedsToTurnOn = false;
bool alertIfOn = true;
void loop(){
char buff[64];
int len = 64;
//check for physical switch for on/off
if (digitalRead(fireplaceSwitch) == LOW){
if(!fireplaceSwitchNeedsToTurnOn){
pinOff(relaySwitch);
fireplaceSwitchNeedsToTurnOn = true;
}
} else if(digitalRead(fireplaceSwitch) == HIGH) {
if(fireplaceSwitchNeedsToTurnOn){
pinOn(relaySwitch);
Particle.publish("fireplaceSwitchNeedsToTurnOnFalse", NULL, 60, PRIVATE);
fireplaceSwitchNeedsToTurnOn = false;
}
}
//alert if fireplace on/off
unsigned long nowCheck = millis();
if ((nowCheck - lastTimeCheck) >= 30 * 1000) {
lastTimeCheck = nowCheck;
if (digitalRead(relaySwitch) == HIGH){
if(!alertIfOn){
Particle.publish("fireplace-off", NULL, 60, PRIVATE);
alertIfOn = true;
}
} else if(digitalRead(relaySwitch) == LOW) {
if(alertIfOn){
Particle.publish("fireplace-on", NULL, 60, PRIVATE);
alertIfOn = false;
}
}
}
//check for timed turn off
unsigned long now = millis();
if ((now - lastTime) >= delayMins * 60 * 1000) {
lastTime = now;
if(relaySwitchTimedNeedsToTurnOff){
relaySwitchTimedNeedsToTurnOff = false;
pinOff(relaySwitch);
}
}
//handle incoming web requests
webserver.processConnection(buff, &len);
}
int pinOn(int pin){
if (digitalRead(pin) == HIGH){
fireplaceSwitchNeedsToTurnOn = true;
digitalWrite(pin, LOW);
}
return 1;
}
int pinOff(int pin){
if (digitalRead(pin) == LOW){
digitalWrite(pin, HIGH);
}
return 1;
}
int rlySwitchOn(String command){
pinOn(relaySwitch);
relaySwitchTimedNeedsToTurnOff = false;
return 1;
}
int rlySwitchOff(String command){
pinOff(relaySwitch);
return 1;
}
int rlySwitch15(String command){
pinOn(relaySwitch);
lastTime = millis();
delayMins = 15;
relaySwitchTimedNeedsToTurnOff = true;
return 1;
}
int rlySwitch60(String command){
pinOn(relaySwitch);
lastTime = millis();
delayMins = 60;
relaySwitchTimedNeedsToTurnOff = true;
return 1;
}
int rlySwitch120(String command){
pinOn(relaySwitch);
lastTime = millis();
delayMins = 120;
relaySwitchTimedNeedsToTurnOff = true;
return 1;
}
/*******************************************************************************
* Function Name : tinkerDigitalRead
* Description : Reads the digital value of a given pin
* Input : Pin
* Output : None.
* Return : Value of the pin (0 or 1) in INT type
Returns a negative number on failure
*******************************************************************************/
int tinkerDigitalRead(String pin)
{
//convert ascii to integer
int pinNumber = pin.charAt(1) - '0';
//Sanity check to see if the pin numbers are within limits
if (pinNumber< 0 || pinNumber >7) return -1;
if(pin.startsWith("D"))
{
pinMode(pinNumber, INPUT_PULLDOWN);
return digitalRead(pinNumber);
}
else if (pin.startsWith("A"))
{
pinMode(pinNumber+10, INPUT_PULLDOWN);
return digitalRead(pinNumber+10);
}
return -2;
}
/*******************************************************************************
* Function Name : tinkerDigitalWrite
* Description : Sets the specified pin HIGH or LOW
* Input : Pin and value
* Output : None.
* Return : 1 on success and a negative number on failure
*******************************************************************************/
int tinkerDigitalWrite(String command)
{
bool value = 0;
//convert ascii to integer
int pinNumber = command.charAt(1) - '0';
//Sanity check to see if the pin numbers are within limits
if (pinNumber< 0 || pinNumber >7) return -1;
if(command.substring(3,7) == "HIGH") value = 1;
else if(command.substring(3,6) == "LOW") value = 0;
else return -2;
if(command.startsWith("D"))
{
pinMode(pinNumber, OUTPUT);
digitalWrite(pinNumber, value);
return 1;
}
else if(command.startsWith("A"))
{
pinMode(pinNumber+10, OUTPUT);
digitalWrite(pinNumber+10, value);
return 1;
}
else return -3;
}
/*******************************************************************************
* Function Name : tinkerAnalogRead
* Description : Reads the analog value of a pin
* Input : Pin
* Output : None.
* Return : Returns the analog value in INT type (0 to 4095)
Returns a negative number on failure
*******************************************************************************/
int tinkerAnalogRead(String pin)
{
//convert ascii to integer
int pinNumber = pin.charAt(1) - '0';
//Sanity check to see if the pin numbers are within limits
if (pinNumber< 0 || pinNumber >7) return -1;
if(pin.startsWith("D"))
{
return -3;
}
else if (pin.startsWith("A"))
{
return analogRead(pinNumber+10);
}
return -2;
}
/*******************************************************************************
* Function Name : tinkerAnalogWrite
* Description : Writes an analog value (PWM) to the specified pin
* Input : Pin and Value (0 to 255)
* Output : None.
* Return : 1 on success and a negative number on failure
*******************************************************************************/
int tinkerAnalogWrite(String command)
{
//convert ascii to integer
int pinNumber = command.charAt(1) - '0';
//Sanity check to see if the pin numbers are within limits
if (pinNumber< 0 || pinNumber >7) return -1;
String value = command.substring(3);
if(command.startsWith("D"))
{
pinMode(pinNumber, OUTPUT);
analogWrite(pinNumber, value.toInt());
return 1;
}
else if(command.startsWith("A"))
{
pinMode(pinNumber+10, OUTPUT);
analogWrite(pinNumber+10, value.toInt());
return 1;
}
else return -2;
}