-
Notifications
You must be signed in to change notification settings - Fork 0
/
relaycontrol.ino
304 lines (240 loc) · 11.4 KB
/
relaycontrol.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
#include <Client.h>
#include <Ethernet.h>
#include <Server.h>
#include <Udp.h>
#include <SPI.h>
//#include <Agentuino.h> //snmp ...we'll test this another day.
#define VERSION "0.1"
//This code was used for a gate. Change this to whatever you're working with
//to make it more appropriate.
//Define the gate and pin values
#define gateopen 2
#define gateclose 3
#define gatestop 4
#define gatestatus 9
// Version 0.1 - Initial code write up.
// Had to change the gate status level because the contact I had was normally closed (open with the magnetic there) instead of normally open (closed with the magnetic near by). Its what I had around...
/*
* Relay Controller
* Provide a web interface to display gate status and the ability to open, stop or close.
*
*/
//-----------------------BEGIN Variable setup -------------------------------
String readString = String(" "); //string for fetching data from address
boolean reading = false;
unsigned long time; //Use this for time stamp for serial/debug logging info.
int gatecommand=0;
int state;
int val=0;
// hardset MAC address
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = {
10, 0, 0, 5 }; //This is the fail back IP if DHCP doesn't get a valid address.
byte gateway[] = { 10, 0, 0, 1 }; //Manual setup only
byte subnet[] = { 255, 255, 255, 0 }; //Manual setup only
EthernetServer server(80);
//Server server(80); //Standard HTTP server port
//-----------------------END Variable setup-------------------------------
void setup()
{
pinMode(gateopen, OUTPUT); // set high to open gate
pinMode(gateclose, OUTPUT); // set high to close gate
pinMode(gatestop, OUTPUT); // set high to stop gate
pinMode(gatestatus, INPUT); //check to see if gate is open. pin high if gate is fully open.
digitalWrite(gatestatus, HIGH); //Turn on pullup resistor.
Serial.begin(19200);
Serial.println("Initializing.... ");
// EthernetDHCP.begin(mac);
// we're going to assume that if DHCP comes back with no address, then we'll use the hard-coded one as a backup.
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
Serial.println("Failing to hard set IP");
Ethernet.begin(mac, ip,gateway, subnet);
}
//Initialize server
server.begin();
//Give server a second to initialize
delay(1000);
Serial.print("Server is at ");
Serial.println(Ethernet.localIP());
}
void loop()
{
/* Need to add DHCP Lease management.
Ethernet.maintain();
*/
//---------------Web Server initialization------------------------------
//see if we have any incoming client requests.
EthernetClient client = server.available();
if (client) {
Serial.print("Client Connected at: ");
time = millis();
//prints time since program started. Lets print time to serial output at client connect and disconnect
Serial.println(time);
boolean current_line_is_blank = true; // an http request ends with a blank line
while (client.connected()) {
/* Need to add some logic to see if a stale session is still open. If so, I want to kill the client/TCP session. Maybe use millis function and compare. Something like below:
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > sessionTimeout) {
client.stop();
client.flush();
previousMillis = 0;
break; //break out of while loop.
}
Also, we need to make it easier to specify interval number since;
A 16MHz oscillator results in the microcontroller having a clock cycle once every 16-millionth of a second. A clock cycle is roughly the time it takes for one instruction cycle (there are exceptions).
So on the Arduino, each clock cycle ticks off at every 1/16,000,000 second, which is:
0.0000000625 seconds
0.0000625 milliseconds (ms)
0.0625 microseconds (µs)
*/
if (client.available()) {
char c = client.read();
if (readString.length() < 100)
{
readString += c;
//Serial.print("readString value: ");
//Serial.println(readString);
//As we go through the while look and increment on 'c', readString will get the whole request.
//example: after going through interation, read string looks like:
//-----------------------
//GET /?GATE-CONTROL-IO1=1 HTTP/1.1
//Host: 10.0.0.5
//User-Agent: Mozilla/5.0 (X11; Linux i686; rv:16
// ------------------------
// I think readString needs to be bigger than 30 char as declared at beginning...check if this is where the memory leak is...
}
// OK. we make this very simple. We want to see if we get a specific radio button enabled. We can use the indexOf function to return a value of greater than 0 if it finds a match.
// if we get a value greater than 0, then we assume we got a hit and then we do what we need to do.
// only issue with this is if more than one radio button is selected.
//Maybe put more logic here in the future to check for this and provide a message back that it is an error.
if(readString.indexOf("GATE-CONTROL-IO1=1") > 0) { //Open request
gatecommand=gateopen; //use gatecommand like a flag
//Serial.println("Got Gate Open request. ");
}
if(readString.indexOf("GATE-CONTROL-IO2=1") > 0) { //Close request
gatecommand=gateclose;
//Serial.println("Got Gate Close Request ");
}
if(readString.indexOf("GATE-CONTROL-IO3=1") > 0) { //Stop request
gatecommand=gatestop;
//Serial.println("Got Gate Stop Request ");
}
///////////////Finish checking and actions for submit button//////////////////
//------------------Standard web Server Jargon-------------------------------
if (c == 'n' && current_line_is_blank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: html");
client.println();
client.println("<html>");
client.println("<head>");
client.println("<title>Xboard interface--Gate Control</title>");
client.println("</head>");
client.println("<body>");
client.println("My Gate Interface");
client.println("<br />");
client.print("//*************************************");
client.println("<br />");
client.println("Current IP is: ");
client.println(Ethernet.localIP());
client.println("<br />");
client.print("//*************************************");
client.println("<br />");
client.println("<br />");
client.print("<form>");
client.print("<input type=radio name=GATE-CONTROL-IO1 value=1 /> Open Gate<br />");
client.print("<input type=radio name=GATE-CONTROL-IO2 value=1 /> Close Gate<br />");
client.print("<input type=radio name=GATE-CONTROL-IO3 value=1 /> Stop Gate<br />");
client.print("<input type=submit value=SUBMIT/> </form><br />");
client.println("</body>");
break;
}
if (c == 'n') {
// we're starting a new line
current_line_is_blank = true;
}
else if (c != 'r') {
// we've gotten a character on the current line
current_line_is_blank = false;
}
}
}
//------------------END Standard web Server Jargon-------------------------------
//-----------------Print gate status on web page and auto refresh----------------
val = digitalRead(gatestatus); //READ Gate Status on pin D9
if (val == HIGH) {
//printing Gate status. If high then gate is closed based on the contacts I have in place.
client.print("<font size='5'>Gate status: ");
client.println("<font color='green' size='5'>CLOSED");
// OK we want all one string <META HTTP-EQUIV=REFRESH CONTENT=2;url=http://IPADDRESS/>" but I don't know how to embed a variable within the string.
// so I'm going to break this up into several strings concatenated. use print function to continue printing.
// use println to add the CR/LF.
client.print("<br>Active Pin value: ");
client.println(gatecommand);
if (gatecommand > 0) {
digitalWrite(gatecommand, HIGH);
delay(2000);
digitalWrite(gatecommand, LOW);
gatecommand=0; //reset to 0 so we don't execute it again.
}
//client.print("<META HTTP-EQUIV=REFRESH CONTENT=5;url=http://"); //Autorefresh
//client.print(Ethernet.localIP());
//client.println("/>");
//Auto-refresh the site after 5 seconds to reset the gate status
client.print("<br><br>");
//client.print("\"<a href=\"javascript:document.location.reload();\"");
//client.print("ONMOUSEOVER=\"window.status='Refresh'; return true\"");
//client.print("ONMOUSEOUT=\"window.status='ah... that was good'\">");
//client.print("REFRESH GATE STATUS");
client.print("<a href=\"http://");
client.print(Ethernet.localIP());
client.print("\">REFRESH GATE STATUS");
client.println("</a>");
}
else{
client.print("<font size='5'>Gate status: ");
client.println("<font color='red' size='5'>OPEN");
// OK we want all one string <META HTTP-EQUIV=REFRESH CONTENT=2;url=http://IPADDRESS/>" but I don't know how to embed a variable within the string.
// so I'm going to break this up into several strings concatenated. use print function to continue printing.
// use println to add the CR/LF.
client.print("<br>Active Pin value: ");
client.println(gatecommand);
if (gatecommand > 0) {
digitalWrite(gatecommand, HIGH);
delay(2000);
digitalWrite(gatecommand, LOW);
gatecommand=0; //reset to 0 so we don't execute it again.
}
//client.print("<META HTTP-EQUIV=REFRESH CONTENT=5;url=http://"); //Autorefresh
//client.print(Ethernet.localIP());
//client.println("/>");
client.print("<br><br>");
// client.print("\"<a href=\"javascript:document.location.reload();\"");
//client.print("ONMOUSEOVER=\"window.status='Refresh'; return true\"");
//client.print("ONMOUSEOUT=\"window.status='ah... that was good'\">");
//client.print("REFRESH GATE STATUS");
client.print("<a href=\"http://");
client.print(Ethernet.localIP());
client.print("\">REFRESH GATE STATUS");
client.println("</a>");
}
client.println("<hr />");
client.println("<hr />");
client.println("</body></html>");
//clearing string for next read
Serial.println("Clearing String for next read");
readString="";
//-----------------END Print door status on web page and auto refresh----------------
client.flush();
Serial.println("Client Flush");
client.stop();
Serial.println("Client stop");
Serial.print("Client Disconnected at: ");
time = millis();
//prints time since program started. Lets print time to serial output at client connect and disconnect
Serial.println(time);
}
}
//******************************************FUNCTIONS**************************************