-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.ino
157 lines (126 loc) · 3.02 KB
/
sketch.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
/*
Web Server
A simple web server to control a relay over web
Circuit:
* Ethernet shield
* Analog inputs attached to pins A0 through A5 (optional)
created 17.05.2017
by Sinan Taviloglu
*/
#include <SPI.h>
#include <Ethernet.h>
//ouput pin for relay control
const int RelayPin = 2;
//constant values for relay state
const int OPEN = 1;
const int CLOSED = 0;
int _relayState = OPEN;
//set mac address
byte mac[] = {
0xDE,
0xAD,
0xBE,
0xEF,
0xFE,
0xED
};
//set static ip
byte ip[] = {
192,
168,
0,
20
};
byte gateway[] = {
192,
168,
0,
1
};
byte subnet[] = {
255,
255,
255,
0
};
//standard HTTP port
EthernetServer server(80);
String readString;
void setup() {
pinMode(RelayPin, OUTPUT);
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
}
void loop() {
EthernetClient client = server.available();
if (client)
{
while (client.connected())
{
if (client.available())
{
char c = client.read();
if (readString.length() < 100)
{
readString += c;
}
if (c == '\n')
{
if (readString.indexOf("?on") > 0)
{
if (_relayState == CLOSED)
{
OpenRelay();
}
}
if (readString.indexOf("?off") > 0)
{
if (_relayState == OPEN)
CloseRelay();
}
readString = "";
SendHttpResponse(client);
delay(1);
client.stop();
}
}
}
}
}
void SendHttpResponse(EthernetClient client)
{
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("<HTML>");
client.println("<HEAD>");
client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
client.println("<link rel='stylesheet' type='text/css' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css' />");
client.println("<TITLE>Online Relay Control - Sinan TAVILOGLU</TITLE>");
client.println("</HEAD>");
client.println("<BODY>");
client.println("<H1>Online Relay Control</H1>");
client.println("<hr />");
client.println("<br />");
if (_relayState == OPEN)
client.println("<a href=\"/?off\"\">Close</a><br />");
else
client.println("<a href=\"/?on\"\">Open</a>");
client.println("<br />");
client.println("<br />");
client.println("<p>Sinan TAVILOGLU - staviloglu@gmail.com</p>");
client.println("<p>Visit <a href='http://about.me/sinantaviloglu'>http://about.me/sinantaviloglu</a></p>");
client.println("</BODY>");
client.println("</HTML>");
}
void OpenRelay() {
//may change according to relay, if it is NC/NO
digitalWrite(RelayPin, LOW);
_relayState = OPEN;
}
void CloseRelay() {
//may change according to relay, if it is NC/NO
digitalWrite(RelayPin, HIGH);
_relayState = CLOSED;
}